Skip to content

Commit ceaa90b

Browse files
committed
Fix GetRelativePath to check against all workspace folders
1 parent 9aa5059 commit ceaa90b

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

src/PowerShellEditorServices/Extensions/FileContext.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public sealed class FileContext
5858
/// <summary>
5959
/// Gets the workspace-relative path of the file.
6060
/// </summary>
61-
public string WorkspacePath => editorOperations.GetWorkspaceRelativePath(
62-
scriptFile.FilePath);
61+
public string WorkspacePath => editorOperations.GetWorkspaceRelativePath(scriptFile);
6362

6463
#endregion
6564

src/PowerShellEditorServices/Extensions/IEditorOperations.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ internal interface IEditorOperations
2828
/// <summary>
2929
/// Resolves the given file path relative to the current workspace path.
3030
/// </summary>
31-
/// <param name="filePath">The file path to be resolved.</param>
3231
/// <returns>The resolved file path.</returns>
33-
string GetWorkspaceRelativePath(string filePath);
32+
string GetWorkspaceRelativePath(ScriptFile scriptFile);
3433

3534
/// <summary>
3635
/// Causes a new untitled file to be created in the editor.

src/PowerShellEditorServices/Services/Extension/EditorOperationsService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public async Task SaveFileAsync(string currentPath, string newSavePath)
195195
// workspace it's in.
196196
public string GetWorkspacePath() => _workspaceService.InitialWorkingDirectory;
197197

198-
public string GetWorkspaceRelativePath(string filePath) => _workspaceService.GetRelativePath(filePath);
198+
public string GetWorkspaceRelativePath(ScriptFile scriptFile) => _workspaceService.GetRelativePath(scriptFile);
199199

200200
public async Task ShowInformationMessageAsync(string message)
201201
{

src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs

+13-8
Original file line numberDiff line numberDiff line change
@@ -306,18 +306,23 @@ public void CloseFile(ScriptFile scriptFile)
306306
/// <summary>
307307
/// Gets the workspace-relative path of the given file path.
308308
/// </summary>
309-
/// <param name="filePath">The original full file path.</param>
310309
/// <returns>A relative file path</returns>
311-
public string GetRelativePath(string filePath)
310+
public string GetRelativePath(ScriptFile scriptFile)
312311
{
313-
string resolvedPath = filePath;
312+
string resolvedPath = scriptFile.FilePath;
314313

315-
if (!IsPathInMemory(filePath) && !string.IsNullOrEmpty(InitialWorkingDirectory))
314+
if (!scriptFile.IsInMemory)
316315
{
317-
Uri workspaceUri = new(InitialWorkingDirectory);
318-
Uri fileUri = new(filePath);
319-
320-
resolvedPath = workspaceUri.MakeRelativeUri(fileUri).ToString();
316+
Uri fileUri = scriptFile.DocumentUri.ToUri();
317+
foreach (WorkspaceFolder workspaceFolder in WorkspaceFolders)
318+
{
319+
Uri workspaceUri = workspaceFolder.Uri.ToUri();
320+
if (workspaceUri.IsBaseOf(fileUri))
321+
{
322+
resolvedPath = workspaceUri.MakeRelativeUri(fileUri).ToString();
323+
break;
324+
}
325+
}
321326

322327
// Convert the directory separators if necessary
323328
if (Path.DirectorySeparatorChar == '\\')

0 commit comments

Comments
 (0)