Skip to content

Commit 2ba484d

Browse files
committed
Fix up extension API
Receive a `EditorOperationResponse` (not yet otherwise used). Delete dead code. Add optional `content` parameter to `NewFile`. Expose `CloseFile` and `SaveFile`. Fix an outdated warning message.
1 parent f96e537 commit 2ba484d

File tree

5 files changed

+52
-36
lines changed

5 files changed

+52
-36
lines changed

src/PowerShellEditorServices/Extensions/EditorRequests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ internal class ExtensionCommandRemovedNotification
2525
internal class GetEditorContextRequest
2626
{ }
2727

28-
internal enum EditorCommandResponse
28+
internal enum EditorOperationResponse
2929
{
30-
Unsupported,
31-
OK
30+
Completed,
31+
Failed
3232
}
3333

3434
internal class InsertTextRequest

src/PowerShellEditorServices/Extensions/EditorWindow.cs

-3
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ internal EditorWindow(IEditorOperations editorOperations)
3939
#endregion
4040

4141
#region Public Methods
42-
#pragma warning disable VSTHRD002 // These are public APIs that use async internal methods.
43-
4442
/// <summary>
4543
/// Shows an informational message to the user.
4644
/// </summary>
@@ -72,7 +70,6 @@ internal EditorWindow(IEditorOperations editorOperations)
7270
/// <param name="timeout">A timeout in milliseconds for how long the message should remain visible.</param>
7371
public void SetStatusBarMessage(string message, int timeout) => editorOperations.SetStatusBarMessageAsync(message, timeout).Wait();
7472

75-
#pragma warning restore VSTHRD002
7673
#endregion
7774
}
7875
}

src/PowerShellEditorServices/Extensions/EditorWorkspace.cs

+27-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public sealed class EditorWorkspace
1818
#region Properties
1919

2020
/// <summary>
21-
/// Gets the current workspace path if there is one for the open editor or null otherwise.
21+
/// Gets the server's initial working directory, since the extension API doesn't have a
22+
/// multi-root workspace concept.
2223
/// </summary>
2324
public string Path => editorOperations.GetWorkspacePath();
2425

@@ -31,30 +32,49 @@ public sealed class EditorWorkspace
3132
#endregion
3233

3334
#region Public Methods
34-
#pragma warning disable VSTHRD002 // These are public APIs that use async internal methods.
35+
// TODO: Consider returning bool instead of void to indicate success?
3536

3637
/// <summary>
37-
/// Creates a new file in the editor
38+
/// Creates a new file in the editor.
3839
/// </summary>
39-
public void NewFile() => editorOperations.NewFileAsync().Wait();
40+
/// <param name="content">The content to place in the new file.</param>
41+
public void NewFile(string content = "") => editorOperations.NewFileAsync(content).Wait();
4042

4143
/// <summary>
42-
/// Opens a file in the workspace. If the file is already open
44+
/// Opens a file in the workspace. If the file is already open
4345
/// its buffer will be made active.
4446
/// </summary>
4547
/// <param name="filePath">The path to the file to be opened.</param>
4648
public void OpenFile(string filePath) => editorOperations.OpenFileAsync(filePath).Wait();
4749

4850
/// <summary>
49-
/// Opens a file in the workspace. If the file is already open
51+
/// Opens a file in the workspace. If the file is already open
5052
/// its buffer will be made active.
5153
/// You can specify whether the file opens as a preview or as a durable editor.
5254
/// </summary>
5355
/// <param name="filePath">The path to the file to be opened.</param>
5456
/// <param name="preview">Determines wether the file is opened as a preview or as a durable editor.</param>
5557
public void OpenFile(string filePath, bool preview) => editorOperations.OpenFileAsync(filePath, preview).Wait();
5658

