Skip to content

Commit

Permalink
Single undo for WorkspaceEdit with multiple edits
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Apr 29, 2022
1 parent 624f20e commit ea27e8e
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 14 deletions.
21 changes: 18 additions & 3 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java
Expand Up @@ -101,6 +101,7 @@
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.DocumentChange;
import org.eclipse.ltk.core.refactoring.PerformChangeOperation;
import org.eclipse.ltk.core.refactoring.RefactoringCore;
import org.eclipse.ltk.core.refactoring.resource.DeleteResourceChange;
import org.eclipse.mylyn.wikitext.markdown.MarkdownLanguage;
import org.eclipse.mylyn.wikitext.parser.MarkupParser;
Expand Down Expand Up @@ -135,6 +136,7 @@
*/
public class LSPEclipseUtils {

private static final String DEFAULT_LABEL = "LSP Workspace Edit"; //$NON-NLS-1$
public static final String HTTP = "http"; //$NON-NLS-1$
public static final String INTRO_URL = "http://org.eclipse.ui.intro"; //$NON-NLS-1$
public static final String FILE_URI = "file://"; //$NON-NLS-1$
Expand Down Expand Up @@ -668,8 +670,20 @@ public static IDocument getDocument(IEditorInput editorInput) {
* @param wsEdit
*/
public static void applyWorkspaceEdit(WorkspaceEdit wsEdit) {
CompositeChange change = toCompositeChange(wsEdit);
applyWorkspaceEdit(wsEdit, null);
}

/**
* Applies a workspace edit. It does simply change the underlying documents.
*
* @param wsEdit
* @param label
*/
public static void applyWorkspaceEdit(WorkspaceEdit wsEdit, String label) {
String name = label == null ? DEFAULT_LABEL : label;
CompositeChange change = toCompositeChange(wsEdit, name);
PerformChangeOperation changeOperation = new PerformChangeOperation(change);
changeOperation.setUndoManager(RefactoringCore.getUndoManager(), name);
try {
ResourcesPlugin.getWorkspace().run(changeOperation, new NullProgressMonitor());
} catch (CoreException e) {
Expand All @@ -681,10 +695,11 @@ public static void applyWorkspaceEdit(WorkspaceEdit wsEdit) {
* Returns a ltk {@link CompositeChange} from a lsp {@link WorkspaceEdit}.
*
* @param wsEdit
* @param name
* @return a ltk {@link CompositeChange} from a lsp {@link WorkspaceEdit}.
*/
public static CompositeChange toCompositeChange(WorkspaceEdit wsEdit) {
CompositeChange change = new CompositeChange("LSP Workspace Edit"); //$NON-NLS-1$
public static CompositeChange toCompositeChange(WorkspaceEdit wsEdit, String name) {
CompositeChange change = new CompositeChange(name);
List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = wsEdit.getDocumentChanges();
if (documentChanges != null) {
// documentChanges are present, the latter are preferred over changes
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2017 Red Hat Inc. and others.
* Copyright (c) 2016, 2022 Red Hat Inc. and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand Down Expand Up @@ -89,7 +89,7 @@ public final CompletableFuture<ApplyWorkspaceEditResponse> applyEdit(ApplyWorksp
Job job = new Job(Messages.serverEdit) {
@Override
public IStatus run(IProgressMonitor monitor) {
LSPEclipseUtils.applyWorkspaceEdit(params.getEdit());
LSPEclipseUtils.applyWorkspaceEdit(params.getEdit(), params.getLabel());
return Status.OK_STATUS;
}
};
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2020 Fraunhofer FOKUS and others.
* Copyright (c) 2019, 2022 Fraunhofer FOKUS and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand Down Expand Up @@ -111,7 +111,7 @@ public static CompletableFuture<Object> executeCommand(@Nullable Command command
// tentative fallback
if (command.getArguments() != null) {
WorkspaceEdit edit = createWorkspaceEdit(command.getArguments(), document);
LSPEclipseUtils.applyWorkspaceEdit(edit);
LSPEclipseUtils.applyWorkspaceEdit(edit, command.getTitle());
return CompletableFuture.completedFuture(null);
}
return null;
Expand Down
Expand Up @@ -76,7 +76,7 @@ public void apply(IDocument document) {
private void apply(CodeAction codeaction) {
if (codeaction != null) {
if (codeaction.getEdit() != null) {
LSPEclipseUtils.applyWorkspaceEdit(codeaction.getEdit());
LSPEclipseUtils.applyWorkspaceEdit(codeaction.getEdit(), codeaction.getTitle());
}
if (codeaction.getCommand() != null) {
executeCommand(codeaction.getCommand());
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018 Red Hat Inc. and others.
* Copyright (c) 2018, 2022 Red Hat Inc. and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand Down Expand Up @@ -53,7 +53,7 @@ public String getLabel() {
@Override
public void run(IMarker marker) {
if (codeAction.getEdit() != null) {
LSPEclipseUtils.applyWorkspaceEdit(codeAction.getEdit());
LSPEclipseUtils.applyWorkspaceEdit(codeAction.getEdit(), codeAction.getTitle());
}
if (codeAction.getCommand() != null) {
IResource resource = marker.getResource();
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016 Red Hat Inc. and others.
* Copyright (c) 2016, 2022 Red Hat Inc. and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand Down Expand Up @@ -130,7 +130,7 @@ public void widgetSelected(SelectionEvent e) {
@Override
public void widgetSelected(SelectionEvent e) {
if (codeAction.getEdit() != null) {
LSPEclipseUtils.applyWorkspaceEdit(codeAction.getEdit());
LSPEclipseUtils.applyWorkspaceEdit(codeAction.getEdit(), codeAction.getTitle());
}
if (codeAction.getCommand() != null) {
executeCommand(info, codeAction.getCommand());
Expand Down
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2017-2021 Angelo ZERR.
* Copyright (c) 2017-2022 Angelo ZERR.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -197,7 +197,7 @@ public Change createChange(IProgressMonitor pm) throws CoreException, OperationC
throw new CoreException(
new Status(IStatus.ERROR, LanguageServerPlugin.PLUGIN_ID, Messages.rename_processor_required));
}
return LSPEclipseUtils.toCompositeChange(rename);
return LSPEclipseUtils.toCompositeChange(rename, "Rename"); //$NON-NLS-1$
}

@Override
Expand Down

0 comments on commit ea27e8e

Please sign in to comment.