Search in sources :

Example 1 with DocumentRewriter

use of org.eclipse.xtext.xbase.ui.document.DocumentRewriter in project xtext-xtend by eclipse.

the class DocumentRewriterTest method testInsertExistingType.

@Test
public void testInsertExistingType() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("import java.io.Serializable");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class Foo implements Serializable {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def foo() {}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final String model = _builder.toString();
    final Procedure2<DocumentRewriter, XtextResource> _function = (DocumentRewriter it, XtextResource r) -> {
        final DocumentRewriter.Section beforeFoo = it.newSection(model.indexOf("foo"), 0);
        beforeFoo.append(this.services.getTypeReferences().findDeclaredType(Serializable.class, r));
        beforeFoo.append(" ");
    };
    String _rewrite = this.rewrite(model, _function);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("import java.io.Serializable");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("class Foo implements Serializable {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def Serializable foo() {}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.andExpect(_rewrite, _builder_1);
}
Also used : DocumentRewriter(org.eclipse.xtext.xbase.ui.document.DocumentRewriter) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 2 with DocumentRewriter

use of org.eclipse.xtext.xbase.ui.document.DocumentRewriter in project xtext-xtend by eclipse.

the class DocumentRewriterTest method testEmptyLinesAround.

@Test
public void testEmptyLinesAround() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def foo() {}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def bar() {}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final String model = _builder.toString();
    final Procedure2<DocumentRewriter, XtextResource> _function = (DocumentRewriter it, XtextResource r) -> {
        final DocumentRewriter.Section beforeDefBar = it.newSection(model.indexOf("def bar"), 0, true);
        beforeDefBar.append("val x = 42");
    };
    String _rewrite = this.rewrite(model, _function);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("class Foo {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def foo() {}");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("val x = 42");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def bar() {}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.andExpect(_rewrite, _builder_1);
}
Also used : DocumentRewriter(org.eclipse.xtext.xbase.ui.document.DocumentRewriter) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 3 with DocumentRewriter

use of org.eclipse.xtext.xbase.ui.document.DocumentRewriter in project xtext-xtend by eclipse.

the class DocumentRewriterTest method testInsertText.

@Test
public void testInsertText() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def foo() {}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def bar() {}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final String model = _builder.toString();
    final Procedure2<DocumentRewriter, XtextResource> _function = (DocumentRewriter it, XtextResource r) -> {
        it.newSection(model.indexOf("class"), 0).append("abstract ");
        it.newSection(model.indexOf("foo"), 0).append("static ");
        it.newSection(model.indexOf("bar"), 0).append("private int ");
        it.newSection(model.lastIndexOf("{}"), 2).append("{ 42 }");
    };
    String _rewrite = this.rewrite(model, _function);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("abstract class Foo {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def static foo() {}");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def private int bar() { 42 }");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.andExpect(_rewrite, _builder_1);
}
Also used : DocumentRewriter(org.eclipse.xtext.xbase.ui.document.DocumentRewriter) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 4 with DocumentRewriter

use of org.eclipse.xtext.xbase.ui.document.DocumentRewriter in project xtext-xtend by eclipse.

the class DocumentRewriterTest method testWhitespaces.

@Test
public void testWhitespaces() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def foo() {}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def bar() {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final String model = _builder.toString();
    final Procedure2<DocumentRewriter, XtextResource> _function = (DocumentRewriter it, XtextResource r) -> {
        int _indexOf = model.indexOf("{}");
        int _plus = (_indexOf + 3);
        it.newSection(_plus, 0, true).append("val x = 42");
        int _lastIndexOf = model.lastIndexOf("{\n");
        int _plus_1 = (_lastIndexOf + 1);
        it.newSection(_plus_1, 0, 2, true).append("val y = 43");
        int _indexOf_1 = model.indexOf("\t}");
        int _plus_2 = (_indexOf_1 + 2);
        it.newSection(_plus_2, 0, true).append("val z = 44");
    };
    String _rewrite = this.rewrite(model, _function);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("class Foo {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def foo() {}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("val x = 42");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def bar() {");
    _builder_1.newLine();
    _builder_1.append("\t\t");
    _builder_1.newLine();
    _builder_1.append("\t\t");
    _builder_1.append("val y = 43");
    _builder_1.newLine();
    _builder_1.append("\t\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("val z = 44");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.andExpect(_rewrite, _builder_1);
}
Also used : DocumentRewriter(org.eclipse.xtext.xbase.ui.document.DocumentRewriter) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 5 with DocumentRewriter

use of org.eclipse.xtext.xbase.ui.document.DocumentRewriter in project xtext-xtend by eclipse.

the class DocumentRewriterTest method testSectionOverlap.

@Test
public void testSectionOverlap() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("//***********");
    _builder.newLine();
    final Procedure2<DocumentRewriter, XtextResource> _function = (DocumentRewriter it, XtextResource r) -> {
        it.newSection(3, 1).append("one");
        it.newSection(4, 2).append("two");
        it.newSection(6, 1).append("three");
        final Procedure1<DocumentRewriter> _function_1 = (DocumentRewriter it_1) -> {
            it_1.newSection(4, 2);
        };
        this.assertFails(it, _function_1);
        final Procedure1<DocumentRewriter> _function_2 = (DocumentRewriter it_1) -> {
            it_1.newSection(5, 0);
        };
        this.assertFails(it, _function_2);
        final Procedure1<DocumentRewriter> _function_3 = (DocumentRewriter it_1) -> {
            it_1.newSection(5, 1);
        };
        this.assertFails(it, _function_3);
        final Procedure1<DocumentRewriter> _function_4 = (DocumentRewriter it_1) -> {
            it_1.newSection(5, 2);
        };
        this.assertFails(it, _function_4);
        final Procedure1<DocumentRewriter> _function_5 = (DocumentRewriter it_1) -> {
            it_1.newSection(6, 0);
        };
        this.assertFails(it, _function_5);
        final Procedure1<DocumentRewriter> _function_6 = (DocumentRewriter it_1) -> {
            it_1.newSection(6, 1);
        };
        this.assertFails(it, _function_6);
        final Procedure1<DocumentRewriter> _function_7 = (DocumentRewriter it_1) -> {
            it_1.newSection(6, 2);
        };
        this.assertFails(it, _function_7);
    };
    String _rewrite = this.rewrite(_builder, _function);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("//*onetwothree******");
    _builder_1.newLine();
    this.andExpect(_rewrite, _builder_1);
}
Also used : DocumentRewriter(org.eclipse.xtext.xbase.ui.document.DocumentRewriter) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Aggregations

StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)8 XtextResource (org.eclipse.xtext.resource.XtextResource)8 DocumentRewriter (org.eclipse.xtext.xbase.ui.document.DocumentRewriter)8 Test (org.junit.Test)8 List (java.util.List)2 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)2 StandardTypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)2 File (java.io.File)1 IFile (org.eclipse.core.resources.IFile)1 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)1