Java Code Examples for org.eclipse.lsp4j.DocumentOnTypeFormattingParams#setTextDocument()

The following examples show how to use org.eclipse.lsp4j.DocumentOnTypeFormattingParams#setTextDocument() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example 3
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example 4
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example 5
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example 6
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example 7
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example 8
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}