From ec0c5657eeeb57013323a1fab1a911bfb5f7ac3d Mon Sep 17 00:00:00 2001 From: Darren Kattan Date: Thu, 15 Feb 2024 10:20:55 -0600 Subject: [PATCH] Fixed TextReader disposal --- .../Services/TextDocument/ScriptFile.cs | 9 ++++----- .../Services/Workspace/WorkspaceService.cs | 2 +- .../Extensions/ExtensionCommandTests.cs | 6 +++--- .../Language/SemanticTokenTest.cs | 12 ++++++------ .../Language/TokenOperationsTests.cs | 2 +- .../Session/WorkspaceTests.cs | 2 +- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/PowerShellEditorServices/Services/TextDocument/ScriptFile.cs b/src/PowerShellEditorServices/Services/TextDocument/ScriptFile.cs index 4909d1020..fb6772d2b 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/ScriptFile.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/ScriptFile.cs @@ -146,15 +146,14 @@ internal ScriptFile( /// The System.Uri of the file. /// The initial contents of the script file. /// The version of PowerShell for which the script is being parsed. - internal ScriptFile( + internal static ScriptFile Create( DocumentUri fileUri, string initialBuffer, Version powerShellVersion) - : this( - fileUri, - new StringReader(initialBuffer), - powerShellVersion) + { + using TextReader textReader = new StringReader(initialBuffer); + return new ScriptFile(fileUri, textReader, powerShellVersion); } #endregion diff --git a/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs b/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs index 54a1f2894..941fcf736 100644 --- a/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs +++ b/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs @@ -275,7 +275,7 @@ public ScriptFile GetFileBuffer(DocumentUri documentUri, string initialBuffer) if (!workspaceFiles.TryGetValue(keyName, out ScriptFile scriptFile) && initialBuffer != null) { scriptFile = - new ScriptFile( + ScriptFile.Create( documentUri, initialBuffer, powerShellVersion); diff --git a/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs b/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs index a740f6fd6..190ee5aae 100644 --- a/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs +++ b/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs @@ -52,7 +52,7 @@ public void Dispose() public async Task CanRegisterAndInvokeCommandWithCmdletName() { string filePath = TestUtilities.NormalizePath(@"C:\Temp\Test.ps1"); - ScriptFile currentFile = new(new Uri(filePath), "This is a test file", new Version("7.0")); + ScriptFile currentFile = ScriptFile.Create(new Uri(filePath), "This is a test file", new Version("7.0")); EditorContext editorContext = new( editorOperations: null, currentFile, @@ -88,7 +88,7 @@ await psesHost.ExecutePSCommandAsync( public async Task CanRegisterAndInvokeCommandWithScriptBlock() { string filePath = TestUtilities.NormalizePath(@"C:\Temp\Test.ps1"); - ScriptFile currentFile = new(new Uri(filePath), "This is a test file", new Version("7.0")); + ScriptFile currentFile = ScriptFile.Create(new Uri(filePath), "This is a test file", new Version("7.0")); EditorContext editorContext = new( editorOperations: null, currentFile, @@ -150,7 +150,7 @@ await psesHost.ExecutePSCommandAsync( public async Task CanUnregisterCommand() { string filePath = TestUtilities.NormalizePath(@"C:\Temp\Test.ps1"); - ScriptFile currentFile = new(new Uri(filePath), "This is a test file", new Version("7.0")); + ScriptFile currentFile = ScriptFile.Create(new Uri(filePath), "This is a test file", new Version("7.0")); EditorContext editorContext = new( editorOperations: null, currentFile, diff --git a/test/PowerShellEditorServices.Test/Language/SemanticTokenTest.cs b/test/PowerShellEditorServices.Test/Language/SemanticTokenTest.cs index 6c66fd697..c9f3c01d4 100644 --- a/test/PowerShellEditorServices.Test/Language/SemanticTokenTest.cs +++ b/test/PowerShellEditorServices.Test/Language/SemanticTokenTest.cs @@ -24,7 +24,7 @@ function Get-Sum { return $a + $b } "; - ScriptFile scriptFile = new( + ScriptFile scriptFile = ScriptFile.Create( // Use any absolute path. Even if it doesn't exist. DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")), text, @@ -61,7 +61,7 @@ function Get-Sum { public void TokenizesStringExpansion() { const string text = "Write-Host \"$(Test-Property Get-Whatever) $(Get-Whatever)\""; - ScriptFile scriptFile = new( + ScriptFile scriptFile = ScriptFile.Create( // Use any absolute path. Even if it doesn't exist. DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")), text, @@ -88,7 +88,7 @@ function Get-A*A { } Get-A*A "; - ScriptFile scriptFile = new( + ScriptFile scriptFile = ScriptFile.Create( // Use any absolute path. Even if it doesn't exist. DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")), text, @@ -113,7 +113,7 @@ function Get-A*A { public void RecognizesArrayPropertyInExpandableString() { const string text = "\"$(@($Array).Count) OtherText\""; - ScriptFile scriptFile = new( + ScriptFile scriptFile = ScriptFile.Create( // Use any absolute path. Even if it doesn't exist. DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")), text, @@ -138,7 +138,7 @@ public void RecognizesArrayPropertyInExpandableString() public void RecognizesCurlyQuotedString() { const string text = "“^[-'a-z]*”"; - ScriptFile scriptFile = new( + ScriptFile scriptFile = ScriptFile.Create( // Use any absolute path. Even if it doesn't exist. DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")), text, @@ -158,7 +158,7 @@ enum MyEnum{ three } "; - ScriptFile scriptFile = new( + ScriptFile scriptFile = ScriptFile.Create( // Use any absolute path. Even if it doesn't exist. DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")), text, diff --git a/test/PowerShellEditorServices.Test/Language/TokenOperationsTests.cs b/test/PowerShellEditorServices.Test/Language/TokenOperationsTests.cs index a47d765e1..19da0af3d 100644 --- a/test/PowerShellEditorServices.Test/Language/TokenOperationsTests.cs +++ b/test/PowerShellEditorServices.Test/Language/TokenOperationsTests.cs @@ -17,7 +17,7 @@ public class TokenOperationsTests /// private static FoldingReference[] GetRegions(string text) { - ScriptFile scriptFile = new( + ScriptFile scriptFile = ScriptFile.Create( // Use any absolute path. Even if it doesn't exist. DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")), text, diff --git a/test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs b/test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs index 01f31325a..4abd80f21 100644 --- a/test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs +++ b/test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs @@ -24,7 +24,7 @@ public class WorkspaceTests ? s_lazyDriveLetter.Value : string.Empty; - internal static ScriptFile CreateScriptFile(string path) => new(path, "", VersionUtils.PSVersion); + internal static ScriptFile CreateScriptFile(string path) => ScriptFile.Create(path, "", VersionUtils.PSVersion); // Remember that LSP does weird stuff to the drive letter, so we have to convert it to a URI // and back to ensure that drive letter gets lower cased and everything matches up.