|
| 1 | +---@diagnostic disable-next-line: redefined-local |
| 2 | +local F = {} |
| 3 | + |
| 4 | +-- Core FP utilities |
| 5 | +F.curry = function(fn) |
| 6 | + local function curry_n(n, fn) |
| 7 | + if n <= 1 then |
| 8 | + return fn |
| 9 | + end |
| 10 | + return function(x) |
| 11 | + return curry_n(n - 1, function(...) |
| 12 | + return fn(x, ...) |
| 13 | + end) |
| 14 | + end |
| 15 | + end |
| 16 | + return curry_n(debug.getinfo(fn).nparams, fn) |
| 17 | +end |
| 18 | + |
| 19 | +F.compose = function(...) |
| 20 | + local fns = { ... } |
| 21 | + return function(...) |
| 22 | + local result = ... |
| 23 | + for i = #fns, 1, -1 do |
| 24 | + result = fns[i](result) |
| 25 | + end |
| 26 | + return result |
| 27 | + end |
| 28 | +end |
| 29 | + |
| 30 | +F.pipe = function(x, ...) |
| 31 | + local fns = { ... } |
| 32 | + local result = x |
| 33 | + for i = 1, #fns do |
| 34 | + result = fns[i](result) |
| 35 | + end |
| 36 | + return result |
| 37 | +end |
| 38 | + |
| 39 | +-- 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) |
| 65 | + |
| 66 | +-- Pure state management |
| 67 | +local StateManager = {} |
| 68 | +StateManager.get = function() |
| 69 | + return { |
| 70 | + get_line = function() |
| 71 | + return vim.api.nvim_get_current_line() |
| 72 | + end, |
| 73 | + get_cursor = function() |
| 74 | + return vim.api.nvim_win_get_cursor(0) |
| 75 | + end, |
| 76 | + get_mode = function() |
| 77 | + return vim.api.nvim_get_mode().mode |
| 78 | + end, |
| 79 | + } |
| 80 | +end |
| 81 | + |
| 82 | +-- State ADT |
| 83 | +local State = {} |
| 84 | +State.new = function() |
| 85 | + local state = StateManager.get() |
| 86 | + return { |
| 87 | + line = state.get_line(), |
| 88 | + cursor = state.get_cursor(), |
| 89 | + mode = state.get_mode(), |
| 90 | + } |
| 91 | +end |
| 92 | + |
| 93 | +-- BracketPair ADT |
| 94 | +local BracketPair = { |
| 95 | + match = { |
| 96 | + ['('] = ')', |
| 97 | + ['['] = ']', |
| 98 | + ['{'] = '}', |
| 99 | + ['"'] = '"', |
| 100 | + ["'"] = "'", |
| 101 | + ['`'] = '`', |
| 102 | + }, |
| 103 | +} |
| 104 | + |
| 105 | +-- Action ADT with smart constructors |
| 106 | +local Action = { |
| 107 | + Skip = { type = 'skip' }, |
| 108 | + Insert = function(opening, closing) |
| 109 | + return { type = 'insert', opening = opening, closing = closing } |
| 110 | + end, |
| 111 | + Move = { type = 'move' }, |
| 112 | + Delete = { type = 'delete' }, |
| 113 | + Nothing = { type = 'nothing' }, |
| 114 | +} |
| 115 | + |
| 116 | +-- Pure functions for character handling |
| 117 | +local get_char_at = F.curry(function(pos, state) |
| 118 | + return Maybe.fromNullable(state.line:sub(pos(state), pos(state))) |
| 119 | +end) |
| 120 | + |
| 121 | +local get_char_before = get_char_at(function(state) |
| 122 | + return state.cursor[2] |
| 123 | +end) |
| 124 | + |
| 125 | +local get_char_after = get_char_at(function(state) |
| 126 | + return state.cursor[2] + 1 |
| 127 | +end) |
| 128 | + |
| 129 | +-- Action determination functions |
| 130 | +local should_skip_completion = function(char) |
| 131 | + local state = State.new() |
| 132 | + local prev_char = get_char_before(state).value |
| 133 | + return char == "'" and prev_char and string.match(prev_char, '[%w]') and Action.Nothing |
| 134 | + or Action.Insert(char, BracketPair.match[char]) |
| 135 | +end |
| 136 | + |
| 137 | +local determine_char_action = function(char) |
| 138 | + return F.compose(should_skip_completion, function(action) |
| 139 | + return action or Action.Insert(char, BracketPair.match[char]) |
| 140 | + end) |
| 141 | +end |
| 142 | + |
| 143 | +local determine_action = F.curry(function(char, state) |
| 144 | + if state.mode == 'v' or state.mode == 'V' then |
| 145 | + return Action.Insert(char, BracketPair.match[char]) |
| 146 | + end |
| 147 | + |
| 148 | + return F.pipe(state, get_char_after, function(next_char) |
| 149 | + return next_char.value == BracketPair.match[char] and Action.Skip |
| 150 | + or determine_char_action(char)(char) |
| 151 | + end) |
| 152 | +end) |
| 153 | + |
| 154 | +-- Action handlers |
| 155 | +local handle_skip = function() |
| 156 | + return '<Right>' |
| 157 | +end |
| 158 | +local handle_insert = function(action) |
| 159 | + return action.opening .. action.closing .. '<Left>' |
| 160 | +end |
| 161 | + |
| 162 | +local handle_delete = function() |
| 163 | + local state = State.new() |
| 164 | + return F.pipe(state, function(s) |
| 165 | + return { |
| 166 | + before = get_char_before(s).value, |
| 167 | + after = get_char_after(s).value, |
| 168 | + } |
| 169 | + end, function(chars) |
| 170 | + return BracketPair.match[chars.before] == chars.after and '<BS><Del>' or '<BS>' |
| 171 | + end) |
| 172 | +end |
| 173 | + |
| 174 | +local handle_action = function(action) |
| 175 | + local handlers = { |
| 176 | + skip = handle_skip, |
| 177 | + insert = handle_insert, |
| 178 | + delete = handle_delete, |
| 179 | + nothing = function() |
| 180 | + return '' |
| 181 | + end, |
| 182 | + } |
| 183 | + return handlers[action.type](action) |
| 184 | +end |
| 185 | + |
| 186 | +return { |
| 187 | + setup = function() |
| 188 | + -- Setup bracket pairs |
| 189 | + for opening, _ in pairs(BracketPair.match) do |
| 190 | + vim.keymap.set('i', opening, function() |
| 191 | + return F.pipe(State.new(), determine_action(opening), handle_action) |
| 192 | + end, { expr = true, noremap = true }) |
| 193 | + end |
| 194 | + |
| 195 | + -- Setup backspace handling |
| 196 | + vim.keymap.set('i', '<BS>', function() |
| 197 | + return handle_action(Action.Delete) |
| 198 | + end, { expr = true, noremap = true }) |
| 199 | + end, |
| 200 | +} |
0 commit comments