Search in sources :

Example 26 with XtextDocument

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

the class XtendFormatterPreview method doUpdate.

@SuppressWarnings("deprecation")
public void doUpdate(final Map<String, String> map) {
    checkEditorHandleIsSet();
    final MapBasedPreferenceValues values = new MapBasedPreferenceValues(map);
    String maxLineWidthValue = values.getPreference(maxLineWidth);
    if (maxLineWidthValue != null) {
        moveMarginToColumn(maxLineWidthValue);
    }
    StyledText widget = null;
    try {
        widget = (StyledText) editorHandle.getViewer().getControl();
        // disable redraw, otherwise this would causes funny animation effects during formating.
        widget.setRedraw(false);
        this.modelAccess.updateModel("", previewContent, "");
        XtextDocument document = editorHandle.getDocument();
        ContentFormatter formatter = formatterProvider.get();
        formatter.setPreferencesProvider(values);
        formatter.format(document, new Region(0, document.getLength()));
        // reset selection, otherwise the whole new content will be selected
        editorHandle.getViewer().setSelection(null);
    } finally {
        if (widget != null)
            widget.setRedraw(true);
    }
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) MapBasedPreferenceValues(org.eclipse.xtext.preferences.MapBasedPreferenceValues) Region(org.eclipse.jface.text.Region) XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument) ContentFormatter(org.eclipse.xtext.ui.editor.formatting2.ContentFormatter)

Example 27 with XtextDocument

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

the class ReplacingAppendableTest method testImports_4.

@Test
public void testImports_4() throws Exception {
    final XtextDocument document = insertListField("package test\n" + "/**\n" + "*/\n" + "\n" + "class Foo {|}", "foo");
    assertEqualsIgnoreWhitespace("package test\n" + "\n" + "import java.util.List\n" + "\n" + "/**\n" + "*/\n" + "\n" + "class Foo {List<?> foo}", document.get());
}
Also used : XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument) Test(org.junit.Test)

Example 28 with XtextDocument

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

the class ReplacingAppendableTest method insertListField.

protected XtextDocument insertListField(final String model, final String fieldName) throws Exception {
    final int cursorPosition = model.indexOf('|');
    String actualModel = model.replace("|", " ");
    final XtendFile file = testHelper.xtendFile("Foo", actualModel);
    final XtextDocument document = documentProvider.get();
    document.set(actualModel);
    XtextResource xtextResource = (XtextResource) file.eResource();
    document.setInput(xtextResource);
    final EObject context = eObjectAtOffsetHelper.resolveElementAt(xtextResource, cursorPosition);
    document.modify(new IUnitOfWork.Void<XtextResource>() {

        @Override
        public void process(XtextResource state) throws Exception {
            ReplacingAppendable a = appendableFactory.create(document, state, cursorPosition, 1);
            ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, context);
            LightweightTypeReference typeRef = owner.toLightweightTypeReference(services.getTypeReferences().getTypeForName(List.class, context, typesFactory.createJvmWildcardTypeReference()));
            a.append(typeRef);
            a.append(" ").append(fieldName);
            a.commitChanges();
        }
    });
    return document;
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument) XtextResource(org.eclipse.xtext.resource.XtextResource) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) EObject(org.eclipse.emf.ecore.EObject) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 29 with XtextDocument

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

the class ReplacingAppendableTest method testImports_5.

@Test
public void testImports_5() throws Exception {
    final XtextDocument document = insertListField("import static java.util.List.* class Foo {|}", "foo");
    assertEqualsIgnoreWhitespace("import static java.util.List.*\n" + "import java.util.List\n" + "\n" + "class Foo {List<?> foo}", document.get());
}
Also used : XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument) Test(org.junit.Test)

Example 30 with XtextDocument

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

the class AbstractOutlineTests method newAssertBuilder.

protected AssertBuilder newAssertBuilder(XtendFile xtendFile) throws Exception, CoreException {
    XtextDocument document = documentProvider.get();
    document.setInput((XtextResource) xtendFile.eResource());
    IOutlineNode root = treeProvider.createRoot(document);
    AssertBuilder assertBuilder = new AssertBuilder(root);
    return assertBuilder;
}
Also used : XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument) IOutlineNode(org.eclipse.xtext.ui.editor.outline.IOutlineNode)

Aggregations

XtextDocument (org.eclipse.xtext.ui.editor.model.XtextDocument)48 Test (org.junit.Test)21 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)14 XtextResource (org.eclipse.xtext.resource.XtextResource)13 Lexer (org.eclipse.xtext.parser.antlr.Lexer)9 DocumentTokenSource (org.eclipse.xtext.ui.editor.model.DocumentTokenSource)8 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)5 AntlrTokenDefProvider (org.eclipse.xtext.parser.antlr.AntlrTokenDefProvider)4 DocumentPartitioner (org.eclipse.xtext.ui.editor.model.DocumentPartitioner)4 IOutlineNode (org.eclipse.xtext.ui.editor.outline.IOutlineNode)4 Job (org.eclipse.core.runtime.jobs.Job)3 ITypedRegion (org.eclipse.jface.text.ITypedRegion)3 CancelIndicator (org.eclipse.xtext.util.CancelIndicator)3 StringInputStream (org.eclipse.xtext.util.StringInputStream)3 ArrayList (java.util.ArrayList)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 IStatus (org.eclipse.core.runtime.IStatus)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 EObject (org.eclipse.emf.ecore.EObject)2 IDocument (org.eclipse.jface.text.IDocument)2