Skip to content

Commit f534ae6

Browse files
Add New-EditorFile (#622)
* add new file capabilities to psedit * Add New-EditorFile and dynamically load functions in module * Support no path opening an Untitled file * add preview to open file
1 parent 9e96d29 commit f534ae6

File tree

8 files changed

+339
-59
lines changed

8 files changed

+339
-59
lines changed

module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ FunctionsToExport = @('Register-EditorCommand',
7777
'Out-CurrentFile',
7878
'Join-ScriptExtent',
7979
'Test-ScriptExtent',
80-
'Open-EditorFile')
80+
'Open-EditorFile',
81+
'New-EditorFile')
8182

8283
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
8384
CmdletsToExport = @()

module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,111 @@ function Unregister-EditorCommand {
7777
}
7878
}
7979

80+
<#
81+
.SYNOPSIS
82+
Creates new files and opens them in your editor window
83+
.DESCRIPTION
84+
Creates new files and opens them in your editor window
85+
.EXAMPLE
86+
PS > New-EditorFile './foo.ps1'
87+
Creates and opens a new foo.ps1 in your editor
88+
.EXAMPLE
89+
PS > Get-Process | New-EditorFile proc.txt
90+
Creates and opens a new foo.ps1 in your editor with the contents of the call to Get-Process
91+
.EXAMPLE
92+
PS > Get-Process | New-EditorFile proc.txt -Force
93+
Creates and opens a new foo.ps1 in your editor with the contents of the call to Get-Process. Overwrites the file if it already exists
94+
.INPUTS
95+
Path
96+
an array of files you want to open in your editor
97+
Value
98+
The content you want in the new files
99+
Force
100+
Overwrites a file if it exists
101+
#>
102+
function New-EditorFile {
103+
[CmdletBinding()]
104+
param(
105+
[Parameter()]
106+
[String[]]
107+
[ValidateNotNullOrEmpty()]
108+
$Path,
109+
110+
[Parameter(ValueFromPipeline=$true)]
111+
$Value,
112+
113+
[Parameter()]
114+
[switch]
115+
$Force
116+
)
117+
118+
begin {
119+
$valueList = @()
120+
}
121+
122+
process {
123+
$valueList += $Value
124+
}
125+
126+
end {
127+
if ($Path) {
128+
foreach ($fileName in $Path)
129+
{
130+
if (-not (Test-Path $fileName) -or $Force) {
131+
New-Item -Path $fileName -ItemType File | Out-Null
132+
133+
if ($Path.Count -gt 1) {
134+
$preview = $false
135+
} else {
136+
$preview = $true
137+
}
138+
139+
$psEditor.Workspace.OpenFile($fileName, $preview)
140+
$psEditor.GetEditorContext().CurrentFile.InsertText(($valueList | Out-String))
141+
} else {
142+
$PSCmdlet.WriteError( (
143+
New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList @(
144+
[System.IO.IOException]"The file '$fileName' already exists.",
145+
'NewEditorFileIOError',
146+
[System.Management.Automation.ErrorCategory]::WriteError,
147+
$fileName) ) )
148+
}
149+
}
150+
} else {
151+
$psEditor.Workspace.NewFile()
152+
$psEditor.GetEditorContext().CurrentFile.InsertText(($valueList | Out-String))
153+
}
154+
}
155+
}
156+
80157
function Open-EditorFile {
81-
param([Parameter(Mandatory=$true)]$FilePaths)
158+
[CmdletBinding()]
159+
param(
160+
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
161+
[ValidateNotNullOrEmpty()]
162+
$Path
163+
)
82164

83-
Get-ChildItem $FilePaths -File | ForEach-Object {
84-
$psEditor.Workspace.OpenFile($_.FullName)
165+
begin {
166+
$Paths = @()
167+
}
168+
169+
process {
170+
$Paths += $Path
171+
}
172+
173+
end {
174+
if ($Paths.Count -gt 1) {
175+
$preview = $false
176+
} else {
177+
$preview = $true
178+
}
179+
180+
Get-ChildItem $Paths -File | ForEach-Object {
181+
$psEditor.Workspace.OpenFile($_.FullName, $preview)
182+
}
85183
}
86184
}
87185
Set-Alias psedit Open-EditorFile -Scope Global
88186

89-
Export-ModuleMember -Function Open-EditorFile
187+
Export-ModuleMember -Function Open-EditorFile,New-EditorFile

src/PowerShellEditorServices.Protocol/LanguageServer/EditorCommands.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,15 @@ public static readonly
111111
public class OpenFileRequest
112112
{
113113
public static readonly
114-
RequestType<string, EditorCommandResponse, object, object> Type =
115-
RequestType<string, EditorCommandResponse, object, object>.Create("editor/openFile");
114+
RequestType<OpenFileDetails, EditorCommandResponse, object, object> Type =
115+
RequestType<OpenFileDetails, EditorCommandResponse, object, object>.Create("editor/openFile");
116+
}
117+
118+
public class OpenFileDetails
119+
{
120+
public string FilePath { get; set; }
121+
122+
public bool Preview { get; set; }
116123
}
117124

118125
public class CloseFileRequest

src/PowerShellEditorServices.Protocol/Server/LanguageServerEditorOperations.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.Server
1212
{
1313
internal class LanguageServerEditorOperations : IEditorOperations
1414
{
15+
private const bool DefaultPreviewSetting = true;
16+
1517
private EditorSession editorSession;
1618
private IMessageSender messageSender;
1719

@@ -115,7 +117,24 @@ public Task OpenFile(string filePath)
115117
return
116118
this.messageSender.SendRequest(
117119
OpenFileRequest.Type,
118-
filePath,
120+
new OpenFileDetails
121+
{
122+
FilePath = filePath,
123+
Preview = DefaultPreviewSetting
124+
},
125+
true);
126+
}
127+
128+
public Task OpenFile(string filePath, bool preview)
129+
{
130+
return
131+
this.messageSender.SendRequest(
132+
OpenFileRequest.Type,
133+
new OpenFileDetails
134+
{
135+
FilePath = filePath,
136+
Preview = preview
137+
},
119138
true);
120139
}
121140

src/PowerShellEditorServices/Extensions/EditorWorkspace.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ public void OpenFile(string filePath)
5858
this.editorOperations.OpenFile(filePath).Wait();
5959
}
6060

61+
/// <summary>
62+
/// Opens a file in the workspace. If the file is already open
63+
/// its buffer will be made active.
64+
/// You can specify whether the file opens as a preview or as a durable editor.
65+
/// </summary>
66+
/// <param name="filePath">The path to the file to be opened.</param>
67+
/// <param name="preview">Determines wether the file is opened as a preview or as a durable editor.</param>
68+
public void OpenFile(string filePath, bool preview)
69+
{
70+
this.editorOperations.OpenFile(filePath, preview).Wait();
71+
}
72+
6173
#endregion
6274
}
6375
}

src/PowerShellEditorServices/Extensions/IEditorOperations.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public interface IEditorOperations
4747
/// <returns>A Task that can be tracked for completion.</returns>
4848
Task OpenFile(string filePath);
4949

50+
/// <summary>
51+
/// Causes a file to be opened in the editor. If the file is
52+
/// already open, the editor must switch to the file.
53+
/// You can specify whether the file opens as a preview or as a durable editor.
54+
/// </summary>
55+
/// <param name="filePath">The path of the file to be opened.</param>
56+
/// <param name="preview">Determines wether the file is opened as a preview or as a durable editor.</param>
57+
/// <returns>A Task that can be tracked for completion.</returns>
58+
Task OpenFile(string filePath, bool preview);
59+
5060
/// <summary>
5161
/// Causes a file to be closed in the editor.
5262
/// </summary>

0 commit comments

Comments
 (0)