Search in sources :

Example 1 with DocumentRangeFormattingParams

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

the class AbstractLanguageServerTest method testRangeFormatting.

protected void testRangeFormatting(final Procedure1<? super DocumentRangeFormattingParams> paramsConfigurator, final Procedure1<? super RangeFormattingConfiguration> configurator) {
    try {
        @Extension final RangeFormattingConfiguration configuration = new RangeFormattingConfiguration();
        configuration.setFilePath(("MyModel." + this.fileExtension));
        configurator.apply(configuration);
        final FileInfo fileInfo = this.initializeContext(configuration);
        DocumentRangeFormattingParams _documentRangeFormattingParams = new DocumentRangeFormattingParams();
        final Procedure1<DocumentRangeFormattingParams> _function = (DocumentRangeFormattingParams it) -> {
            String _uri = fileInfo.getUri();
            TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(_uri);
            it.setTextDocument(_textDocumentIdentifier);
            it.setRange(configuration.getRange());
            if ((paramsConfigurator != null)) {
                paramsConfigurator.apply(it);
            }
        };
        DocumentRangeFormattingParams _doubleArrow = ObjectExtensions.<DocumentRangeFormattingParams>operator_doubleArrow(_documentRangeFormattingParams, _function);
        final CompletableFuture<List<? extends TextEdit>> changes = this.languageServer.rangeFormatting(_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) DocumentRangeFormattingParams(org.eclipse.lsp4j.DocumentRangeFormattingParams) TextEdit(org.eclipse.lsp4j.TextEdit) List(java.util.List) CompletionList(org.eclipse.lsp4j.CompletionList) Document(org.eclipse.xtext.ide.server.Document)

Example 2 with DocumentRangeFormattingParams

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

DocumentRangeFormattingParams (org.eclipse.lsp4j.DocumentRangeFormattingParams)2 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)2 List (java.util.List)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 CompletionList (org.eclipse.lsp4j.CompletionList)1 FormattingOptions (org.eclipse.lsp4j.FormattingOptions)1 Position (org.eclipse.lsp4j.Position)1 Range (org.eclipse.lsp4j.Range)1 TextEdit (org.eclipse.lsp4j.TextEdit)1 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)1 Document (org.eclipse.xtext.ide.server.Document)1 Extension (org.eclipse.xtext.xbase.lib.Extension)1 Test (org.junit.Test)1