Search in sources :

Example 6 with FormattingOptions

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

the class FormatterHandlerTest method testJavaFormatEnable.

@Test
public void testJavaFormatEnable() throws Exception {
    String text = // @formatter:off
    "package org.sample   ;\n\n" + "      public class Baz {  String name;}\n";
    // @formatter:on"
    ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java", text);
    preferenceManager.getPreferences().setJavaFormatEnabled(false);
    String uri = JDTUtils.toURI(unit);
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
    // ident == 4 spaces
    FormattingOptions options = new FormattingOptions(4, true);
    DocumentFormattingParams params = new DocumentFormattingParams(textDocument, options);
    List<? extends TextEdit> edits = server.formatting(params).get();
    assertNotNull(edits);
    String newText = TextEditUtil.apply(unit, edits);
    assertEquals(text, 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 7 with FormattingOptions

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

the class FormatterHandlerTest method testDocumentFormatting.

@Test
public void testDocumentFormatting() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java", // @formatter:off
    "package org.sample   ;\n\n" + "      public class Baz {  String name;}\n");
    String uri = JDTUtils.toURI(unit);
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
    // ident == 4 spaces
    FormattingOptions options = new FormattingOptions(4, true);
    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" + "    String name;\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 8 with FormattingOptions

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

the class FormattingTest method testRangeFormattingService_02.

@Test
public void testRangeFormattingService_02() {
    testRangeFormatting(it -> it.setOptions(new FormattingOptions(4, true)), (RangeFormattingConfiguration it) -> {
        it.setModel("type Foo{int bar} type Bar{Foo foo}");
        it.setRange(new Range(new Position(0, 0), new Position(0, 17)));
        String expectedText = "type Foo{\n" + "    int bar\n" + "} type Bar{Foo foo}";
        it.setExpectedText(expectedText);
    });
}
Also used : FormattingOptions(org.eclipse.lsp4j.FormattingOptions) RangeFormattingConfiguration(org.eclipse.xtext.testing.RangeFormattingConfiguration) Position(org.eclipse.lsp4j.Position) 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