|
| 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 | +} |
0 commit comments