Module: Rvim::Lua::Api

Defined in:
lib/rvim/lua/api.rb

Overview

vim.api.nvim_* — the namespaced API that Lua plugins target.

v3.4 introduces this module with the autocmd subset; later ships add buffer/window/exec/eval functions.

Class Method Summary collapse

Class Method Details

.install(state, editor, _runtime) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rvim/lua/api.rb', line 12

def install(state, editor, _runtime)
  # Augroup registry: name -> integer id, persisted on the editor side.
  editor.instance_variable_set(:@lua_augroups, {}) unless editor.instance_variable_defined?(:@lua_augroups)
  augroups = editor.instance_variable_get(:@lua_augroups)
  next_group_id = [0]

  state.function '_rvim_api_create_augroup' do |name, opts|
    opts_h = opts.respond_to?(:to_h) ? opts.to_h : {}
    if augroups.key?(name.to_s) && opts_h['clear'] != false
      editor.autocommands.clear_group(augroups[name.to_s])
    end
    unless augroups.key?(name.to_s)
      next_group_id[0] += 1
      augroups[name.to_s] = next_group_id[0]
    end
    augroups[name.to_s]
  end

  state.function '_rvim_api_del_augroup_by_name' do |name|
    gid = augroups.delete(name.to_s)
    editor.autocommands.clear_group(gid) if gid
  end

  state.function '_rvim_api_create_autocmd' do |events, opts|
    events_arr = if events.respond_to?(:to_h)
                   events.to_h.values.map(&:to_s)
                 else
                   [events.to_s]
                 end
    opts_h = opts.respond_to?(:to_h) ? opts.to_h : {}
    patterns_raw = opts_h['pattern']
    patterns = if patterns_raw.respond_to?(:to_h)
                 patterns_raw.to_h.values.map(&:to_s)
               elsif patterns_raw.nil? || patterns_raw == ''
                 ['*']
               else
                 [patterns_raw.to_s]
               end
    group_id = case opts_h['group']
               when nil then nil
               when Numeric then opts_h['group'].to_i
               else augroups[opts_h['group'].to_s]
               end

    callback = nil
    command = ''
    if opts_h['callback'].is_a?(Rufus::Lua::Function)
      cb_lua = opts_h['callback']
      callback = ->(args) { cb_lua.call(args) }
    elsif opts_h['command']
      command = opts_h['command'].to_s
    end

    editor.autocommands.add(events_arr, patterns, command, callback: callback, group: group_id)
  end

  install_buffer_api(state, editor)
  install_window_api(state, editor)
  install_extended_api(state, editor)

  # Build vim.api as a Lua table mapping nvim_* names to the bridges.
  state.eval(<<~LUA)
    vim.api = vim.api or {}
    vim.api.nvim_create_augroup       = _rvim_api_create_augroup
    vim.api.nvim_del_augroup_by_name  = _rvim_api_del_augroup_by_name
    vim.api.nvim_create_autocmd       = _rvim_api_create_autocmd

    vim.api.nvim_buf_get_lines        = _rvim_api_buf_get_lines
    vim.api.nvim_buf_set_lines        = _rvim_api_buf_set_lines
    vim.api.nvim_buf_get_name         = _rvim_api_buf_get_name
    vim.api.nvim_buf_set_name         = _rvim_api_buf_set_name
    vim.api.nvim_buf_line_count       = _rvim_api_buf_line_count
    vim.api.nvim_buf_get_option       = _rvim_api_buf_get_option
    vim.api.nvim_buf_set_option       = _rvim_api_buf_set_option
    vim.api.nvim_get_current_buf      = _rvim_api_get_current_buf
    vim.api.nvim_set_current_buf      = _rvim_api_set_current_buf

    vim.api.nvim_win_get_cursor       = _rvim_api_win_get_cursor
    vim.api.nvim_win_set_cursor       = _rvim_api_win_set_cursor
    vim.api.nvim_win_get_height       = _rvim_api_win_get_height
    vim.api.nvim_win_set_height       = _rvim_api_win_set_height
    vim.api.nvim_win_get_width        = _rvim_api_win_get_width
    vim.api.nvim_win_get_buf          = _rvim_api_win_get_buf
    vim.api.nvim_get_current_win      = _rvim_api_get_current_win

    vim.api.nvim_list_bufs            = _rvim_api_list_bufs
    vim.api.nvim_buf_is_valid         = _rvim_api_buf_is_valid
    vim.api.nvim_buf_is_loaded        = _rvim_api_buf_is_loaded
    vim.api.nvim_buf_get_var          = _rvim_api_buf_get_var
    vim.api.nvim_buf_set_var          = _rvim_api_buf_set_var
    vim.api.nvim_buf_del_var          = _rvim_api_buf_del_var
    vim.api.nvim_buf_get_changedtick  = _rvim_api_buf_get_changedtick

    vim.api.nvim_list_wins            = _rvim_api_list_wins
    vim.api.nvim_win_is_valid         = _rvim_api_win_is_valid

    vim.api.nvim_get_var              = _rvim_api_get_var
    vim.api.nvim_set_var              = _rvim_api_set_var
    vim.api.nvim_del_var              = _rvim_api_del_var
    vim.api.nvim_get_option           = _rvim_api_get_option
    vim.api.nvim_set_option           = _rvim_api_set_option
    vim.api.nvim_get_option_value     = _rvim_api_get_option_value
    vim.api.nvim_set_option_value     = _rvim_api_set_option_value
    vim.api.nvim_get_mode             = _rvim_api_get_mode
    vim.api.nvim_command              = _rvim_api_command
    vim.api.nvim_echo                 = _rvim_api_echo
    vim.api.nvim_err_writeln          = _rvim_api_err_writeln
    vim.api.nvim_out_write            = _rvim_api_out_write
    vim.api.nvim_strwidth             = _rvim_api_strwidth
    vim.api.nvim_replace_termcodes    = _rvim_api_replace_termcodes
    vim.api.nvim_set_hl               = _rvim_api_set_hl
  LUA
