diff --git a/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs b/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs index 89681b7bc..b4e6a8b30 100644 --- a/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs +++ b/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs @@ -120,9 +120,7 @@ public ScriptFile GetFile(DocumentUri documentUri) { Validate.IsNotNull(nameof(documentUri), documentUri); - string keyName = VersionUtils.IsLinux - ? documentUri.ToString() - : documentUri.ToString().ToLower(); + string keyName = GetFileKey(documentUri); // Make sure the file isn't already loaded into the workspace if (!workspaceFiles.TryGetValue(keyName, out ScriptFile scriptFile)) @@ -258,9 +256,7 @@ public ScriptFile GetFileBuffer(DocumentUri documentUri, string initialBuffer) { Validate.IsNotNull(nameof(documentUri), documentUri); - string keyName = VersionUtils.IsLinux - ? documentUri.ToString() - : documentUri.ToString().ToLower(); + string keyName = GetFileKey(documentUri); // Make sure the file isn't already loaded into the workspace if (!workspaceFiles.TryGetValue(keyName, out ScriptFile scriptFile) && initialBuffer != null) @@ -293,7 +289,8 @@ public void CloseFile(ScriptFile scriptFile) { Validate.IsNotNull(nameof(scriptFile), scriptFile); - workspaceFiles.TryRemove(scriptFile.Id, out ScriptFile _); + string keyName = GetFileKey(scriptFile.DocumentUri); + workspaceFiles.TryRemove(keyName, out ScriptFile _); } /// @@ -540,6 +537,14 @@ internal string ResolveRelativeScriptPath(string baseFilePath, string relativePa return combinedPath; } + /// + /// Returns a normalized string for a given documentUri to be used as key name. + /// Case-sensitive uri on Linux and lowercase for other platforms. + /// + /// A DocumentUri object to get a normalized key name from + private static string GetFileKey(DocumentUri documentUri) + => VersionUtils.IsLinux ? documentUri.ToString() : documentUri.ToString().ToLower(); + #endregion } } diff --git a/test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs b/test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs index 33e3c653b..c76da5328 100644 --- a/test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs +++ b/test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging.Abstractions; using Microsoft.PowerShell.EditorServices.Services; using Microsoft.PowerShell.EditorServices.Test.Shared; +using Microsoft.PowerShell.EditorServices.Services.TextDocument; using Xunit; namespace PowerShellEditorServices.Test.Session @@ -175,5 +176,18 @@ public void CanDetermineIsPathInMemory() Assert.All(notInMemoryPaths, (p) => Assert.False(WorkspaceService.IsPathInMemory(p))); } + + [Fact] + public void CanOpenAndCloseFile() + { + WorkspaceService workspace = FixturesWorkspace(); + string filePath = Path.GetFullPath(Path.Combine(workspace.WorkspacePath, "rootfile.ps1")); + + ScriptFile file = workspace.GetFile(filePath); + Assert.Equal(workspace.GetOpenedFiles(), new[] { file }); + + workspace.CloseFile(file); + Assert.Equal(workspace.GetOpenedFiles(), Array.Empty()); + } } }