From 5ba14e053d29de359e96c15430a0d26a5af72efa Mon Sep 17 00:00:00 2001
From: Andy Jordan <2226434+andyleejordan@users.noreply.github.com>
Date: Mon, 15 Jun 2026 17:09:13 -0700
Subject: [PATCH] Accept `IFileRange` in `FileContext.GetText` overloads
`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>
---
.../Extensions/FileContext.cs | 4 +-
.../Extensions/FileContextTests.cs | 68 +++++++++++++++++++
2 files changed, 70 insertions(+), 2 deletions(-)
create mode 100644 test/PowerShellEditorServices.Test/Extensions/FileContextTests.cs
diff --git a/src/PowerShellEditorServices/Extensions/FileContext.cs b/src/PowerShellEditorServices/Extensions/FileContext.cs
index 39a4a275e..cb635d261 100644
--- a/src/PowerShellEditorServices/Extensions/FileContext.cs
+++ b/src/PowerShellEditorServices/Extensions/FileContext.cs
@@ -104,7 +104,7 @@ internal FileContext(
///
/// The buffer range for which content will be extracted.
/// A string with the specified range of content.
- public string GetText(FileRange bufferRange)
+ public string GetText(IFileRange bufferRange)
{
return
string.Join(
@@ -123,7 +123,7 @@ public string GetText(FileRange bufferRange)
///
/// The buffer range for which content will be extracted.
/// An array of strings, each representing a line in the file within the specified range.
- public string[] GetTextLines(FileRange fileRange) => scriptFile.GetLinesInRange(fileRange.ToBufferRange());
+ public string[] GetTextLines(IFileRange fileRange) => scriptFile.GetLinesInRange(fileRange.ToBufferRange());
#endregion
diff --git a/test/PowerShellEditorServices.Test/Extensions/FileContextTests.cs b/test/PowerShellEditorServices.Test/Extensions/FileContextTests.cs
new file mode 100644
index 000000000..6ad981737
--- /dev/null
+++ b/test/PowerShellEditorServices.Test/Extensions/FileContextTests.cs
@@ -0,0 +1,68 @@
+// Copyright (c) Microsoft Corporation.
+// Licensed under the MIT License.
+
+using System;
+using Microsoft.PowerShell.EditorServices.Extensions;
+using Microsoft.PowerShell.EditorServices.Services.TextDocument;
+using Microsoft.PowerShell.EditorServices.Test.Shared;
+using Xunit;
+
+namespace PowerShellEditorServices.Test.Extensions
+{
+ [Trait("Category", "Extensions")]
+ public class FileContextTests
+ {
+ private static EditorContext CreateEditorContext(string content, BufferRange selectedRange)
+ {
+ string filePath = TestUtilities.NormalizePath(@"C:\Temp\Test.ps1");
+ ScriptFile scriptFile = ScriptFile.Create(new Uri(filePath), content, new Version("7.0"));
+ return new EditorContext(
+ editorOperations: null,
+ scriptFile,
+ new BufferPosition(line: 1, column: 1),
+ selectedRange);
+ }
+
+ // Regression test for https://github.com/PowerShell/PowerShellEditorServices/issues/1496
+ // where $Context.CurrentFile.GetText($Context.SelectedRange) failed because GetText only
+ // accepted the concrete FileRange type rather than the IFileRange that SelectedRange returns.
+ [Fact]
+ public void CanGetTextFromSelectedRange()
+ {
+ EditorContext editorContext = CreateEditorContext(
+ "Line One\nLine Two\nLine Three",
+ new BufferRange(2, 1, 2, 9));
+
+ IFileRange selectedRange = editorContext.SelectedRange;
+ string text = editorContext.CurrentFile.GetText(selectedRange);
+
+ Assert.Equal("Line Two", text);
+ }
+
+ [Fact]
+ public void CanGetTextLinesFromSelectedRange()
+ {
+ EditorContext editorContext = CreateEditorContext(
+ "Line One\nLine Two\nLine Three",
+ new BufferRange(1, 1, 2, 9));
+
+ string[] lines = editorContext.CurrentFile.GetTextLines(editorContext.SelectedRange);
+
+ Assert.Equal(new[] { "Line One", "Line Two" }, lines);
+ }
+
+ [Fact]
+ public void CanGetTextFromConstructedFileRange()
+ {
+ EditorContext editorContext = CreateEditorContext(
+ "Line One\nLine Two\nLine Three",
+ BufferRange.None);
+
+ IFileRange range = new FileRange(
+ new Microsoft.PowerShell.EditorServices.Extensions.FilePosition(3, 1),
+ new Microsoft.PowerShell.EditorServices.Extensions.FilePosition(3, 11));
+
+ Assert.Equal("Line Three", editorContext.CurrentFile.GetText(range));
+ }
+ }
+}