|
5 | 5 |
|
6 | 6 | namespace Microsoft.PowerShell.EditorServices.Extensions |
7 | 7 | { |
8 | | - /// <summary> |
9 | | - /// A document currently open in the editor workspace. |
10 | | - /// </summary> |
11 | | - public sealed class EditorWorkspaceDocument |
12 | | - { |
13 | | - private readonly EditorWorkspace _workspace; |
14 | | - |
15 | | - internal EditorWorkspaceDocument(EditorWorkspace workspace, string path, bool saved) |
16 | | - { |
17 | | - _workspace = workspace; |
18 | | - Path = path; |
19 | | - Saved = saved; |
20 | | - } |
21 | | - |
22 | | - /// <summary> |
23 | | - /// Gets the path of the document. |
24 | | - /// </summary> |
25 | | - public string Path { get; } |
26 | | - |
27 | | - /// <summary> |
28 | | - /// Gets whether the document is backed by a saved file path (not in-memory). |
29 | | - /// </summary> |
30 | | - public bool Saved { get; } |
31 | | - |
32 | | - /// <summary> |
33 | | - /// Opens this document in the editor. |
34 | | - /// </summary> |
35 | | - public void Open() => _workspace.OpenFile(Path); |
36 | | - |
37 | | - /// <summary> |
38 | | - /// Saves this document in the editor. |
39 | | - /// </summary> |
40 | | - public void Save() => _workspace.SaveFile(Path); |
41 | | - |
42 | | - /// <summary> |
43 | | - /// Closes this document in the editor. |
44 | | - /// </summary> |
45 | | - public void Close() => _workspace.CloseFile(Path); |
46 | | - |
47 | | - /// <summary> |
48 | | - /// Gets the display name of this document and unsaved status. |
49 | | - /// </summary> |
50 | | - /// <returns>The display name of this document.</returns> |
51 | | - public override string ToString() |
52 | | - { |
53 | | - string documentPath = Path ?? string.Empty; |
54 | | - string fileName = System.IO.Path.GetFileName(documentPath); |
55 | | - return Saved ? fileName : fileName + " [Unsaved]"; |
56 | | - } |
57 | | - } |
58 | | - |
59 | 8 | /// <summary> |
60 | 9 | /// Provides a PowerShell-facing API which allows scripts to |
61 | 10 | /// interact with the editor's workspace. |
@@ -84,11 +33,7 @@ public sealed class EditorWorkspace |
84 | 33 | /// <summary> |
85 | 34 | /// Get all currently open documents in the workspace. |
86 | 35 | /// </summary> |
87 | | - public EditorWorkspaceDocument[] Documents => [.. |
88 | | - editorOperations |
89 | | - .GetWorkspaceOpenDocuments() |
90 | | - .Select(doc => new EditorWorkspaceDocument(this, doc.Path, doc.Saved)) |
91 | | - ]; |
| 36 | + public WorkspaceOpenDocument[] Documents => [.. editorOperations.GetWorkspaceOpenDocuments()]; |
92 | 37 |
|
93 | 38 | #endregion |
94 | 39 |
|
@@ -138,13 +83,15 @@ public sealed class EditorWorkspace |
138 | 83 | /// <param name="filePath">The path to the file to be closed.</param> |
139 | 84 | [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")] |
140 | 85 | public void CloseFile(string filePath) => editorOperations.CloseFileAsync(filePath).Wait(); |
| 86 | + public void CloseFile(WorkspaceOpenDocument document) => CloseFile(document.Path); |
141 | 87 |
|
142 | 88 | /// <summary> |
143 | 89 | /// Saves an open file in the workspace. |
144 | 90 | /// </summary> |
145 | 91 | /// <param name="filePath">The path to the file to be saved.</param> |
146 | 92 | [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")] |
147 | 93 | public void SaveFile(string filePath) => editorOperations.SaveFileAsync(filePath).Wait(); |
| 94 | + public void SaveFile(WorkspaceOpenDocument document) => SaveFile(document.Path); |
148 | 95 |
|
149 | 96 | /// <summary> |
150 | 97 | /// Saves a file with a new name AKA a copy. |
|
0 commit comments