@SuppressWarnings({ "rawtypes", "unchecked" }) protected void replaceOldSemanticElement(EObject oldElement, IParseResult previousParseResult, IParseResult newParseResult) { EObject oldSemanticParentElement = oldElement.eContainer(); if (oldSemanticParentElement != null) { EStructuralFeature feature = oldElement.eContainingFeature(); if (feature.isMany()) { List featureValueList = (List) oldSemanticParentElement.eGet(feature); int index = featureValueList.indexOf(oldElement); unloadSemanticObject(oldElement); featureValueList.set(index, newParseResult.getRootASTElement()); } else { unloadSemanticObject(oldElement); oldSemanticParentElement.eSet(feature, newParseResult.getRootASTElement()); } ((ParseResult) newParseResult).setRootASTElement(previousParseResult.getRootASTElement()); } else { unloadSemanticObject(oldElement); } }
protected void readNodeModel(final StorageAwareResource resource, final InputStream inputStream) throws IOException { final SerializableNodeModel serializableNodeModel = new SerializableNodeModel(resource); boolean _exists = resource.getResourceSet().getURIConverter().exists(resource.getURI(), resource.getResourceSet().getLoadOptions()); boolean _not = (!_exists); if (_not) { URI _uRI = resource.getURI(); String _plus = ("Skipping loading node model for synthetic resource " + _uRI); ResourceStorageLoadable.LOG.info(_plus); return; } final InputStream stream = resource.getResourceSet().getURIConverter().createInputStream(resource.getURI()); String _encoding = resource.getEncoding(); final InputStreamReader in = new InputStreamReader(stream, _encoding); final String completeContent = CharStreams.toString(in); final DeserializationConversionContext deserializationContext = new DeserializationConversionContext(resource, completeContent); final DataInputStream dataIn = new DataInputStream(inputStream); serializableNodeModel.readObjectData(dataIn, deserializationContext); EObject _head = IterableExtensions.<EObject>head(resource.getContents()); boolean _hasErrors = deserializationContext.hasErrors(); ParseResult _parseResult = new ParseResult(_head, serializableNodeModel.root, _hasErrors); resource.setParseResult(_parseResult); }
/** {@inheritDoc} */ @Override public void insertProxyModel() { // Node model can be accessed by: // 1. checking the adapters of an EObject ICompositeNode rootNode = null; TreeIterator<EObject> iterator = EcoreUtil.getAllProperContents(resource, false); while (iterator.hasNext()) { EObject eObject = iterator.next(); LazyLoadingCompositeNode node = new LazyLoadingCompositeNode(); eObject.eAdapters().add(node); if (rootNode == null) { rootNode = node; } } // 2. fetching the ParseResult instance of the Resource EObject emfRootObject = resource.getContents().isEmpty() ? null : resource.getContents().get(0); resource.setParseResult(new ParseResult(emfRootObject, rootNode, false)); // Add cache now, otherwise it will trigger model inference later. IResourceScopeCache cache = resource.getCache(); if (cache instanceof OnChangeEvictingCache) { ((OnChangeEvictingCache) cache).getOrCreate(resource); } }
/** * Read the node model from the given input stream. * * @param resource * target resource, must not be {@code null} * @param inputStream * input stream, must not be {@code null} * @param content * corresponding source content as required by node model, must not be {@code null} */ protected void readNodeModel(final StorageAwareResource resource, final InputStream inputStream, final String content) { try { // if this is a synthetic resource (i.e. tests or so, don't load the node model) if (!resource.getResourceSet().getURIConverter().exists(resource.getURI(), resource.getResourceSet().getLoadOptions())) { LOG.info("Skipping loading node model for synthetic resource " + resource.getURI()); //$NON-NLS-1$ return; } DeserializationConversionContext deserializationContext = new FixedDeserializationConversionContext(resource, content); DataInputStream dataIn = new DataInputStream(inputStream); SerializableNodeModel serializableNodeModel = new SerializableNodeModel(resource); serializableNodeModel.readObjectData(dataIn, deserializationContext); resource.setParseResult(new ParseResult(resource.getContents().get(0), serializableNodeModel.root, deserializationContext.hasErrors())); } catch (IOException e) { LOG.error(e.getMessage(), e); } }
/** {@inheritDoc} */ @Override public void insertModel(final byte[] modelData) throws IOException { ZippedByteArrayInputStream modelInputStream = new ZippedByteArrayInputStream(modelData); String completeSourceContent = resource.getSourceText(); DeserializationConversionContext deserializationContext = new DeserializationConversionContext((XtextResource) resource, completeSourceContent); SerializableNodeModel nodeModel = new SerializableNodeModel(); nodeModel.readObjectData(new DataInputStream(modelInputStream), deserializationContext); EObject emfRootObject = resource.getContents().isEmpty() ? null : resource.getContents().get(0); resource.setParseResult(new ParseResult(emfRootObject, nodeModel.root, deserializationContext.hasErrors()), true); modelInputStream.close(); }
@Override public IParseResult reparse(IParser parser, IParseResult previousParseResult, ReplaceRegion changedRegion) { if (isBrokenPreviousState(previousParseResult, changedRegion.getOffset())) { return fullyReparse(parser, previousParseResult, changedRegion); } ICompositeNode oldRootNode = previousParseResult.getRootNode(); Iterator<ILeafNode> leafNodes = oldRootNode.getLeafNodes().iterator(); ILeafNode leftNode = getLeftNode(leafNodes, changedRegion.getOffset()); if (leftNode == null) { return fullyReparse(parser, previousParseResult, changedRegion); } ILeafNode rightNode = getRightNode(leafNodes, changedRegion.getEndOffset()); if (rightNode == null) { return fullyReparse(parser, previousParseResult, changedRegion); } while(leafNodes.hasNext()) { if (leafNodes.next().getSyntaxErrorMessage() != null) { return fullyReparse(parser, previousParseResult, changedRegion); } } String originalText = oldRootNode.getText().substring(leftNode.getTotalOffset()); StringBuilder newTextBuilder = new StringBuilder(originalText); changedRegion.shiftBy(-leftNode.getTotalOffset()).applyTo(newTextBuilder); String newText = newTextBuilder.toString(); if (originalText.equals(newText)) { // nothing to do return previousParseResult; } int originalLength = rightNode.getTotalEndOffset() - leftNode.getTotalOffset(); int expectedLength = originalLength - changedRegion.getLength() + changedRegion.getText().length(); if (!isSameTokenSequence(originalText.substring(0, originalLength), newText, expectedLength)) { // different token sequence, cannot perform a partial parse run return fullyReparse(parser, previousParseResult, changedRegion); } PartialParsingPointers parsingPointers = calculatePartialParsingPointers(oldRootNode, leftNode, rightNode); ICompositeNode replaceMe = getReplacedNode(parsingPointers); if (replaceMe == null || replaceMe == oldRootNode || replaceMe.getOffset() == 0 && replaceMe.getEndOffset() == oldRootNode.getLength()) { return fullyReparse(parser, previousParseResult, changedRegion); } String reparseRegion = insertChangeIntoReplaceRegion(replaceMe, changedRegion); EObject oldSemanticElement = getOldSemanticElement(replaceMe, parsingPointers); if (oldSemanticElement == null) return fullyReparse(parser, previousParseResult, changedRegion); if (oldSemanticElement == replaceMe.getParent().getSemanticElement()) { throw new IllegalStateException("oldParent == oldElement"); } IParseResult newParseResult = doParseRegion(parser, parsingPointers, replaceMe, reparseRegion); if (newParseResult == null) { throw new IllegalStateException("Could not perform a partial parse operation"); } replaceOldSemanticElement(oldSemanticElement, previousParseResult, newParseResult); nodeModelBuilder.replaceAndTransferLookAhead(replaceMe, newParseResult.getRootNode()); ((ParseResult) newParseResult).setRootNode(oldRootNode); StringBuilder builder = new StringBuilder(oldRootNode.getText()); changedRegion.applyTo(builder); nodeModelBuilder.setCompleteContent(oldRootNode, builder.toString()); return newParseResult; }
/** * Sets the parse result for this resource. * * @param parseResult * the parse result containing the EMF and Node model, must not be {@code null} * @param refreshSyntaxErrors * whether diagnostics should be created for syntax errors */ @Override public void setParseResult(final ParseResult parseResult, final boolean refreshSyntaxErrors) { setParseResult(parseResult); if (refreshSyntaxErrors) { addSyntaxErrors(); } }
/** * Sets the parse result for this resource. * * @param parseResult * the parse result containing the EMF and Node model, must not be {@code null} * @param refreshSyntaxErrors * whether diagnostics should be created for syntax errors */ void setParseResult(final ParseResult parseResult, final boolean refreshSyntaxErrors);