sqry-lang-lua 8.0.2

Lua language plugin for sqry
Documentation
1
2
3
4
5
6
7
8
9
10
11
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
125
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
209
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
238
239
240
241
242
243
244
245
246
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
-- ============================================================================
-- Neovim Plugin: CodeNavigator
-- A realistic Neovim plugin demonstrating common Lua patterns
-- ============================================================================
--
-- GROUND TRUTH ANNOTATIONS:
--
-- EXPORTS (18 total):
-- 1. M::setup (function, line 45)
-- 2. M::navigate_to_definition (function, line 97)
-- 3. M::find_references (function, line 132)
-- 4. M::show_hover (function, line 167)
-- 5. M::list_diagnostics (function, line 198)
-- 6. M::format_buffer (function, line 229)
-- 7. Config::new (method, colon, line 263)
-- 8. Config::merge (method, colon, line 278)
-- 9. Config::validate (method, colon, line 294)
-- 10. Config::get (method, colon, line 316)
-- 11. Buffer::new (method, colon, line 342)
-- 12. Buffer::get_content (method, colon, line 356)
-- 13. Buffer::set_content (method, colon, line 368)
-- 14. Buffer::save (method, colon, line 381)
-- 15. Window::new (method, colon, line 407)
-- 16. Window::focus (method, colon, line 421)
-- 17. Window::close (method, colon, line 433)
-- 18. Window::resize (method, colon, line 445)
--
-- CALLS (42 total):
-- 1. vim.api.nvim_create_user_command (line 72)
-- 2. vim.api.nvim_set_keymap (line 73)
-- 3. vim.api.nvim_set_keymap (line 74)
-- 4. vim.api.nvim_set_keymap (line 75)
-- 5. vim.api.nvim_create_augroup (line 79)
-- 6. vim.api.nvim_create_autocmd (line 80)
-- 7. config:validate (line 86, colon)
-- 8. vim.notify (line 90)
-- 9. Config:new (line 104, colon)
-- 10. config:merge (line 105, colon)
-- 11. vim.lsp.buf.definition (line 109)
-- 12. vim.api.nvim_win_set_cursor (line 113)
-- 13. Buffer:new (line 116, colon)
-- 14. buffer:get_content (line 117, colon)
-- 15. vim.notify (line 120)
-- 16. vim.lsp.util.show_line_diagnostics (line 125)
-- 17. Config:new (line 139, colon)
-- 18. config:merge (line 140, colon)
-- 19. vim.lsp.buf.references (line 144)
-- 20. Window:new (line 147, colon)
-- 21. window:focus (line 148, colon)
-- 22. vim.api.nvim_buf_set_lines (line 152)
-- 23. window:resize (line 156)
-- 24. vim.notify (line 160)
-- 25. Config:new (line 174, colon)
-- 26. config:merge (line 175, colon)
-- 27. vim.lsp.buf.hover (line 179)
-- 28. Window:new (line 182, colon)
-- 29. window:focus (line 183, colon)
-- 30. window:close (line 187)
-- 31. vim.notify (line 191)
-- 32. vim.diagnostic.get (line 205)
-- 33. Window:new (line 209, colon)
-- 34. window:focus (line 210, colon)
-- 35. vim.api.nvim_buf_set_lines (line 214)
-- 36. window:resize (line 218)
-- 37. vim.notify (line 222)
-- 38. Buffer:new (line 236, colon)
-- 39. buffer:get_content (line 237, colon)
-- 40. vim.lsp.buf.format (line 241)
-- 41. buffer:set_content (line 245)
-- 42. buffer:save (line 246)
--
-- ============================================================================

local M = {}

-- Module-level configuration
function M.setup(opts)
  opts = opts or {}

  -- Default configuration
  local config = {
    enable_diagnostics = true,
    enable_hover = true,
    enable_references = true,
    keymaps = {
      goto_definition = '<leader>gd',
      find_references = '<leader>gr',
      show_hover = '<leader>K',
    },
    window = {
      width = 80,
      height = 20,
      border = 'rounded',
    },
  }

  -- Merge user options
  config = vim.tbl_deep_extend('force', config, opts)

  -- Store configuration globally
  vim.g.code_navigator_config = config

  -- Register user commands
  vim.api.nvim_create_user_command('CodeNavDefinition', M.navigate_to_definition, {})
  vim.api.nvim_set_keymap('n', config.keymaps.goto_definition, '<cmd>CodeNavDefinition<CR>', { noremap = true, silent = true })
  vim.api.nvim_set_keymap('n', config.keymaps.find_references, '<cmd>lua require("code_navigator").find_references()<CR>', { noremap = true, silent = true })
  vim.api.nvim_set_keymap('n', config.keymaps.show_hover, '<cmd>lua require("code_navigator").show_hover()<CR>', { noremap = true, silent = true })

  -- Setup autocmds for diagnostics
  local group = vim.api.nvim_create_augroup('CodeNavigator', { clear = true })
  vim.api.nvim_create_autocmd('DiagnosticChanged', {
    group = group,
    callback = function()
      if config.enable_diagnostics then
        -- Update diagnostic display
        config:validate()
      end
    end,
  })

  -- Notify user
  vim.notify('CodeNavigator: Setup complete', vim.log.levels.INFO)
