Skip to content

Commit

Permalink
Update settings version
Browse files Browse the repository at this point in the history
Signed-off-by: Shi Chen <[email protected]>
  • Loading branch information
CsCherrYY committed Mar 17, 2021
1 parent 13bf32a commit bfea5ca
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions;
import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileVersionerCore;
import org.eclipse.jdt.ls.core.internal.IConstants;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
Expand All @@ -54,6 +55,8 @@ public class FormatterManager {

private final static String FORMATTER_OPTION_PREFIX = JavaCore.PLUGIN_ID + ".formatter"; //$NON-NLS-1$

private static int version = 13; // Keep consistent with default formatter profile version

/**
* A SAX event handler to parse the xml format for profiles.
*/
Expand All @@ -63,6 +66,7 @@ private final static class ProfileDefaultHandler extends DefaultHandler {
private String fName;
private Map<String, String> fSettings;
private String fKind;
private int fVersion;
private boolean reading = false;

/**
Expand Down Expand Up @@ -91,6 +95,7 @@ public void startElement(String uri, String localName, String qName, Attributes
fKind = CODE_FORMATTER_PROFILE_KIND;
}
fSettings = new HashMap<>(200);
fVersion = Integer.parseInt(attributes.getValue(XML_ATTRIBUTE_VERSION));
}
}
else if (qName.equals(XML_NODE_ROOT)) {
Expand All @@ -111,6 +116,10 @@ private Map<String, String> getSettings() {
return fSettings;
}

public int getVersion() {
return fVersion;
}

}

/**
Expand All @@ -124,6 +133,7 @@ private Map<String, String> getSettings() {
private final static String XML_ATTRIBUTE_NAME= "name"; //$NON-NLS-1$
private final static String XML_ATTRIBUTE_PROFILE_KIND= "kind"; //$NON-NLS-1$
private final static String XML_ATTRIBUTE_VALUE= "value"; //$NON-NLS-1$
private final static String XML_ATTRIBUTE_VERSION= "version"; //$NON-NLS-1$

public FormatterManager() {
}
Expand Down Expand Up @@ -161,6 +171,7 @@ public static Map<String, String> readSettingsFromStream(InputSource inputSource
} catch (Exception e) {
throw new CoreException(new Status(IStatus.WARNING, IConstants.PLUGIN_ID, e.getMessage(), e));
}
FormatterManager.version = handler.getVersion();
return handler.getSettings();
}

Expand All @@ -177,6 +188,7 @@ public static void configureFormatter(Preferences preferences) {
}
}
if (options != null && !options.isEmpty()) {
options = ProfileVersionerCore.updateAndComplete(options, FormatterManager.version);
setFormattingOptions(preferences, options);
} else {
Map<String, String> defaultOptions = DefaultCodeFormatterOptions.getEclipseDefaultSettings().getMap();
Expand Down
6 changes: 6 additions & 0 deletions org.eclipse.jdt.ls.tests/formatter resources/version13.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<profiles version="13">
<profile kind="CodeFormatterProfile" name="Eclipse" version="13">
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_anonymous_type_declaration" value="do not insert"/>
</profile>
</profiles>
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,45 @@ public void testDisableFormattingOnType() throws Exception {
assertEquals(text, newText);
}

@Test
public void testUpdateFormatterVersion() throws Exception {
// see: https:/redhat-developer/vscode-java/issues/1640
String text =
//@formatter:off
"package org.sample;\n\n" +
"public class Baz {\n"+
"\tpublic void test1() {\n"+
"\t\tObject o = new Object() {};\n"+
"\t}\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(2, true);// ident == 2 spaces
DocumentFormattingParams params = new DocumentFormattingParams(textDocument, options);
Bundle bundle = Platform.getBundle(JavaLanguageServerTestPlugin.PLUGIN_ID);
URL testFormatter = bundle.getEntry("/formatter resources/version13.xml");
URL url = FileLocator.resolve(testFormatter);
File file = ResourceUtils.toFile(URIUtil.toURI(url));
preferences.setFormatterUrl(file.getAbsolutePath());
FormatterManager.configureFormatter(preferences);
List<? extends TextEdit> edits = server.formatting(params).get();
assertNotNull(edits);
String newText = TextEditUtil.apply(unit, edits);
String textResult =
//@formatter:off
"package org.sample;\n\n" +
"public class Baz {\n"+
" public void test1() {\n"+
" Object o = new Object() {};\n"+
" }\n"+
"}\n";
//@formatter:on
assertEquals(textResult, newText);
}


@After
public void tearDown() {
javaProject.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, originalTabChar);
Expand Down

0 comments on commit bfea5ca

Please sign in to comment.