Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WorkspaceFolders and use it when enumerating files #1995

Merged
merged 3 commits into from
Feb 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Rename WorkspacePath to InitialWorkingDirectory
And note where the API needs to be updated to get the workspace path for the
current open editor (since there could be multiple workspaces).
  • Loading branch information
andyleejordan committed Feb 10, 2023

Verified

This commit was signed with the committer’s verified signature.
yuetloo yuetloo
commit 101784e94b56d15fb4c4c825e6ca8969a1594d8f
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ public interface IEditorScriptFile
public interface IWorkspaceService
{
/// <summary>
/// The root path of the workspace.
/// The root path of the workspace for the current editor.
/// </summary>
string WorkspacePath { get; }

@@ -116,7 +116,9 @@ internal WorkspaceService(
ExcludedFileGlobs = _workspaceService.ExcludeFilesGlob.AsReadOnly();
}

public string WorkspacePath => _workspaceService.WorkspacePath;
// TODO: This needs to use the associated EditorContext to get the workspace for the current
// editor instead of the initial working directory.
public string WorkspacePath => _workspaceService.InitialWorkingDirectory;

public bool FollowSymlinks => _workspaceService.FollowSymlinks;

2 changes: 1 addition & 1 deletion src/PowerShellEditorServices/Extensions/EditorWorkspace.cs
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ public sealed class EditorWorkspace
#region Properties

/// <summary>
/// Gets the current workspace path if there is one or null otherwise.
/// Gets the current workspace path if there is one for the open editor or null otherwise.
/// </summary>
public string Path => editorOperations.GetWorkspacePath();

4 changes: 2 additions & 2 deletions src/PowerShellEditorServices/Server/PsesLanguageServer.cs
Original file line number Diff line number Diff line change
@@ -133,7 +133,7 @@ public async Task StartAsync()
// TODO: Support multi-workspace.
foreach (WorkspaceFolder workspaceFolder in initializeParams.WorkspaceFolders)
{
workspaceService.WorkspacePath = workspaceFolder.Uri.GetFileSystemPath();
workspaceService.InitialWorkingDirectory = workspaceFolder.Uri.GetFileSystemPath();
break;
}
}
@@ -152,7 +152,7 @@ public async Task StartAsync()
LoadProfiles = initializationOptions?.GetValue("enableProfileLoading")?.Value<bool>() ?? true,
// TODO: Consider deprecating the setting which sets this and
// instead use WorkspacePath exclusively.
InitialWorkingDirectory = initializationOptions?.GetValue("initialWorkingDirectory")?.Value<string>() ?? workspaceService.WorkspacePath,
InitialWorkingDirectory = initializationOptions?.GetValue("initialWorkingDirectory")?.Value<string>() ?? workspaceService.InitialWorkingDirectory,
ShellIntegrationEnabled = initializationOptions?.GetValue("shellIntegrationEnabled")?.Value<bool>() ?? false
};

Original file line number Diff line number Diff line change
@@ -191,7 +191,9 @@ public async Task SaveFileAsync(string currentPath, string newSavePath)
}).ReturningVoid(CancellationToken.None).ConfigureAwait(false);
}

public string GetWorkspacePath() => _workspaceService.WorkspacePath;
// TODO: This should get the current editor's context and use it to determine which
// workspace it's in.
public string GetWorkspacePath() => _workspaceService.InitialWorkingDirectory;

