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 21, 2020
1 parent 5327f23 commit 484963d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Map;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
import org.eclipse.jdt.ls.core.internal.IConstants;
import org.eclipse.jdt.ls.core.internal.JSONUtility;
import org.eclipse.jdt.ls.core.internal.JVMConfigurator;
Expand Down Expand Up @@ -114,6 +117,14 @@ public InitializeResult initialize(InitializeParams param) {
Preferences prefs = Preferences.createFrom((Map<String, Object>) settings);
prefs.setRootPaths(rootPaths);
preferenceManager.update(prefs);
Hashtable<String, String> eclipseOptions = JavaCore.getOptions();
int tabSize = prefs.getTabSize();
if (tabSize > 0) {
eclipseOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, String.valueOf(tabSize));
}
boolean insertSpaces = prefs.isInsertSpaces();
eclipseOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, insertSpaces ? JavaCore.SPACE : JavaCore.TAB);
JavaCore.setOptions(eclipseOptions);
if (!isWorkspaceInitialized()) {
// We don't care about triggering a full build here, like in onDidChangeConfiguration
try {
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.editor.insertSpaces";
/**
* Tab Size
*/
public static final String JAVA_CONFIGURATION_TABSIZE = "java.editor.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 @@ -53,6 +53,7 @@
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.IVMInstallChangedListener;
import org.eclipse.jdt.launching.JavaRuntime;
Expand Down Expand Up @@ -464,5 +465,24 @@ private InitializeResult initialize(boolean dynamicRegistration) throws Interrup
return result.get();
}

@Test
public void testInsertSpacesTabSize() {
InitHandler handler = new InitHandler(projectsManager, preferenceManager, server.getClientConnection(), commandHandler);
InitializeParams params = new InitializeParams();
Map<String, Object> initializationOptions = new HashMap<>();
HashMap<String, Object> homeMap = new HashMap<>();
Map<String, Object> javaMap = getMap("java", homeMap);
initializationOptions.put(InitHandler.SETTINGS_KEY, javaMap);
params.setInitializationOptions(initializationOptions);
handler.handleInitializationOptions(params);
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 484963d

Please sign in to comment.