Skip to content

Commit ec0c565

Browse files
committed
Fixed TextReader disposal
1 parent e4b2fc4 commit ec0c565

File tree

6 files changed

+16
-17
lines changed

6 files changed

+16
-17
lines changed

src/PowerShellEditorServices/Services/TextDocument/ScriptFile.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,14 @@ internal ScriptFile(
146146
/// <param name="fileUri">The System.Uri of the file.</param>
147147
/// <param name="initialBuffer">The initial contents of the script file.</param>
148148
/// <param name="powerShellVersion">The version of PowerShell for which the script is being parsed.</param>
149-
internal ScriptFile(
149+
internal static ScriptFile Create(
150150
DocumentUri fileUri,
151151
string initialBuffer,
152152
Version powerShellVersion)
153-
: this(
154-
fileUri,
155-
new StringReader(initialBuffer),
156-
powerShellVersion)
153+
157154
{
155+
using TextReader textReader = new StringReader(initialBuffer);
156+
return new ScriptFile(fileUri, textReader, powerShellVersion);
158157
}
159158

160159
#endregion

src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public ScriptFile GetFileBuffer(DocumentUri documentUri, string initialBuffer)
275275
if (!workspaceFiles.TryGetValue(keyName, out ScriptFile scriptFile) && initialBuffer != null)
276276
{
277277
scriptFile =
278-
new ScriptFile(
278+
ScriptFile.Create(
279279
documentUri,
280280
initialBuffer,
281281
powerShellVersion);

test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void Dispose()
5252
public async Task CanRegisterAndInvokeCommandWithCmdletName()
5353
{
5454
string filePath = TestUtilities.NormalizePath(@"C:\Temp\Test.ps1");
55-
ScriptFile currentFile = new(new Uri(filePath), "This is a test file", new Version("7.0"));
55+
ScriptFile currentFile = ScriptFile.Create(new Uri(filePath), "This is a test file", new Version("7.0"));
5656
EditorContext editorContext = new(
5757
editorOperations: null,
5858
currentFile,
@@ -88,7 +88,7 @@ await psesHost.ExecutePSCommandAsync(
8888
public async Task CanRegisterAndInvokeCommandWithScriptBlock()
8989
{
9090
string filePath = TestUtilities.NormalizePath(@"C:\Temp\Test.ps1");
91-
ScriptFile currentFile = new(new Uri(filePath), "This is a test file", new Version("7.0"));
91+
ScriptFile currentFile = ScriptFile.Create(new Uri(filePath), "This is a test file", new Version("7.0"));
9292
EditorContext editorContext = new(
9393
editorOperations: null,
9494
currentFile,
@@ -150,7 +150,7 @@ await psesHost.ExecutePSCommandAsync(
150150
public async Task CanUnregisterCommand()
151151
{
152152
string filePath = TestUtilities.NormalizePath(@"C:\Temp\Test.ps1");
153-
ScriptFile currentFile = new(new Uri(filePath), "This is a test file", new Version("7.0"));
153+
ScriptFile currentFile = ScriptFile.Create(new Uri(filePath), "This is a test file", new Version("7.0"));
154154
EditorContext editorContext = new(
155155
editorOperations: null,
156156
currentFile,

test/PowerShellEditorServices.Test/Language/SemanticTokenTest.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function Get-Sum {
2424
return $a + $b
2525
}
2626
";
27-
ScriptFile scriptFile = new(
27+
ScriptFile scriptFile = ScriptFile.Create(
2828
// Use any absolute path. Even if it doesn't exist.
2929
DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")),
3030
text,
@@ -61,7 +61,7 @@ function Get-Sum {
6161
public void TokenizesStringExpansion()
6262
{
6363
const string text = "Write-Host \"$(Test-Property Get-Whatever) $(Get-Whatever)\"";
64-
ScriptFile scriptFile = new(
64+
ScriptFile scriptFile = ScriptFile.Create(
6565
// Use any absolute path. Even if it doesn't exist.
6666
DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")),
6767
text,
@@ -88,7 +88,7 @@ function Get-A*A {
8888
}
8989
Get-A*A
9090
";
91-
ScriptFile scriptFile = new(
91+
ScriptFile scriptFile = ScriptFile.Create(
9292
// Use any absolute path. Even if it doesn't exist.
9393
DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")),
9494
text,
@@ -113,7 +113,7 @@ function Get-A*A {
113113
public void RecognizesArrayPropertyInExpandableString()
114114
{
115115
const string text = "\"$(@($Array).Count) OtherText\"";
116-
ScriptFile scriptFile = new(
116+
ScriptFile scriptFile = ScriptFile.Create(
117117
// Use any absolute path. Even if it doesn't exist.
118118
DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")),
119119
text,
@@ -138,7 +138,7 @@ public void RecognizesArrayPropertyInExpandableString()
138138
public void RecognizesCurlyQuotedString()
139139
{
140140
const string text = "“^[-'a-z]*”";
141-
ScriptFile scriptFile = new(
141+
ScriptFile scriptFile = ScriptFile.Create(
142142
// Use any absolute path. Even if it doesn't exist.
143143
DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")),
144144
text,
@@ -158,7 +158,7 @@ enum MyEnum{
158158
three
159159
}
160160
";
161-
ScriptFile scriptFile = new(
161+
ScriptFile scriptFile = ScriptFile.Create(
162162
// Use any absolute path. Even if it doesn't exist.
163163
DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")),
164164
text,

test/PowerShellEditorServices.Test/Language/TokenOperationsTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class TokenOperationsTests
1717
/// </summary>
1818
private static FoldingReference[] GetRegions(string text)
1919
{
20-
ScriptFile scriptFile = new(
20+
ScriptFile scriptFile = ScriptFile.Create(
2121
// Use any absolute path. Even if it doesn't exist.
2222
DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")),
2323
text,

test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class WorkspaceTests
2424
? s_lazyDriveLetter.Value
2525
: string.Empty;
2626

27-
internal static ScriptFile CreateScriptFile(string path) => new(path, "", VersionUtils.PSVersion);
27+
internal static ScriptFile CreateScriptFile(string path) => ScriptFile.Create(path, "", VersionUtils.PSVersion);
2828

2929
// Remember that LSP does weird stuff to the drive letter, so we have to convert it to a URI
3030
// and back to ensure that drive letter gets lower cased and everything matches up.

0 commit comments

Comments
 (0)