Skip to content

Commit bd87941

Browse files
committed
update
1 parent 696b408 commit bd87941

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

lua/fnpairs/init.lua

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---@diagnostic disable-next-line: redefined-local
1+
local api = vim.api
22
local F = {}
33

44
-- Core FP utilities
@@ -37,44 +37,46 @@ F.pipe = function(x, ...)
3737
end
3838

3939
-- Maybe Monad
40-
local Maybe = {}
41-
Maybe.of = function(x)
42-
return { value = x }
43-
end
44-
Maybe.nothing = Maybe.of(nil)
45-
Maybe.isNothing = function(maybe)
46-
return maybe.value == nil
47-
end
48-
Maybe.fromNullable = function(x)
49-
return x == nil and Maybe.nothing or Maybe.of(x)
50-
end
51-
52-
Maybe.map = F.curry(function(fn, maybe)
53-
if Maybe.isNothing(maybe) then
54-
return Maybe.nothing
55-
end
56-
return Maybe.of(fn(maybe.value))
57-
end)
58-
59-
Maybe.chain = F.curry(function(fn, maybe)
60-
if Maybe.isNothing(maybe) then
61-
return Maybe.nothing
62-
end
63-
return fn(maybe.value)
64-
end)
40+
F.Maybe = {
41+
of = function(x)
42+
return { kind = 'Maybe', value = x }
43+
end,
44+
nothing = { kind = 'Maybe', value = nil },
45+
isNothing = function(maybe)
46+
return maybe.value == nil
47+
end,
48+
fromNullable = function(x)
49+
return x == nil and F.Maybe.nothing or F.Maybe.of(x)
50+
end,
51+
map = function(maybe, f)
52+
if F.Maybe.isNothing(maybe) then
53+
return F.Maybe.nothing
54+
end
55+
return F.Maybe.of(f(maybe.value))
56+
end,
57+
chain = function(maybe, f)
58+
if F.Maybe.isNothing(maybe) then
59+
return F.Maybe.nothing
60+
end
61+
return f(maybe.value)
62+
end,
63+
getOrElse = function(maybe, default)
64+
return maybe.value or default
65+
end,
66+
}
6567

6668
-- Pure state management
6769
local StateManager = {}
6870
StateManager.get = function()
6971
return {
7072
get_line = function()
71-
return vim.api.nvim_get_current_line()
73+
return api.nvim_get_current_line()
7274
end,
7375
get_cursor = function()
74-
return vim.api.nvim_win_get_cursor(0)
76+
return api.nvim_win_get_cursor(0)
7577
end,
7678
get_mode = function()
77-
return vim.api.nvim_get_mode().mode
79+
return api.nvim_get_mode().mode
7880
end,
7981
}
8082
end
@@ -90,7 +92,7 @@ State.new = function()
9092
}
9193
end
9294

93-
-- BracketPair ADT
95+
-- BracketPair ADT with configuration support
9496
local BracketPair = {
9597
['('] = ')',
9698
['['] = ']',
@@ -115,7 +117,7 @@ local Action = {
115117

116118
-- Pure functions for character handling
117119
local get_char_at = F.curry(function(pos, state)
118-
return Maybe.fromNullable(state.line:sub(pos(state), pos(state)))
120+
return F.Maybe.fromNullable(state.line:sub(pos(state), pos(state)))
119121
end)
120122

121123
local get_char_before = get_char_at(function(state)
@@ -162,7 +164,7 @@ local determine_action = F.curry(function(char, state)
162164
end
163165

164166
local next_char = get_char_after(state)
165-
if not Maybe.isNothing(next_char) and next_char.value == BracketPair[char] then
167+
if not F.Maybe.isNothing(next_char) and next_char.value == BracketPair[char] then
166168
local substr = state.line:sub(state.cursor[2] + 1, state.cursor[2] + 1)
167169
if check_bracket_balance(substr, char) then
168170
return Action.Skip
@@ -176,6 +178,7 @@ end)
176178
local handle_skip = function()
177179
return '<Right>'
178180
end
181+
179182
local handle_insert = function(action)
180183
return action.opening .. action.closing .. '<Left>'
181184
end

0 commit comments

Comments
 (0)