Skip to content

Commit c5631ec

Browse files
committed
Fix peeked editor closing without reopening when triggered again at the same position in the same file
1 parent d801d41 commit c5631ec

File tree

1 file changed

+70
-15
lines changed

1 file changed

+70
-15
lines changed

src/sourcekit-lsp/peekDocuments.ts

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,82 @@ import * as vscode from "vscode";
1616
import * as langclient from "vscode-languageclient/node";
1717
import { PeekDocumentsParams, PeekDocumentsRequest } from "./lspExtensions";
1818

19+
/**
20+
* Opens a peeked editor in `uri` at `position` having contents from `locations`.
21+
*
22+
* **NOTE**:
23+
* - If the `uri` is not open in the editor, this opens the `uri` in the editor and then opens a peeked editor.
24+
* - This reopens a peeked editor in `uri` **if and only if** the previous peeked editor is being displayed at a
25+
* different position in `uri`.
26+
* - This always closes any previously displayed peeked editor in `uri`.
27+
*
28+
* @param uri The uri of the file in which a peeked editor is to be opened
29+
* @param position The position in the file in which a peeked editor is to be opened
30+
* @param locations The locations of the contents which has to be displayed by the peeked editor
31+
*/
32+
async function openPeekedEditorIn(
33+
uri: vscode.Uri,
34+
position: vscode.Position,
35+
locations: vscode.Location[]
36+
) {
37+
await vscode.commands.executeCommand(
38+
"editor.action.peekLocations",
39+
uri,
40+
position,
41+
locations,
42+
"peek"
43+
);
44+
}
45+
46+
/**
47+
* This function opens at dummy peeked editor in `uri` so that the previously displayed peeked editor gets closed.
48+
*
49+
* This function exists as a **workaround** for not having any API available to close the previously displayed peeked
50+
* editor at `uri`
51+
*
52+
* **NOTE**: This function call must be immediately followed up by a call to `openPeekedEditorIn` at the same `uri`
53+
* inorder to ensure that the dummy peeked editor isn't displayed to the user.
54+
*
55+
* @param uri The file in which the actual peeked editor is meant to be shown
56+
*/
57+
async function openDummyPeekedEditorIn(uri: vscode.Uri) {
58+
// Since there isn't any API available to close the previous peeked editor, as a workaround, we open a dummy
59+
// peeked editor, causing the previous one to close.
60+
await openPeekedEditorIn(uri, new vscode.Position(0, 0), [
61+
new vscode.Location(vscode.Uri.parse(""), new vscode.Position(0, 0)),
62+
]);
63+
}
64+
1965
export function activatePeekDocuments(client: langclient.LanguageClient): vscode.Disposable {
2066
const peekDocuments = client.onRequest(
2167
PeekDocumentsRequest.method,
2268
async (params: PeekDocumentsParams) => {
23-
const locations = params.locations.map(uri => {
24-
const location = new vscode.Location(
25-
client.protocol2CodeConverter.asUri(uri),
26-
new vscode.Position(0, 0)
27-
);
28-
29-
return location;
30-
});
31-
32-
await vscode.commands.executeCommand(
33-
"editor.action.peekLocations",
34-
client.protocol2CodeConverter.asUri(params.uri),
35-
new vscode.Position(params.position.line, params.position.character),
36-
locations,
37-
"peek"
69+
const peekURI = client.protocol2CodeConverter.asUri(params.uri);
70+
71+
const peekPosition = new vscode.Position(
72+
params.position.line,
73+
params.position.character
74+
);
75+
76+
const peekLocations = params.locations.map(
77+
location =>
78+
new vscode.Location(
79+
client.protocol2CodeConverter.asUri(location),
80+
new vscode.Position(0, 0)
81+
)
3882
);
3983

84+
// If there already exists a peeked editor in `peekURI` at `peekPosition`, then the
85+
// `openPeekedEditorIn` call will just close the peeked editor without reopening the peeked
86+
// editor with the new `peekLocations`.
87+
//
88+
// As a workaround, we close the previous peeked editor by opening a dummy peeked editor so that the
89+
// `openPeekedEditorIn` call below can just open the peeked editor with the new `peekLocations` in
90+
// `peekURI` at `peekPosition`.
91+
openDummyPeekedEditorIn(peekURI);
92+
93+
openPeekedEditorIn(peekURI, peekPosition, peekLocations);
94+
4095
return { success: true };
4196
}
4297
);

0 commit comments

Comments
 (0)