Search in sources :

Example 1 with FormattingOptions

use of org.eclipse.lsp4j.FormattingOptions in project eclipse.jdt.ls by eclipse.

the class FormatterHandlerTest method testDocumentFormattingWithTabs.

@Test
public void testDocumentFormattingWithTabs() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java", // @formatter:off
    "package org.sample;\n\n" + "public class Baz {\n" + "    void foo(){\n" + "}\n" + "}\n");
    String uri = JDTUtils.toURI(unit);
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
    // ident == tab
    FormattingOptions options = new FormattingOptions(2, false);
    DocumentFormattingParams params = new DocumentFormattingParams(textDocument, options);
    List<? extends TextEdit> edits = server.formatting(params).get();
    assertNotNull(edits);
    // @formatter:off
    String expectedText = "package org.sample;\n" + "\n" + "public class Baz {\n" + "\tvoid foo() {\n" + "\t}\n" + "}\n";
    // @formatter:on
    String newText = TextEditUtil.apply(unit, edits);
    assertEquals(expectedText, newText);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) FormattingOptions(org.eclipse.lsp4j.FormattingOptions) DocumentFormattingParams(org.eclipse.lsp4j.DocumentFormattingParams) Test(org.junit.Test)

Example 2 with FormattingOptions

use of org.eclipse.lsp4j.FormattingOptions in project eclipse.jdt.ls by eclipse.

the class FormatterHandlerTest method testDocumentFormattingWithCustomOption.

@Test
public void testDocumentFormattingWithCustomOption() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java", // @formatter:off
    "@Deprecated package org.sample;\n\n" + "public class Baz {\n" + "    /**Java doc @param a some parameter*/\n" + "\tvoid foo(int a){;;\n" + "}\n" + "}\n");
    String uri = JDTUtils.toURI(unit);
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
    FormattingOptions options = new FormattingOptions(2, true);
    options.putNumber("org.eclipse.jdt.core.formatter.blank_lines_before_package", Integer.valueOf(2));
    options.putString("org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_package", "do not insert");
    options.putBoolean("org.eclipse.jdt.core.formatter.put_empty_statement_on_new_line", Boolean.TRUE);
    DocumentFormattingParams params = new DocumentFormattingParams(textDocument, options);
    List<? extends TextEdit> edits = server.formatting(params).get();
    assertNotNull(edits);
    String expectedText = "\n" + "\n" + "@Deprecated package org.sample;\n" + "\n" + "public class Baz {\n" + "  /**Java doc @param a some parameter*/\n" + "  void foo(int a) {\n" + "    ;\n" + "    ;\n" + "  }\n" + "}\n";
    String newText = TextEditUtil.apply(unit, edits);
    assertEquals(expectedText, newText);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) FormattingOptions(org.eclipse.lsp4j.FormattingOptions) DocumentFormattingParams(org.eclipse.lsp4j.DocumentFormattingParams) Test(org.junit.Test)

Example 3 with FormattingOptions

use of org.eclipse.lsp4j.FormattingOptions in project eclipse.jdt.ls by eclipse.

the class FormatterHandlerTest method testFormatting_onOffTags.

@Test
public void testFormatting_onOffTags() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java", // @formatter:off
    "package org.sample;\n\n" + "      public class Baz {\n" + "// @formatter:off\n" + "\tvoid foo(){\n" + "    }\n" + "// @formatter:on\n" + "}\n");
    String uri = JDTUtils.toURI(unit);
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
    // ident == tab
    FormattingOptions options = new FormattingOptions(4, false);
    DocumentFormattingParams params = new DocumentFormattingParams(textDocument, options);
    List<? extends TextEdit> edits = server.formatting(params).get();
    assertNotNull(edits);
    // @formatter:off
    String expectedText = "package org.sample;\n\n" + "public class Baz {\n" + "// @formatter:off\n" + "\tvoid foo(){\n" + "    }\n" + "// @formatter:on\n" + "}\n";
    // @formatter:on
    String newText = TextEditUtil.apply(unit, edits);
    assertEquals(expectedText, newText);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) FormattingOptions(org.eclipse.lsp4j.FormattingOptions) DocumentFormattingParams(org.eclipse.lsp4j.DocumentFormattingParams) Test(org.junit.Test)

