Skip to content

Commit

Permalink
Add $psEditor CurrentFile SaveAs support (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmholt authored and TylerLeonhardt committed Apr 18, 2018
1 parent 437e71e commit 4716b7f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,15 @@ public static readonly
public class SaveFileRequest
{
public static readonly
RequestType<string, EditorCommandResponse, object, object> Type =
RequestType<string, EditorCommandResponse, object, object>.Create("editor/saveFile");
RequestType<SaveFileDetails, EditorCommandResponse, object, object> Type =
RequestType<SaveFileDetails, EditorCommandResponse, object, object>.Create("editor/saveFile");
}

public class SaveFileDetails
{
public string FilePath { get; set; }

public string NewPath { get; set; }
}

public class ShowInformationMessageRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
23 changes: 23 additions & 0 deletions src/PowerShellEditorServices/Extensions/FileContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//

using System;
using System.IO;
using System.Linq;
using System.Management.Automation.Language;

Expand Down Expand Up @@ -239,6 +240,28 @@ public void Save()
this.editorOperations.SaveFile(this.scriptFile.FilePath);
}

/// <summary>
/// Save this file under a new path and open a new editor window on that file.
/// </summary>
/// <param name="newFilePath">
/// the path where the file should be saved,
/// including the file name with extension as the leaf
/// </param>
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
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/PowerShellEditorServices/Extensions/IEditorOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public interface IEditorOperations
/// <returns>A Task that can be tracked for completion.</returns>
Task SaveFile(string filePath);

/// <summary>
/// Causes a file to be saved as a new file in a new editor window.
/// </summary>
/// <param name="oldFilePath">the path of the current file being saved</param>
/// <param name="newFilePath">the path of the new file where the current window content will be saved</param>
/// <returns></returns>
Task SaveFile(string oldFilePath, string newFilePath);

/// <summary>
/// Inserts text into the specified range for the file at the specified path.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ await this.extensionEventQueue.EnqueueAsync(

public class TestEditorOperations : IEditorOperations
{

public string GetWorkspacePath()
{
throw new NotImplementedException();
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 4716b7f

Please sign in to comment.