Search in sources :

Example 6 with DocumentFormattingParams

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

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

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

the class AbstractLanguageServerTest method testFormatting.

protected void testFormatting(final Procedure1<? super DocumentFormattingParams> paramsConfigurator, final Procedure1<? super FormattingConfiguration> configurator) {
    try {
        @Extension final FormattingConfiguration configuration = new FormattingConfiguration();
        configuration.setFilePath(("MyModel." + this.fileExtension));
        configurator.apply(configuration);
        final FileInfo fileInfo = this.initializeContext(configuration);
        DocumentFormattingParams _documentFormattingParams = new DocumentFormattingParams();
        final Procedure1<DocumentFormattingParams> _function = (DocumentFormattingParams it) -> {
            String _uri = fileInfo.getUri();
            TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(_uri);
            it.setTextDocument(_textDocumentIdentifier);
            if ((paramsConfigurator != null)) {
                paramsConfigurator.apply(it);
            }
        };
        DocumentFormattingParams _doubleArrow = ObjectExtensions.<DocumentFormattingParams>operator_doubleArrow(_documentFormattingParams, _function);
        final CompletableFuture<List<? extends TextEdit>> changes = this.languageServer.formatting(_doubleArrow);
        String _contents = fileInfo.getContents();
        final Document result = new Document(Integer.valueOf(1), _contents).applyChanges(ListExtensions.<TextEdit>reverse(CollectionLiterals.<TextEdit>newArrayList(((TextEdit[]) Conversions.unwrapArray(changes.get(), TextEdit.class)))));
        this.assertEqualsStricter(configuration.getExpectedText(), result.getContents());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : Extension(org.eclipse.xtext.xbase.lib.Extension) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) DocumentFormattingParams(org.eclipse.lsp4j.DocumentFormattingParams) TextEdit(org.eclipse.lsp4j.TextEdit) List(java.util.List) CompletionList(org.eclipse.lsp4j.CompletionList) Document(org.eclipse.xtext.ide.server.Document)

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