Skip to content

Commit

Permalink
Fix GetRelativePath to check against all workspace folders
Browse files Browse the repository at this point in the history
  • Loading branch information
andyleejordan committed Aug 9, 2023
1 parent ad8dd14 commit 3d10a3a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
3 changes: 1 addition & 2 deletions src/PowerShellEditorServices/Extensions/FileContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public sealed class FileContext
/// <summary>
/// Gets the workspace-relative path of the file.
/// </summary>
public string WorkspacePath => editorOperations.GetWorkspaceRelativePath(
scriptFile.FilePath);
public string WorkspacePath => editorOperations.GetWorkspaceRelativePath(scriptFile);

#endregion

Expand Down
3 changes: 1 addition & 2 deletions src/PowerShellEditorServices/Extensions/IEditorOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ internal interface IEditorOperations
/// <summary>
/// Resolves the given file path relative to the current workspace path.
/// </summary>
/// <param name="filePath">The file path to be resolved.</param>
/// <returns>The resolved file path.</returns>
string GetWorkspaceRelativePath(string filePath);
string GetWorkspaceRelativePath(ScriptFile scriptFile);

/// <summary>
/// Causes a new untitled file to be created in the editor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public async Task SaveFileAsync(string currentPath, string newSavePath)
// workspace it's in.
public string GetWorkspacePath() => _workspaceService.InitialWorkingDirectory;

public string GetWorkspaceRelativePath(string filePath) => _workspaceService.GetRelativePath(filePath);
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 @@ -306,18 +306,23 @@ public void CloseFile(ScriptFile scriptFile)
/// <summary>
/// Gets the workspace-relative path of the given file path.
/// </summary>
/// <param name="filePath">The original full file path.</param>
/// <returns>A relative file path</returns>
public string GetRelativePath(string filePath)
public string GetRelativePath(ScriptFile scriptFile)
{
string resolvedPath = filePath;
string resolvedPath = scriptFile.FilePath;

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

resolvedPath = workspaceUri.MakeRelativeUri(fileUri).ToString();
Uri fileUri = scriptFile.DocumentUri.ToUri();
foreach (WorkspaceFolder workspaceFolder in WorkspaceFolders)
{
Uri workspaceUri = workspaceFolder.Uri.ToUri();
if (workspaceUri.IsBaseOf(fileUri))
{
resolvedPath = workspaceUri.MakeRelativeUri(fileUri).ToString();
break;
}
}

// Convert the directory separators if necessary
if (Path.DirectorySeparatorChar == '\\')
Expand Down

0 comments on commit 3d10a3a

Please sign in to comment.