diff --git a/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1 b/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1 index cd4bf764c..9e4fcb42e 100644 --- a/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1 +++ b/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1 @@ -74,6 +74,7 @@ FunctionsToExport = @('Register-EditorCommand', 'ConvertFrom-ScriptExtent', 'ConvertTo-ScriptExtent', 'Get-Token', + 'Out-CurrentFile', 'Join-ScriptExtent', 'Test-ScriptExtent', 'psedit') diff --git a/module/PowerShellEditorServices/Commands/Public/Out-CurrentFile.ps1 b/module/PowerShellEditorServices/Commands/Public/Out-CurrentFile.ps1 new file mode 100644 index 000000000..cf0bad607 --- /dev/null +++ b/module/PowerShellEditorServices/Commands/Public/Out-CurrentFile.ps1 @@ -0,0 +1,13 @@ +function Out-CurrentFile { + param( + [Parameter(ValueFromPipeline)] + $data + ) + + Begin { $d = @() } + Process { $d += $data } + End { + $target = "@`"`r`n{0}`r`n`"@" -f ($d|out-string).Trim() + $pseditor.GetEditorContext().currentfile.inserttext($target) + } +} \ No newline at end of file diff --git a/src/PowerShellEditorServices.Protocol/LanguageServer/EditorCommands.cs b/src/PowerShellEditorServices.Protocol/LanguageServer/EditorCommands.cs index 04d15c0e9..be66829a7 100644 --- a/src/PowerShellEditorServices.Protocol/LanguageServer/EditorCommands.cs +++ b/src/PowerShellEditorServices.Protocol/LanguageServer/EditorCommands.cs @@ -101,6 +101,13 @@ public static readonly public Position CursorPosition { get; set; } } + public class NewFileRequest + { + public static readonly + RequestType Type = + RequestType.Create("editor/newFile"); + } + public class OpenFileRequest { public static readonly diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServerEditorOperations.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServerEditorOperations.cs index c8648660d..2b2746d04 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServerEditorOperations.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServerEditorOperations.cs @@ -101,6 +101,15 @@ public EditorContext ConvertClientEditorContext( clientContext.SelectionRange.End.Character + 1)); } + public Task NewFile() + { + return + this.messageSender.SendRequest( + NewFileRequest.Type, + null, + true); + } + public Task OpenFile(string filePath) { return diff --git a/src/PowerShellEditorServices/Extensions/EditorWorkspace.cs b/src/PowerShellEditorServices/Extensions/EditorWorkspace.cs index 68d113523..8923b48ef 100644 --- a/src/PowerShellEditorServices/Extensions/EditorWorkspace.cs +++ b/src/PowerShellEditorServices/Extensions/EditorWorkspace.cs @@ -40,6 +40,14 @@ internal EditorWorkspace(IEditorOperations editorOperations) #region Public Methods + /// + /// Creates a new file in the editor + /// + public void NewFile() + { + this.editorOperations.NewFile().Wait(); + } + /// /// Opens a file in the workspace. If the file is already open /// its buffer will be made active. diff --git a/src/PowerShellEditorServices/Extensions/IEditorOperations.cs b/src/PowerShellEditorServices/Extensions/IEditorOperations.cs index 9471aec80..e9a970ce1 100644 --- a/src/PowerShellEditorServices/Extensions/IEditorOperations.cs +++ b/src/PowerShellEditorServices/Extensions/IEditorOperations.cs @@ -33,6 +33,8 @@ public interface IEditorOperations /// The resolved file path. string GetWorkspaceRelativePath(string filePath); + Task NewFile(); + /// /// Causes a file to be opened in the editor. If the file is /// already open, the editor must switch to the file. diff --git a/test/PowerShellEditorServices.Test/Extensions/ExtensionServiceTests.cs b/test/PowerShellEditorServices.Test/Extensions/ExtensionServiceTests.cs index a0f251fee..84bee5562 100644 --- a/test/PowerShellEditorServices.Test/Extensions/ExtensionServiceTests.cs +++ b/test/PowerShellEditorServices.Test/Extensions/ExtensionServiceTests.cs @@ -174,6 +174,7 @@ await this.extensionEventQueue.EnqueueAsync( public class TestEditorOperations : IEditorOperations { + public string GetWorkspacePath() { throw new NotImplementedException(); @@ -184,6 +185,11 @@ public string GetWorkspaceRelativePath(string filePath) throw new NotImplementedException(); } + public Task NewFile() + { + throw new NotImplementedException(); + } + public Task OpenFile(string filePath) { throw new NotImplementedException();