Skip to content

Commit 183814f

Browse files
committed
Add "remote files" feature that handles files from remote sessions
This change adds a new remote files feature which basically just handles the cleanup of remote session files when the extension is loaded or unloaded. This is to ensure that the user doesn't accidentally edit a locally cached remote file thinking it is their local copy. It also shows a warning to the user when they save changes to a remote file because we currently don't propagate those changes back to the remote session.
1 parent d84582b commit 183814f

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/features/RemoteFiles.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*---------------------------------------------------------
2+
* Copyright (C) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------*/
4+
5+
import os = require('os');
6+
import path = require('path');
7+
import vscode = require('vscode');
8+
import { IFeature } from '../feature';
9+
import { LanguageClient, RequestType, NotificationType } from 'vscode-languageclient';
10+
11+
export class RemoteFilesFeature implements IFeature {
12+
13+
private tempSessionPathPrefix: string;
14+
15+
constructor() {
16+
// Get the common PowerShell Editor Services temporary file path
17+
// so that remote files from previous sessions can be closed.
18+
this.tempSessionPathPrefix =
19+
path.join(os.tmpdir(), 'PSES-')
20+
.toLowerCase();
21+
22+
// At startup, close any lingering temporary remote files
23+
this.closeRemoteFiles();
24+
25+
// TEMPORARY: Register for the onDidSave event so that we can alert
26+
// the user when they attempt to save a remote file. We don't
27+
// currently propagate saved content back to the remote session.
28+
vscode.workspace.onDidSaveTextDocument(doc => {
29+
if (this.isDocumentRemote(doc)) {
30+
vscode.window.showWarningMessage(
31+
"Changes to remote files are not yet saved back in the remote session, coming in 0.10.0.");
32+
}
33+
})
34+
}
35+
36+
public setLanguageClient(languageclient: LanguageClient) {
37+
}
38+
39+
public dispose() {
40+
// Close any leftover remote files before exiting
41+
this.closeRemoteFiles();
42+
}
43+
44+
private isDocumentRemote(doc: vscode.TextDocument) {
45+
return doc.languageId === "powershell" &&
46+
doc.fileName.toLowerCase().startsWith(this.tempSessionPathPrefix);
47+
}
48+
49+
private closeRemoteFiles() {
50+
var remoteDocuments =
51+
vscode.workspace.textDocuments.filter(
52+
doc => this.isDocumentRemote(doc));
53+
54+
function innerCloseFiles(): Thenable<{}> {
55+
if (remoteDocuments.length > 0) {
56+
var doc = remoteDocuments.pop();
57+
58+
return vscode.window
59+
.showTextDocument(doc)
60+
.then(editor => vscode.commands.executeCommand("workbench.action.closeActiveEditor"))
61+
.then(_ => innerCloseFiles());
62+
}
63+
};
64+
65+
innerCloseFiles();
66+
}
67+
}

src/main.ts

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { OpenInISEFeature } from './features/OpenInISE';
1616
import { ExpandAliasFeature } from './features/ExpandAlias';
1717
import { ShowHelpFeature } from './features/ShowOnlineHelp';
1818
import { CodeActionsFeature } from './features/CodeActions';
19+
import { RemoteFilesFeature } from './features/RemoteFiles';
1920
import { DebugSessionFeature } from './features/DebugSession';
2021
import { PickPSHostProcessFeature } from './features/DebugSession';
2122
import { SelectPSSARulesFeature } from './features/SelectPSSARules';
@@ -106,6 +107,7 @@ export function activate(context: vscode.ExtensionContext): void {
106107
new CodeActionsFeature(),
107108
new NewFileOrProjectFeature(),
108109
new DocumentFormatterFeature(),
110+
new RemoteFilesFeature(),
109111
new DebugSessionFeature(),
110112
new PickPSHostProcessFeature()
111113
];

0 commit comments

Comments
 (0)