-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathRemoteFiles.ts
82 lines (67 loc) · 2.76 KB
/
RemoteFiles.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import os = require("os");
import path = require("path");
import vscode = require("vscode");
import { NotificationType, TextDocumentIdentifier } from "vscode-languageclient";
import { LanguageClientConsumer } from "../languageClientConsumer";
import type { LanguageClient } from "vscode-languageclient/node";
// NOTE: The following two DidSaveTextDocument* types will
// be removed when #593 gets fixed.
export interface IDidSaveTextDocumentParams {
/**
* The document that was closed.
*/
textDocument: TextDocumentIdentifier;
}
export const DidSaveTextDocumentNotificationType =
new NotificationType<IDidSaveTextDocumentParams>(
"textDocument/didSave");
export class RemoteFilesFeature extends LanguageClientConsumer {
private command: vscode.Disposable;
private tempSessionPathPrefix: string;
constructor() {
super();
// Get the common PowerShell Editor Services temporary file path
// so that remote files from previous sessions can be closed.
this.tempSessionPathPrefix =
path.join(os.tmpdir(), "PSES-")
.toLowerCase();
// At startup, close any lingering temporary remote files
this.closeRemoteFiles();
this.command = vscode.workspace.onDidSaveTextDocument(async (doc) => {
if (this.isDocumentRemote(doc)) {
const client = await LanguageClientConsumer.getLanguageClient();
await client.sendNotification(
DidSaveTextDocumentNotificationType,
{
textDocument: TextDocumentIdentifier.create(doc.uri.toString()),
});
}
});
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
public override onLanguageClientSet(_languageClient: LanguageClient): void {}
public dispose(): void {
this.command.dispose();
// Close any leftover remote files before exiting
this.closeRemoteFiles();
}
private isDocumentRemote(doc: vscode.TextDocument): boolean {
return doc.fileName.toLowerCase().startsWith(this.tempSessionPathPrefix);
}
private closeRemoteFiles(): void {
const remoteDocuments =
vscode.workspace.textDocuments.filter((doc) => this.isDocumentRemote(doc));
async function innerCloseFiles(): Promise<void> {
const doc = remoteDocuments.pop();
if (doc === undefined) {
return;
}
await vscode.window.showTextDocument(doc);
await vscode.commands.executeCommand("workbench.action.closeActiveEditor");
await innerCloseFiles();
}
void innerCloseFiles();
}
}