Search in sources :

Example 56 with TextDocumentIdentifier

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

the class FormatterHandlerTest method testGoogleFormatterFilePath.

@Test
public void testGoogleFormatterFilePath() throws Exception {
    try {
        String text = // @formatter:off
        "package org.sample;\n\n" + "public class Baz {\n" + "  String name;\n" + "}\n";
        // @formatter:on"
        ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java", text);
        String uri = JDTUtils.toURI(unit);
        TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
        // ident == 2 spaces
        FormattingOptions options = new FormattingOptions(2, true);
        DocumentFormattingParams params = new DocumentFormattingParams(textDocument, options);
        Bundle bundle = Platform.getBundle(JavaLanguageServerTestPlugin.PLUGIN_ID);
        URL googleFormatter = bundle.getEntry("/formatter resources/eclipse-java-google-style.xml");
        URL url = FileLocator.resolve(googleFormatter);
        File file = ResourceUtils.toFile(URIUtil.toURI(url));
        preferences.setFormatterUrl(file.getAbsolutePath());
        StandardProjectsManager.configureSettings(preferences);
        List<? extends TextEdit> edits = server.formatting(params).get();
        assertNotNull(edits);
        String newText = TextEditUtil.apply(unit, edits);
        assertEquals(text, newText);
    } finally {
        preferences.setFormatterUrl(null);
        StandardProjectsManager.configureSettings(preferences);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) FormattingOptions(org.eclipse.lsp4j.FormattingOptions) DocumentFormattingParams(org.eclipse.lsp4j.DocumentFormattingParams) Bundle(org.osgi.framework.Bundle) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 57 with TextDocumentIdentifier

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

the class FormatterHandlerTest method testDisableFormattingOnType.

@Test
public void testDisableFormattingOnType() throws Exception {
    // @formatter:off
    String text = "package org.sample;\n" + "\n" + "    public      class     Baz {  \n" + "String          name       ;\n" + "}\n";
    // @formatter:on
    ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java", text);
    String uri = JDTUtils.toURI(unit);
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
    // ident == 4 spaces
    FormattingOptions options = new FormattingOptions(4, true);
    DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams(new Position(3, 28), "\n");
    params.setTextDocument(textDocument);
    params.setOptions(options);
    // Check it's disabled by default
    List<? extends TextEdit> edits = server.onTypeFormatting(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) Position(org.eclipse.lsp4j.Position) DocumentOnTypeFormattingParams(org.eclipse.lsp4j.DocumentOnTypeFormattingParams) Test(org.junit.Test)

Example 58 with TextDocumentIdentifier

use of org.eclipse.lsp4j.TextDocumentIdentifier 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 59 with TextDocumentIdentifier

use of org.eclipse.lsp4j.TextDocumentIdentifier 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);
    preferences.setJavaFormatComments(false);
    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);
    preferences.setJavaFormatComments(true);
    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 60 with TextDocumentIdentifier

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

the class HoverHandlerTest method testHoverThrowable.

@Test
public void testHoverThrowable() throws Exception {
    String uriString = ClassFileUtil.getURI(project, "java.lang.Exception");
    IClassFile classFile = JDTUtils.resolveClassFile(uriString);
    String contents = JavaLanguageServerPlugin.getContentProviderManager().getSource(classFile, monitor);
    IDocument document = new Document(contents);
    IRegion region = new FindReplaceDocumentAdapter(document).find(0, "Throwable", true, false, false, false);
    int offset = region.getOffset();
    int line = document.getLineOfOffset(offset);
    int character = offset - document.getLineOffset(line);
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uriString);
    Position position = new Position(line, character);
    TextDocumentPositionParams params = new TextDocumentPositionParams(textDocument, position);
    Hover hover = handler.hover(params, monitor);
    assertNotNull(hover);
    assertTrue("Unexpected hover ", !hover.getContents().getLeft().isEmpty());
}
Also used : TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) IClassFile(org.eclipse.jdt.core.IClassFile) Position(org.eclipse.lsp4j.Position) Hover(org.eclipse.lsp4j.Hover) MarkedString(org.eclipse.lsp4j.MarkedString) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Aggregations

TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)172 Test (org.junit.Test)113 Position (org.eclipse.lsp4j.Position)102 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)56 Range (org.eclipse.lsp4j.Range)47 TextDocumentPositionParams (org.eclipse.lsp4j.TextDocumentPositionParams)37 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)35 URI (java.net.URI)34 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)33 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)32 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)32 List (java.util.List)26 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)25 Location (org.eclipse.lsp4j.Location)23 AbstractQuickFixTest (org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)22 Command (org.eclipse.lsp4j.Command)22 PrepareRenameParams (org.eclipse.lsp4j.PrepareRenameParams)20 FormattingOptions (org.eclipse.lsp4j.FormattingOptions)19 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)18 CodeAction (org.eclipse.lsp4j.CodeAction)18