Example 4 with FormattingOptions

use of org.eclipse.lsp4j.FormattingOptions in project xtext-core by eclipse.

the class FormattingTest method testFormattingService_02.

@Test
public void testFormattingService_02() {
    final Procedure1<DocumentFormattingParams> _function = (DocumentFormattingParams it) -> {
        FormattingOptions _formattingOptions = new FormattingOptions(4, true);
        it.setOptions(_formattingOptions);
    };
    final Procedure1<FormattingConfiguration> _function_1 = (FormattingConfiguration it) -> {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("type Foo{int bar} type Bar{Foo foo}");
        it.setModel(_builder.toString());
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("type Foo{");
        _builder_1.newLine();
        _builder_1.append("    ");
        _builder_1.append("int bar");
        _builder_1.newLine();
        _builder_1.append("}");
        _builder_1.newLine();
        _builder_1.append("type Bar{");
        _builder_1.newLine();
        _builder_1.append("    ");
        _builder_1.append("Foo foo");
        _builder_1.newLine();
        _builder_1.append("}");
        _builder_1.newLine();
        it.setExpectedText(_builder_1.toString());
    };
    this.testFormatting(_function, _function_1);
}
Also used : FormattingOptions(org.eclipse.lsp4j.FormattingOptions) DocumentFormattingParams(org.eclipse.lsp4j.DocumentFormattingParams) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) FormattingConfiguration(org.eclipse.xtext.testing.FormattingConfiguration) RangeFormattingConfiguration(org.eclipse.xtext.testing.RangeFormattingConfiguration) AbstractTestLangLanguageServerTest(org.eclipse.xtext.ide.tests.server.AbstractTestLangLanguageServerTest) Test(org.junit.Test)

Example 5 with FormattingOptions

use of org.eclipse.lsp4j.FormattingOptions in project eclipse.jdt.ls by eclipse.

the class FormatterHandlerTest method testRangeFormatting.

@Test
public void testRangeFormatting() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java", // @formatter:off
    "package org.sample;\n" + "      public class Baz {\n" + "\tvoid foo(){\n" + "    }\n" + "	}\n");
    String uri = JDTUtils.toURI(unit);
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
    // range around foo()
    Range range = new Range(new Position(2, 0), new Position(3, 5));
    DocumentRangeFormattingParams params = new DocumentRangeFormattingParams(range);
    params.setTextDocument(textDocument);
    // ident == 3 spaces
    params.setOptions(new FormattingOptions(3, true));
    List<? extends TextEdit> edits = server.rangeFormatting(params).get();
    // @formatter:off
    String expectedText = "package org.sample;\n" + "      public class Baz {\n" + "   void foo() {\n" + "   }\n" + "	}\n";
    // @formatter:on
    String newText = TextEditUtil.apply(unit, edits);
    assertEquals(expectedText, newText);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) FormattingOptions(org.eclipse.lsp4j.FormattingOptions) Position(org.eclipse.lsp4j.Position) DocumentRangeFormattingParams(org.eclipse.lsp4j.DocumentRangeFormattingParams) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test)

Aggregations

FormattingOptions (org.eclipse.lsp4j.FormattingOptions)8 Test (org.junit.Test)8 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 DocumentFormattingParams (org.eclipse.lsp4j.DocumentFormattingParams)6 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)6 Position (org.eclipse.lsp4j.Position)2 Range (org.eclipse.lsp4j.Range)2 RangeFormattingConfiguration (org.eclipse.xtext.testing.RangeFormattingConfiguration)2 DocumentRangeFormattingParams (org.eclipse.lsp4j.DocumentRangeFormattingParams)1 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)1 AbstractTestLangLanguageServerTest (org.eclipse.xtext.ide.tests.server.AbstractTestLangLanguageServerTest)1 FormattingConfiguration (org.eclipse.xtext.testing.FormattingConfiguration)1