Skip to content

Commit 0de6368

Browse files
authored
Remove LineIsMultiline in favor of multi-line agnostic algorithms (#1125) (#2047)
1 parent 6cf129e commit 0de6368

File tree

5 files changed

+36
-56
lines changed

5 files changed

+36
-56
lines changed

PSReadLine/BasicEditing.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -522,25 +522,17 @@ public static void InsertLineAbove(ConsoleKeyInfo? key = null, object arg = null
522522
/// </summary>
523523
public static void InsertLineBelow(ConsoleKeyInfo? key = null, object arg = null)
524524
{
525-
// Move the current position to the end of the current line and only the current line.
526-
if (_singleton.LineIsMultiLine())
525+
int i = _singleton._current;
526+
for (; i < _singleton._buffer.Length; i++)
527527
{
528-
int i = _singleton._current;
529-
for (; i < _singleton._buffer.Length; i++)
528+
if (_singleton._buffer[i] == '\n')
530529
{
531-
if (_singleton._buffer[i] == '\n')
532-
{
533-
break;
534-
}
530+
break;
535531
}
536-
537-
_singleton._current = i;
538-
}
539-
else
540-
{
541-
_singleton._current = _singleton._buffer.Length;
542532
}
543533

534+
_singleton._current = i;
535+
544536
Insert('\n');
545537
}
546538
}

PSReadLine/Movement.cs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,16 @@ public partial class PSConsoleReadLine
2121
/// </summary>
2222
public static void EndOfLine(ConsoleKeyInfo? key = null, object arg = null)
2323
{
24-
if (_singleton.LineIsMultiLine())
24+
int i = _singleton._current;
25+
for (; i < _singleton._buffer.Length; i++)
2526
{
26-
int i = _singleton._current;
27-
for (; i < _singleton._buffer.Length; i++)
27+
if (_singleton._buffer[i] == '\n')
2828
{
29-
if (_singleton._buffer[i] == '\n')
30-
{
31-
break;
32-
}
29+
break;
3330
}
34-
35-
_singleton.MoveCursor((i == _singleton._current) ? _singleton._buffer.Length : i);
36-
}
37-
else
38-
{
39-
_singleton.MoveCursor(_singleton._buffer.Length);
4031
}
32+
33+
_singleton.MoveCursor((i == _singleton._current) ? _singleton._buffer.Length : i);
4134
}
4235

4336
/// <summary>
@@ -97,7 +90,7 @@ public static void ViForwardChar(ConsoleKeyInfo? key = null, object arg = null)
9790
}
9891
else
9992
{
100-
ViOffsetCursorPosition(+ numericArg);
93+
ViOffsetCursorPosition(+numericArg);
10194
}
10295
}
10396
}
@@ -109,7 +102,7 @@ public static void ViBackwardChar(ConsoleKeyInfo? key = null, object arg = null)
109102
{
110103
if (TryGetArgAsInt(arg, out var numericArg, 1))
111104
{
112-
ViOffsetCursorPosition(- numericArg);
105+
ViOffsetCursorPosition(-numericArg);
113106
}
114107
}
115108

@@ -225,7 +218,8 @@ private void MoveToLineImpl(int lineOffset)
225218
point = point ?? ConvertOffsetToPoint(_current);
226219
int newY = point.Value.Y + lineOffset;
227220

228-
Point newPoint = new Point() {
221+
Point newPoint = new Point()
222+
{
229223
X = _moveToLineDesiredColumn,
230224
Y = Math.Max(newY, _initialY)
231225
};
@@ -470,18 +464,18 @@ public static void GotoBrace(ConsoleKeyInfo? key = null, object arg = null)
470464
int direction;
471465
switch (token.Kind)
472466
{
473-
case TokenKind.LParen: toMatch = TokenKind.RParen; direction = 1; break;
474-
case TokenKind.LCurly: toMatch = TokenKind.RCurly; direction = 1; break;
475-
case TokenKind.LBracket: toMatch = TokenKind.RBracket; direction = 1; break;
467+
case TokenKind.LParen: toMatch = TokenKind.RParen; direction = 1; break;
468+
case TokenKind.LCurly: toMatch = TokenKind.RCurly; direction = 1; break;
469+
case TokenKind.LBracket: toMatch = TokenKind.RBracket; direction = 1; break;
476470

477-
case TokenKind.RParen: toMatch = TokenKind.LParen; direction = -1; break;
478-
case TokenKind.RCurly: toMatch = TokenKind.LCurly; direction = -1; break;
479-
case TokenKind.RBracket: toMatch = TokenKind.LBracket; direction = -1; break;
471+
case TokenKind.RParen: toMatch = TokenKind.LParen; direction = -1; break;
472+
case TokenKind.RCurly: toMatch = TokenKind.LCurly; direction = -1; break;
473+
case TokenKind.RBracket: toMatch = TokenKind.LBracket; direction = -1; break;
480474

481-
default:
482-
// Nothing to match (don't match inside strings/comments)
483-
Ding();
484-
return;
475+
default:
476+
// Nothing to match (don't match inside strings/comments)
477+
Ding();
478+
return;
485479
}
486480

487481
var matchCount = 0;

PSReadLine/Movement.vi.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,14 @@ public static void ViEndOfPreviousGlob(ConsoleKeyInfo? key = null, object arg =
159159
/// Move the cursor to the end of the current logical line.
160160
/// </summary>
161161
public static void MoveToEndOfLine(ConsoleKeyInfo? key = null, object arg = null)
162-
{
163-
if (_singleton.LineIsMultiLine())
164-
{
165-
var eol = GetEndOfLogicalLinePos(_singleton._current);
166-
if (eol != _singleton._current)
167-
{
168-
_singleton.MoveCursor(eol);
169-
}
170-
_singleton._moveToEndOfLineCommandCount++;
171-
_singleton._moveToLineDesiredColumn = int.MaxValue;
172-
}
173-
else
162+
{
163+
var eol = GetEndOfLogicalLinePos(_singleton._current);
164+
if (eol != _singleton._current)
174165
{
175-
_singleton.MoveCursor(Math.Max(0, _singleton._buffer.Length + ViEndOfLineFactor));
176-
}
166+
_singleton.MoveCursor(eol);
167+
}
168+
_singleton._moveToEndOfLineCommandCount++;
169+
_singleton._moveToLineDesiredColumn = int.MaxValue;
177170
}
178171

179172
/// <summary>

PSReadLine/Movement.vi.multiline.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public void MoveToFirstLine(ConsoleKeyInfo? key = null, object arg = null)
3838
/// <param name="arg" />
3939
public void MoveToLastLine(ConsoleKeyInfo? key = null, object arg = null)
4040
{
41-
if (!LineIsMultiLine())
41+
var count = GetLogicalLineCount();
42+
if (count == 1)
4243
{
4344
Ding(key, arg);
4445
return;
4546
}
4647

47-
var count = GetLogicalLineCount();
4848
var currentLine = GetLogicalLineNumber();
4949

5050
var pos = ConvertOffsetToPoint(_singleton._current);

PSReadLine/ReadLine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ private string InputLoop()
555555
// nor a "move to line" command. In that case, the desired column
556556
// number will be computed from the current position on the logical line.
557557

558+
_moveToEndOfLineCommandCount = 0;
558559
_moveToLineDesiredColumn = -1;
559560
}
560561
}

0 commit comments

Comments
 (0)