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);
}
}
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);
}
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);
}
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);
}
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());
}
Aggregations