Skip to content

Commit

Permalink
add compute async
Browse files Browse the repository at this point in the history
  • Loading branch information
hopehadfield committed Mar 15, 2024
1 parent f7b4ce8 commit d8c9e62
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
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 @@ -81,9 +80,7 @@
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;
Expand Down Expand Up @@ -378,19 +375,9 @@ 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) {
public static void handleFileRenameForTypeDeclaration(String documentUri) {
ICompilationUnit cu = JDTUtils.resolveCompilationUnit(documentUri);
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
IProblem[] problems = astRoot.getProblems();
Expand All @@ -400,10 +387,7 @@ private void handleSaveActionRenameFile(String documentUri) {
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);
WorkspaceEdit edit = new WorkspaceEdit(List.of(Either.forRight(new RenameFile(documentUri, newUri))));
edit.setChanges(Collections.emptyMap());
final boolean applyNow = JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isWorkspaceApplyEditSupported();
if (applyNow) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -933,6 +934,19 @@ public CompletableFuture<List<TextEdit>> willSaveWaitUntil(WillSaveTextDocumentP
public void didSave(DidSaveTextDocumentParams params) {
debugTrace(">> document/didSave");
documentLifeCycleHandler.didSave(params);
String documentUri = params.getTextDocument().getUri();
Preferences preferences = preferenceManager.getPreferences();
List<String> lspCleanups = Collections.emptyList();
if (preferences.getCleanUpActionsOnSaveEnabled()) {
lspCleanups = preferences.getCleanUpActions();
}

if (lspCleanups.contains("renameFile")) {
computeAsync((monitor) -> {
DocumentLifeCycleHandler.handleFileRenameForTypeDeclaration(documentUri);
return null;
});
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
Expand All @@ -29,9 +28,6 @@
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.manipulation.CoreASTProvider;
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
Expand All @@ -40,14 +36,10 @@
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.RenameFile;
import org.eclipse.lsp4j.ResourceOperation;
import org.eclipse.lsp4j.TextDocumentEdit;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WillSaveTextDocumentParams;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.osgi.service.prefs.BackingStoreException;

public class SaveActionHandler {
Expand Down Expand Up @@ -165,26 +157,4 @@ private List<TextEdit> handleSaveActionOrganizeImports(String documentUri, IProg
return edit;
}

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);
return;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@ public boolean visit(IResourceDelta delta) throws CoreException {
}
if (!cu.isWorkingCopy()) {
markers = resource.findMarkers(null, false, IResource.DEPTH_ONE);
document = JsonRpcHelpers.toDocument(cu.getBuffer());
try {
document = JsonRpcHelpers.toDocument(cu.getBuffer());
} catch (JavaModelException e) {
// do nothing
}
} else if (handler != null) {
handler.triggerValidation(cu);
}
Expand Down

0 comments on commit d8c9e62

Please sign in to comment.