Skip to content

Commit

Permalink
Add Paths to workspace API for multi-root workspaces
Browse files Browse the repository at this point in the history
Since `Path` now refers to initial working directory.
  • Loading branch information
andyleejordan committed Aug 17, 2023
1 parent e540922 commit 056a4d5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/PowerShellEditorServices/Extensions/EditorWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public sealed class EditorWorkspace
/// </summary>
public string Path => editorOperations.GetWorkspacePath();

/// <summary>
/// Get all the workspace folders' paths.
/// </summary>
public string[] Paths => editorOperations.GetWorkspacePaths();

#endregion

#region Constructors
Expand Down
6 changes: 6 additions & 0 deletions src/PowerShellEditorServices/Extensions/IEditorOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ internal interface IEditorOperations
/// <returns>The server's initial working directory.</returns>
string GetWorkspacePath();

/// <summary>
/// Get all the workspace folders' paths.
/// </summary>
/// <returns></returns>
string[] GetWorkspacePaths();

/// <summary>
/// Resolves the given file path relative to the current workspace path.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -192,6 +193,8 @@ public async Task SaveFileAsync(string currentPath, string newSavePath)
// from another for the extension API.
public string GetWorkspacePath() => _workspaceService.InitialWorkingDirectory;

public string[] GetWorkspacePaths() => _workspaceService.WorkspacePaths.ToArray();

public string GetWorkspaceRelativePath(ScriptFile scriptFile) => _workspaceService.GetRelativePath(scriptFile);

public async Task ShowInformationMessageAsync(string message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public WorkspaceService(ILoggerFactory factory)

#region Public Methods

public IEnumerable<string> WorkspacePaths => WorkspaceFolders.Count == 0
? new List<string> { InitialWorkingDirectory }
: WorkspaceFolders.Select(i => i.Uri.GetFileSystemPath());

/// <summary>
/// Gets an open file in the workspace. If the file isn't open but exists on the filesystem, load and return it.
/// <para>IMPORTANT: Not all documents have a backing file e.g. untitled: scheme documents. Consider using
Expand Down Expand Up @@ -358,15 +362,11 @@ public IEnumerable<string> EnumeratePSFiles(
int maxDepth,
bool ignoreReparsePoints)
{
IEnumerable<string> rootPaths = WorkspaceFolders.Count == 0
? new List<string> { InitialWorkingDirectory }
: WorkspaceFolders.Select(i => i.Uri.GetFileSystemPath());

Matcher matcher = new();
foreach (string pattern in includeGlobs) { matcher.AddInclude(pattern); }
foreach (string pattern in excludeGlobs) { matcher.AddExclude(pattern); }

foreach (string rootPath in rootPaths)
foreach (string rootPath in WorkspacePaths)
{
if (!Directory.Exists(rootPath))
{
Expand Down
8 changes: 8 additions & 0 deletions test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ internal static WorkspaceService FixturesWorkspace()
};
}

[Fact]
public void HasDefaultForWorkspacePaths()
{
WorkspaceService workspace = FixturesWorkspace();
string actual = Assert.Single(workspace.WorkspacePaths);
Assert.Equal(workspace.InitialWorkingDirectory, actual);
}

// These are the default values for the EnumeratePSFiles() method
// in Microsoft.PowerShell.EditorServices.Workspace class
private static readonly string[] s_defaultExcludeGlobs = Array.Empty<string>();
Expand Down

0 comments on commit 056a4d5

Please sign in to comment.