Search in sources :

Example 1 with IParseResult

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

the class XtextReconcilerDebugger method assertResouceParsedCorrectly.

public void assertResouceParsedCorrectly(XtextResource resource, final ReconcilerReplaceRegion region) {
    IParseResult parseResult = resource.getParseResult();
    if (parseResult != null) {
        ICompositeNode rootNode = parseResult.getRootNode();
        final String resourceContent = rootNode.getText();
        IParseResult reparseResult = parser.parse(new StringReader(resourceContent));
        if (!emfStructureComparator.isSameStructure(parseResult.getRootASTElement(), reparseResult.getRootASTElement())) {
            new DisplayRunnable() {

                @Override
                protected void run() throws Exception {
                    LOG.error("PartialParsing produced wrong model");
                    LOG.error("Events: \n\t" + Joiner.on("\n\t").join(region.getDocumentEvents()));
                    LOG.error("ReplaceRegion: \n\t'" + region + "'");
                    MessageDialog.openError(Display.getCurrent().getActiveShell(), "XtextReconcilerDebugger", "PartialParsing produced wrong model." + "\n\nSee log for details.");
                }
            };
        }
    }
}
Also used : StringReader(java.io.StringReader) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) DisplayRunnable(org.eclipse.xtext.ui.util.DisplayRunnable) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 2 with IParseResult

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

the class XtextReconcilerDebugger method assertModelInSyncWithDocument.

public void assertModelInSyncWithDocument(IDocument document, XtextResource resource, final ReconcilerReplaceRegion region) {
    if (document instanceof IDocumentExtension4 && resource != null) {
        long beforeGet = ((IDocumentExtension4) document).getModificationStamp();
        final String documentContent = document.get();
        long afterGet = ((IDocumentExtension4) document).getModificationStamp();
        if (beforeGet == afterGet && beforeGet == resource.getModificationStamp()) {
            IParseResult parseResult = resource.getParseResult();
            if (parseResult != null) {
                ICompositeNode rootNode = parseResult.getRootNode();
                final String resourceContent = rootNode.getText();
                if (!resourceContent.equals(documentContent)) {
                    new DisplayRunnable() {

                        @Override
                        protected void run() throws Exception {
                            LOG.error("XtextDocument and XtextResource have run out of sync:\n" + DiffUtil.diff(documentContent, resourceContent));
                            LOG.error("Events: \n\t" + Joiner.on("\n\t").join(region.getDocumentEvents()));
                            LOG.error("ReplaceRegion: \n\t'" + region + "'");
                            MessageDialog.openError(Display.getCurrent().getActiveShell(), "XtextReconcilerDebugger", "XtextDocument and XtextResource have run out of sync." + "\n\nSee log for details.");
                        }
                    }.syncExec();
                } else {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Model and document are in sync");
                }
            }
        }
    }
}
Also used : IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) DisplayRunnable(org.eclipse.xtext.ui.util.DisplayRunnable) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 3 with IParseResult

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

the class AstSelectionProvider method selectEnclosing.

public ITextRegion selectEnclosing(XtextResource resource, ITextRegion currentEditorSelection) {
    Pair<EObject, EObject> currentlySelected = getSelectedAstElements(resource, currentEditorSelection);
    if (currentlySelected == null) {
        IParseResult parseResult = resource.getParseResult();
        if (parseResult != null) {
            ICompositeNode rootNode = parseResult.getRootNode();
            int offset = getSelectionOffset(rootNode, currentEditorSelection);
            INode node = findLeafNodeAtOffset(rootNode, offset);
            if (node != null) {
                ITextRegion fineGrainedRegion = computeInitialFineGrainedSelection(node, currentEditorSelection);
                if (fineGrainedRegion != null) {
                    selectionHistory.clear();
                    register(currentEditorSelection);
                    return register(fineGrainedRegion);
                }
                EObject eObject = findSemanticObjectFor(node);
                return register(getTextRegion(eObject));
            }
        }
    } else {
        EObject first = currentlySelected.getFirst();
        if (first.eContainer() != null) {
            return register(getTextRegion(first.eContainer()));
        }
    }
    return ITextRegion.EMPTY_REGION;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) 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 4 with IParseResult

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

the class AstSelectionProvider method getEObjectAtOffset.

protected EObject getEObjectAtOffset(XtextResource resource, ITextRegion currentEditorSelection) {
    IParseResult parseResult = resource.getParseResult();
    if (parseResult != null) {
        ICompositeNode rootNode = parseResult.getRootNode();
        INode nodeAtOffset = findLeafNodeAtOffset(rootNode, currentEditorSelection.getOffset());
        return findSemanticObjectFor(nodeAtOffset);
    }
    return null;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 5 with IParseResult

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

the class ExpressionUtil method trimSelection.

protected ITextSelection trimSelection(XtextResource resource, ITextSelection selection) {
    IParseResult parseResult = resource.getParseResult();
    if (parseResult != null) {
        String model = parseResult.getRootNode().getText();
        String selectedText = model.substring(selection.getOffset(), selection.getOffset() + selection.getLength());
        String trimmedSelection = selectedText.trim();
        return new TextSelection(selection.getOffset() + selectedText.indexOf(trimmedSelection), trimmedSelection.length());
    }
    return null;
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection) TextSelection(org.eclipse.jface.text.TextSelection) IParseResult(org.eclipse.xtext.parser.IParseResult)

Aggregations

IParseResult (org.eclipse.xtext.parser.IParseResult)68 EObject (org.eclipse.emf.ecore.EObject)27 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)18 StringReader (java.io.StringReader)16 XtextResource (org.eclipse.xtext.resource.XtextResource)13 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)12 INode (org.eclipse.xtext.nodemodel.INode)11 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)10 ParserRule (org.eclipse.xtext.ParserRule)8 Test (org.junit.Test)7 ITextRegion (org.eclipse.xtext.util.ITextRegion)6 TextRegion (org.eclipse.xtext.util.TextRegion)5 Resource (org.eclipse.emf.ecore.resource.Resource)4 URI (org.eclipse.emf.common.util.URI)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 ArrayList (java.util.ArrayList)2 List (java.util.List)2