Skip to content

Commit

Permalink
Surround with try/catch reformats document
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza committed Sep 22, 2020
1 parent 5327f23 commit 4b831cb
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,25 +181,27 @@ public static void configureFormatter(PreferenceManager preferenceManager, Proje
}
}
if (options != null && !options.isEmpty()) {
setFormattingOptions(options);
setFormattingOptions(preferenceManager, options);
} else {
Map<String, String> defaultOptions = DefaultCodeFormatterOptions.getEclipseDefaultSettings().getMap();
Hashtable<String, String> javaOptions = JavaCore.getOptions();
defaultOptions.forEach((k, v) -> {
javaOptions.put(k, v);
});
JavaCore.setOptions(javaOptions);
JavaLanguageServerPlugin.getPreferencesManager().initializeJavaCoreOptions();
preferenceManager.updateTabSizeInsertSpaces(javaOptions);
JavaCore.setOptions(javaOptions);
}
}

private static void setFormattingOptions(Map<String, String> options) {
private static void setFormattingOptions(PreferenceManager preferenceManager, Map<String, String> options) {
Map<String, String> defaultOptions = DefaultCodeFormatterOptions.getEclipseDefaultSettings().getMap();
defaultOptions.putAll(options);
Hashtable<String, String> javaOptions = JavaCore.getOptions();
defaultOptions.entrySet().stream().filter(p -> p.getKey().startsWith(FORMATTER_OPTION_PREFIX)).forEach(p -> {
javaOptions.put(p.getKey(), p.getValue());
});
preferenceManager.updateTabSizeInsertSpaces(javaOptions);
JavaCore.setOptions(javaOptions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.google.common.base.Objects;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -130,6 +129,8 @@ public static void initializeJavaCoreOptions() {
javaCoreOptions.put(JavaCore.COMPILER_PB_UNHANDLED_WARNING_TOKEN, JavaCore.IGNORE);
javaCoreOptions.put(JavaCore.COMPILER_PB_REDUNDANT_SUPERINTERFACE, JavaCore.WARNING);
javaCoreOptions.put(JavaCore.CODEASSIST_SUBWORD_MATCH, JavaCore.DISABLED);
javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.TAB);
javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "4");
JavaCore.setOptions(javaCoreOptions);
}

Expand Down Expand Up @@ -158,7 +159,7 @@ private static void reloadTemplateStore() {
private static boolean updateTemplate(String templateId, String content) {
Template template = templates.get(templateId);
if ((StringUtils.isEmpty(content) && template == null)
|| (template != null && Objects.equal(content, template.getPattern()))) {
|| (template != null && Objects.equals(content, template.getPattern()))) {
return false;
}

Expand All @@ -178,7 +179,6 @@ public void update(Preferences preferences) {
Preferences oldPreferences = this.preferences;
this.preferences = preferences;
preferencesChanged(oldPreferences, preferences); // listener will get latest preference from getPreferences()

// Update the templates according to the new preferences.
boolean templateChanged = false;
List<String> fileHeader = preferences.getFileHeaderTemplate();
Expand All @@ -191,10 +191,21 @@ public void update(Preferences preferences) {
if (templateChanged) {
reloadTemplateStore();
}

Hashtable<String, String> options = JavaCore.getOptions();
updateTabSizeInsertSpaces(options);
JavaCore.setOptions(options);
// TODO serialize preferences
}

public void updateTabSizeInsertSpaces(Hashtable<String, String> options) {
int tabSize = preferences.getTabSize();
if (tabSize > 0) {
options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, String.valueOf(tabSize));
}
boolean insertSpaces = preferences.isInsertSpaces();
options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, insertSpaces ? JavaCore.SPACE : JavaCore.TAB);
}

private void preferencesChanged(Preferences oldPreferences, Preferences newPreferences) {
for (final IPreferencesChangeListener listener : preferencesChangeListeners) {
ISafeRunnable job = new ISafeRunnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public class Preferences {
* Specifies the folder path to the JDK .
*/
public static final String JAVA_HOME = "java.home";
/**
* Insert spaces when pressing Tab
*/
public static final String JAVA_CONFIGURATION_INSERTSPACES = "java.format.insertSpaces";
/**
* Tab Size
*/
public static final String JAVA_CONFIGURATION_TABSIZE = "java.format.tabSize";
/**
* Specifies Java Execution Environments.
*/
Expand Down Expand Up @@ -458,6 +466,8 @@ public class Preferences {

private List<String> fileHeaderTemplate = new LinkedList<>();
private List<String> typeCommentTemplate = new LinkedList<>();
private boolean insertSpaces;
private int tabSize;

static {
JAVA_IMPORT_EXCLUSIONS_DEFAULT = new LinkedList<>();
Expand Down Expand Up @@ -630,6 +640,8 @@ public Preferences() {
staticImportOnDemandThreshold = IMPORTS_STATIC_ONDEMANDTHRESHOLD_DEFAULT;
referencedLibraries = JAVA_PROJECT_REFERENCED_LIBRARIES_DEFAULT;
resourceFilters = JAVA_RESOURCE_FILTERS_DEFAULT;
insertSpaces = true;
tabSize = 4;
}

/**
Expand All @@ -652,6 +664,10 @@ public static Preferences createFrom(Map<String, Object> configuration) {

boolean importGradleEnabled = getBoolean(configuration, IMPORT_GRADLE_ENABLED, true);
prefs.setImportGradleEnabled(importGradleEnabled);
boolean insertSpaces = getBoolean(configuration, JAVA_CONFIGURATION_INSERTSPACES, true);
prefs.setInsertSpaces(insertSpaces);
int tabSize = getInt(configuration, JAVA_CONFIGURATION_TABSIZE, 4);
prefs.setTabSize(tabSize);
boolean importGradleOfflineEnabled = getBoolean(configuration, IMPORT_GRADLE_OFFLINE_ENABLED, false);
prefs.setImportGradleOfflineEnabled(importGradleOfflineEnabled);
boolean gradleWrapperEnabled = getBoolean(configuration, GRADLE_WRAPPER_ENABLED, true);
Expand Down Expand Up @@ -1461,4 +1477,23 @@ public Preferences setTypeCommentTemplate(List<String> typeCommentTemplate) {
this.typeCommentTemplate = typeCommentTemplate;
return this;
}

public Preferences setInsertSpaces(boolean insertSpaces) {
this.insertSpaces = insertSpaces;
return this;
}

public Preferences setTabSize(int tabSize) {
this.tabSize = tabSize;
return this;
}

public boolean isInsertSpaces() {
return insertSpaces;
}

public int getTabSize() {
return tabSize;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
import org.eclipse.jdt.core.manipulation.JavaManipulation;
import org.eclipse.jdt.internal.core.manipulation.CodeTemplateContextType;
import org.eclipse.jface.text.templates.Template;
Expand Down Expand Up @@ -135,4 +137,18 @@ public void testUpdateTypeCommentTemplate() {
assertNotNull(template);
assertEquals("/** */", template.getPattern());
}

@Test
public void testInsertSpacesTabSize() {
preferenceManager.initialize();
Preferences preferences = new Preferences();
preferenceManager.update(preferences);
assertTrue(preferenceManager.getPreferences().isInsertSpaces());
assertEquals(4, preferenceManager.getPreferences().getTabSize());
Map<String, String> eclipseOptions = JavaCore.getOptions();
String tabSize = eclipseOptions.get(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE);
assertEquals(4, Integer.valueOf(tabSize).intValue());
String insertSpaces = eclipseOptions.get(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR);
assertEquals(JavaCore.SPACE, insertSpaces);
}
}

0 comments on commit 4b831cb

Please sign in to comment.