neovim

Personal neovim configuration files
git clone git://gtms.dev/neovim
Log | Files | Refs

commit bf69328290c0f4e29cac75e76da5ffca98d5c796
parent 6849dd6e38d82927bdf0dbdc8d20f130ac966e02
Author: Tomas Nemec <owl@gtms.dev>
Date:   Thu,  9 May 2024 13:01:10 +0200

update

Diffstat:
Mafter/plugin/lsp.lua | 66+++++++++++++++++++++++++++++++++++++++---------------------------
Mlua/tms/lsp/references.lua | 157+++++++++++++++++++++++++++++++++----------------------------------------------
Mlua/tms/quickfix.lua | 2++
3 files changed, 106 insertions(+), 119 deletions(-)

diff --git a/after/plugin/lsp.lua b/after/plugin/lsp.lua @@ -5,7 +5,7 @@ end local lsp_group = vim.api.nvim_create_augroup('user-lsp', {}) local function keymap(client, buf) - local function opt(desc) + local function opts(desc) return { buffer = buf, desc = desc } end @@ -14,61 +14,73 @@ local function keymap(client, buf) local prefix = 'gl' if client.supports_method('textDocument/definition') then - vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opt 'LSP Definition') - vim.keymap.set('n', prefix .. 'd', vim.lsp.buf.definition, opt 'LSP Definition') + vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts 'LSP Definition') + vim.keymap.set('n', prefix .. 'd', vim.lsp.buf.definition, opts 'LSP Definition') end if client.supports_method('textDocument/typeDefinition') then - vim.keymap.set('n', prefix .. 't', vim.lsp.buf.type_definition, opt 'LSP Type Definition') + vim.keymap.set('n', prefix .. 't', vim.lsp.buf.type_definition, opts 'LSP Type Definition') end if client.supports_method('callHierarchy/incomingCalls') then - vim.keymap.set('n', prefix .. 'i', vim.lsp.buf.incoming_calls, opt 'LSP Incoming calls') - vim.keymap.set('n', ']e', vim.lsp.buf.incoming_calls, opt 'LSP Incoming calls') + vim.keymap.set('n', prefix .. 'i', vim.lsp.buf.incoming_calls, opts 'LSP Incoming calls') + vim.keymap.set('n', ']e', vim.lsp.buf.incoming_calls, opts 'LSP Incoming calls') end if client.supports_method('callHierarchy/outgoingCalls') then - vim.keymap.set('n', prefix .. 'o', vim.lsp.buf.outgoing_calls, opt 'LSP Outgoing calls') - vim.keymap.set('n', '[e', vim.lsp.buf.outgoing_calls, opt 'LSP Outgoing calls') + vim.keymap.set('n', prefix .. 'o', vim.lsp.buf.outgoing_calls, opts 'LSP Outgoing calls') + vim.keymap.set('n', '[e', vim.lsp.buf.outgoing_calls, opts 'LSP Outgoing calls') end if client.supports_method('textDocument/implementation') then - vim.keymap.set('n', prefix .. 'm', vim.lsp.buf.implementation, opt 'LSP Implementation') + vim.keymap.set('n', prefix .. 'm', vim.lsp.buf.implementation, opts 'LSP Implementation') vim.keymap.set('i', '<c-r><c-m>', function() vim.cmd.split() vim.lsp.buf.implementation() - end, opt 'LSP Implementation (Split)') + end, opts 'LSP Implementation (Split)') end if client.supports_method('textDocument/signatureHelp') then - vim.keymap.set({ 'n', 'i' }, '<c-s>', vim.lsp.buf.signature_help, opt 'LSP Signature Help') + vim.keymap.set({ 'n', 'i' }, '<c-s>', vim.lsp.buf.signature_help, opts 'LSP Signature Help') end if client.supports_method('textDocument/references') then local rhs = { - all = function() require 'tms.lsp.references'.all { includeDeclaration = false } end, + all = function() vim.lsp.buf.references { includeDeclaration = false } end, + first_in_file = function() + vim.lsp.buf.references(nil, + { on_list = require 'tms.lsp.references'.first_in_file }) + end, curr_file = function() - require 'tms.lsp.references'.all { includeDeclaration = false, loclist = true, curr_file = true } + vim.lsp.buf.references(nil, + { on_list = require 'tms.lsp.references'.curr_file }) end, after_line = function() - require 'tms.lsp.references'.all { includeDeclaration = false, loclist = true, after_line = true } + vim.lsp.buf.references(nil, + { on_list = require 'tms.lsp.references'.after_line }) + end, + next = function() + vim.lsp.buf.references(nil, { on_list = require 'tms.lsp.references'.next }) end, + prev = function() + vim.lsp.buf.references(nil, { on_list = require 'tms.lsp.references'.prev }) + end + } - vim.keymap.set('n', prefix .. 'f', rhs.all, opt 'LSP References') - vim.keymap.set('n', '[<c-i>', rhs.all, opt 'LSP References') - vim.keymap.set('n', '[I', rhs.curr_file, opt 'LSP References in Current File') - vim.keymap.set('n', ']I', rhs.after_line, opt 'LSP References Below') - vim.keymap.set('n', '[i', require 'tms.lsp.references'.prev, opt 'LSP Previous Reference') - vim.keymap.set('n', ']i', require 'tms.lsp.references'.next, opt 'LSP Next Reference') + vim.keymap.set('n', '[<c-r>', rhs.all, opts 'All References') + vim.keymap.set('n', '[R', rhs.first_in_file, opts 'First References in File') + vim.keymap.set('n', ']R', rhs.curr_file, opts 'Local References') + vim.keymap.set('n', '[r', rhs.prev, opts 'Previous Reference') + vim.keymap.set('n', ']r', rhs.next, opts 'Next Reference') end if client.supports_method('textDocument/codeAction') then - vim.keymap.set({ 'n', 'v' }, prefix .. 'a', vim.lsp.buf.code_action, opt 'LSP Code Actions') + vim.keymap.set({ 'n', 'v' }, prefix .. 'a', vim.lsp.buf.code_action, opts 'LSP Code Actions') end if client.supports_method('textDocument/documentHighlight') then - vim.keymap.set('n', '<leader>k', vim.lsp.buf.document_highlight, opt 'LSP Highlight') + vim.keymap.set('n', '<leader>k', vim.lsp.buf.document_highlight, opts 'LSP Highlight') vim.api.nvim_create_autocmd('CursorMoved', { group = lsp_group, buffer = buf, callback = vim.lsp.buf.clear_references }) end @@ -76,24 +88,24 @@ local function keymap(client, buf) if client.supports_method('textDocument/inlayHint') then vim.keymap.set('n', prefix .. 'h', function() vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled()) - end, opt 'LSP Inlay hints') + end, opts 'LSP Inlay hints') end if client.supports_method('textDocument/rename') then - vim.keymap.set('n', 'cd', vim.lsp.buf.rename, opt 'LSP Rename (Change Definition)') - vim.keymap.set('n', prefix .. 'n', vim.lsp.buf.rename, opt 'LSP Rename') + vim.keymap.set('n', 'cd', vim.lsp.buf.rename, opts 'LSP Rename (Change Definition)') + vim.keymap.set('n', prefix .. 'n', vim.lsp.buf.rename, opts 'LSP Rename') end if client.supports_method('typeHierarchy/supertypes') then vim.keymap.set('n', prefix .. 'p', function() vim.lsp.buf.typehierarchy('supertypes') - end, opt 'LSP Supertypes (Parents)') + end, opts 'LSP Supertypes (Parents)') end if client.supports_method('typeHierarchy/subtypes') then vim.keymap.set('n', prefix .. 'c', function() vim.lsp.buf.typehierarchy('subtypes') - end, opt 'LSP Subtypes (Children)') + end, opts 'LSP Subtypes (Children)') end end diff --git a/lua/tms/lsp/references.lua b/lua/tms/lsp/references.lua @@ -1,120 +1,93 @@ local M = {} -local function set_list(opts) - opts = opts or {} - - return function(o) - if opts.loclist then - vim.fn.setloclist(0, {}, ' ', { title = o.title, items = o.items }) - vim.api.nvim_command('botright lopen') - else - vim.fn.setqflist({}, ' ', { title = o.title, items = o.items }) - vim.api.nvim_command('botright copen') - end - end +local function filter_curr_file(bufnr, items) + local curr_filename = vim.api.nvim_buf_get_name(bufnr) + return vim.tbl_filter(function(item) + return item.filename == curr_filename + end, items) end -local function after_line_list(opts) - opts = opts or {} - - return function(o) - local curr_filename = vim.api.nvim_buf_get_name(o.context.bufnr) - local curr_lnum = vim.fn.getcurpos()[2] - o.items = vim.tbl_filter(function(item) - return item.filename == curr_filename and item.lnum > curr_lnum - end, o.items) - - if #o.items == 0 then - vim.notify('No references after current line') - return - end - - o.title = 'References (After Current Line)' - set_list(opts)(o) - end +local function filter_before_line(bufnr, items) + local curr_filename = vim.api.nvim_buf_get_name(bufnr) + local curr_lnum = vim.fn.getcurpos()[2] + return vim.tbl_filter(function(item) + return item.filename == curr_filename and item.lnum < curr_lnum + end, items) end -local function curr_file_list(opts) - opts = opts or {} - - return function(o) - local curr_filename = vim.api.nvim_buf_get_name(o.context.bufnr) - o.items = vim.tbl_filter(function(item) - return item.filename == curr_filename - end, o.items) +local function filter_after_line(bufnr, items) + local curr_filename = vim.api.nvim_buf_get_name(bufnr) + local curr_lnum = vim.fn.getcurpos()[2] + return vim.tbl_filter(function(item) + return item.filename == curr_filename and item.lnum > curr_lnum + end, items) +end - if #o.items == 0 then - vim.notify('No references in current file') - return - end +function M.after_line(opts) + opts.items = filter_after_line(opts.context.bufnr, opts.items) - o.title = 'References (Current File)' - set_list(opts)(o) + if #opts.items == 0 then + vim.notify('No references after current line') + return end -end -local function prev(opts) - opts = opts or {} - return function(o) - local curr_filename = vim.api.nvim_buf_get_name(o.context.bufnr) - local curr_lnum = vim.fn.getcurpos()[2] - - o.items = vim.tbl_filter(function(item) - return item.filename == curr_filename and item.lnum < curr_lnum - end, o.items) + opts.title = 'References (After Current Line)' + vim.fn.setloclist(0, {}, ' ', opts) + vim.cmd.lopen() +end - if #o.items == 0 then - vim.notify('No Previous References') - return - end +function M.curr_file(opts) + opts.items = filter_curr_file(opts.context.bufnr, opts.items) - local client = assert(vim.lsp.get_client_by_id(o.context.client_id)) - vim.lsp.util.jump_to_location(o.items[#o.items].user_data, client.offset_encoding) + if #opts.items == 0 then + vim.notify('No references in current file') + return end + + opts.title = 'Local References' + vim.fn.setloclist(0, {}, ' ', opts) + vim.cmd.lopen() end -local function next(opts) - opts = opts or {} - return function(o) - local curr_filename = vim.api.nvim_buf_get_name(o.context.bufnr) - local curr_lnum = vim.fn.getcurpos()[2] - - vim.print(curr_lnum) - vim.print(o) - o.items = vim.tbl_filter(function(item) - return item.filename == curr_filename and item.lnum > curr_lnum - end, o.items) - vim.print(o) - - if #o.items == 0 then - vim.notify('No Following References') - return - end - - local client = assert(vim.lsp.get_client_by_id(o.context.client_id)) - vim.lsp.util.jump_to_location(o.items[1].user_data, client.offset_encoding) +function M.first_in_file(opts) + opts.items = filter_curr_file(opts.context.bufnr, opts.items) + + if #opts.items == 0 then + vim.notify('No References') + return end -end + local client = assert(vim.lsp.get_client_by_id(opts.context.client_id)) + vim.lsp.util.jump_to_location(opts.items[1].user_data, client.offset_encoding) -function M.prev(opts) - vim.lsp.buf.references(opts, { on_list = prev(opts) }) + opts.title = 'Local References' + vim.fn.setloclist(0, {}, ' ', opts) + vim.cmd.lopen() + vim.cmd [[wincmd p]] end function M.next(opts) - vim.lsp.buf.references(opts, { on_list = next(opts) }) + opts.items = filter_after_line(opts.context.bufnr, opts.items) + + if #opts.items == 0 then + vim.notify('No Following References') + return + end + + local client = assert(vim.lsp.get_client_by_id(opts.context.client_id)) + vim.lsp.util.jump_to_location(opts.items[1].user_data, client.offset_encoding) end -function M.all(opts) - opts = opts or {} +function M.prev(opts) + opts.items = filter_before_line(opts.context.bufnr, opts.items) - if opts.curr_file then - vim.lsp.buf.references(opts, { on_list = curr_file_list(opts) }) - elseif opts.after_line then - vim.lsp.buf.references(opts, { on_list = after_line_list(opts) }) - else - vim.lsp.buf.references(opts, { on_list = set_list(opts) }) + if #opts.items == 0 then + vim.notify('No Previous References') + return end + + local client = assert(vim.lsp.get_client_by_id(opts.context.client_id)) + vim.lsp.util.jump_to_location(opts.items[#opts.items].user_data, client.offset_encoding) end return M diff --git a/lua/tms/quickfix.lua b/lua/tms/quickfix.lua @@ -2,10 +2,12 @@ local M = {} function M.clear_loclist() vim.fn.setloclist(0, {}, 'r') + vim.cmd [[silent! lclose]] end function M.clear_qflist() vim.fn.setqflist({}, 'r') + vim.cmd [[silent! cclose]] end function M.toggle_loclist()