Skip to content

Commit b781fd1

Browse files
committed
Simplify methods
1 parent 866c2aa commit b781fd1

1 file changed

Lines changed: 17 additions & 27 deletions

File tree

src/PowerShellEditorServices/Extensions/EditorWorkspace.cs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using System.Collections.Generic;
5+
using System.Linq;
6+
47
namespace Microsoft.PowerShell.EditorServices.Extensions
58
{
69
/// <summary>
@@ -27,19 +30,6 @@ internal EditorWorkspaceDocument(EditorWorkspace workspace, string path, bool sa
2730
/// </summary>
2831
public bool Saved { get; }
2932

30-
/// <summary>
31-
/// Gets the display name of this document and unsaved status.
32-
/// </summary>
33-
/// <returns>The display name of this document.</returns>
34-
public override string ToString()
35-
{
36-
string documentPath = Path ?? string.Empty;
37-
// Handle Windows and POSIX separators consistently across platforms.
38-
int fileNameStartIndex = System.Math.Max(documentPath.LastIndexOf('\\'), documentPath.LastIndexOf('/')) + 1;
39-
string fileName = documentPath.Substring(fileNameStartIndex);
40-
return Saved ? fileName : fileName + " [Unsaved]";
41-
}
42-
4333
/// <summary>
4434
/// Opens this document in the editor.
4535
/// </summary>
@@ -54,6 +44,17 @@ public override string ToString()
5444
/// Closes this document in the editor.
5545
/// </summary>
5646
public void Close() => _workspace.CloseFile(Path);
47+
48+
/// <summary>
49+
/// Gets the display name of this document and unsaved status.
50+
/// </summary>
51+
/// <returns>The display name of this document.</returns>
52+
public override string ToString()
53+
{
54+
string documentPath = Path ?? string.Empty;
55+
string fileName = System.IO.Path.GetFileName(documentPath);
56+
return Saved ? fileName : fileName + " [Unsaved]";
57+
}
5758
}
5859

5960
/// <summary>
@@ -84,20 +85,9 @@ public sealed class EditorWorkspace
8485
/// <summary>
8586
/// Get all currently open documents in the workspace.
8687
/// </summary>
87-
public EditorWorkspaceDocument[] Documents
88-
{
89-
get
90-
{
91-
WorkspaceOpenDocument[] openDocuments = editorOperations.GetWorkspaceOpenDocuments();
92-
EditorWorkspaceDocument[] documents = new EditorWorkspaceDocument[openDocuments.Length];
93-
for (int i = 0; i < openDocuments.Length; i++)
94-
{
95-
documents[i] = new EditorWorkspaceDocument(this, openDocuments[i].Path, openDocuments[i].Saved);
96-
}
97-
98-
return documents;
99-
}
100-
}
88+
public IEnumerable<EditorWorkspaceDocument> Documents => editorOperations
89+
.GetWorkspaceOpenDocuments()
90+
.Select(doc => new EditorWorkspaceDocument(this, doc.Path, doc.Saved));
10191

10292
#endregion
10393

0 commit comments

Comments
 (0)