Skip to content

Commit

Permalink
Fixed TextReader disposal
Browse files Browse the repository at this point in the history
  • Loading branch information
dkattan authored and andyleejordan committed Feb 15, 2024
1 parent e4b2fc4 commit e1720c6
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,14 @@ internal ScriptFile(
/// <param name="fileUri">The System.Uri of the file.</param>
/// <param name="initialBuffer">The initial contents of the script file.</param>
/// <param name="powerShellVersion">The version of PowerShell for which the script is being parsed.</param>
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions test/PowerShellEditorServices.Test/Language/SemanticTokenTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TokenOperationsTests
/// </summary>
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e1720c6

Please sign in to comment.