Search in sources :

Example 1 with DocumentFormattingParams

use of org.eclipse.lsp4j.DocumentFormattingParams 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 DocumentFormattingParams

use of org.eclipse.lsp4j.DocumentFormattingParams 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 DocumentFormattingParams

use of org.eclipse.lsp4j.DocumentFormattingParams 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 DocumentFormattingParams

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

the class FormattingTest method doTestAPIMaintenanceLayer.

private void doTestAPIMaintenanceLayer(final FormattingService fs) {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("type Foo{int bar} type Bar{Foo foo}");
        final String model = _builder.toString();
        final XtextResourceSet rs = this.rsp.get();
        Resource _createResource = rs.createResource(URI.createURI("dummy.testlang"));
        final XtextResource r = ((XtextResource) _createResource);
        StringInputStream _stringInputStream = new StringInputStream(model);
        r.load(_stringInputStream, null);
        try {
            Document _document = new Document(1, model);
            DocumentFormattingParams _documentFormattingParams = new DocumentFormattingParams();
            fs.format(_document, r, _documentFormattingParams, CancelIndicator.NullImpl);
            Assert.fail("IllegalStateException expected");
        } catch (final Throwable _t) {
            if (_t instanceof IllegalStateException) {
                final IllegalStateException e = (IllegalStateException) _t;
                String _message = e.getMessage();
                boolean _notEquals = (!Objects.equal(_message, "api maintenance layer broken"));
                if (_notEquals) {
                    throw e;
                }
            } else {
                throw Exceptions.sneakyThrow(_t);
            }
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : StringInputStream(org.eclipse.xtext.util.StringInputStream) DocumentFormattingParams(org.eclipse.lsp4j.DocumentFormattingParams) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) Document(org.eclipse.xtext.ide.server.Document)

Example 5 with DocumentFormattingParams

use of org.eclipse.lsp4j.DocumentFormattingParams 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)

Aggregations

DocumentFormattingParams (org.eclipse.lsp4j.DocumentFormattingParams)8 FormattingOptions (org.eclipse.lsp4j.FormattingOptions)6 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)6 Test (org.junit.Test)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)2 Document (org.eclipse.xtext.ide.server.Document)2 List (java.util.List)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 CompletionList (org.eclipse.lsp4j.CompletionList)1 TextEdit (org.eclipse.lsp4j.TextEdit)1 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)1 AbstractTestLangLanguageServerTest (org.eclipse.xtext.ide.tests.server.AbstractTestLangLanguageServerTest)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)1 FormattingConfiguration (org.eclipse.xtext.testing.FormattingConfiguration)1 RangeFormattingConfiguration (org.eclipse.xtext.testing.RangeFormattingConfiguration)1 StringInputStream (org.eclipse.xtext.util.StringInputStream)1 Extension (org.eclipse.xtext.xbase.lib.Extension)1