Search in sources :

Example 11 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode 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 12 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-eclipse by eclipse.

the class OutlineNodeFactory method createEObjectNode.

public EObjectNode createEObjectNode(IOutlineNode parentNode, EObject modelElement, ImageDescriptor imageDescriptor, Object text, boolean isLeaf) {
    EObjectNode eObjectNode = new EObjectNode(modelElement, parentNode, imageDescriptor, text, isLeaf);
    ICompositeNode parserNode = NodeModelUtils.getNode(modelElement);
    if (parserNode != null)
        eObjectNode.setTextRegion(parserNode.getTextRegion());
    if (isLocalElement(parentNode, modelElement))
        eObjectNode.setShortTextRegion(locationInFileProvider.getSignificantTextRegion(modelElement));
    return eObjectNode;
}
Also used : ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 13 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-eclipse by eclipse.

the class DefaultQuickfixProvider method getUnresolvedEReference.

protected EReference getUnresolvedEReference(final Issue issue, EObject target) {
    final ICompositeNode node = NodeModelUtils.getNode(target);
    if (node == null)
        return null;
    ICompositeNode rootNode = node.getRootNode();
    ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNode, issue.getOffset());
    CrossReference crossReference = findCrossReference(target, leaf);
    if (crossReference != null) {
        return GrammarUtil.getReference(crossReference, target.eClass());
    }
    return null;
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) CrossReference(org.eclipse.xtext.CrossReference)

Example 14 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-eclipse by eclipse.

the class DefaultQuickfixProvider method createLinkingIssueResolutions.

public void createLinkingIssueResolutions(final Issue issue, final IssueResolutionAcceptor issueResolutionAcceptor) {
    final IModificationContext modificationContext = modificationContextFactory.createModificationContext(issue);
    final IXtextDocument xtextDocument = modificationContext.getXtextDocument();
    if (xtextDocument == null)
        return;
    xtextDocument.readOnly(new CancelableUnitOfWork<Void, XtextResource>() {

        IssueResolutionAcceptor myAcceptor = null;

        @Override
        public java.lang.Void exec(XtextResource state, CancelIndicator cancelIndicator) throws Exception {
            myAcceptor = getCancelableAcceptor(issueResolutionAcceptor, cancelIndicator);
            EObject target = state.getEObject(issue.getUriToProblem().fragment());
            EReference reference = getUnresolvedEReference(issue, target);
            if (reference == null)
                return null;
            fixUnresolvedReference(issue, xtextDocument, target, reference);
            return null;
        }

        protected void fixUnresolvedReference(final Issue issue, final IXtextDocument xtextDocument, EObject target, EReference reference) throws BadLocationException {
            boolean caseInsensitive = caseInsensitivityHelper.isIgnoreCase(reference);
            EObject crossReferenceTerminal = getCrossReference(issue, target);
            String ruleName = null;
            Keyword keyword = null;
            if (crossReferenceTerminal instanceof RuleCall) {
                RuleCall ruleCall = (RuleCall) crossReferenceTerminal;
                ruleName = ruleCall.getRule().getName();
            } else if (crossReferenceTerminal instanceof Keyword) {
                keyword = (Keyword) crossReferenceTerminal;
            }
            String issueString = xtextDocument.get(issue.getOffset(), issue.getLength());
            IScope scope = scopeProvider.getScope(target, reference);
            List<IEObjectDescription> discardedDescriptions = Lists.newArrayList();
            Set<String> qualifiedNames = Sets.newHashSet();
            int addedDescriptions = 0;
            int checkedDescriptions = 0;
            for (IEObjectDescription referableElement : queryScope(scope)) {
                String referableElementQualifiedName = qualifiedNameConverter.toString(referableElement.getQualifiedName());
                if (similarityMatcher.isSimilar(issueString, qualifiedNameConverter.toString(referableElement.getName()))) {
                    addedDescriptions++;
                    createResolution(issueString, referableElement, ruleName, keyword, caseInsensitive);
                    qualifiedNames.add(referableElementQualifiedName);
                } else {
                    if (qualifiedNames.add(referableElementQualifiedName))
                        discardedDescriptions.add(referableElement);
                }
                checkedDescriptions++;
                if (checkedDescriptions > 100)
                    break;
            }
            if (discardedDescriptions.size() + addedDescriptions <= 5) {
                for (IEObjectDescription referableElement : discardedDescriptions) {
                    createResolution(issueString, referableElement, ruleName, keyword, caseInsensitive);
                }
            }
        }

        protected AbstractElement getCrossReference(final Issue issue, EObject target) {
            final ICompositeNode node = NodeModelUtils.getNode(target);
            if (node == null)
                throw new IllegalStateException("Cannot happen since we found a reference");
            ICompositeNode rootNode = node.getRootNode();
            ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNode, issue.getOffset());
            CrossReference crossReference = findCrossReference(target, leaf);
            return crossReference.getTerminal();
        }

        public void createResolution(String issueString, IEObjectDescription solution, String ruleName, Keyword keyword, boolean caseInsensitive) {
            String replacement = qualifiedNameConverter.toString(solution.getName());
            String replaceLabel = fixCrossReferenceLabel(issueString, replacement);
            if (keyword != null) {
                if (caseInsensitive && !replacement.equalsIgnoreCase(keyword.getValue()))
                    return;
                if (!caseInsensitive && !replacement.equals(keyword.getValue()))
                    return;
            } else if (ruleName != null) {
                replacement = converter.convertToString(replacement, ruleName);
                if (replacement == null) {
                    return;
                }
            } else {
                logger.error("either keyword or ruleName have to present", new IllegalStateException());
            }
            myAcceptor.accept(issue, replaceLabel, replaceLabel, fixCrossReferenceImage(issueString, replacement), new ReplaceModification(issue, replacement));
        }
    });
}
Also used : Issue(org.eclipse.xtext.validation.Issue) Set(java.util.Set) XtextResource(org.eclipse.xtext.resource.XtextResource) RuleCall(org.eclipse.xtext.RuleCall) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ArrayList(java.util.ArrayList) List(java.util.List) EReference(org.eclipse.emf.ecore.EReference) Keyword(org.eclipse.xtext.Keyword) AbstractElement(org.eclipse.xtext.AbstractElement) BadLocationException(org.eclipse.jface.text.BadLocationException) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) CrossReference(org.eclipse.xtext.CrossReference) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 15 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-eclipse by eclipse.