57-
#pragma warning restore VSTHRD002
59+
/// <summary>
60+
/// Closes a file in the workspace.
61+
/// </summary>
62+
/// <param name="filePath">The path to the file to be closed.</param>
63+
public void CloseFile(string filePath) => editorOperations.CloseFileAsync(filePath).Wait();
64+
65+
/// <summary>
66+
/// Saves an open file in the workspace.
67+
/// </summary>
68+
/// <param name="filePath">The path to the file to be saved.</param>
69+
public void SaveFile(string filePath) => editorOperations.SaveFileAsync(filePath).Wait();
70+
71+
/// <summary>
72+
/// Saves a file with a new name AKA a copy.
73+
/// </summary>
74+
/// <param name="oldFilePath">The file to copy.</param>
75+
/// <param name="newFilePath">The file to create.</param>
76+
public void SaveFile(string oldFilePath, string newFilePath) => editorOperations.SaveFileAsync(oldFilePath, newFilePath).Wait();
77+
5878
#endregion
5979
}
6080
}

src/PowerShellEditorServices/Extensions/IEditorOperations.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ internal interface IEditorOperations
2020
Task<EditorContext> GetEditorContextAsync();
2121

2222
/// <summary>
23-
/// Gets the path to the editor's active workspace.
23+
/// Gets the server's initial working directory, since the extension API doesn't have a
24+
/// multi-root workspace concept.
2425
/// </summary>
25-
/// <returns>The workspace path or null if there isn't one.</returns>
26+
/// <returns>The server's initial working directory.</returns>
2627
string GetWorkspacePath();
2728

2829
/// <summary>
@@ -35,8 +36,9 @@ internal interface IEditorOperations
3536
/// <summary>
3637
/// Causes a new untitled file to be created in the editor.
3738
/// </summary>
39+
/// <param name="content">The content to insert into the new file.</param>
3840
/// <returns>A task that can be awaited for completion.</returns>
39-
Task NewFileAsync();
41+
Task NewFileAsync(string content = "");
4042

4143
/// <summary>
4244
/// Causes a file to be opened in the editor. If the file is

src/PowerShellEditorServices/Services/Extension/EditorOperationsService.cs

+17-20
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ namespace Microsoft.PowerShell.EditorServices.Services.Extension
1313
{
1414
internal class EditorOperationsService : IEditorOperations
1515
{
16-
private const bool DefaultPreviewSetting = true;
17-
1816
private readonly PsesInternalHost _psesHost;
1917
private readonly WorkspaceService _workspaceService;
20-
2118
private readonly ILanguageServerFacade _languageServer;
2219

2320
public EditorOperationsService(
@@ -72,7 +69,7 @@ public async Task InsertTextAsync(string filePath, string text, BufferRange inse
7269
Character = insertRange.End.Column - 1
7370
}
7471
}
75-
}).ReturningVoid(CancellationToken.None).ConfigureAwait(false);
72+
}).Returning<EditorOperationResponse>(CancellationToken.None).ConfigureAwait(false);
7673
}
7774

7875
public async Task SetSelectionAsync(BufferRange selectionRange)
@@ -98,7 +95,7 @@ public async Task SetSelectionAsync(BufferRange selectionRange)
9895
Character = selectionRange.End.Column - 1
9996
}
10097
}
101-
}).ReturningVoid(CancellationToken.None).ConfigureAwait(false);
98+
}).Returning<EditorOperationResponse>(CancellationToken.None).ConfigureAwait(false);
10299
}
103100

104101
public EditorContext ConvertClientEditorContext(
@@ -123,15 +120,15 @@ public EditorContext ConvertClientEditorContext(
123120
clientContext.CurrentFileLanguage);
124121
}
125122

126-
public async Task NewFileAsync()
123+
public async Task NewFileAsync(string content = "")
127124
{
128125
if (!TestHasLanguageServer())
129126
{
130127
return;
131128
}
132129

133-
await _languageServer.SendRequest<string>("editor/newFile", null)
134-
.ReturningVoid(CancellationToken.None)
130+
await _languageServer.SendRequest("editor/newFile", content)
131+
.Returning<EditorOperationResponse>(CancellationToken.None)
135132
.ConfigureAwait(false);
136133
}
137134

