Skip to content

Commit 7d07953

Browse files
committed
Fix that ViFindBrace doesn't search for brace when current char is not a brace
1 parent e266ca6 commit 7d07953

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

PSReadLine/Movement.vi.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,29 @@ private int ViFindBrace(int i)
254254
case ')':
255255
return ViFindBackward(i, '(', withoutPassing: ')');
256256
default:
257-
return i;
257+
ReadOnlySpan<char> parenthese = stackalloc char[] { '{', '}', '(', ')', '[', ']' };
258+
int nextParen = i;
259+
for (; nextParen < _buffer.Length; nextParen++)
260+
{
261+
for (int idx = 0; idx < parenthese.Length; idx++)
262+
if (parenthese[idx] == _buffer[nextParen]) goto Outer;
263+
}
264+
265+
Outer:
266+
int match = _buffer[nextParen] switch
267+
{
268+
// if next is opening, find forward
269+
'{' => ViFindForward(nextParen, '}', withoutPassing: '{'),
270+
'[' => ViFindForward(nextParen, ']', withoutPassing: '['),
271+
'(' => ViFindForward(nextParen, ')', withoutPassing: '('),
272+
// if next is closing, find backward
273+
'}' => ViFindBackward(nextParen, '}', withoutPassing: '}'),
274+
']' => ViFindBackward(nextParen, '[', withoutPassing: ']'),
275+
')' => ViFindBackward(nextParen, '(', withoutPassing: ')'),
276+
_ => nextParen
277+
};
278+
279+
return match == nextParen ? i : match;
258280
}
259281
}
260282

test/MovementTest.VI.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ public void ViGlobMovement_EmptyBuffer_Defect1195()
370370
TestSetup(KeyMode.Vi);
371371

372372
TestMustDing("", Keys(
373-
_.Escape, "W"
373+
_.Escape, "W"
374374
));
375375
}
376376

@@ -454,6 +454,23 @@ public void ViGotoBrace()
454454
));
455455
}
456456

457+
Test("0[2[4foo]", Keys(
458+
"0[2[4foo]",
459+
CheckThat(() => AssertCursorLeftIs(9)),
460+
_.Escape, CheckThat(() => AssertCursorLeftIs(7)),
461+
"0ff", CheckThat(() => AssertCursorLeftIs(5)),
462+
_.Percent, CheckThat(() => AssertCursorLeftIs(3)),
463+
_.Percent, CheckThat(() => AssertCursorLeftIs(8))
464+
));
465+
466+
Test("0[2[4foo)}", Keys(
467+
"0[2[4foo)}",
468+
CheckThat(() => AssertCursorLeftIs(7)),
469+
_.Escape, CheckThat(() => AssertCursorLeftIs(6)),
470+
"0ff", CheckThat(() => AssertCursorLeftIs(5)),
471+
_.Percent, CheckThat(() => AssertCursorLeftIs(5)), // stay still
472+
_.Percent, CheckThat(() => AssertCursorLeftIs(5))
473+
));
457474
// <%> with empty text buffer should work fine.
458475
Test("", Keys(
459476
_.Escape, _.Percent,

0 commit comments

Comments
 (0)