Search in sources :

Example 56 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-core by eclipse.

the class PartialParsingProcessor method processFile.

@Override
public String processFile(String completeData, String data, int offset, int len, String change) throws Exception {
    IParseResult initialParseResult = parser.parse(new StringReader(data));
    String newData = applyDelta(data, offset, len, change);
    ReplaceRegion replaceRegion = new ReplaceRegion(offset, len, change);
    try {
        IParseResult reparsed = parser.reparse(initialParseResult, replaceRegion);
        IParseResult parsedFromScratch = parser.parse(new StringReader(newData));
        assertEqual(data, newData, parsedFromScratch, reparsed);
        return newData;
    } catch (Throwable e) {
        ComparisonFailure throwMe = new ComparisonFailure(e.getMessage(), newData, replaceRegion + DELIM + data);
        throwMe.initCause(e);
        throw throwMe;
    }
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) ComparisonFailure(org.junit.ComparisonFailure) StringReader(java.io.StringReader) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 57 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-core by eclipse.

the class TestLanguageRenameService method getElementWithIdentifierAt.

@Override
protected EObject getElementWithIdentifierAt(XtextResource xtextResource, int offset) {
    if (offset >= 0) {
        if (xtextResource != null) {
            IParseResult parseResult = xtextResource.getParseResult();
            if (parseResult != null) {
                ICompositeNode rootNode = parseResult.getRootNode();
                if (rootNode != null) {
                    ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNode, offset);
                    if (leaf != null && isIdentifier(leaf)) {
                        EObject element = eObjectAtOffsetHelper.resolveElementAt(xtextResource, offset);
                        if (element != null) {
                            IQualifiedNameProvider nameProvider = xtextResource.getResourceServiceProvider().get(IQualifiedNameProvider.class);
                            QualifiedName fqn = nameProvider.getFullyQualifiedName(element);
                            if (fqn != null) {
                                String leafText = NodeModelUtils.getTokenText(leaf);
                                if (fqn.getSegmentCount() == 1 && Objects.equal(fqn.toString(), leafText) || Objects.equal(fqn.getLastSegment(), leafText)) {
                                    return element;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) IQualifiedNameProvider(org.eclipse.xtext.naming.IQualifiedNameProvider) EObject(org.eclipse.emf.ecore.EObject) QualifiedName(org.eclipse.xtext.naming.QualifiedName) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 58 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-core by eclipse.

the class DefaultDocumentHighlightService method getDocumentHighlights.

public List<DocumentHighlight> getDocumentHighlights(final XtextResource resource, final int offset) {
    if (resource == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Resource was null.");
        }
        return emptyList();
    }
    final URI uri = resource.getURI();
    if (offset < 0) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Invalid offset argument. Offset must be a non-negative integer for resource: " + uri);
        }
        return emptyList();
    }
    final IParseResult parseResult = resource.getParseResult();
    if (parseResult == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Parse result was null for resource: " + uri);
        }
        return emptyList();
    }
    final ICompositeNode rootNode = parseResult.getRootNode();
    final String docContent = rootNode.getText();
    final int docLength = docContent.length();
    if (offset >= docLength) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Offset exceeds document lenght. Document was " + docLength + " and offset was: " + offset + " for resource: " + uri);
        }
        return emptyList();
    }
    final EObject selectedElement = offsetHelper.resolveElementAt(resource, offset);
    if (!isDocumentHighlightAvailableFor(selectedElement, resource, offset)) {
        return emptyList();
    }
    final Supplier<Document> docSupplier = Suppliers.memoize(() -> new Document(UNUSED_VERSION, docContent));
    Iterable<URI> targetURIs = getTargetURIs(selectedElement);
    if (!(targetURIs instanceof TargetURIs)) {
        final TargetURIs result = targetURIsProvider.get();
        result.addAllURIs(targetURIs);
        targetURIs = result;
    }
    final Builder<DocumentHighlight> resultBuilder = ImmutableList.<DocumentHighlight>builder();
    final Acceptor acceptor = (Acceptor2) (source, sourceURI, eReference, index, targetOrProxy, targetURI) -> {
        final ITextRegion region = locationInFileProvider.getSignificantTextRegion(source, eReference, index);
        if (!isNullOrEmpty(region)) {
            resultBuilder.add(textRegionTransformer.apply(docSupplier.get(), region, DocumentHighlightKind.Read));
        }
    };
    referenceFinder.findReferences((TargetURIs) targetURIs, resource, acceptor, new NullProgressMonitor());
    if (resource.equals(selectedElement.eResource())) {
        final ITextRegion region = locationInFileProvider.getSignificantTextRegion(selectedElement);
        if (!isNullOrEmpty(region)) {
            resultBuilder.add(textRegionTransformer.apply(docSupplier.get(), region, DocumentHighlightKind.Write));
        }
    }
    return FluentIterable.from(resultBuilder.build()).toSortedList(comparator);
}
Also used : DocumentHighlight(org.eclipse.lsp4j.DocumentHighlight) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Acceptor(org.eclipse.xtext.findReferences.IReferenceFinder.Acceptor) TargetURIs(org.eclipse.xtext.findReferences.TargetURIs) Document(org.eclipse.xtext.ide.server.Document) URI(org.eclipse.emf.common.util.URI) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 59 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-core by eclipse.

the class HoverService method createContext.

protected HoverContext createContext(Document document, XtextResource resource, int offset) {
    EObject crossLinkedEObject = eObjectAtOffsetHelper.resolveCrossReferencedElementAt(resource, offset);
    if (crossLinkedEObject != null) {
        if (crossLinkedEObject.eIsProxy()) {
            return null;
        }
        IParseResult parseResult = resource.getParseResult();
        if (parseResult == null) {
            return null;
        }
        ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset);
        if (leafNode != null && leafNode.isHidden() && leafNode.getOffset() == offset) {
            leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset - 1);
        }
        if (leafNode == null) {
            return null;
        }
        ITextRegion leafRegion = leafNode.getTextRegion();
        return new HoverContext(document, resource, offset, leafRegion, crossLinkedEObject);
    }
    EObject element = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
    if (element == null) {
        return null;
    }
    ITextRegion region = locationInFileProvider.getSignificantTextRegion(element);
    return new HoverContext(document, resource, offset, region, element);
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 60 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-core by eclipse.

