Skip to content

Commit 7d334a6

Browse files
committed
update
1 parent d548819 commit 7d334a6

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

lua/fnpairs/init.lua

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ local Action = {
113113
Nothing = { type = 'nothing' },
114114
}
115115

116-
--- @class Value
117-
--- @field value string
118-
119116
-- Pure functions for character handling
120-
-- @return Value
121117
local get_char_at = F.curry(function(pos, state)
122118
return Maybe.fromNullable(state.line:sub(pos(state), pos(state)))
123119
end)
@@ -149,10 +145,31 @@ local determine_action = F.curry(function(char, state)
149145
return Action.Insert(char, BracketPair.match[char])
150146
end
151147

152-
return F.pipe(state, get_char_after, function(next_char)
153-
return next_char.value == BracketPair.match[char] and Action.Skip
154-
or determine_char_action(char)(char)
155-
end)
148+
local function check_bracket_balance(str, opening_char)
149+
local stack = {}
150+
for i = 1, #str do
151+
local c = str:sub(i, i)
152+
if BracketPair.match[c] then
153+
table.insert(stack, c)
154+
elseif c == BracketPair.match[opening_char] then
155+
if #stack == 0 or stack[#stack] ~= opening_char then
156+
return false
157+
end
158+
table.remove(stack)
159+
end
160+
end
161+
return true
162+
end
163+
164+
local next_char = get_char_after(state)
165+
if not Maybe.isNothing(next_char) and next_char.value == BracketPair.match[char] then
166+
local substr = state.line:sub(state.cursor[2] + 1, state.cursor[2] + 1)
167+
if check_bracket_balance(substr, char) then
168+
return Action.Skip
169+
end
170+
end
171+
172+
return determine_char_action(char)(char)
156173
end)
157174

158175
-- Action handlers

0 commit comments

Comments
 (0)