-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathRemoteFiles.ts
83 lines (67 loc) · 2.71 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
83
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import os = require('os');
import path = require('path');
import vscode = require('vscode');
import { IFeature } from '../feature';
import { LanguageClient, RequestType, NotificationType, TextDocumentIdentifier } from 'vscode-languageclient';
// NOTE: The following two DidSaveTextDocument* types will
// be removed when #593 gets fixed.
export interface DidSaveTextDocumentParams {
/**
* The document that was closed.
*/
textDocument: TextDocumentIdentifier;
}
export namespace DidSaveTextDocumentNotification {
export const type = new NotificationType<DidSaveTextDocumentParams, void>('textDocument/didSave');
}
export class RemoteFilesFeature implements IFeature {
private tempSessionPathPrefix: string;
private languageClient: LanguageClient;
constructor() {
// 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();
vscode.workspace.onDidSaveTextDocument(doc => {
if (this.languageClient && this.isDocumentRemote(doc)) {
this.languageClient.sendNotification(
DidSaveTextDocumentNotification.type,
{
textDocument: TextDocumentIdentifier.create(doc.uri.toString())
});
}
})
}
public setLanguageClient(languageclient: LanguageClient) {
this.languageClient = languageclient;
}
public dispose() {
// Close any leftover remote files before exiting
this.closeRemoteFiles();
}
private isDocumentRemote(doc: vscode.TextDocument) {
return doc.languageId === "powershell" &&
doc.fileName.toLowerCase().startsWith(this.tempSessionPathPrefix);
}
private closeRemoteFiles() {
var remoteDocuments =
vscode.workspace.textDocuments.filter(
doc => this.isDocumentRemote(doc));
function innerCloseFiles(): Thenable<{}> {
if (remoteDocuments.length > 0) {
var doc = remoteDocuments.pop();
return vscode.window
.showTextDocument(doc)
.then(editor => vscode.commands.executeCommand("workbench.action.closeActiveEditor"))
.then(_ => innerCloseFiles());
}
};
innerCloseFiles();
}
}