|
2 | 2 |
|
3 | 3 | import * as net from 'net'; |
4 | 4 | import * as path from 'path'; |
5 | | -import { CancellationToken, CodeActionKind, commands, ConfigurationTarget, DocumentSelector, EventEmitter, ExtensionContext, extensions, languages, Location, ProgressLocation, TextEditor, Uri, ViewColumn, window, workspace, WorkspaceConfiguration } from "vscode"; |
| 5 | +import { CancellationToken, CodeActionKind, Command, commands, ConfigurationTarget, DocumentSelector, EventEmitter, ExtensionContext, extensions, languages, Location, ProgressLocation, Range, TextEditor, TextEditorRevealType, Uri, ViewColumn, window, workspace, WorkspaceConfiguration } from "vscode"; |
6 | 6 | import { ConfigurationParams, ConfigurationRequest, LanguageClientOptions, Location as LSLocation, MessageType, Position as LSPosition, TextDocumentPositionParams, WorkspaceEdit, StaticFeature, ClientCapabilities, FeatureState, TelemetryEventNotification } from "vscode-languageclient"; |
7 | 7 | import { LanguageClient, StreamInfo } from "vscode-languageclient/node"; |
8 | 8 | import { apiManager } from "./apiManager"; |
@@ -438,8 +438,30 @@ export class StandardLanguageClient { |
438 | 438 |
|
439 | 439 | context.subscriptions.push(commands.registerCommand(Commands.NULL_ANALYSIS_SET_MODE, (status) => setNullAnalysisStatus(status))); |
440 | 440 |
|
441 | | - context.subscriptions.push(commands.registerCommand(Commands.APPLY_WORKSPACE_EDIT, (obj) => { |
442 | | - applyWorkspaceEdit(obj, this.languageClient); |
| 441 | + context.subscriptions.push(commands.registerCommand(Commands.APPLY_WORKSPACE_EDIT, async (obj) => { |
| 442 | + const activeUri = window.activeTextEditor?.document.uri.toString(); |
| 443 | + await applyWorkspaceEdit(obj, this.languageClient); |
| 444 | + await revealWorkspaceEdit(obj, this.languageClient, activeUri); |
| 445 | + })); |
| 446 | + |
| 447 | + context.subscriptions.push(commands.registerCommand(Commands.REVEAL_EDIT, async (uri: Uri, range: Range, originalCommand?: Command) => { |
| 448 | + if (originalCommand) { |
| 449 | + await commands.executeCommand(originalCommand.command, ...(originalCommand.arguments ?? [])); |
| 450 | + } |
| 451 | + if (window.activeTextEditor?.document.uri.toString() === uri.toString()) { |
| 452 | + return; |
| 453 | + } |
| 454 | + // The edit may still be being applied (e.g. a snippet edit opens the file in the background), |
| 455 | + // so open the document and retry until its editor is the active one. |
| 456 | + const document = await workspace.openTextDocument(uri); |
| 457 | + for (let i = 0; i < 10; i++) { |
| 458 | + const editor = await window.showTextDocument(document, { preview: false }); |
| 459 | + if (window.activeTextEditor?.document.uri.toString() === uri.toString()) { |
| 460 | + editor.revealRange(range, TextEditorRevealType.InCenter); |
| 461 | + return; |
| 462 | + } |
| 463 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 464 | + } |
443 | 465 | })); |
444 | 466 |
|
445 | 467 | context.subscriptions.push(commands.registerCommand(Commands.NAVIGATE_TO_SUPER_IMPLEMENTATION_COMMAND, async (location: LinkLocation | Uri) => { |
@@ -927,6 +949,27 @@ export async function applyWorkspaceEdit(workspaceEdit: WorkspaceEdit, languageC |
927 | 949 | } |
928 | 950 | } |
929 | 951 |
|
| 952 | +/** |
| 953 | + * Reveals the first change of the workspace edit in an editor. |
| 954 | + * If `onlyIfNotUri` is given, nothing happens when the change is in that document. |
| 955 | + */ |
| 956 | +export async function revealWorkspaceEdit(workspaceEdit: WorkspaceEdit, languageClient: LanguageClient, onlyIfNotUri?: string): Promise<void> { |
| 957 | + const codeWorkspaceEdit = await languageClient.protocol2CodeConverter.asWorkspaceEdit(workspaceEdit); |
| 958 | + if (!codeWorkspaceEdit) { |
| 959 | + return; |
| 960 | + } |
| 961 | + for (const [uri, edits] of codeWorkspaceEdit.entries()) { |
| 962 | + if (edits.length > 0) { |
| 963 | + if (uri.toString() !== onlyIfNotUri) { |
| 964 | + // reveal first available change of the workspace edit |
| 965 | + const editor = await window.showTextDocument(uri); |
| 966 | + editor.revealRange(edits[0].range, TextEditorRevealType.InCenter); |
| 967 | + } |
| 968 | + break; |
| 969 | + } |
| 970 | + } |
| 971 | +} |
| 972 | + |
930 | 973 | /** |
931 | 974 | * 'workspace/willRenameFiles' already handled so we need to disable it. |
932 | 975 | * @see fileEventHandler.registerFileEventHandlers |
|
0 commit comments