|
| 1 | +/* |
| 2 | + * Copyright (c) 2012-2017 Red Hat, Inc. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Red Hat, Inc. - initial API and implementation |
| 10 | + */ |
| 11 | +package org.eclipse.che.plugin.languageserver.ide.editor.quickassist; |
| 12 | + |
| 13 | +import com.google.inject.Inject; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Comparator; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.function.Supplier; |
| 19 | +import java.util.stream.Collectors; |
| 20 | +import org.eclipse.che.api.languageserver.shared.dto.DtoClientImpls.FileEditParamsDto; |
| 21 | +import org.eclipse.che.api.languageserver.shared.model.FileEditParams; |
| 22 | +import org.eclipse.che.api.languageserver.shared.util.RangeComparator; |
| 23 | +import org.eclipse.che.api.promises.client.Function; |
| 24 | +import org.eclipse.che.api.promises.client.Promise; |
| 25 | +import org.eclipse.che.api.promises.client.PromiseError; |
| 26 | +import org.eclipse.che.api.promises.client.PromiseProvider; |
| 27 | +import org.eclipse.che.api.promises.client.js.Executor; |
| 28 | +import org.eclipse.che.api.promises.client.js.RejectFunction; |
| 29 | +import org.eclipse.che.api.promises.client.js.ResolveFunction; |
| 30 | +import org.eclipse.che.ide.api.action.Action; |
| 31 | +import org.eclipse.che.ide.api.action.ActionEvent; |
| 32 | +import org.eclipse.che.ide.api.editor.EditorAgent; |
| 33 | +import org.eclipse.che.ide.api.editor.EditorPartPresenter; |
| 34 | +import org.eclipse.che.ide.api.editor.document.Document; |
| 35 | +import org.eclipse.che.ide.api.editor.texteditor.HandlesUndoRedo; |
| 36 | +import org.eclipse.che.ide.api.editor.texteditor.TextEditor; |
| 37 | +import org.eclipse.che.ide.api.notification.Notification; |
| 38 | +import org.eclipse.che.ide.api.notification.NotificationManager; |
| 39 | +import org.eclipse.che.ide.api.notification.StatusNotification; |
| 40 | +import org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode; |
| 41 | +import org.eclipse.che.ide.api.notification.StatusNotification.Status; |
| 42 | +import org.eclipse.che.ide.dto.DtoFactory; |
| 43 | +import org.eclipse.che.ide.util.loging.Log; |
| 44 | +import org.eclipse.che.plugin.languageserver.ide.LanguageServerLocalization; |
| 45 | +import org.eclipse.che.plugin.languageserver.ide.service.WorkspaceServiceClient; |
| 46 | +import org.eclipse.che.plugin.languageserver.ide.util.PromiseHelper; |
| 47 | +import org.eclipse.lsp4j.Position; |
| 48 | +import org.eclipse.lsp4j.Range; |
| 49 | +import org.eclipse.lsp4j.TextDocumentEdit; |
| 50 | +import org.eclipse.lsp4j.TextEdit; |
| 51 | +import org.eclipse.lsp4j.WorkspaceEdit; |
| 52 | + |
| 53 | +public class ApplyWorkspaceEditAction extends Action { |
| 54 | + private static final Comparator<TextEdit> COMPARATOR = |
| 55 | + RangeComparator.transform(new RangeComparator().reversed(), TextEdit::getRange); |
| 56 | + |
| 57 | + private EditorAgent editorAgent; |
| 58 | + private DtoFactory dtoFactory; |
| 59 | + private WorkspaceServiceClient workspaceService; |
| 60 | + private PromiseHelper promiseHelper; |
| 61 | + private LanguageServerLocalization localization; |
| 62 | + private NotificationManager notificationManager; |
| 63 | + private PromiseProvider promiseProvider; |
| 64 | + |
| 65 | + @Inject |
| 66 | + public ApplyWorkspaceEditAction( |
| 67 | + EditorAgent editorAgent, |
| 68 | + DtoFactory dtoFactory, |
| 69 | + WorkspaceServiceClient workspaceService, |
| 70 | + PromiseHelper promiseHelper, |
| 71 | + LanguageServerLocalization localization, |
| 72 | + NotificationManager notificationManager, |
| 73 | + PromiseProvider promiseProvider) { |
| 74 | + this.editorAgent = editorAgent; |
| 75 | + this.dtoFactory = dtoFactory; |
| 76 | + this.workspaceService = workspaceService; |
| 77 | + this.promiseHelper = promiseHelper; |
| 78 | + this.localization = localization; |
| 79 | + this.notificationManager = notificationManager; |
| 80 | + this.promiseProvider = promiseProvider; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void actionPerformed(ActionEvent evt) { |
| 85 | + QuickassistActionEvent qaEvent = (QuickassistActionEvent) evt; |
| 86 | + List<Object> arguments = qaEvent.getArguments(); |
| 87 | + WorkspaceEdit edit = |
| 88 | + dtoFactory.createDtoFromJson(arguments.get(0).toString(), WorkspaceEdit.class); |
| 89 | + List<Supplier<Promise<Void>>> undos = new ArrayList<>(); |
| 90 | + |
| 91 | + StatusNotification notification = |
| 92 | + notificationManager.notify( |
| 93 | + localization.applyWorkspaceActionNotificationTitle(), |
| 94 | + Status.PROGRESS, |
| 95 | + DisplayMode.FLOAT_MODE); |
| 96 | + |
| 97 | + Map<String, List<TextEdit>> changes = null; |
| 98 | + if (edit.getChanges() != null) { |
| 99 | + changes = edit.getChanges(); |
| 100 | + } else if (edit.getDocumentChanges() != null) { |
| 101 | + changes = |
| 102 | + edit.getDocumentChanges() |
| 103 | + .stream() |
| 104 | + .collect( |
| 105 | + Collectors.toMap( |
| 106 | + (TextDocumentEdit e) -> e.getTextDocument().getUri(), |
| 107 | + TextDocumentEdit::getEdits)); |
| 108 | + } |
| 109 | + |
| 110 | + Promise<Void> done = |
| 111 | + promiseHelper.forEach( |
| 112 | + changes.entrySet().iterator(), |
| 113 | + (entry) -> handleFileChange(notification, entry.getKey(), entry.getValue()), |
| 114 | + undos::add); |
| 115 | + |
| 116 | + done.then( |
| 117 | + (Void v) -> { |
| 118 | + Log.debug(getClass(), "done applying changes"); |
| 119 | + notification.setStatus(Status.SUCCESS); |
| 120 | + notification.setContent(localization.applyWorkspaceActionNotificationDone()); |
| 121 | + }) |
| 122 | + .catchError( |
| 123 | + (error) -> { |
| 124 | + Log.info(getClass(), "caught error applying changes", error); |
| 125 | + notification.setStatus(Status.FAIL); |
| 126 | + notification.setContent(localization.applyWorkspaceActionNotificationUndoing()); |
| 127 | + promiseHelper |
| 128 | + .forEach(undos.iterator(), (d) -> d.get(), (Void v) -> {}) |
| 129 | + .then( |
| 130 | + (Void v) -> { |
| 131 | + notification.setContent( |
| 132 | + localization.applyWorkspaceActionNotificationUndone()); |
| 133 | + }) |
| 134 | + .catchError( |
| 135 | + e -> { |
| 136 | + Log.info(getClass(), "Error undoing changes", e); |
| 137 | + notification.setContent( |
| 138 | + localization.applyWorkspaceActionNotificationUndoFailed()); |
| 139 | + }); |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + private Promise<Supplier<Promise<Void>>> handleFileChange( |
| 144 | + Notification notification, String uri, List<TextEdit> edits) { |
| 145 | + for (EditorPartPresenter editor : editorAgent.getOpenedEditors()) { |
| 146 | + if (editor instanceof TextEditor |
| 147 | + && uri.endsWith(editor.getEditorInput().getFile().getLocation().toString())) { |
| 148 | + notification.setContent(localization.applyWorkspaceActionNotificationModifying(uri)); |
| 149 | + TextEditor textEditor = (TextEditor) editor; |
| 150 | + HandlesUndoRedo undoRedo = textEditor.getEditorWidget().getUndoRedo(); |
| 151 | + undoRedo.beginCompoundChange(); |
| 152 | + Document document = textEditor.getDocument(); |
| 153 | + edits |
| 154 | + .stream() |
| 155 | + .sorted(COMPARATOR) |
| 156 | + .forEach( |
| 157 | + e -> { |
| 158 | + Range r = e.getRange(); |
| 159 | + Position start = r.getStart(); |
| 160 | + Position end = r.getEnd(); |
| 161 | + document.replace( |
| 162 | + start.getLine(), |
| 163 | + start.getCharacter(), |
| 164 | + end.getLine(), |
| 165 | + end.getCharacter(), |
| 166 | + e.getNewText()); |
| 167 | + }); |
| 168 | + undoRedo.endCompoundChange(); |
| 169 | + Supplier<Promise<Void>> value = |
| 170 | + () -> { |
| 171 | + return promiseProvider.create( |
| 172 | + Executor.create( |
| 173 | + (ResolveFunction<Void> resolve, RejectFunction reject) -> { |
| 174 | + try { |
| 175 | + undoRedo.undo(); |
| 176 | + resolve.apply(null); |
| 177 | + } catch (Exception e) { |
| 178 | + reject.apply( |
| 179 | + new PromiseError() { |
| 180 | + public String getMessage() { |
| 181 | + return "Error during undo"; |
| 182 | + } |
| 183 | + |
| 184 | + public Throwable getCause() { |
| 185 | + return e; |
| 186 | + } |
| 187 | + }); |
| 188 | + } |
| 189 | + })); |
| 190 | + }; |
| 191 | + return promiseProvider.resolve(value); |
| 192 | + } |
| 193 | + } |
| 194 | + Promise<List<TextEdit>> undoPromise = |
| 195 | + workspaceService.editFile(new FileEditParamsDto(new FileEditParams(uri, edits))); |
| 196 | + return undoPromise.then( |
| 197 | + (Function<List<TextEdit>, Supplier<Promise<Void>>>) |
| 198 | + (List<TextEdit> undoEdits) -> { |
| 199 | + return () -> { |
| 200 | + Promise<List<TextEdit>> redoPromise = |
| 201 | + workspaceService.editFile( |
| 202 | + new FileEditParamsDto(new FileEditParams(uri, undoEdits))); |
| 203 | + return redoPromise.then( |
| 204 | + (List<TextEdit> redo) -> { |
| 205 | + return null; |
| 206 | + }); |
| 207 | + }; |
| 208 | + }); |
| 209 | + } |
| 210 | +} |
0 commit comments