Skip to content

Commit

Permalink
move setting to clean-up actions and move rename to didSave
Browse files Browse the repository at this point in the history
  • Loading branch information
hopehadfield committed Mar 13, 2024
1 parent 428f537 commit cfc4b4e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -69,6 +71,7 @@
import org.eclipse.jdt.ls.core.internal.managers.InvisibleProjectImporter;
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.lsp4j.DidChangeTextDocumentParams;
Expand All @@ -77,8 +80,13 @@
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.RenameFile;
import org.eclipse.lsp4j.ResourceOperation;
import org.eclipse.lsp4j.TextDocumentContentChangeEvent;
import org.eclipse.lsp4j.TextDocumentEdit;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.jsonrpc.messages.ResponseError;
import org.eclipse.osgi.util.NLS;
import org.eclipse.text.edits.DeleteEdit;
Expand Down Expand Up @@ -355,7 +363,8 @@ public void didChange(DidChangeTextDocumentParams params) {
}

public void didSave(DidSaveTextDocumentParams params) {
lastSyncedDocumentLengths.remove(params.getTextDocument().getUri());
String documentUri = params.getTextDocument().getUri();
lastSyncedDocumentLengths.remove(documentUri);
IFile file = JDTUtils.findFile(params.getTextDocument().getUri());
if (file != null && !Objects.equals(ProjectsManager.getDefaultProject(), file.getProject())) {
// no need for a workspace runnable, change is trivial
Expand All @@ -369,6 +378,38 @@ public void didSave(DidSaveTextDocumentParams params) {
JavaLanguageServerPlugin.logException("Handle document save ", e);
}
}

Preferences preferences = preferenceManager.getPreferences();
List<String> lspCleanups = Collections.emptyList();
if (preferences.getCleanUpActionsOnSaveEnabled()) {
lspCleanups = preferences.getCleanUpActions();
}

if (lspCleanups.contains("renameFile")) {
handleSaveActionRenameFile(documentUri);
}
}

private void handleSaveActionRenameFile(String documentUri) {
ICompilationUnit cu = JDTUtils.resolveCompilationUnit(documentUri);
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
IProblem[] problems = astRoot.getProblems();
Optional<IProblem> desiredProblem = Arrays.stream(problems).filter(p -> p.getID() == IProblem.PublicClassMustMatchFileName).findFirst();
if (desiredProblem.isPresent()) {
IProblem renameProblem = desiredProblem.get();
String newName = renameProblem.getArguments()[1];
String oldName = cu.getElementName();
String newUri = documentUri.replace(oldName, newName + ".java");
List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = new ArrayList<>();
RenameFile operation = new RenameFile(documentUri, newUri);
documentChanges.add(Either.forRight(operation));
WorkspaceEdit edit = new WorkspaceEdit(documentChanges);
edit.setChanges(Collections.emptyMap());
final boolean applyNow = JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isWorkspaceApplyEditSupported();
if (applyNow) {
JavaLanguageServerPlugin.getInstance().getClientConnection().applyWorkspaceEdit(edit);
}
}
}