end

.install_buffer_api(state, editor) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/rvim/lua/api.rb', line 247

def self.install_buffer_api(state, editor)
  resolve = ->(bufnr) { resolve_buffer(editor, bufnr) }

  state.function '_rvim_api_buf_get_lines' do |bufnr, start_idx, end_idx, _strict|
    buf = resolve.call(bufnr)
    next [] unless buf

    lines = buf.lines || []
    s = start_idx.to_i
    e = end_idx.to_i
    s = lines.size + s if s < 0
    e = lines.size + e + 1 if e < 0
    lines[s...e] || []
  end

  state.function '_rvim_api_buf_set_lines' do |bufnr, start_idx, end_idx, _strict, replacement|
    buf = resolve.call(bufnr)
    next nil unless buf

    lines = buf.lines || []
    s = start_idx.to_i
    e = end_idx.to_i
    s = lines.size + s if s < 0
    e = lines.size + e + 1 if e < 0
    new_lines = if replacement.respond_to?(:to_h)
                  replacement.to_h.values.map(&:to_s)
                elsif replacement.is_a?(Array)
                  replacement.map(&:to_s)
                else
                  []
                end
    buf.lines = lines[0...s] + new_lines + (lines[e..] || [])
    if buf == editor.current_buffer
      editor.instance_variable_set(:@buffer_of_lines, buf.lines)
      editor.instance_variable_set(:@modified, true)
    end
  end

  state.function('_rvim_api_buf_get_name')   { |bufnr| (resolve.call(bufnr)&.filepath).to_s }
  state.function('_rvim_api_buf_set_name')   { |bufnr, name| buf = resolve.call(bufnr); buf.filepath = name.to_s if buf }
  state.function('_rvim_api_buf_line_count') { |bufnr| (resolve.call(bufnr)&.lines || []).size }

  state.function '_rvim_api_buf_get_option' do |bufnr, name|
    buf = resolve.call(bufnr)
    editor.settings.get(name.to_s, buffer: buf || :current)
  end

  state.function '_rvim_api_buf_set_option' do |bufnr, name, value|
    buf = resolve.call(bufnr)
    coerced = value.is_a?(Float) && value == value.to_i ? value.to_i : value
    editor.settings.set(name.to_s, coerced, buffer: buf)
  end

  state.function('_rvim_api_get_current_buf') { editor.current_buffer&.id || 0 }
  state.function '_rvim_api_set_current_buf' do |bufnr|
    buf = editor.buffers&.values&.find { |b| b.id == bufnr.to_i }
    editor.swap_to_buffer(buf) if buf
  end
end

.install_extended_api(state, editor) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rvim/lua/api.rb', line 126