the class DefaultSemanticHighlightingCalculator method searchAndHighlightElements.

protected void searchAndHighlightElements(XtextResource resource, IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) {
    IParseResult parseResult = resource.getParseResult();
    if (parseResult == null)
        throw new IllegalStateException("resource#parseResult may not be null");
    EObject element = parseResult.getRootASTElement();
    highlightElementRecursively(element, acceptor, cancelIndicator);
}
Also used : EObject(org.eclipse.emf.ecore.EObject) IParseResult(org.eclipse.xtext.parser.IParseResult)

Aggregations

IParseResult (org.eclipse.xtext.parser.IParseResult)70 EObject (org.eclipse.emf.ecore.EObject)27 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)19 StringReader (java.io.StringReader)16 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)13 XtextResource (org.eclipse.xtext.resource.XtextResource)13 INode (org.eclipse.xtext.nodemodel.INode)11 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)10 ParserRule (org.eclipse.xtext.ParserRule)8 ITextRegion (org.eclipse.xtext.util.ITextRegion)7 Test (org.junit.Test)7 TextRegion (org.eclipse.xtext.util.TextRegion)5 Resource (org.eclipse.emf.ecore.resource.Resource)4 ArrayList (java.util.ArrayList)3 WrappedException (org.eclipse.emf.common.util.WrappedException)3 ParseResult (org.eclipse.xtext.parser.ParseResult)3 Scope (org.yakindu.sct.model.sgraph.Scope)3 IOException (java.io.IOException)2 List (java.util.List)2 URI (org.eclipse.emf.common.util.URI)2