Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion PSReadLine/Movement.vi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,28 @@ private int ViFindBrace(int i)
case ')':
return ViFindBackward(i, '(', withoutPassing: ')');
default:
return i;
ReadOnlySpan<char> parenthese = stackalloc char[] { '{', '}', '(', ')', '[', ']' };
int nextParen = i;
// find next of any kind of paren
for (; nextParen < _buffer.Length; nextParen++)
for (int idx = 0; idx < parenthese.Length; idx++)
if (parenthese[idx] == _buffer[nextParen]) goto Outer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rare great use of goto


Outer:
int match = _buffer[nextParen] switch
{
// if next is opening, find forward
'{' => ViFindForward(nextParen, '}', withoutPassing: '{'),
'[' => ViFindForward(nextParen, ']', withoutPassing: '['),
'(' => ViFindForward(nextParen, ')', withoutPassing: '('),
// if next is closing, find backward
'}' => ViFindBackward(nextParen, '{', withoutPassing: '}'),
']' => ViFindBackward(nextParen, '[', withoutPassing: ']'),
')' => ViFindBackward(nextParen, '(', withoutPassing: ')'),
_ => nextParen
};

return match == nextParen ? i : match;
}
}

Expand Down
52 changes: 50 additions & 2 deletions test/MovementTest.VI.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xunit;
using System.Collections.Generic;
using Xunit;

namespace Test
{
Expand Down Expand Up @@ -370,7 +371,7 @@ public void ViGlobMovement_EmptyBuffer_Defect1195()
TestSetup(KeyMode.Vi);

TestMustDing("", Keys(
_.Escape, "W"
_.Escape, "W"
));
}

Expand Down Expand Up @@ -454,6 +455,53 @@ public void ViGotoBrace()
));
}

// tests when cursor not on any paren
foreach (var (opening, closing) in new[] { ('(', ')'), ('{', '}'), ('[', ']') })
{
// closing paren with backward match
string input1 = $"0{opening}2{opening}4foo{closing}";
Test(input1, Keys(
input1,
CheckThat(() => AssertCursorLeftIs(9)),
_.Escape, CheckThat(() => AssertCursorLeftIs(8)),
"0ff", CheckThat(() => AssertCursorLeftIs(5)),
_.Percent, CheckThat(() => AssertCursorLeftIs(3)),
_.Percent, CheckThat(() => AssertCursorLeftIs(8))
));

// closing paren without backward match
string input2 = $"0]2)4foo{closing}";
Test(input2, Keys(
input2,
CheckThat(() => AssertCursorLeftIs(9)),
_.Escape, CheckThat(() => AssertCursorLeftIs(8)),
"0ff", CheckThat(() => AssertCursorLeftIs(5)),
_.Percent, CheckThat(() => AssertCursorLeftIs(5)), // stay still
_.Percent, CheckThat(() => AssertCursorLeftIs(5))
));

// opening paren with forward match
string input3 = $"0{opening}2foo6{closing}";
Test(input3, Keys(
input3,
CheckThat(() => AssertCursorLeftIs(8)),
_.Escape, CheckThat(() => AssertCursorLeftIs(7)),
"0ff", CheckThat(() => AssertCursorLeftIs(3)),
_.Percent, CheckThat(() => AssertCursorLeftIs(1)),
_.Percent, CheckThat(() => AssertCursorLeftIs(7))
));
// opening paren without forward match
string input4 = $"0)2]4foo{opening}(";
Test(input4, Keys(
input4,
CheckThat(() => AssertCursorLeftIs(10)),
_.Escape, CheckThat(() => AssertCursorLeftIs(9)),
"0ff", CheckThat(() => AssertCursorLeftIs(5)),
_.Percent, CheckThat(() => AssertCursorLeftIs(5)), // stay still
_.Percent, CheckThat(() => AssertCursorLeftIs(5))
));
}

// <%> with empty text buffer should work fine.
Test("", Keys(
_.Escape, _.Percent,
Expand Down