Search in sources :

Example 36 with IXtextDocument

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

the class XtextReconciler method handleInputDocumentChanged.

protected void handleInputDocumentChanged(IDocument oldInput, IDocument newInput) {
    if (Display.getCurrent() == null) {
        log.error("Changes to the document must only be applied from the Display thread to keep them ordered", new Exception());
    }
    if (shouldInstallCompletionListener) {
        ContentAssistantFacade facade = ((ISourceViewerExtension4) textViewer).getContentAssistantFacade();
        if (facade != null) {
            facade.addCompletionListener(documentListener);
        }
        shouldInstallCompletionListener = false;
    }
    if (oldInput != newInput) {
        if (oldInput instanceof IXtextDocument) {
            ((IXtextDocument) oldInput).removeXtextDocumentContentObserver(documentListener);
        }
        if (newInput instanceof IXtextDocument) {
            ((IXtextDocument) newInput).addXtextDocumentContentObserver(documentListener);
            final IXtextDocument document = xtextDocumentUtil.getXtextDocument(textViewer);
            strategy.setDocument(document);
            if (!initalProcessDone && strategy instanceof IReconcilingStrategyExtension) {
                initalProcessDone = true;
                IReconcilingStrategyExtension reconcilingStrategyExtension = (IReconcilingStrategyExtension) strategy;
                reconcilingStrategyExtension.initialReconcile();
            }
        }
    }
    if (oldInput != null && newInput != null) {
        handleDocumentChanged(new InputChangedDocumentEvent(oldInput, newInput));
    }
}
Also used : IReconcilingStrategyExtension(org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension) ContentAssistantFacade(org.eclipse.jface.text.source.ContentAssistantFacade) ISourceViewerExtension4(org.eclipse.jface.text.source.ISourceViewerExtension4) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 37 with IXtextDocument

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

the class ImportsAwareClipboardAction method getXtextDocument.

private IXtextDocument getXtextDocument() {
    XtextEditor xtextEditor = EditorUtils.getXtextEditor(getTextEditor());
    IXtextDocument document = xtextEditor.getDocument();
    return document;
}
Also used : XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 38 with IXtextDocument

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

the class XbaseQuickfixProvider method moveUp.

protected void moveUp(ICompositeNode node, ICompositeNode previousNode, IModificationContext context) throws BadLocationException {
    IXtextDocument document = context.getXtextDocument();
    String text = node.getText() + previousNode.getText();
    text = text.trim();
    remove(document, node);
    document.replace(previousNode.getOffset(), previousNode.getLength(), text);
}
Also used : IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 39 with IXtextDocument

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

the class XbaseQuickfixProvider method fixUnreachableIfBlock.

