Skip to content

Commit 4716b7f

Browse files
rjmholtTylerLeonhardt
authored andcommitted
Add $psEditor CurrentFile SaveAs support (#650)
1 parent 437e71e commit 4716b7f

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

src/PowerShellEditorServices.Protocol/LanguageServer/EditorCommands.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,15 @@ public static readonly
132132
public class SaveFileRequest
133133
{
134134
public static readonly
135-
RequestType<string, EditorCommandResponse, object, object> Type =
136-
RequestType<string, EditorCommandResponse, object, object>.Create("editor/saveFile");
135+
RequestType<SaveFileDetails, EditorCommandResponse, object, object> Type =
136+
RequestType<SaveFileDetails, EditorCommandResponse, object, object>.Create("editor/saveFile");
137+
}
138+
139+
public class SaveFileDetails
140+
{
141+
public string FilePath { get; set; }
142+
143+
public string NewPath { get; set; }
137144
}
138145

139146
public class ShowInformationMessageRequest

src/PowerShellEditorServices.Protocol/Server/LanguageServerEditorOperations.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,20 @@ public Task CloseFile(string filePath)
148148
}
149149

150150
public Task SaveFile(string filePath)
151+
{
152+
return SaveFile(filePath, null);
153+
}
154+
155+
public Task SaveFile(string currentPath, string newSavePath)
151156
{
152157
return
153158
this.messageSender.SendRequest(
154159
SaveFileRequest.Type,
155-
filePath,
160+
new SaveFileDetails
161+
{
162+
FilePath = currentPath,
163+
NewPath = newSavePath
164+
},
156165
true);
157166
}
158167

src/PowerShellEditorServices/Extensions/FileContext.cs

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55

66
using System;
7+
using System.IO;
78
using System.Linq;
89
using System.Management.Automation.Language;
910

@@ -239,6 +240,28 @@ public void Save()
239240
this.editorOperations.SaveFile(this.scriptFile.FilePath);
240241
}
241242

243+
/// <summary>
244+
/// Save this file under a new path and open a new editor window on that file.
245+
/// </summary>
246+
/// <param name="newFilePath">
247+
/// the path where the file should be saved,
248+
/// including the file name with extension as the leaf
249+
/// </param>
250+
public void SaveAs(string newFilePath)
251+
{
252+
// Do some validation here so that we can provide a helpful error if the path won't work
253+
string absolutePath = System.IO.Path.IsPathRooted(newFilePath) ?
254+
newFilePath :
255+
System.IO.Path.GetFullPath(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(this.scriptFile.FilePath), newFilePath));
256+
257+
if (File.Exists(absolutePath))
258+
{
259+
throw new IOException(String.Format("The file '{0}' already exists", absolutePath));
260+
}
261+
262+
this.editorOperations.SaveFile(this.scriptFile.FilePath, newFilePath);
263+
}
264+
242265
#endregion
243266
}
244267
}

src/PowerShellEditorServices/Extensions/IEditorOperations.cs

+8
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public interface IEditorOperations
7171
/// <returns>A Task that can be tracked for completion.</returns>
7272
Task SaveFile(string filePath);
7373

74+
/// <summary>
75+
/// Causes a file to be saved as a new file in a new editor window.
76+
/// </summary>
77+
/// <param name="oldFilePath">the path of the current file being saved</param>
78+
/// <param name="newFilePath">the path of the new file where the current window content will be saved</param>
79+
/// <returns></returns>
80+
Task SaveFile(string oldFilePath, string newFilePath);
81+
7482
/// <summary>
7583
/// Inserts text into the specified range for the file at the specified path.
7684
/// </summary>

test/PowerShellEditorServices.Test/Extensions/ExtensionServiceTests.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ await this.extensionEventQueue.EnqueueAsync(
174174

175175
public class TestEditorOperations : IEditorOperations
176176
{
177-
177+
178178
public string GetWorkspacePath()
179179
{
180180
throw new NotImplementedException();
@@ -206,6 +206,11 @@ public Task CloseFile(string filePath)
206206
}
207207

208208
public Task SaveFile(string filePath)
209+
{
210+
return SaveFile(filePath, null);
211+
}
212+
213+
public Task SaveFile(string filePath, string newSavePath)
209214
{
210215
throw new NotImplementedException();
211216
}

0 commit comments

Comments
 (0)