public string GetWorkspaceRelativePath(string filePath) => _workspaceService.GetRelativePath(filePath);

Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ public override async Task<Unit> Handle(DidChangeConfigurationParams request, Ca

_configurationService.CurrentSettings.Update(
incomingSettings.Powershell,
_workspaceService.WorkspacePath,
_workspaceService.InitialWorkingDirectory,
_logger);

// Run any events subscribed to configuration updates
Original file line number Diff line number Diff line change
@@ -58,9 +58,14 @@ internal class WorkspaceService
#region Properties

/// <summary>
/// Gets or sets the root path of the workspace.
/// <para>Gets or sets the initial working directory.</para>
/// <para>
/// This is settable by the same key in the initialization options, and likely corresponds
/// to the root of the workspace if only one workspace folder is being used. However, in
/// multi-root workspaces this may be any workspace folder's root (or none if overridden).
/// </para>
/// </summary>
public string WorkspacePath { get; set; }
public string InitialWorkingDirectory { get; set; }

/// <summary>
/// Gets or sets the default list of file globs to exclude during workspace searches.
@@ -299,9 +304,9 @@ public string GetRelativePath(string filePath)
{
string resolvedPath = filePath;

if (!IsPathInMemory(filePath) && !string.IsNullOrEmpty(WorkspacePath))
if (!IsPathInMemory(filePath) && !string.IsNullOrEmpty(InitialWorkingDirectory))
{
Uri workspaceUri = new(WorkspacePath);
Uri workspaceUri = new(InitialWorkingDirectory);
Uri fileUri = new(filePath);

resolvedPath = workspaceUri.MakeRelativeUri(fileUri).ToString();
@@ -341,7 +346,7 @@ public IEnumerable<string> EnumeratePSFiles(
bool ignoreReparsePoints
)
{
if (WorkspacePath is null || !Directory.Exists(WorkspacePath))
if (InitialWorkingDirectory is null || !Directory.Exists(InitialWorkingDirectory))
{
yield break;
}
@@ -351,7 +356,7 @@ bool ignoreReparsePoints
foreach (string pattern in excludeGlobs) { matcher.AddExclude(pattern); }

WorkspaceFileSystemWrapperFactory fsFactory = new(
WorkspacePath,
InitialWorkingDirectory,
maxDepth,
VersionUtils.IsNetCore ? s_psFileExtensionsCoreFramework : s_psFileExtensionsFullFramework,
ignoreReparsePoints,
@@ -363,7 +368,7 @@ bool ignoreReparsePoints
// item.Path always contains forward slashes in paths when it should be backslashes on Windows.
// Since we're returning strings here, it's important to use the correct directory separator.
string path = VersionUtils.IsWindows ? item.Path.Replace('/', Path.DirectorySeparatorChar) : item.Path;
yield return Path.Combine(WorkspacePath, path);
yield return Path.Combine(InitialWorkingDirectory, path);
}
}

@@ -423,7 +428,7 @@ internal static bool IsPathInMemory(string filePath)
return isInMemory;
}

internal string ResolveWorkspacePath(string path) => ResolveRelativeScriptPath(WorkspacePath, path);
internal string ResolveWorkspacePath(string path) => ResolveRelativeScriptPath(InitialWorkingDirectory, path);

internal string ResolveRelativeScriptPath(string baseFilePath, string relativePath)
{
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ public SymbolsServiceTests()
psesHost = PsesHostFactory.Create(NullLoggerFactory.Instance);
workspace = new WorkspaceService(NullLoggerFactory.Instance)
{
WorkspacePath = TestUtilities.GetSharedPath("References")
InitialWorkingDirectory = TestUtilities.GetSharedPath("References")
};
symbolsService = new SymbolsService(
NullLoggerFactory.Instance,
22 changes: 11 additions & 11 deletions test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ public void CanResolveWorkspaceRelativePath()
string expectedOutsidePath = TestUtilities.NormalizePath("../PeerPath/FilePath.ps1");

// Test with a workspace path
workspace.WorkspacePath = workspacePath;
workspace.InitialWorkingDirectory = workspacePath;
Assert.Equal(expectedInsidePath, workspace.GetRelativePath(testPathInside));
Assert.Equal(expectedOutsidePath, workspace.GetRelativePath(testPathOutside));
Assert.Equal(testPathAnotherDrive, workspace.GetRelativePath(testPathAnotherDrive));
@@ -49,7 +49,7 @@ internal static WorkspaceService FixturesWorkspace()
{
return new WorkspaceService(NullLoggerFactory.Instance)
{
WorkspacePath = TestUtilities.NormalizePath("Fixtures/Workspace")
InitialWorkingDirectory = TestUtilities.NormalizePath("Fixtures/Workspace")
};
}

@@ -94,18 +94,18 @@ public void CanRecurseDirectoryTree()

List<string> expected = new()
{
Path.Combine(workspace.WorkspacePath, "nested", "donotfind.ps1"),
Path.Combine(workspace.WorkspacePath, "nested", "nestedmodule.psd1"),
Path.Combine(workspace.WorkspacePath, "nested", "nestedmodule.psm1"),
Path.Combine(workspace.WorkspacePath, "rootfile.ps1")
Path.Combine(workspace.InitialWorkingDirectory, "nested", "donotfind.ps1"),
Path.Combine(workspace.InitialWorkingDirectory, "nested", "nestedmodule.psd1"),
Path.Combine(workspace.InitialWorkingDirectory, "nested", "nestedmodule.psm1"),
Path.Combine(workspace.InitialWorkingDirectory, "rootfile.ps1")
};

// .NET Core doesn't appear to use the same three letter pattern matching rule although the docs
// suggest it should be find the '.ps1xml' files because we search for the pattern '*.ps1'
// ref https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.getfiles?view=netcore-2.1#System_IO_Directory_GetFiles_System_String_System_String_System_IO_EnumerationOptions_
if (RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework"))
{
expected.Insert(3, Path.Combine(workspace.WorkspacePath, "other", "other.ps1xml"));
expected.Insert(3, Path.Combine(workspace.InitialWorkingDirectory, "other", "other.ps1xml"));
}

Assert.Equal(expected, actual);
@@ -122,7 +122,7 @@ public void CanRecurseDirectoryTreeWithLimit()
maxDepth: 1,
ignoreReparsePoints: s_defaultIgnoreReparsePoints
);
Assert.Equal(new[] { Path.Combine(workspace.WorkspacePath, "rootfile.ps1") }, actual);
Assert.Equal(new[] { Path.Combine(workspace.InitialWorkingDirectory, "rootfile.ps1") }, actual);
}

[Fact]
@@ -138,8 +138,8 @@ public void CanRecurseDirectoryTreeWithGlobs()
);

Assert.Equal(new[] {
Path.Combine(workspace.WorkspacePath, "nested", "nestedmodule.psd1"),
Path.Combine(workspace.WorkspacePath, "rootfile.ps1")
Path.Combine(workspace.InitialWorkingDirectory, "nested", "nestedmodule.psd1"),
Path.Combine(workspace.InitialWorkingDirectory, "rootfile.ps1")
}, actual);
}

@@ -181,7 +181,7 @@ public void CanDetermineIsPathInMemory()
public void CanOpenAndCloseFile()
{
WorkspaceService workspace = FixturesWorkspace();
string filePath = Path.GetFullPath(Path.Combine(workspace.WorkspacePath, "rootfile.ps1"));
string filePath = Path.GetFullPath(Path.Combine(workspace.InitialWorkingDirectory, "rootfile.ps1"));

ScriptFile file = workspace.GetFile(filePath);
Assert.Equal(workspace.GetOpenedFiles(), new[] { file });