@Fix(IssueCodes.UNREACHABLE_IF_BLOCK)
public void fixUnreachableIfBlock(final Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Remove if block", "Remove if block", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            XIfExpression ifExpression = EcoreUtil2.getContainerOfType(element, XIfExpression.class);
            if (ifExpression == null) {
                return;
            }
            ICompositeNode node = NodeModelUtils.findActualNodeFor(ifExpression);
            if (node == null) {
                return;
            }
            int[] offsetAndLength = getOffsetAndLength(ifExpression, node);
            context.getXtextDocument().replace(offsetAndLength[0], offsetAndLength[1], "");
        }
    });
    acceptor.accept(issue, "Move if block up", "Move if block up", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            XIfExpression ifExpression = EcoreUtil2.getContainerOfType(element, XIfExpression.class);
            if (ifExpression == null) {
                return;
            }
            ICompositeNode node = NodeModelUtils.findActualNodeFor(ifExpression);
            if (node == null) {
                return;
            }
            XIfExpression firstIfExpression = getFirstIfExpression(ifExpression);
            if (firstIfExpression == null) {
                return;
            }
            XInstanceOfExpression actualIfPart = (XInstanceOfExpression) ifExpression.getIf();
            XAbstractFeatureCall actualFeatureCall = (XAbstractFeatureCall) actualIfPart.getExpression();
            JvmIdentifiableElement actualFeature = actualFeatureCall.getFeature();
            ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, firstIfExpression);
            LightweightTypeReference actualTypeReference = owner.toLightweightTypeReference(actualIfPart.getType());
            List<XExpression> ifParts = collectIfParts(firstIfExpression, new ArrayList<XExpression>());
            for (XExpression previousIfPart : ifParts) {
                if (actualIfPart == previousIfPart) {
                    return;
                }
                if (!(previousIfPart instanceof XInstanceOfExpression)) {
                    continue;
                }
                XInstanceOfExpression instanceOfExpression = (XInstanceOfExpression) previousIfPart;
                if (!(instanceOfExpression.getExpression() instanceof XAbstractFeatureCall)) {
                    continue;
                }
                XAbstractFeatureCall previousFeatureCall = (XAbstractFeatureCall) instanceOfExpression.getExpression();
                if (previousFeatureCall.getFeature() != actualFeature) {
                    continue;
                }
                LightweightTypeReference previousTypeReference = owner.toLightweightTypeReference(instanceOfExpression.getType());
                if (typesOrderUtil.isHandled(actualTypeReference, previousTypeReference)) {
                    ICompositeNode previousNode = NodeModelUtils.findActualNodeFor(instanceOfExpression.eContainer());
                    if (previousNode == null) {
                        return;
                    }
                    int[] offsetAndLength = getOffsetAndLength(ifExpression, node);
                    int offset = offsetAndLength[0];
                    int length = offsetAndLength[1];
                    int endOffset = offset + length;
                    String text = node.getRootNode().getText().substring(offset, endOffset).trim();
                    if (text.startsWith("else")) {
                        text = text.substring("else".length()).trim();
                    }
                    if (!text.endsWith("else")) {
                        text = text + " else ";
                    } else {
                        text = text + " ";
                    }
                    IXtextDocument document = context.getXtextDocument();
                    document.replace(offset, length, "");
                    document.replace(previousNode.getOffset(), 0, text);
                    return;
                }
            }
        }

        private XIfExpression getFirstIfExpression(XIfExpression ifExpression) {
            EObject container = ifExpression.eContainer();
            if (container instanceof XIfExpression) {
                XIfExpression parentIfExpression = (XIfExpression) container;
                if (parentIfExpression.getElse() == ifExpression) {
                    return getFirstIfExpression(parentIfExpression);
                }
            }
            return ifExpression;
        }

        private List<XExpression> collectIfParts(XIfExpression expression, List<XExpression> result) {
            result.add(expression.getIf());
            if (expression.getElse() instanceof XIfExpression) {
                collectIfParts((XIfExpression) expression.getElse(), result);
            }
            return result;
        }
    });
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) ArrayList(java.util.ArrayList) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) XAbstractFeatureCall(org.eclipse.xtext.xbase.XAbstractFeatureCall) WrappedException(org.eclipse.emf.common.util.WrappedException) BadLocationException(org.eclipse.jface.text.BadLocationException) XInstanceOfExpression(org.eclipse.xtext.xbase.XInstanceOfExpression) XIfExpression(org.eclipse.xtext.xbase.XIfExpression) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) XExpression(org.eclipse.xtext.xbase.XExpression) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 40 with IXtextDocument

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

the class XbaseQuickfixProvider method createLinkingIssueResolutions.

/**
 * Filter quickfixes for types and constructors.
 */
@Override
public void createLinkingIssueResolutions(final Issue issue, final IssueResolutionAcceptor issueResolutionAcceptor) {
    final IModificationContext modificationContext = getModificationContextFactory().createModificationContext(issue);
    final IXtextDocument xtextDocument = modificationContext.getXtextDocument();
    if (xtextDocument != null) {
        xtextDocument.tryReadOnly(new CancelableUnitOfWork<Void, XtextResource>() {

            @Override
            public java.lang.Void exec(XtextResource state, CancelIndicator cancelIndicator) throws Exception {
                try {
                    EObject target = state.getEObject(issue.getUriToProblem().fragment());
                    EReference reference = getUnresolvedEReference(issue, target);
                    if (reference != null && reference.getEReferenceType() != null) {
                        createLinkingIssueQuickfixes(issue, getCancelableAcceptor(issueResolutionAcceptor, cancelIndicator), xtextDocument, state, target, reference);
                    }
                } catch (WrappedException e) {
                // issue information seems to be out of sync, e.g. there is no
                // EObject with the given fragment
                }
                return null;
            }
        });
    }
}
Also used : WrappedException(org.eclipse.emf.common.util.WrappedException) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XtextResource(org.eclipse.xtext.resource.XtextResource) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) WrappedException(org.eclipse.emf.common.util.WrappedException) BadLocationException(org.eclipse.jface.text.BadLocationException) EReference(org.eclipse.emf.ecore.EReference) 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