end

-- Navigate to definition of symbol under cursor
function M.navigate_to_definition()
  local bufnr = vim.api.nvim_get_current_buf()
  local cursor = vim.api.nvim_win_get_cursor(0)
  local row, col = cursor[1], cursor[2]

  -- Get configuration
  local config = Config:new(vim.g.code_navigator_config)
  config:merge({ buffer = bufnr, position = { row, col } })

  -- Request definition from LSP
  vim.lsp.buf.definition({
    on_list = function(items)
      if #items > 0 then
        vim.api.nvim_win_set_cursor(0, { items[1].lnum, items[1].col - 1 })

        -- Log to buffer
        local buffer = Buffer:new(bufnr)
        local content = buffer:get_content()

        -- Notify success
        vim.notify('Navigated to definition', vim.log.levels.INFO)

        -- Show diagnostics for current line
        vim.lsp.util.show_line_diagnostics()
      end
    end,
  })
end

-- Find all references to symbol under cursor
function M.find_references()
  local bufnr = vim.api.nvim_get_current_buf()
  local cursor = vim.api.nvim_win_get_cursor(0)
  local row, col = cursor[1], cursor[2]

  -- Get configuration
  local config = Config:new(vim.g.code_navigator_config)
  config:merge({ buffer = bufnr, position = { row, col } })

  -- Request references from LSP
  vim.lsp.buf.references(nil, {
    on_list = function(items)
      local win = Window:new(vim.g.code_navigator_config.window)
      win:focus()

      -- Display references in floating window
      local lines = {}
      vim.api.nvim_buf_set_lines(win.bufnr, 0, -1, false, lines)

      -- Resize window based on content
      win:resize(#lines)

      -- Notify user
      vim.notify(string.format('Found %d references', #items), vim.log.levels.INFO)
    end,
  })
end

-- Show hover information for symbol under cursor
function M.show_hover()
  local bufnr = vim.api.nvim_get_current_buf()
  local cursor = vim.api.nvim_win_get_cursor(0)
  local row, col = cursor[1], cursor[2]

  -- Get configuration
  local config = Config:new(vim.g.code_navigator_config)
  config:merge({ buffer = bufnr, position = { row, col } })

  -- Request hover from LSP
  vim.lsp.buf.hover({
    on_list = function(items)
      local win = Window:new(vim.g.code_navigator_config.window)
      win:focus()

      -- Close after timeout
      vim.defer_fn(function()
        win:close()
      end, 3000)

      -- Notify user
      vim.notify('Showing hover information', vim.log.levels.INFO)
    end,
  })
end

-- List all diagnostics in current buffer
function M.list_diagnostics()
  local bufnr = vim.api.nvim_get_current_buf()

  -- Get diagnostics
  local diagnostics = vim.diagnostic.get(bufnr)

  if #diagnostics > 0 then
    -- Create window for diagnostics
    local win = Window:new(vim.g.code_navigator_config.window)
    win:focus()

    -- Format diagnostics
    local lines = {}
    vim.api.nvim_buf_set_lines(win.bufnr, 0, -1, false, lines)

    -- Resize window
    win:resize(#lines)

    -- Notify user
    vim.notify(string.format('Found %d diagnostics', #diagnostics), vim.log.levels.INFO)
  end
end

-- Format current buffer
function M.format_buffer()
  local bufnr = vim.api.nvim_get_current_buf()

  -- Get current content
  local buffer = Buffer:new(bufnr)
  local content = buffer:get_content()

  -- Format using LSP
  vim.lsp.buf.format({
    bufnr = bufnr,
    async = false,
  })

  -- Update content
  buffer:set_content(content)
  buffer:save()

  -- Notify user
  vim.notify('Buffer formatted', vim.log.levels.INFO)
end

-- ============================================================================
-- Configuration Class
-- ============================================================================

Config = {}
Config.__index = Config

-- Constructor for Config
function Config:new(opts)
  opts = opts or {}
  local instance = {
    data = opts,
    valid = false,
  }
  setmetatable(instance, Config)
  return instance
end

-- Merge configuration with new options
function Config:merge(new_opts)
  if not new_opts then
    return self
  end

  self.data = vim.tbl_deep_extend('force', self.data, new_opts)
  self.valid = false

  return self
end

-- Validate configuration
function Config:validate()
  if self.valid then
    return true
  end

  -- Validation logic
  local required_keys = { 'enable_diagnostics', 'enable_hover', 'enable_references' }
  for _, key in ipairs(required_keys) do
    if self.data[key] == nil then
      vim.notify(string.format('Missing required config key: %s', key), vim.log.levels.ERROR)
      return false
    end
  end

  self.valid = true
  return true
end

-- Get configuration value
function Config:get(key)
  return self.data[key]
end

-- ============================================================================
-- Buffer Class
-- ============================================================================

Buffer = {}
Buffer.__index = Buffer

-- Constructor for Buffer
function Buffer:new(bufnr)
  bufnr = bufnr or vim.api.nvim_get_current_buf()
  local instance = {
    bufnr = bufnr,
    name = vim.api.nvim_buf_get_name(bufnr),
  }
  setmetatable(instance, Buffer)
  return instance
end

-- Get buffer content
function Buffer:get_content()
  local lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false)
  return table.concat(lines, '\n')
end

-- Set buffer content
function Buffer:set_content(content)
  local lines = vim.split(content, '\n')
  vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, lines)
  return self
end

-- Save buffer
function Buffer:save()
  vim.api.nvim_buf_call(self.bufnr, function()
    vim.cmd('write')
  end)
  return self
end

-- ============================================================================
-- Window Class
-- ============================================================================

Window = {}
Window.__index = Window

-- Constructor for Window
function Window:new(opts)
  opts = opts or {}
  local bufnr = vim.api.nvim_create_buf(false, true)
  local width = opts.width or 80
  local height = opts.height or 20
  local instance = {
    bufnr = bufnr,
    width = width,
    height = height,
  }
  setmetatable(instance, Window)
  return instance
end

-- Focus window
function Window:focus()
  vim.api.nvim_set_current_buf(self.bufnr)
  return self
end

-- Close window
function Window:close()
  vim.api.nvim_buf_delete(self.bufnr, { force = true })
  return self
end

-- Resize window
function Window:resize(new_height)
  if new_height then
    self.height = new_height
  end
  return self
end

-- ============================================================================
-- Utility Functions (internal, not exported)
-- ============================================================================

local function log_debug(message)
  if vim.g.code_navigator_config and vim.g.code_navigator_config.debug then
    vim.notify(message, vim.log.levels.DEBUG)
  end
end

local function get_cursor_word()
  return vim.fn.expand('<cword>')
end

local function is_lsp_available()
  return #vim.lsp.get_active_clients() > 0
end

-- Callback handlers
local function on_definition_found(items)
  log_debug('Definition found')
  return items[1]
end

local function on_references_found(items)
  log_debug(string.format('Found %d references', #items))
  return items
end

local function on_hover_shown(result)
  log_debug('Hover information displayed')
  return result
end

-- Chained method calls for testing
local function chain_example()
  local config = Config:new({ debug = true })
    :merge({ enable_diagnostics = false })
    :validate()

  local buffer = Buffer:new()
    :get_content()

  local window = Window:new({ width = 100, height = 30 })
    :focus()
    :resize(40)
    :close()
end

-- Bracket-style calls (common in event handlers)
local handlers = {
  definition = M.navigate_to_definition,
  references = M.find_references,
  hover = M.show_hover,
  diagnostics = M.list_diagnostics,
  format = M.format_buffer,
}

local function dispatch_handler(handler_name)
  if handlers[handler_name] then
    handlers[handler_name]()
  end
end

-- Higher-order function example
local function with_lsp_client(callback)
  if is_lsp_available() then
    callback()
  else
    vim.notify('No LSP client available', vim.log.levels.WARN)
  end
end

-- Usage example
local function safe_navigate()
  with_lsp_client(function()
    M.navigate_to_definition()
  end)
end

-- Anonymous function in table
local autocmd_callbacks = {
  on_save = function()
    M.format_buffer()
  end,
  on_change = function()
    M.list_diagnostics()
  end,
}

-- Conditional method calls
local function conditional_example(should_format)
  local buffer = Buffer:new()

  if should_format then
    M.format_buffer()
  else
    buffer:get_content()
  end
end

-- Module pattern with metatable
local MetaModule = {}
MetaModule.__index = MetaModule

function MetaModule:create()
  local instance = {}
  setmetatable(instance, MetaModule)
  return instance
end

function MetaModule:process()
  Config:new():validate()
end

-- Varargs and dynamic calls
local function call_method(obj, method_name, ...)
  return obj[method_name](obj, ...)
end

-- Callback registration pattern
local registered_callbacks = {}

local function register_callback(name, callback)
  registered_callbacks[name] = callback
end

local function trigger_callback(name, ...)
  if registered_callbacks[name] then
    registered_callbacks[name](...)
  end
end

-- Setup default callbacks
register_callback('format', M.format_buffer)
register_callback('diagnostics', M.list_diagnostics)

-- Table constructor with method references
local actions = {
  gd = M.navigate_to_definition,
  gr = M.find_references,
  K = M.show_hover,

  -- Nested table
  advanced = {
    format = M.format_buffer,
    diagnostics = M.list_diagnostics,
  },
}

-- Execute action by key
local function execute_action(key)
  if actions[key] then
    actions[key]()
  end
end

-- Complex chaining with conditional branches
local function complex_chain()
  local config = Config:new()

  if config:validate() then
    config:merge({ debug = true })
  end

  return config:get('debug')
end

return M