Search in sources :

Example 6 with IXtextDocument

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

the class FixedContentFormatter method format.

/**
 * This method makes sure that no changes are applied (no dirty state), if there are no changes. This fixes bug
 * GHOLD-272
 */
@Override
public void format(IDocument document, IRegion region) {
    IXtextDocument doc = (IXtextDocument) document;
    TextEdit e = doc.priorityReadOnly(new FormattingUnitOfWork(doc, region));
    if (e == null)
        return;
    if (e instanceof ReplaceEdit) {
        ReplaceEdit r = (ReplaceEdit) e;
        if ((r.getOffset() == 0) && (r.getLength() == 0) && (r.getText().isEmpty())) {
            return;
        }
    }
    try {
        e.apply(document);
    } catch (BadLocationException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 7 with IXtextDocument

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

the class AsyncXtextContentAssistProcessor method computeCompletionProposals.

@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
    if (getContentProposalProvider() == null)
        return null;
    IXtextDocument document = (IXtextDocument) viewer.getDocument();
    final CancelableCompletionProposalComputer computer = createCompletionProposalComputer(viewer, offset);
    ICompletionProposal[] result = document.readOnly(new CancelableUnitOfWork<ICompletionProposal[], XtextResource>() {

        @Override
        public ICompletionProposal[] exec(XtextResource state, CancelIndicator cancelIndicator) throws Exception {
            computer.setCancelIndicator(cancelIndicator);
            try {
                return computer.exec(state);
            } catch (Throwable t) {
                return new ICompletionProposal[] {};
            }
        }
    });
    Arrays.sort(result, getCompletionProposalComparator());
    result = getCompletionProposalPostProcessor().postProcess(result);
    return result;
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) XtextResource(org.eclipse.xtext.resource.XtextResource) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 8 with IXtextDocument

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

the class CheckCfgQuickfixProvider method fixSeverityToMaxSeverity.

/**
 * Fix severity by setting it to a legal value as is defined by severity range of referenced check. Legal
 * severities are passed as issue data (org.eclipse.xtext.validation.Issue#getData()).
 *
 * @param issue
 *          the issue
 * @param acceptor
 *          the acceptor
 */
@Fix(IssueCodes.SEVERITY_NOT_ALLOWED)
public void fixSeverityToMaxSeverity(final Issue issue, final IssueResolutionAcceptor acceptor) {
    if (issue.getData() != null) {
        for (final String severityProposal : issue.getData()) {
            final String label = NLS.bind(Messages.CheckCfgQuickfixProvider_CORRECT_SEVERITY_LABEL, severityProposal);
            final String descn = NLS.bind(Messages.CheckCfgQuickfixProvider_CORRECT_SEVERITY_DESCN, severityProposal);
            acceptor.accept(issue, label, descn, NO_IMAGE, new IModification() {

                public void apply(final IModificationContext context) throws BadLocationException {
                    IXtextDocument xtextDocument = context.getXtextDocument();
                    xtextDocument.replace(issue.getOffset(), issue.getLength(), severityProposal);
                }
            });
        }
    }
}
Also used : IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) BadLocationException(org.eclipse.jface.text.BadLocationException) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 9 with IXtextDocument

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

the class EObjectContentProviderTest method mockSelectedElement.

/**
 * Mocks XtextEditor and XtextElementSelectionListener to return an element selection that has:
 * - given EAttribute with a specific value (AttributeValuePair)
 * - given EStructuralFeature
 * - given EOperation.
 *
 * @param attributeValuePair
 *          EAttribute with a specific value
 * @param feature
 *          the EStructuralFeature
 * @param operation
 *          the EOperation
 * @return the EClass of the "selected" element
 */
@SuppressWarnings("unchecked")
private EClass mockSelectedElement(final AttributeValuePair attributeValuePair, final EStructuralFeature feature, final EOperation operation) {
    EClass mockSelectionEClass = mock(EClass.class);
    when(mockSelectionListener.getSelectedElementType()).thenReturn(mockSelectionEClass);
    // Mockups for returning AttributeValuePair
    URI elementUri = URI.createURI("");
    when(mockSelectionListener.getSelectedElementUri()).thenReturn(elementUri);
    XtextEditor mockEditor = mock(XtextEditor.class);
    when(mockSelectionListener.getEditor()).thenReturn(mockEditor);
    IXtextDocument mockDocument = mock(IXtextDocument.class);
    when(mockEditor.getDocument()).thenReturn(mockDocument);
    when(mockDocument.<ArrayList<AttributeValuePair>>readOnly(any(IUnitOfWork.class))).thenReturn(newArrayList(attributeValuePair));
    // Mockups for returning EOperation
    BasicEList<EOperation> mockEOperationsList = new BasicEList<EOperation>();
    mockEOperationsList.add(operation);
    when(mockSelectionEClass.getEAllOperations()).thenReturn(mockEOperationsList);
    // Mockups for returning EStructuralFeature
    BasicEList<EStructuralFeature> mockEStructuralFeatureList = new BasicEList<EStructuralFeature>();
    mockEStructuralFeatureList.add(feature);
    mockEStructuralFeatureList.add(attributeValuePair.getAttribute());
    when(mockSelectionEClass.getEAllStructuralFeatures()).thenReturn(mockEStructuralFeatureList);
    return mockSelectionEClass;
}
Also used : IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) EClass(org.eclipse.emf.ecore.EClass) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) BasicEList(org.eclipse.emf.common.util.BasicEList) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EOperation(org.eclipse.emf.ecore.EOperation) URI(org.eclipse.emf.common.util.URI) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 10 with IXtextDocument

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

the class XtextElementSelectionListenerTest method mockNodeAtSelection.

/**
 * Mocks a text selection and causes the (mocked) XtextEditor to return an ILeafNode on any read(IUnitOfWork).
 *
 * @return the mocked ILeafNode
 */
@SuppressWarnings("unchecked")
private ILeafNode mockNodeAtSelection(final XtextEditor mockEditor) {
    IXtextDocument mockDocument = mock(IXtextDocument.class);
    ILeafNode mockNode = mock(ILeafNode.class);
    when(mockEditor.getDocument()).thenReturn(mockDocument);
    when(mockDocument.<ILeafNode>priorityReadOnly(any(IUnitOfWork.class))).thenReturn(mockNode);
    when(mockDocument.<ILeafNode>readOnly(any(IUnitOfWork.class))).thenReturn(mockNode);
    // when(mockDocument.readOnly(any(IUnitOfWork<ILeafNode, XtextResource>.class))).thenReturn(mockLeafNode);
    selectionListener.selectionChanged(null, mock(ITextSelection.class));
    return mockNode;
}
Also used : IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ITextSelection(org.eclipse.jface.text.ITextSelection) 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