@Test public void testJavaFormatEnable() throws Exception { String text = //@formatter:off "package org.sample ;\n\n" + " public class Baz { String name;}\n"; //@formatter:on" ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java", text); preferenceManager.getPreferences().setJavaFormatEnabled(false); String uri = JDTUtils.toURI(unit); TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri); FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces DocumentFormattingParams params = new DocumentFormattingParams(textDocument, options); List<? extends TextEdit> edits = server.formatting(params).get(); assertNotNull(edits); String newText = TextEditUtil.apply(unit, edits); assertEquals(text, newText); }
@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" //@formatter:on ); String uri = JDTUtils.toURI(unit); TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri); Range range = new Range(new Position(2, 0), new Position(3, 5));// range around foo() DocumentRangeFormattingParams params = new DocumentRangeFormattingParams(range); params.setTextDocument(textDocument); params.setOptions(new FormattingOptions(3, true));// ident == 3 spaces 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); }
private static Map<String, String> getOptions(FormattingOptions options, ICompilationUnit cu) { Map<String, String> eclipseOptions = cu.getJavaProject().getOptions(true); Integer tabSize = options.getTabSize(); if (tabSize != null) { int tSize = tabSize.intValue(); if (tSize > 0) { eclipseOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, Integer.toString(tSize)); } } boolean insertSpaces = options.isInsertSpaces(); if (insertSpaces) { eclipseOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, insertSpaces ? JavaCore.SPACE : JavaCore.TAB); } return eclipseOptions; }
@Test public void testDocumentFormatting() throws Exception { ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java", //@formatter:off "package org.sample ;\n\n" + " public class Baz { String name;}\n" //@formatter:on ); String uri = JDTUtils.toURI(unit); TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri); FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces 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" + " String name;\n" + "}\n"; //@formatter:on String newText = TextEditUtil.apply(unit, edits); assertEquals(expectedText, newText); }
@Test public void testDocumentFormattingWithTabs() throws Exception { ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java", //@formatter:off "package org.sample;\n\n" + "public class Baz {\n"+ " void foo(){\n"+ "}\n"+ "}\n" //@formatter:on ); String uri = JDTUtils.toURI(unit); TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri); FormattingOptions options = new FormattingOptions(2, false);// ident == tab 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"+ "\tvoid foo() {\n"+ "\t}\n"+ "}\n"; //@formatter:on String newText = TextEditUtil.apply(unit, edits); assertEquals(expectedText, newText); }
@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" //@formatter:off ); String uri = JDTUtils.toURI(unit); TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri); FormattingOptions options = new FormattingOptions(4, false);// ident == tab 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); }
public DocumentFormattingParams(@NonNull final TextDocumentIdentifier textDocument, @NonNull final FormattingOptions options) { this.textDocument = textDocument; this.options = options; }
/** * The format options */ @Pure @NonNull public FormattingOptions getOptions() { return this.options; }
/** * The format options */ public void setOptions(@NonNull final FormattingOptions options) { this.options = options; }
private FormattingOptions getFormattingOptions() { FormattingOptions options = dtoFactory.createDto(FormattingOptions.class); options.setInsertSpaces(Boolean.parseBoolean(getEditorProperty(EditorProperties.EXPAND_TAB))); options.setTabSize(Integer.parseInt(getEditorProperty(EditorProperties.TAB_SIZE))); return options; }