From 4137b4ece88b3f9a0eac6f046f8a860901598da0 Mon Sep 17 00:00:00 2001 From: sharpchen Date: Wed, 23 Jul 2025 07:00:58 +0800 Subject: [PATCH] Fix that ViFindBrace doesn't search for brace when current char is not a brace --- PSReadLine/Movement.vi.cs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/PSReadLine/Movement.vi.cs b/PSReadLine/Movement.vi.cs index 011c146d9..dc52dcb94 100644 --- a/PSReadLine/Movement.vi.cs +++ b/PSReadLine/Movement.vi.cs @@ -254,7 +254,32 @@ private int ViFindBrace(int i) case ')': return ViFindBackward(i, '(', withoutPassing: ')'); default: - return i; + int l1 = ViFindForward(i, '{', withoutPassing: '}') is var x && x > i ? x : int.MaxValue; + int l2 = ViFindForward(i, '[', withoutPassing: ']') is var y && y > i ? y : int.MaxValue; + int l3 = ViFindForward(i, '(', withoutPassing: ')') is var z && z > i ? z : int.MaxValue; + int r1 = ViFindForward(i, '}', withoutPassing: '{') is var a && a > i ? a : int.MaxValue; + int r2 = ViFindForward(i, ']', withoutPassing: '[') is var b && b > i ? b : int.MaxValue; + int r3 = ViFindForward(i, ')', withoutPassing: '(') is var c && c > i ? c : int.MaxValue; + int closestLeft = Math.Min(Math.Min(l1, l2), l3); + int closestRight = Math.Min(Math.Min(r1, r2), r3); + + if (closestRight <= closestLeft && closestRight != int.MaxValue) + { + return closestRight; + } + + closestLeft = closestLeft == int.MaxValue ? i : closestLeft; + switch (_buffer[closestLeft]) + { + case '{': + return ViFindForward(closestLeft, '}', withoutPassing: '{'); + case '[': + return ViFindForward(closestLeft, ']', withoutPassing: '['); + case '(': + return ViFindForward(closestLeft, ')', withoutPassing: '('); + default: + return i; + } } }