Search in sources :

Example 31 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.

the class ContentFormatter method format.

@Override
public void format(IDocument document, IRegion region) {
    IXtextDocument doc = xtextDocumentUtil.getXtextDocument(document);
    TextEdit r = doc.priorityReadOnly(new FormattingUnitOfWork(doc, region));
    try {
        if (r != null)
            r.apply(document);
    } catch (BadLocationException e) {
        throw new RuntimeException(e);
    }
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 32 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.

the class HighlightingReconciler method uninstall.

/**
 * Uninstall this reconciler from the editor
 */
public void uninstall() {
    if (presenter != null)
        presenter.setCanceled(true);
    if (sourceViewer.getDocument() != null) {
        if (oldCalculator != null || newCalculator != null) {
            IXtextDocument document = xtextDocumentUtil.getXtextDocument(sourceViewer);
            if (document != null) {
                document.removeModelListener(this);
            }
            sourceViewer.removeTextInputListener(this);
        }
    }
    editor = null;
    sourceViewer = null;
    presenter = null;
}
Also used : IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 33 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument 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.tryReadOnly(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 34 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.

the class XtextReconciler method uninstall.

@Override
public void uninstall() {
    if (isInstalled) {
        textViewer.removeTextInputListener(textInputListener);
        isInstalled = false;
        if (documentListener != null) {
            if (textViewer instanceof ISourceViewerExtension4) {
                ContentAssistantFacade facade = ((ISourceViewerExtension4) textViewer).getContentAssistantFacade();
                facade.removeCompletionListener(documentListener);
            }
            if (textViewer.getDocument() instanceof IXtextDocument) {
                ((IXtextDocument) textViewer.getDocument()).removeXtextDocumentContentObserver(documentListener);
            }
        }
        cancel();
    }
}
Also used : ContentAssistantFacade(org.eclipse.jface.text.source.ContentAssistantFacade) ISourceViewerExtension4(org.eclipse.jface.text.source.ISourceViewerExtension4) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 35 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.

the class XtextReconciler method run.

@Override
protected IStatus run(final IProgressMonitor monitor) {
    if (monitor.isCanceled() || paused)
        return Status.CANCEL_STATUS;
    if (pendingChanges.isEmpty()) {
        return Status.OK_STATUS;
    }
    long start = System.currentTimeMillis();
    final IXtextDocument document = xtextDocumentUtil.getXtextDocument(textViewer);
    if (document instanceof XtextDocument) {
        ((XtextDocument) document).internalModify(new IUnitOfWork.Void<XtextResource>() {

            @Override
            public void process(XtextResource state) throws Exception {
                doRun(state, monitor);
            }
        });
    }
    if (monitor.isCanceled()) {
        return Status.CANCEL_STATUS;
    }
    if (log.isDebugEnabled())
        // $NON-NLS-1$
        log.debug("Reconciliation finished. Time required: " + (System.currentTimeMillis() - start));
    return Status.OK_STATUS;
}
Also used : IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) XtextResource(org.eclipse.xtext.resource.XtextResource) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Aggregations

IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)147 XtextResource (org.eclipse.xtext.resource.XtextResource)51 Test (org.junit.Test)46 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)38 IFile (org.eclipse.core.resources.IFile)37 BadLocationException (org.eclipse.jface.text.BadLocationException)26 AbstractEditorTest (org.eclipse.xtext.ui.testing.AbstractEditorTest)26 IUnitOfWork (org.eclipse.xtext.util.concurrent.IUnitOfWork)25 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)23 EObject (org.eclipse.emf.ecore.EObject)22 DefaultFoldingRegionProvider (org.eclipse.xtext.ui.editor.folding.DefaultFoldingRegionProvider)18 FoldedPosition (org.eclipse.xtext.ui.editor.folding.FoldedPosition)18 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)16 List (java.util.List)12 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)12 ArrayList (java.util.ArrayList)11 Issue (org.eclipse.xtext.validation.Issue)11 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)10 URI (org.eclipse.emf.common.util.URI)9 IRegion (org.eclipse.jface.text.IRegion)9