def self.install_extended_api(state, editor)
  state.function('_rvim_api_list_bufs') { (editor.buffers&.values || []).map(&:id) }
  state.function('_rvim_api_buf_is_valid') { |bufnr| !resolve_buffer(editor, bufnr).nil? }
  state.function('_rvim_api_buf_is_loaded') { |bufnr| !resolve_buffer(editor, bufnr).nil? }

  state.function '_rvim_api_buf_get_var' do |bufnr, name|
    buf = resolve_buffer(editor, bufnr)
    buf&.vars&.[](name.to_s)
  end
  state.function '_rvim_api_buf_set_var' do |bufnr, name, value|
    buf = resolve_buffer(editor, bufnr)
    buf.vars[name.to_s] = value if buf
  end
  state.function '_rvim_api_buf_del_var' do |bufnr, name|
    buf = resolve_buffer(editor, bufnr)
    buf.vars.delete(name.to_s) if buf
  end
  state.function '_rvim_api_buf_get_changedtick' do |bufnr|
    buf = resolve_buffer(editor, bufnr)
    # Approximate via undo history index when present.
    buf&.undo_redo_index.to_i
  end

  state.function('_rvim_api_list_wins') { (editor.windows || []).each_with_index.map { |_, i| i + 1 } }
  state.function('_rvim_api_win_is_valid') { |winid| !resolve_window(editor, winid).nil? }

  state.function('_rvim_api_get_var') { |name| editor.let_vars[name.to_s] }
  state.function('_rvim_api_set_var') { |name, value| editor.let_vars[name.to_s] = value }
  state.function('_rvim_api_del_var') { |name| editor.let_vars.delete(name.to_s) }

  state.function('_rvim_api_get_option') { |name| editor.settings.get(name.to_s) }
  state.function '_rvim_api_set_option' do |name, value|
    coerced = value.is_a?(Float) && value == value.to_i ? value.to_i : value
    editor.settings.set(name.to_s, coerced)
  end

  state.function '_rvim_api_get_option_value' do |name, opts|
    opts_h = opts.respond_to?(:to_h) ? opts.to_h : {}
    if opts_h['buf']
      buf = resolve_buffer(editor, opts_h['buf'])
      editor.settings.get(name.to_s, buffer: buf || :current)
    else
      editor.settings.get(name.to_s)
    end
  end

  state.function '_rvim_api_set_option_value' do |name, value, opts|
    coerced = value.is_a?(Float) && value == value.to_i ? value.to_i : value
    opts_h = opts.respond_to?(:to_h) ? opts.to_h : {}
    if opts_h['buf']
      buf = resolve_buffer(editor, opts_h['buf'])
      editor.settings.set(name.to_s, coerced, buffer: buf)
    else
      editor.settings.set(name.to_s, coerced)
    end
  end

  state.function '_rvim_api_get_mode' do
    { 'mode' => Rvim::Lua::Fn.mode(editor), 'blocking' => false }
  end

  state.function '_rvim_api_command' do |cmd|
    parsed = Rvim::Command.parse(cmd.to_s)
    Rvim::Command.execute(editor, parsed) if parsed
  end

  state.function('_rvim_api_echo') { |_chunks, history, _opts| editor.status_message = '' if history }
  state.function('_rvim_api_err_writeln') { |msg| editor.status_message = "ERR: #{msg}" }
  state.function('_rvim_api_out_write') { |msg| editor.status_message = msg.to_s }

  state.function('_rvim_api_strwidth') { |s| s.to_s.length }

  state.function '_rvim_api_replace_termcodes' do |s, _from_part, _do_lt, _special|
    Rvim::Keymap.expand(s.to_s, leader: editor.mapleader)
  end

  state.function '_rvim_api_set_hl' do |_ns_id, name, _val|
    # Highlight registry is mostly visual; for v1 stash the name so
    # plugins probing for highlights see them as defined.
    editor.instance_variable_get(:@lua_highlights) || editor.instance_variable_set(:@lua_highlights, {})
    editor.instance_variable_get(:@lua_highlights)[name.to_s] = true
  end
end

.install_window_api(state, editor) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/rvim/lua/api.rb', line 210

def self.install_window_api(state, editor)
  resolve = ->(winid) { resolve_window(editor, winid) }

  state.function '_rvim_api_win_get_cursor' do |_winid|
    # NeoVim returns {row(1-based), col(0-based)}.
    [editor.line_index + 1, editor.byte_pointer]
  end

  state.function '_rvim_api_win_set_cursor' do |_winid, pos|
    arr = if pos.respond_to?(:to_h)
            pos.to_h.values
          elsif pos.is_a?(Array)
            pos
          else
            []
          end
    row = arr[0].to_i - 1
    col = arr[1].to_i
    editor.instance_variable_set(:@line_index, [[row, 0].max, [editor.buffer_of_lines.size - 1, 0].max].min)
    editor.instance_variable_set(:@byte_pointer, [col, 0].max)
  end

  state.function('_rvim_api_win_get_height') { |winid| (resolve.call(winid)&.height) || 24 }
  state.function('_rvim_api_win_set_height') { |winid, h| w = resolve.call(winid); w.height = h.to_i if w }
  state.function('_rvim_api_win_get_width')  { |winid| (resolve.call(winid)&.width) || 80 }
  state.function('_rvim_api_win_get_buf')    { |winid| resolve.call(winid)&.buffer&.id || 0 }
  state.function('_rvim_api_get_current_win') { (editor.windows || []).index(editor.current_window).to_i + 1 }
end

.resolve_buffer(editor, bufnr) ⇒ Object



307
308
309
310
311
312
# File 'lib/rvim/lua/api.rb', line 307

def self.resolve_buffer(editor, bufnr)
  n = bufnr.to_i
  return editor.current_buffer if n.zero?

  editor.buffers&.values&.find { |b| b.id == n }
end

.resolve_window(editor, winid) ⇒ Object



239
240
241
242
243
244
245
# File 'lib/rvim/lua/api.rb', line 239

def self.resolve_window(editor, winid)
  n = winid.to_i
  return editor.current_window if n.zero?

  idx = n - 1
  (editor.windows || [])[idx] || editor.current_window
end