Search in sources :

Example 61 with IParseResult

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

the class DefaultSemanticHighlightingCalculator method provideHighlightingFor.

@Override
public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) {
    if (resource == null)
        return;
    IParseResult parseResult = resource.getParseResult();
    if (parseResult == null || parseResult.getRootASTElement() == null)
        return;
    doProvideHighlightingFor(resource, acceptor, cancelIndicator);
}
Also used : IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 62 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project statecharts by Yakindu.

the class AbstractSCTResource method parse.

protected IParseResult parse(SpecificationElement element, String rule) {
    ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
    parserRule.setName(rule);
    String specification = element.getSpecification();
    IParseResult result = parser.parse(parserRule, new StringReader(specification != null ? specification : ""));
    createDiagnostics(result, element);
    return result;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) StringReader(java.io.StringReader) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 63 with IParseResult

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

the class ContentFormatter method exec.

protected TextEdit exec(IXtextDocument document, IRegion region, XtextResource resource) throws Exception {
    try {
        IParseResult parseResult = resource.getParseResult();
        if (parseResult != null && parseResult.getRootASTElement() != null) {
            FormatterRequest request = requestProvider.get();
            initRequest(document, region, resource, request);
            IFormatter2 formatter = formatterProvider.get();
            List<ITextReplacement> replacements = formatter.format(request);
            final TextEdit mte = createTextEdit(replacements);
            return mte;
        }
    } catch (Exception e) {
        LOG.error("Error formatting " + resource.getURI() + ": " + e.getMessage(), e);
    }
    return new MultiTextEdit();
}
Also used : IFormatter2(org.eclipse.xtext.formatting2.IFormatter2) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) IParseResult(org.eclipse.xtext.parser.IParseResult) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) BadLocationException(org.eclipse.jface.text.BadLocationException) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 64 with IParseResult

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

the class ExpressionUtil method findSelectedExpression.

/**
 * @return the smallest single expression containing the selection.
 */
public XExpression findSelectedExpression(XtextResource resource, ITextSelection selection) {
    IParseResult parseResult = resource.getParseResult();
    if (parseResult != null) {
        ICompositeNode rootNode = parseResult.getRootNode();
        INode node = NodeModelUtils.findLeafNodeAtOffset(rootNode, selection.getOffset());
        if (node == null) {
            return null;
        }
        if (isHidden(node)) {
            if (selection.getLength() > node.getLength()) {
                node = NodeModelUtils.findLeafNodeAtOffset(rootNode, node.getEndOffset());
            } else {
                node = NodeModelUtils.findLeafNodeAtOffset(rootNode, selection.getOffset() - 1);
            }
        } else if (node.getOffset() == selection.getOffset() && !isBeginOfExpression(node)) {
            node = NodeModelUtils.findLeafNodeAtOffset(rootNode, selection.getOffset() - 1);
        }
        if (node != null) {
            EObject currentSemanticElement = NodeModelUtils.findActualSemanticObjectFor(node);
            while (!(contains(currentSemanticElement, node, selection) && currentSemanticElement instanceof XExpression)) {
                node = nextNodeForFindSelectedExpression(currentSemanticElement, node, selection);
                if (node == null)
                    return null;
                currentSemanticElement = NodeModelUtils.findActualSemanticObjectFor(node);
            }
            return (XExpression) currentSemanticElement;
        }
    }
    return null;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XExpression(org.eclipse.xtext.xbase.XExpression) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 65 with IParseResult

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

the class AbstractEObjectHover method getXtextElementAt.

/**
 * Call this method only from within an IUnitOfWork
 */
protected Pair<EObject, IRegion> getXtextElementAt(XtextResource resource, final int offset) {
    // check for cross reference
    EObject crossLinkedEObject = eObjectAtOffsetHelper.resolveCrossReferencedElementAt(resource, offset);
    if (crossLinkedEObject != null) {
        if (!crossLinkedEObject.eIsProxy()) {
            IParseResult parseResult = resource.getParseResult();
            if (parseResult != 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) {
                    ITextRegion leafRegion = leafNode.getTextRegion();
                    return Tuples.create(crossLinkedEObject, (IRegion) new Region(leafRegion.getOffset(), leafRegion.getLength()));
                }
            }
        }
    } else {
        EObject o = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
        if (o != null) {
            ITextRegion region = locationInFileProvider.getSignificantTextRegion(o);
            final IRegion region2 = new Region(region.getOffset(), region.getLength());
            if (TextUtilities.overlaps(region2, new Region(offset, 0)))
                return Tuples.create(o, region2);
        }
    }
    return null;
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) Region(org.eclipse.jface.text.Region) ITextRegion(org.eclipse.xtext.util.ITextRegion) IRegion(org.eclipse.jface.text.IRegion) IParseResult(org.eclipse.xtext.parser.IParseResult) IRegion(org.eclipse.jface.text.IRegion)

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