the class DefaultOutlineTreeProvider method createEObjectNode.

/**
 * @since 2.1
 */
protected EObjectNode createEObjectNode(IOutlineNode parentNode, EObject modelElement, Image image, Object text, boolean isLeaf) {
    EObjectNode eObjectNode = new EObjectNode(modelElement, parentNode, image, text, isLeaf);
    ICompositeNode parserNode = NodeModelUtils.getNode(modelElement);
    if (parserNode != null)
        eObjectNode.setTextRegion(parserNode.getTextRegion());
    if (isLocalElement(parentNode, modelElement))
        eObjectNode.setShortTextRegion(locationInFileProvider.getSignificantTextRegion(modelElement));
    return eObjectNode;
}
Also used : ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Aggregations

ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)263 Test (org.junit.Test)87 INode (org.eclipse.xtext.nodemodel.INode)79 EObject (org.eclipse.emf.ecore.EObject)70 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)57 XtextResource (org.eclipse.xtext.resource.XtextResource)41 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)24 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)20 Resource (org.eclipse.emf.ecore.resource.Resource)19 Model (org.eclipse.xtext.valueconverter.bug250313.Model)15 IParseResult (org.eclipse.xtext.parser.IParseResult)14 ParserRule (org.eclipse.xtext.ParserRule)12 ITextRegion (org.eclipse.xtext.util.ITextRegion)12 RuleCall (org.eclipse.xtext.RuleCall)11 CrossReference (org.eclipse.xtext.CrossReference)10 ArrayList (java.util.ArrayList)8 List (java.util.List)8 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)8 AbstractRule (org.eclipse.xtext.AbstractRule)8 Keyword (org.eclipse.xtext.Keyword)8