Search in sources :

Example 1 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 = (IXtextDocument) 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 2 with IXtextDocument

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

the class ContentAssistProcessorTestBuilder method applyProposal.

public ContentAssistProcessorTestBuilder applyProposal(int position, String proposalString) throws Exception {
    IXtextDocument document = getDocument(getModel());
    Shell shell = new Shell();
    try {
        ICompletionProposal[] proposals = computeCompletionProposals(document, position, shell);
        ICompletionProposal proposal = findProposal(proposalString, proposals);
        return applyProposal(proposal, position, document);
    } finally {
        shell.dispose();
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 3 with IXtextDocument

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

the class ContentAssistProcessorTestBuilder method appendAndApplyProposal.

public ContentAssistProcessorTestBuilder appendAndApplyProposal(String model, int position, String proposalString) throws Exception {
    IXtextDocument document = getDocument(getModel());
    Shell shell = new Shell();
    try {
        XtextSourceViewerConfiguration configuration = get(XtextSourceViewerConfiguration.class);
        ISourceViewer sourceViewer = getSourceViewer(shell, document, configuration);
        ICompletionProposal[] proposals = computeCompletionProposals(document, position, shell);
        ICompletionProposal proposal = findProposal(proposalString, proposals);
        return appendAndApplyProposal(proposal, sourceViewer, model, position);
    } finally {
        shell.dispose();
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) XtextSourceViewerConfiguration(org.eclipse.xtext.ui.editor.XtextSourceViewerConfiguration)

Example 4 with IXtextDocument

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

the class ToSaveOrNotToSaveTest method renameFooToFooBar.

protected void renameFooToFooBar(final XtextEditor contextEditor) throws Exception {
    contextEditor.getEditorSite().getPage().activate(contextEditor);
    waitForDisplay();
    IXtextDocument document = contextEditor.getDocument();
    final int offset = document.get().indexOf("foo");
    contextEditor.selectAndReveal(offset, 3);
    EvaluationContext evaluationContext = new EvaluationContext(null, new Object());
    evaluationContext.addVariable(ISources.ACTIVE_EDITOR_NAME, contextEditor);
    ExecutionEvent executionEvent = new ExecutionEvent(null, newHashMap(), null, evaluationContext);
    renameElementHandler.execute(executionEvent);
    // syncUtil.totalSync(refactoringPreferences.isSaveAllBeforeRefactoring());
    // IRenameElementContext context = document.readOnly(new IUnitOfWork<IRenameElementContext, XtextResource>() {
    // public IRenameElementContext exec(XtextResource state) throws Exception {
    // EObject target = eObjectAtOffsetHelper.resolveElementAt(state, offset);
    // return renameContextFactory.createRenameElementContext(target, contextEditor, new TextSelection(offset,
    // 3), state);
    // }
    // });
    // controller.initialize(context);
    // waitForDisplay();
    // controller.startRefactoring(RefactoringType.LINKED_EDITING);
    // waitForDisplay();
    pressKeys(contextEditor, "fooBar\n");
    waitForDisplay();
    waitForReconciler(fooEditor);
    waitForReconciler(barEditor);
    waitForDisplay();
}
Also used : ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) EvaluationContext(org.eclipse.core.expressions.EvaluationContext) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 5 with IXtextDocument

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

the class XtextDocumentModifyTest method testTextualModification.

// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=406811
@Test
public void testTextualModification() throws Exception {
    final String grammar = "grammar foo.Foo\n" + "generate foo \"http://foo.net/foo\"\n" + "Foo: 'foo';";
    final IXtextDocument document = createDocument(grammar);
    document.modify(new IUnitOfWork.Void<XtextResource>() {

        @Override
        public void process(XtextResource state) throws Exception {
            document.replace(grammar.indexOf("Foo"), 3, "Bar");
        }
    });
    assertEquals(grammar.replace("foo.Foo", "foo.Bar"), document.get());
}
Also used : IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) XtextResource(org.eclipse.xtext.resource.XtextResource) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Test(org.junit.Test)

Aggregations

IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)137 XtextResource (org.eclipse.xtext.resource.XtextResource)44 Test (org.junit.Test)43 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)31 IFile (org.eclipse.core.resources.IFile)30 BadLocationException (org.eclipse.jface.text.BadLocationException)24 AbstractEditorTest (org.eclipse.xtext.ui.testing.AbstractEditorTest)24 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)22 IUnitOfWork (org.eclipse.xtext.util.concurrent.IUnitOfWork)21 EObject (org.eclipse.emf.ecore.EObject)19 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)15 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)13 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)10 IRegion (org.eclipse.jface.text.IRegion)10 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)10 List (java.util.List)9 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)9 IModification (org.eclipse.xtext.ui.editor.model.edit.IModification)9