Skip to content

Commit

Permalink
Add support for manual cleanup actions command
Browse files Browse the repository at this point in the history
Signed-off-by: Hope Hadfield <hhadfiel@redhat.com>
  • Loading branch information
hopehadfield authored and rgrunber committed Feb 3, 2024
1 parent 92245b7 commit 9f5440e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,13 @@ public CompletableFuture<WorkspaceEdit> organizeImports(CodeActionParams params)
return computeAsync((monitor) -> OrganizeImportsHandler.organizeImports(client, params, monitor));
}

@Override
public CompletableFuture<WorkspaceEdit> cleanup(TextDocumentIdentifier doc) {
debugTrace(">> java/cleanup");
SaveActionHandler handler = new SaveActionHandler(preferenceManager);
return computeAsync((monitor) -> handler.performManualCleanupActions(doc, monitor));
}

@Override
public CompletableFuture<AccessorField[]> resolveUnimplementedAccessors(AccessorCodeActionParams params) {
debugTrace(">> java/resolveUnimplementedAccessors");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
Expand All @@ -34,6 +36,7 @@
import org.eclipse.jdt.ls.core.internal.commands.OrganizeImportsCommand;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WillSaveTextDocumentParams;
import org.eclipse.lsp4j.WorkspaceEdit;
Expand Down Expand Up @@ -69,7 +72,11 @@ public List<TextEdit> willSaveWaitUntil(WillSaveTextDocumentParams params, IProg
}

LinkedHashSet<String> cleanUpIds = new LinkedHashSet<>();
List<String> lspCleanups = preferences.getCleanUpActionsOnSave();

List<String> lspCleanups = Collections.emptyList();
if (preferences.getCleanUpActionsOnSaveEnabled()) {
lspCleanups = preferences.getCleanUpActions();
}
Collection<String> jdtSettingCleanups = getCleanupsFromJDTUIPreferences(jdtUiPreferences);

cleanUpIds.addAll(canUseInternalSettings ? jdtSettingCleanups : lspCleanups);
Expand All @@ -78,6 +85,31 @@ public List<TextEdit> willSaveWaitUntil(WillSaveTextDocumentParams params, IProg
return edit;
}

public WorkspaceEdit performManualCleanupActions(TextDocumentIdentifier doc, IProgressMonitor monitor) {
List<TextEdit> edit = new ArrayList<>();

if (monitor.isCanceled()) {
return null;
}

String documentUri = doc.getUri();
Preferences preferences = preferenceManager.getPreferences();
IEclipsePreferences jdtUiPreferences = getJdtUiProjectPreferences(documentUri);
boolean canUseInternalSettings = preferenceManager.getClientPreferences().canUseInternalSettings();
LinkedHashSet<String> cleanUpIds = new LinkedHashSet<>();

List<String> lspCleanups = preferences.getCleanUpActions();
Collection<String> jdtSettingCleanups = getCleanupsFromJDTUIPreferences(jdtUiPreferences);

cleanUpIds.addAll(canUseInternalSettings ? jdtSettingCleanups : lspCleanups);
List<TextEdit> cleanUpEdits = cleanUpRegistry.getEditsForAllActiveCleanUps(doc, new ArrayList<>(cleanUpIds), monitor);
edit.addAll(cleanUpEdits);
Map<String, List<TextEdit>> editMap = new HashMap<>();
editMap.put(doc.getUri(), edit);
WorkspaceEdit finalEdit = new WorkspaceEdit(editMap);
return finalEdit;
}

private Collection<String> getCleanupsFromJDTUIPreferences(IEclipsePreferences jdtUIPrefs) {
if (jdtUIPrefs == null) {
return List.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public interface JavaProtocolExtensions {
@JsonRequest
CompletableFuture<WorkspaceEdit> organizeImports(CodeActionParams params);

@JsonRequest
CompletableFuture<WorkspaceEdit> cleanup(TextDocumentIdentifier documentUri);

@JsonRequest
CompletableFuture<CheckToStringResponse> checkToStringStatus(CodeActionParams params);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,9 @@ public class Preferences {
/**
* Preference key for list of cleanups to run on save
*/
public static final String JAVA_CLEANUPS_ACTIONS_ON_SAVE = "java.cleanup.actionsOnSave";

public static final String JAVA_CLEANUPS_ACTIONS = "java.cleanup.actions";
public static final String JAVA_CLEANUPS_ACTIONS_ON_SAVE_DEPRECATED = "java.cleanup.actionsOnSave";
public static final String JAVA_CLEANUPS_ACTIONS_ON_SAVE_CLEANUP = "java.saveActions.cleanup";
public static final String JAVA_REFACTORING_EXTRACT_INTERFACE_REPLACE = "java.refactoring.extract.interface.replace";

/**
Expand Down Expand Up @@ -682,7 +683,8 @@ public class Preferences {
private List<String> nullableTypes;
private List<String> nonnullbydefaultTypes;
private FeatureStatus nullAnalysisMode;
private List<String> cleanUpActionsOnSave;
private List<String> cleanUpActions;
private boolean cleanUpActionsOnSaveEnabled;
private boolean extractInterfaceReplaceEnabled;
private boolean telemetryEnabled;
private boolean validateAllOpenBuffersOnChanges;
Expand Down Expand Up @@ -916,7 +918,8 @@ public Preferences() {
nullableTypes = new ArrayList<>();
nonnullbydefaultTypes = new ArrayList<>();
nullAnalysisMode = FeatureStatus.disabled;
cleanUpActionsOnSave = new ArrayList<>();
cleanUpActions = new ArrayList<>();
cleanUpActionsOnSaveEnabled = false;
extractInterfaceReplaceEnabled = false;
telemetryEnabled = false;
validateAllOpenBuffersOnChanges = true;
Expand Down Expand Up @@ -1291,8 +1294,14 @@ public static Preferences createFrom(Map<String, Object> configuration) {
prefs.setNonnullbydefaultTypes(nonullbydefaultTypes);
String nullAnalysisMode = getString(configuration, JAVA_COMPILE_NULLANALYSIS_MODE, null);
prefs.setNullAnalysisMode(FeatureStatus.fromString(nullAnalysisMode, FeatureStatus.disabled));
List<String> cleanupActionsOnSave = getList(configuration, JAVA_CLEANUPS_ACTIONS_ON_SAVE, Collections.emptyList());
prefs.setCleanUpActionsOnSave(cleanupActionsOnSave);
List<String> cleanupActionsTemp = getList(configuration, JAVA_CLEANUPS_ACTIONS_ON_SAVE_DEPRECATED, Collections.emptyList());
List<String> cleanupActions = getList(configuration, JAVA_CLEANUPS_ACTIONS, Collections.emptyList());
if(cleanupActions.isEmpty() && !cleanupActionsTemp.isEmpty()) {
cleanupActions = cleanupActionsTemp;
}
prefs.setCleanUpActions(cleanupActions);
boolean cleanUpActionsOnSaveEnabled = getBoolean(configuration, JAVA_CLEANUPS_ACTIONS_ON_SAVE_CLEANUP, false);
prefs.setCleanUpActionsOnSaveEnabled(cleanUpActionsOnSaveEnabled);
boolean extractInterfaceReplaceEnabled = getBoolean(configuration, JAVA_REFACTORING_EXTRACT_INTERFACE_REPLACE, false);
prefs.setExtractInterfaceReplaceEnabled(extractInterfaceReplaceEnabled);
boolean telemetryEnabled = getBoolean(configuration, JAVA_TELEMETRY_ENABLED_KEY, false);
Expand All @@ -1310,8 +1319,12 @@ public static Preferences createFrom(Map<String, Object> configuration) {
* @param enabledCleanUps
* the new list of enabled clean ups
*/
public void setCleanUpActionsOnSave(List<String> enabledCleanUps) {
this.cleanUpActionsOnSave = enabledCleanUps;
public void setCleanUpActions(List<String> enabledCleanUps) {
this.cleanUpActions = enabledCleanUps;
}

public void setCleanUpActionsOnSaveEnabled(boolean cleanup) {
this.cleanUpActionsOnSaveEnabled = cleanup;
}

public Preferences setJavaHome(String javaHome) {
Expand Down Expand Up @@ -2321,8 +2334,12 @@ public boolean isChainCompletionEnabled() {
*
* @return the list of enabled clean ups
*/
public List<String> getCleanUpActionsOnSave() {
return this.cleanUpActionsOnSave;
public List<String> getCleanUpActions() {
return this.cleanUpActions;
}

public boolean getCleanUpActionsOnSaveEnabled() {
return this.cleanUpActionsOnSaveEnabled;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ public void testMissingFormatterUrl() throws Exception {
@Test
public void testNoConflictBetweenLSPAndJDTUI() throws Exception {
// invertEquals enabled via. LSP settings
preferences.setCleanUpActionsOnSave(Arrays.asList("invertEquals"));
preferences.setCleanUpActions(Arrays.asList("invertEquals"));
preferences.setCleanUpActionsOnSaveEnabled(true);
// addFinalModifier enabled via. cleanup.make_variable_declarations_final
IFile jdtCorePrefs = project.getFile(Path.fromOSString(".settings/org.eclipse.jdt.ui.prefs"));
Files.writeString(jdtCorePrefs.getLocation().toPath(), """
Expand Down

0 comments on commit 9f5440e

Please sign in to comment.