mirror of
https://github.com/PowerShell/PSReadLine
synced 2026-06-08 12:12:13 +00:00
Remove duplicated code (#1700)
This commit is contained in:
@@ -1,72 +0,0 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.PowerShell
|
||||
{
|
||||
internal static class MultiLineBufferHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a range of text (subset) of the buffer.
|
||||
/// </summary>
|
||||
public class Range
|
||||
{
|
||||
public int Offset { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the offset and the length of the fragment
|
||||
/// in the specified buffer that corresponds to a
|
||||
/// given number of lines starting from the specified line index
|
||||
/// </summary>
|
||||
/// <param name="buffer" />
|
||||
/// <param name="lineOffset">
|
||||
/// The 0-based number of the logical line for the current cursor position.
|
||||
/// This argument comes from a call to the <see cref="PSConsoleReadLine.GetLogicalLineNumber()" />
|
||||
/// method and is thus guaranteed to represent a valid line number.
|
||||
/// </param>
|
||||
/// <param name="lineCount">
|
||||
/// The number of lines to be taken into account.
|
||||
/// If more lines are taken into account than there are lines available,
|
||||
/// this method still returns a valid range corresponding to the available
|
||||
/// lines from the buffer.
|
||||
/// </param>
|
||||
public static Range GetRange(StringBuilder buffer, int lineOffset, int lineCount)
|
||||
{
|
||||
var length = buffer.Length;
|
||||
|
||||
var startPosition = 0;
|
||||
var startPositionIdentified = false;
|
||||
|
||||
var endPosition = length - 1;
|
||||
|
||||
var currentLine = 0;
|
||||
|
||||
for (var position = 0; position < length; position++)
|
||||
{
|
||||
if (currentLine == lineOffset && !startPositionIdentified)
|
||||
{
|
||||
startPosition = position;
|
||||
startPositionIdentified = true;
|
||||
}
|
||||
|
||||
if (buffer[position] == '\n')
|
||||
{
|
||||
currentLine++;
|
||||
}
|
||||
|
||||
if (currentLine == lineOffset + lineCount)
|
||||
{
|
||||
endPosition = position;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return new Range
|
||||
{
|
||||
Offset = startPosition,
|
||||
Count = endPosition - startPosition + 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,7 +66,7 @@ namespace Microsoft.PowerShell
|
||||
/// <param name="lineCount">The number of lines to record to the unnamed register</param>
|
||||
private void SaveLinesToClipboard(int lineIndex, int lineCount)
|
||||
{
|
||||
var range = MultiLineBufferHelper.GetRange(_buffer, lineIndex, lineCount);
|
||||
var range = _buffer.GetRange(lineIndex, lineCount);
|
||||
_clipboard.LinewiseRecord(_buffer.ToString(range.Offset, range.Count));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
using System.Text;
|
||||
using Microsoft.PowerShell;
|
||||
using Xunit;
|
||||
|
||||
namespace Test
|
||||
{
|
||||
public sealed class MultiLineBufferHelperTests
|
||||
{
|
||||
[Fact]
|
||||
public void MultiLineBufferHelper_LinewiseYank_Lines()
|
||||
{
|
||||
var buffer = new StringBuilder("line1\nline2\nline3\nline4");
|
||||
|
||||
// system under test
|
||||
|
||||
var range = MultiLineBufferHelper.GetRange(buffer, 1, 2);
|
||||
|
||||
// assert
|
||||
|
||||
Assert.Equal(6, range.Offset);
|
||||
Assert.Equal(12, range.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultilineBufferHelper_LinewiseYank_MoreLinesThanAvailable()
|
||||
{
|
||||
var buffer = new StringBuilder("line1\nline2");
|
||||
|
||||
// system under test
|
||||
|
||||
var range = MultiLineBufferHelper.GetRange(buffer, 1, 42);
|
||||
|
||||
// assert
|
||||
|
||||
Assert.Equal(6, range.Offset);
|
||||
Assert.Equal(5, range.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user