下面列出了org.eclipse.lsp4j.DocumentOnTypeFormattingParams#setTextDocument ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces
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);
}
@Test // typing ; should format the current line
public void testFormattingOnTypeSemiColumn() throws Exception {
javaProject.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE);
ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
//@formatter:off
"package org.sample;\n\n"
+ "public class Baz { \n"
+ "String name ;\n"//typed ; here
+ "}\n"
//@formatter:on
);
String uri = JDTUtils.toURI(unit);
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
FormattingOptions options = new FormattingOptions(4, false);// ident == tab
DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams(new Position(3, 27), ";");
params.setTextDocument(textDocument);
params.setOptions(options);
preferenceManager.getPreferences().setJavaFormatOnTypeEnabled(true);
List<? extends TextEdit> edits = server.onTypeFormatting(params).get();
assertNotNull(edits);
//@formatter:off
String expectedText =
"package org.sample;\n"
+ "\n"
+ "public class Baz { \n"
+ "\tString name;\n"
+ "}\n";
//@formatter:on
String newText = TextEditUtil.apply(unit, edits);
assertEquals(expectedText, newText);
}
@Test // typing new_line should format the current line if previous character doesn't close a block
public void testFormattingOnTypeNewLine() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
//@formatter:off
"package org.sample;\n"
+ "\n"
+ " public class Baz { \n"
+ "String name ;\n"//typed \n here
+ "}\n"
//@formatter:on
);
String uri = JDTUtils.toURI(unit);
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces
DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams(new Position(3, 28), "\n");
params.setTextDocument(textDocument);
params.setOptions(options);
preferenceManager.getPreferences().setJavaFormatOnTypeEnabled(true);
List<? extends TextEdit> edits = server.onTypeFormatting(params).get();
assertNotNull(edits);
//@formatter:off
String expectedText =
"package org.sample;\n"
+ "\n"
+ " public class Baz { \n"//this part won't be formatted
+ " String name;\n"
+ "}\n";
//@formatter:on
String newText = TextEditUtil.apply(unit, edits);
assertEquals(expectedText, newText);
}
@Test // typing } should format the previous block
public void testFormattingOnTypeCloseBlock() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
//@formatter:off
"package org.sample;\n"
+ "\n"
+ " public class Baz { \n"
+ "String name ;\n"
+ "} "//typed } here
//@formatter:on
);
String uri = JDTUtils.toURI(unit);
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces
DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams(new Position(4, 0), "}");
params.setTextDocument(textDocument);
params.setOptions(options);
preferenceManager.getPreferences().setJavaFormatOnTypeEnabled(true);
List<? extends TextEdit> edits = server.onTypeFormatting(params).get();
assertNotNull(edits);
//@formatter:off
String expectedText =
"package org.sample;\n"
+ "\n"
+ "public class Baz {\n"
+ " String name;\n"
+ "}";
//@formatter:on
String newText = TextEditUtil.apply(unit, edits);
assertEquals(expectedText, newText);
}
@Test // typing new_line after opening a block should only format the current line
public void testFormattingOnTypeReturnAfterOpeningBlock() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
//@formatter:off
"package org.sample;\n"
+ "\n"
+ " public class Baz { \n"//typed \n here
+ "String name ;\n"
+ "} \n"
//@formatter:on
);
String uri = JDTUtils.toURI(unit);
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces
DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams(new Position(2, 33), "\n");
params.setTextDocument(textDocument);
params.setOptions(options);
preferenceManager.getPreferences().setJavaFormatOnTypeEnabled(true);
List<? extends TextEdit> edits = server.onTypeFormatting(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);
}
@Test // typing new_line after closing a block should format the that block
public void testFormattingOnTypeReturnAfterClosedBlock() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
//@formatter:off
"package org.sample;\n"
+ "\n"
+ " public class Baz { \n"
+ "String name ;\n"
+ "} \n"//typed \n here
//@formatter:on
);
String uri = JDTUtils.toURI(unit);
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces
DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams(new Position(4, 3), "\n");
params.setTextDocument(textDocument);
params.setOptions(options);
preferenceManager.getPreferences().setJavaFormatOnTypeEnabled(true);
List<? extends TextEdit> edits = server.onTypeFormatting(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);
}
@Test // typing new_line after inserting a new line should format the previous block if previous non-whitespace char is }
public void testFormattingOnTypeReturnAfterEmptyLine() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
//@formatter:off
"package org.sample;\n"
+ "\n"
+ " public class Baz { \n"
+ "String name ;\n"
+ "} \n"
+ " \n"//typed \n here
//@formatter:on
);
String uri = JDTUtils.toURI(unit);
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces
DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams(new Position(5, 3), "\n");
params.setTextDocument(textDocument);
params.setOptions(options);
preferenceManager.getPreferences().setJavaFormatOnTypeEnabled(true);
List<? extends TextEdit> edits = server.onTypeFormatting(params).get();
assertNotNull(edits);
//@formatter:off
String expectedText =
"package org.sample;\n"
+ "\n"
+ "public class Baz {\n"
+ " String name;\n"
+ "}\n"
+ " \n";
//@formatter:on
String newText = TextEditUtil.apply(unit, edits);
assertEquals(expectedText, newText);
}
@Test // typing new_line after an empty block on a single line should format that block
public void testFormattingOnTypeReturnAfterEmptyBlock() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
//@formatter:off
"package org.sample;\n"
+ "\n"
+ " public class Baz {} \n"//typed \n here
//@formatter:on
);
String uri = JDTUtils.toURI(unit);
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces
DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams(new Position(2, 34), "\n");
params.setTextDocument(textDocument);
params.setOptions(options);
preferenceManager.getPreferences().setJavaFormatOnTypeEnabled(true);
List<? extends TextEdit> edits = server.onTypeFormatting(params).get();
assertNotNull(edits);
//@formatter:off
String expectedText =
"package org.sample;\n"
+ "\n"
+ "public class Baz {\n"
+ "}\n";
//@formatter:on
String newText = TextEditUtil.apply(unit, edits);
assertEquals(expectedText, newText);
}