Skip to content

Commit 37dc288

Browse files
Accept IFileRange in FileContext.GetText overloads (#2306)
`EditorContext.SelectedRange` is typed as `IFileRange` (it's actually a `BufferFileRange`), but `FileContext.GetText` and `GetTextLines` only accepted the concrete `FileRange` class. That meant the obvious script `$Context.CurrentFile.GetText($Context.SelectedRange)` failed overload resolution with "Cannot find an overload for GetText and the argument count: 1", and the documented workaround of constructing a `FileRange` isn't even reachable when that type isn't loaded. Widen both parameters from `FileRange` to `IFileRange`. The bodies already route through the `ToBufferRange()` extension defined on `IFileRange`, so nothing else changes, and `FileRange` callers keep working since it implements the interface. Adds focused regression tests covering `GetText`/`GetTextLines` with a `SelectedRange` and with a hand-built `FileRange`. Fixes #1496. Drafted by Copilot (Claude Opus 4.8). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b9fd1b3 commit 37dc288

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

src/PowerShellEditorServices/Extensions/FileContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ internal FileContext(
104104
/// </summary>
105105
/// <param name="bufferRange">The buffer range for which content will be extracted.</param>
106106
/// <returns>A string with the specified range of content.</returns>
107-
public string GetText(FileRange bufferRange)
107+
public string GetText(IFileRange bufferRange)
108108
{
109109
return
110110
string.Join(
@@ -123,7 +123,7 @@ public string GetText(FileRange bufferRange)
123123
/// </summary>
124124
/// <param name="fileRange">The buffer range for which content will be extracted.</param>
125125
/// <returns>An array of strings, each representing a line in the file within the specified range.</returns>
126-
public string[] GetTextLines(FileRange fileRange) => scriptFile.GetLinesInRange(fileRange.ToBufferRange());
126+
public string[] GetTextLines(IFileRange fileRange) => scriptFile.GetLinesInRange(fileRange.ToBufferRange());
127127

128128
#endregion
129129

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using Microsoft.PowerShell.EditorServices.Extensions;
6+
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
7+
using Microsoft.PowerShell.EditorServices.Test.Shared;
8+
using Xunit;
9+
10+
namespace PowerShellEditorServices.Test.Extensions
11+
{
12+
[Trait("Category", "Extensions")]
13+
public class FileContextTests
14+
{
15+
private static EditorContext CreateEditorContext(string content, BufferRange selectedRange)
16+
{
17+
string filePath = TestUtilities.NormalizePath(@"C:\Temp\Test.ps1");
18+
ScriptFile scriptFile = ScriptFile.Create(new Uri(filePath), content, new Version("7.0"));
19+
return new EditorContext(
20+
editorOperations: null,
21+
scriptFile,
22+
new BufferPosition(line: 1, column: 1),
23+
selectedRange);
24+
}
25+
26+
// Regression test for https://github.com/PowerShell/PowerShellEditorServices/issues/1496
27+
// where $Context.CurrentFile.GetText($Context.SelectedRange) failed because GetText only
28+
// accepted the concrete FileRange type rather than the IFileRange that SelectedRange returns.
29+
[Fact]
30+
public void CanGetTextFromSelectedRange()
31+
{
32+
EditorContext editorContext = CreateEditorContext(
33+
"Line One\nLine Two\nLine Three",
34+
new BufferRange(2, 1, 2, 9));
35+
36+
IFileRange selectedRange = editorContext.SelectedRange;
37+
string text = editorContext.CurrentFile.GetText(selectedRange);
38+
39+
Assert.Equal("Line Two", text);
40+
}
41+
42+
[Fact]
43+
public void CanGetTextLinesFromSelectedRange()
44+
{
45+
EditorContext editorContext = CreateEditorContext(
46+
"Line One\nLine Two\nLine Three",
47+
new BufferRange(1, 1, 2, 9));
48+
49+
string[] lines = editorContext.CurrentFile.GetTextLines(editorContext.SelectedRange);
50+
51+
Assert.Equal(new[] { "Line One", "Line Two" }, lines);
52+
}
53+
54+
[Fact]
55+
public void CanGetTextFromConstructedFileRange()
56+
{
57+
EditorContext editorContext = CreateEditorContext(
58+
"Line One\nLine Two\nLine Three",
59+
BufferRange.None);
60+
61+
IFileRange range = new FileRange(
62+
new Microsoft.PowerShell.EditorServices.Extensions.FilePosition(3, 1),
63+
new Microsoft.PowerShell.EditorServices.Extensions.FilePosition(3, 11));
64+
65+
Assert.Equal("Line Three", editorContext.CurrentFile.GetText(range));
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)