Skip to content

Commit 68321a0

Browse files
committed
Add handler for editor/closeFile request
This change adds a handler for the editor/closeFile request which is part of the editor extensibility model of PowerShell Editor Services. This request currently gets fired when the user exits a remote debugging session where a remote file had been opened. It will later be exposed through the $psEditor API.
1 parent a557fe0 commit 68321a0

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/features/ExtensionCommands.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ export namespace OpenFileRequest {
122122
{ get method() { return 'editor/openFile'; } };
123123
}
124124

125+
export namespace CloseFileRequest {
126+
export const type: RequestType<string, EditorOperationResponse, void> =
127+
{ get method() { return 'editor/closeFile'; } };
128+
}
129+
125130
export namespace ShowErrorMessageRequest {
126131
export const type: RequestType<string, EditorOperationResponse, void> =
127132
{ get method() { return 'editor/showErrorMessage'; } };
@@ -198,6 +203,10 @@ export class ExtensionCommandsFeature implements IFeature {
198203
OpenFileRequest.type,
199204
filePath => this.openFile(filePath));
200205

206+
this.languageClient.onRequest(
207+
CloseFileRequest.type,
208+
filePath => this.closeFile(filePath));
209+
201210
this.languageClient.onRequest(
202211
ShowInformationMessageRequest.type,
203212
message => this.showInformationMessage(message));
@@ -317,6 +326,37 @@ export class ExtensionCommandsFeature implements IFeature {
317326
return promise;
318327
}
319328

329+
private closeFile(filePath: string): Thenable<EditorOperationResponse> {
330+
331+
var promise: Thenable<EditorOperationResponse>;
332+
333+
// Make sure the file path is absolute
334+
if (!path.win32.isAbsolute(filePath))
335+
{
336+
filePath = path.win32.resolve(
337+
vscode.workspace.rootPath,
338+
filePath);
339+
}
340+
341+
// Normalize file path case for comparison
342+
var normalizedFilePath = filePath.toLowerCase();
343+
344+
if (vscode.workspace.textDocuments.find(doc => doc.fileName.toLowerCase() == normalizedFilePath))
345+
{
346+
promise =
347+
vscode.workspace.openTextDocument(filePath)
348+
.then(doc => vscode.window.showTextDocument(doc))
349+
.then(editor => vscode.commands.executeCommand("workbench.action.closeActiveEditor"))
350+
.then(_ => EditorOperationResponse.Completed);
351+
}
352+
else
353+
{
354+
promise = Promise.resolve(EditorOperationResponse.Completed);
355+
}
356+
357+
return promise;
358+
}
359+
320360
private setSelection(details: SetSelectionRequestArguments): EditorOperationResponse {
321361
vscode.window.activeTextEditor.selections = [
322362
new vscode.Selection(

0 commit comments

Comments
 (0)