Skip to content

Commit 750f17d

Browse files
New-EditorFile works on non-powershell untitled files
1 parent 78d96b8 commit 750f17d

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/PowerShellEditorServices.Protocol/LanguageServer/EditorCommands.cs

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public static readonly
3838

3939
public class ClientEditorContext
4040
{
41+
public string CurrentFileContent { get; set; }
42+
4143
public string CurrentFilePath { get; set; }
4244

4345
public Position CursorPosition { get; set; }

src/PowerShellEditorServices.Protocol/Server/LanguageServerEditorOperations.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6+
using Microsoft.PowerShell.EditorServices;
67
using Microsoft.PowerShell.EditorServices.Extensions;
78
using Microsoft.PowerShell.EditorServices.Protocol.LanguageServer;
89
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
@@ -89,10 +90,17 @@ public Task SetSelection(BufferRange selectionRange)
8990
public EditorContext ConvertClientEditorContext(
9091
ClientEditorContext clientContext)
9192
{
93+
94+
ScriptFile scriptFile = null;
95+
if (!this.editorSession.Workspace.TryGetFile(clientContext.CurrentFilePath, out scriptFile))
96+
{
97+
scriptFile = this.editorSession.Workspace.GetFileBuffer(clientContext.CurrentFilePath, clientContext.CurrentFileContent);
98+
}
99+
92100
return
93101
new EditorContext(
94102
this,
95-
this.editorSession.Workspace.GetFile(clientContext.CurrentFilePath),
103+
scriptFile,
96104
new BufferPosition(
97105
clientContext.CursorPosition.Line + 1,
98106
clientContext.CursorPosition.Character + 1),

src/PowerShellEditorServices/Workspace/Workspace.cs

+19
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,25 @@ public ScriptFile GetFile(string filePath)
101101
return scriptFile;
102102
}
103103

104+
/// <summary>
105+
/// Tries to get an open file in the workspace. Returns true or false if it succeeds.
106+
/// </summary>
107+
/// <param name="filePath">The file path at which the script resides.</param>
108+
/// <param name="scriptFile">The out parameter that will contain the ScriptFile object.</param>
109+
public bool TryGetFile(string filePath, out ScriptFile scriptFile)
110+
{
111+
try
112+
{
113+
scriptFile = GetFile(filePath);
114+
return true;
115+
}
116+
catch (FileNotFoundException)
117+
{
118+
scriptFile = null;
119+
return false;
120+
}
121+
}
122+
104123
/// <summary>
105124
/// Gets a new ScriptFile instance which is identified by the given file path.
106125
/// </summary>

0 commit comments

Comments
 (0)