public ICompilationUnit handleOpen(DidOpenTextDocumentParams params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ public List<TextEdit> willSaveWaitUntil(WillSaveTextDocumentParams params, IProg
edit.addAll(handleSaveActionOrganizeImports(documentUri, monitor));
}

if (preferences.isJavaSaveActionsRenameFileEnabled()) {
handleSaveActionRenameFile(documentUri, monitor);
}

LinkedHashSet<String> cleanUpIds = new LinkedHashSet<>();

List<String> lspCleanups = Collections.emptyList();
Expand All @@ -92,6 +88,7 @@ public List<TextEdit> willSaveWaitUntil(WillSaveTextDocumentParams params, IProg
Collection<String> jdtSettingCleanups = getCleanupsFromJDTUIPreferences(jdtUiPreferences);

cleanUpIds.addAll(canUseInternalSettings ? jdtSettingCleanups : lspCleanups);
cleanUpIds.remove("renameFile");
List<TextEdit> cleanUpEdits = cleanUpRegistry.getEditsForAllActiveCleanUps(params.getTextDocument(), new ArrayList<>(cleanUpIds), monitor);
edit.addAll(cleanUpEdits);
return edit;
Expand All @@ -114,6 +111,7 @@ public WorkspaceEdit performManualCleanupActions(TextDocumentIdentifier doc, IPr
Collection<String> jdtSettingCleanups = getCleanupsFromJDTUIPreferences(jdtUiPreferences);

cleanUpIds.addAll(canUseInternalSettings ? jdtSettingCleanups : lspCleanups);
cleanUpIds.remove("renameFile");
List<TextEdit> cleanUpEdits = cleanUpRegistry.getEditsForAllActiveCleanUps(doc, new ArrayList<>(cleanUpIds), monitor);
edit.addAll(cleanUpEdits);
Map<String, List<TextEdit>> editMap = new HashMap<>();
Expand Down Expand Up @@ -167,7 +165,7 @@ private List<TextEdit> handleSaveActionOrganizeImports(String documentUri, IProg
return edit;
}

private void handleSaveActionRenameFile(String documentUri, IProgressMonitor monitor) {
private void handleSaveActionRenameFile(String documentUri) {
ICompilationUnit cu = JDTUtils.resolveCompilationUnit(documentUri);
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
IProblem[] problems = astRoot.getProblems();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,6 @@ public class Preferences {
*/
public static final String JAVA_SAVE_ACTIONS_ORGANIZE_IMPORTS_KEY = "java.saveActions.organizeImports";

/**
* Preference key to enable/disable rename file on save
*/
public static final String JAVA_SAVE_ACTIONS_RENAME_FILE_ENABLED = "java.saveActions.renameFile";

/**
* Preference key to enable/disable signature help.
*/
Expand Down Expand Up @@ -616,7 +611,6 @@ public class Preferences {
private String javaQuickFixShowAt;
private boolean javaFormatOnTypeEnabled;
private boolean javaSaveActionsOrganizeImportsEnabled;
private boolean javaSaveActionsRenameFileEnabled;
private boolean signatureHelpEnabled;
private boolean signatureHelpDescriptionEnabled;
private boolean renameEnabled;
Expand Down Expand Up @@ -875,7 +869,6 @@ public Preferences() {
javaQuickFixShowAt = LINE;
javaFormatOnTypeEnabled = false;
javaSaveActionsOrganizeImportsEnabled = false;
javaSaveActionsRenameFileEnabled = true;
signatureHelpEnabled = false;
signatureHelpDescriptionEnabled = false;
renameEnabled = true;
Expand Down Expand Up @@ -1048,9 +1041,6 @@ public static Preferences createFrom(Map<String, Object> configuration) {
boolean javaSaveActionAutoOrganizeImportsEnabled = getBoolean(configuration, JAVA_SAVE_ACTIONS_ORGANIZE_IMPORTS_KEY, false);
prefs.setJavaSaveActionAutoOrganizeImportsEnabled(javaSaveActionAutoOrganizeImportsEnabled);

boolean javaSaveActionRenameFileEnabled = getBoolean(configuration, JAVA_SAVE_ACTIONS_RENAME_FILE_ENABLED, true);
prefs.setJavaSaveActionRenameFileEnabled(javaSaveActionRenameFileEnabled);

boolean signatureHelpEnabled = getBoolean(configuration, SIGNATURE_HELP_ENABLED_KEY, true);
prefs.setSignatureHelpEnabled(signatureHelpEnabled);

Expand Down Expand Up @@ -1594,11 +1584,6 @@ public Preferences setJavaSaveActionAutoOrganizeImportsEnabled(boolean javaSaveA
return this;
}

public Preferences setJavaSaveActionRenameFileEnabled(boolean javaSaveActionsRenameFileEnabled) {
this.javaSaveActionsRenameFileEnabled = javaSaveActionsRenameFileEnabled;
return this;
}

public Preferences setHashCodeEqualsTemplateUseJava7Objects(boolean hashCodeEqualsTemplateUseJ7Objects) {
this.hashCodeEqualsTemplateUseJava7Objects = hashCodeEqualsTemplateUseJ7Objects;
return this;
Expand Down Expand Up @@ -1864,10 +1849,6 @@ public boolean isJavaSaveActionsOrganizeImportsEnabled() {
return javaSaveActionsOrganizeImportsEnabled;
}

public boolean isJavaSaveActionsRenameFileEnabled() {
return javaSaveActionsRenameFileEnabled;
}

public boolean isSignatureHelpEnabled() {
return signatureHelpEnabled;
}
Expand Down

0 comments on commit cfc4b4e

Please sign in to comment.