@@ -145,8 +142,8 @@ public async Task OpenFileAsync(string filePath)
145142
await _languageServer.SendRequest("editor/openFile", new OpenFileDetails
146143
{
147144
FilePath = filePath,
148-
Preview = DefaultPreviewSetting
149-
}).ReturningVoid(CancellationToken.None).ConfigureAwait(false);
145+
Preview = true
146+
}).Returning<EditorOperationResponse>(CancellationToken.None).ConfigureAwait(false);
150147
}
151148

152149
public async Task OpenFileAsync(string filePath, bool preview)
@@ -160,7 +157,7 @@ public async Task OpenFileAsync(string filePath, bool preview)
160157
{
161158
FilePath = filePath,
162159
Preview = preview
163-
}).ReturningVoid(CancellationToken.None).ConfigureAwait(false);
160+
}).Returning<EditorOperationResponse>(CancellationToken.None).ConfigureAwait(false);
164161
}
165162

166163
public async Task CloseFileAsync(string filePath)
@@ -171,7 +168,7 @@ public async Task CloseFileAsync(string filePath)
171168
}
172169

173170
await _languageServer.SendRequest("editor/closeFile", filePath)
174-
.ReturningVoid(CancellationToken.None)
171+
.Returning<EditorOperationResponse>(CancellationToken.None)
175172
.ConfigureAwait(false);
176173
}
177174

@@ -188,11 +185,11 @@ public async Task SaveFileAsync(string currentPath, string newSavePath)
188185
{
189186
FilePath = currentPath,
190187
NewPath = newSavePath
191-
}).ReturningVoid(CancellationToken.None).ConfigureAwait(false);
188+
}).Returning<EditorOperationResponse>(CancellationToken.None).ConfigureAwait(false);
192189
}
193190

194-
// TODO: This should get the current editor's context and use it to determine which
195-
// workspace it's in.
191+
// NOTE: This name is now outdated since we don't have a way to distinguish one workspace
192+
// from another for the extension API.
196193
public string GetWorkspacePath() => _workspaceService.InitialWorkingDirectory;
197194

198195
public string GetWorkspaceRelativePath(string filePath) => _workspaceService.GetRelativePath(filePath);
@@ -205,7 +202,7 @@ public async Task ShowInformationMessageAsync(string message)
205202
}
206203

207204
await _languageServer.SendRequest("editor/showInformationMessage", message)
208-
.ReturningVoid(CancellationToken.None)
205+
.Returning<EditorOperationResponse>(CancellationToken.None)
209206
.ConfigureAwait(false);
210207
}
211208

@@ -217,7 +214,7 @@ public async Task ShowErrorMessageAsync(string message)
217214
}
218215

219216
await _languageServer.SendRequest("editor/showErrorMessage", message)
220-
.ReturningVoid(CancellationToken.None)
217+
.Returning<EditorOperationResponse>(CancellationToken.None)
221218
.ConfigureAwait(false);
222219
}
223220

@@ -229,7 +226,7 @@ public async Task ShowWarningMessageAsync(string message)
229226
}
230227

231228
await _languageServer.SendRequest("editor/showWarningMessage", message)
232-
.ReturningVoid(CancellationToken.None)
229+
.Returning<EditorOperationResponse>(CancellationToken.None)
233230
.ConfigureAwait(false);
234231
}
235232

@@ -244,7 +241,7 @@ public async Task SetStatusBarMessageAsync(string message, int? timeout)
244241
{
245242
Message = message,
246243
Timeout = timeout
247-
}).ReturningVoid(CancellationToken.None).ConfigureAwait(false);
244+
}).Returning<EditorOperationResponse>(CancellationToken.None).ConfigureAwait(false);
248245
}
249246

250247
public void ClearTerminal()
@@ -267,7 +264,7 @@ private bool TestHasLanguageServer(bool warnUser = true)
267264
if (warnUser)
268265
{
269266
_psesHost.UI.WriteWarningLine(
270-
"Editor operations are not supported in temporary consoles. Re-run the command in the main PowerShell Intergrated Console.");
267+
"Editor operations are not supported in temporary consoles. Re-run the command in the main Extension Terminal.");
271268
}
272269

273270
return false;

0 commit comments

Comments
 (0)