From 204b0ad4ab70debf82257a357b540473b0dea2a8 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Fri, 6 Apr 2018 14:07:20 -0700 Subject: [PATCH] Add $psEditor CurrentFile SaveAs support --- .../LanguageServer/EditorCommands.cs | 11 +++++++-- .../Server/LanguageServerEditorOperations.cs | 11 ++++++++- .../Extensions/FileContext.cs | 23 +++++++++++++++++++ .../Extensions/IEditorOperations.cs | 8 +++++++ .../Extensions/ExtensionServiceTests.cs | 7 +++++- 5 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/LanguageServer/EditorCommands.cs b/src/PowerShellEditorServices.Protocol/LanguageServer/EditorCommands.cs index f5dd40217..2637ec4cb 100644 --- a/src/PowerShellEditorServices.Protocol/LanguageServer/EditorCommands.cs +++ b/src/PowerShellEditorServices.Protocol/LanguageServer/EditorCommands.cs @@ -132,8 +132,15 @@ public static readonly public class SaveFileRequest { public static readonly - RequestType Type = - RequestType.Create("editor/saveFile"); + RequestType Type = + RequestType.Create("editor/saveFile"); + } + + public class SaveFileDetails + { + public string FilePath { get; set; } + + public string NewPath { get; set; } } public class ShowInformationMessageRequest diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServerEditorOperations.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServerEditorOperations.cs index f8989cfb5..84a561b0a 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServerEditorOperations.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServerEditorOperations.cs @@ -148,11 +148,20 @@ public Task CloseFile(string filePath) } public Task SaveFile(string filePath) + { + return SaveFile(filePath, null); + } + + public Task SaveFile(string currentPath, string newSavePath) { return this.messageSender.SendRequest( SaveFileRequest.Type, - filePath, + new SaveFileDetails + { + FilePath = currentPath, + NewPath = newSavePath + }, true); } diff --git a/src/PowerShellEditorServices/Extensions/FileContext.cs b/src/PowerShellEditorServices/Extensions/FileContext.cs index b725a1b54..35b7e18fd 100644 --- a/src/PowerShellEditorServices/Extensions/FileContext.cs +++ b/src/PowerShellEditorServices/Extensions/FileContext.cs @@ -4,6 +4,7 @@ // using System; +using System.IO; using System.Linq; using System.Management.Automation.Language; @@ -239,6 +240,28 @@ public void Save() this.editorOperations.SaveFile(this.scriptFile.FilePath); } + /// + /// Save this file under a new path and open a new editor window on that file. + /// + /// + /// the path where the file should be saved, + /// including the file name with extension as the leaf + /// + public void SaveAs(string newFilePath) + { + // Do some validation here so that we can provide a helpful error if the path won't work + string absolutePath = System.IO.Path.IsPathRooted(newFilePath) ? + newFilePath : + System.IO.Path.GetFullPath(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(this.scriptFile.FilePath), newFilePath)); + + if (File.Exists(absolutePath)) + { + throw new IOException(String.Format("The file '{0}' already exists", absolutePath)); + } + + this.editorOperations.SaveFile(this.scriptFile.FilePath, newFilePath); + } + #endregion } } diff --git a/src/PowerShellEditorServices/Extensions/IEditorOperations.cs b/src/PowerShellEditorServices/Extensions/IEditorOperations.cs index 2cf097708..51d0ac5b7 100644 --- a/src/PowerShellEditorServices/Extensions/IEditorOperations.cs +++ b/src/PowerShellEditorServices/Extensions/IEditorOperations.cs @@ -71,6 +71,14 @@ public interface IEditorOperations /// A Task that can be tracked for completion. Task SaveFile(string filePath); + /// + /// Causes a file to be saved as a new file in a new editor window. + /// + /// the path of the current file being saved + /// the path of the new file where the current window content will be saved + /// + Task SaveFile(string oldFilePath, string newFilePath); + /// /// Inserts text into the specified range for the file at the specified path. /// diff --git a/test/PowerShellEditorServices.Test/Extensions/ExtensionServiceTests.cs b/test/PowerShellEditorServices.Test/Extensions/ExtensionServiceTests.cs index 858ccaa98..1476af82d 100644 --- a/test/PowerShellEditorServices.Test/Extensions/ExtensionServiceTests.cs +++ b/test/PowerShellEditorServices.Test/Extensions/ExtensionServiceTests.cs @@ -174,7 +174,7 @@ await this.extensionEventQueue.EnqueueAsync( public class TestEditorOperations : IEditorOperations { - + public string GetWorkspacePath() { throw new NotImplementedException(); @@ -206,6 +206,11 @@ public Task CloseFile(string filePath) } public Task SaveFile(string filePath) + { + return SaveFile(filePath, null); + } + + public Task SaveFile(string filePath, string newSavePath) { throw new NotImplementedException(); }