Skip to content

Commit 2a0df44

Browse files
committed
Simplify API abstraction to just the existing WorkspaceOpenDocument
1 parent 8220f53 commit 2a0df44

3 files changed

Lines changed: 23 additions & 72 deletions

File tree

src/PowerShellEditorServices/Extensions/EditorWorkspace.cs

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,6 @@
55

66
namespace Microsoft.PowerShell.EditorServices.Extensions
77
{
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-
598
/// <summary>
609
/// Provides a PowerShell-facing API which allows scripts to
6110
/// interact with the editor's workspace.
@@ -84,11 +33,7 @@ public sealed class EditorWorkspace
8433
/// <summary>
8534
/// Get all currently open documents in the workspace.
8635
/// </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()];
9237

9338
#endregion
9439

@@ -138,13 +83,15 @@ public sealed class EditorWorkspace
13883
/// <param name="filePath">The path to the file to be closed.</param>
13984
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
14085
public void CloseFile(string filePath) => editorOperations.CloseFileAsync(filePath).Wait();
86+
public void CloseFile(WorkspaceOpenDocument document) => CloseFile(document.Path);
14187

14288
/// <summary>
14389
/// Saves an open file in the workspace.
14490
/// </summary>
14591
/// <param name="filePath">The path to the file to be saved.</param>
14692
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
14793
public void SaveFile(string filePath) => editorOperations.SaveFileAsync(filePath).Wait();
94+
public void SaveFile(WorkspaceOpenDocument document) => SaveFile(document.Path);
14895

14996
/// <summary>
15097
/// Saves a file with a new name AKA a copy.

src/PowerShellEditorServices/Extensions/IEditorOperations.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,32 @@
33

44
using System.Threading.Tasks;
55
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
6+
#nullable enable
67

78
namespace Microsoft.PowerShell.EditorServices.Extensions
89
{
9-
internal readonly struct WorkspaceOpenDocument
10+
public readonly struct WorkspaceOpenDocument(string path, bool saved)
1011
{
11-
internal WorkspaceOpenDocument(string path, bool saved)
12-
{
13-
Path = path;
14-
Saved = saved;
15-
}
16-
1712
/// <summary>
1813
/// Gets the path or URI of the open document.
1914
/// </summary>
20-
public string Path { get; }
15+
public string Path { get; } = path;
2116

2217
/// <summary>
2318
/// Gets whether the document is backed by a saved file path (not in-memory).
2419
/// </summary>
25-
public bool Saved { get; }
20+
public bool Saved { get; } = saved;
21+
22+
/// <summary>
23+
/// Gets the display name of this document and unsaved status.
24+
/// </summary>
25+
/// <returns>The display name of this document.</returns>
26+
public override string ToString()
27+
{
28+
string documentPath = Path ?? string.Empty;
29+
string fileName = System.IO.Path.GetFileName(documentPath);
30+
return Saved ? fileName : fileName + " [Unsaved]";
31+
}
2632
}
2733

2834
/// <summary>

test/PowerShellEditorServices.Test/Extensions/EditorWorkspaceTests.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void DocumentsReturnsOpenWorkspaceDocuments()
3333

3434
EditorWorkspace workspace = new(editorOperations);
3535

36-
EditorWorkspaceDocument[] documents = workspace.Documents;
36+
WorkspaceOpenDocument[] documents = workspace.Documents;
3737

3838
Assert.Collection(
3939
documents,
@@ -50,7 +50,7 @@ public void DocumentsReturnsOpenWorkspaceDocuments()
5050
}
5151

5252
[Fact]
53-
public void DocumentOpenSaveAndCloseUseWorkspaceOperations()
53+
public void DocumentSaveAndCloseUseWorkspaceOperations()
5454
{
5555
string filePath = Path.Combine(WorkspacePath, "file.ps1");
5656
TestEditorOperations editorOperations = new()
@@ -59,15 +59,13 @@ public void DocumentOpenSaveAndCloseUseWorkspaceOperations()
5959
};
6060

6161
EditorWorkspace workspace = new(editorOperations);
62-
EditorWorkspaceDocument document = Assert.Single(workspace.Documents);
62+
WorkspaceOpenDocument document = Assert.Single(workspace.Documents);
6363

64-
document.Open();
6564
document.Save();
6665
document.Close();
6766

6867
Assert.Collection(
6968
editorOperations.Calls,
70-
call => Assert.Equal("OpenFile:" + filePath, call),
7169
call => Assert.Equal("SaveFile:" + filePath, call),
7270
call => Assert.Equal("CloseFile:" + filePath, call));
7371
}
@@ -86,7 +84,7 @@ public void DocumentToStringReturnsFileNameAndSavedStatus()
8684
};
8785

8886
EditorWorkspace workspace = new(editorOperations);
89-
IEnumerable<EditorWorkspaceDocument> documents = workspace.Documents;
87+
IEnumerable<WorkspaceOpenDocument> documents = workspace.Documents;
9088

9189
Assert.Collection(
9290
documents,
@@ -106,7 +104,7 @@ public void DocumentSavedReturnsWorkspaceSavedState()
106104
};
107105

108106
EditorWorkspace workspace = new(editorOperations);
109-
IEnumerable<EditorWorkspaceDocument> documents = workspace.Documents;
107+
IEnumerable<WorkspaceOpenDocument> documents = workspace.Documents;
110108

111109
Assert.Collection(
112110
documents,

0 commit comments

Comments
 (0)