Refactor LSP and Telescope configuration

This commit is contained in:
Oliver Davies 2024-07-06 15:23:28 +01:00
parent 38fc21d38a
commit d16cfae1b1
7 changed files with 69 additions and 443 deletions

View file

@ -1,14 +1,5 @@
pcall("require", impatient)
require "opdavies.globals"
require "opdavies.options"
require "opdavies.lsp"
require "opdavies.telescope.setup"
require "opdavies.telescope.mappings"
vim.api.nvim_create_user_command("GoToFile", function()
require("opdavies.telescope").git_files()
end, {})

View file

@ -1,23 +0,0 @@
local M = {}
M.imap = function(tbl)
vim.keymap.set("i", tbl[1], tbl[2], tbl[3])
end
M.map = function(tbl)
vim.keymap.set(tbl[1], tbl[2], tbl[3], tbl[4] or {})
end
M.nmap = function(tbl)
vim.keymap.set("n", tbl[1], tbl[2], tbl[3])
end
M.vmap = function(tbl)
vim.keymap.set("v", tbl[1], tbl[2], tbl[3])
end
M.xmap = function(tbl)
vim.keymap.set("x", tbl[1], tbl[2], tbl[3])
end
return M

View file

@ -1,66 +1,6 @@
local has_lsp, lspconfig = pcall(require, "lspconfig")
if not has_lsp then
return
end
local lspconfig = require "lspconfig"
local nvim_status = require "lsp-status"
local imap = require("opdavies.keymap").imap
local nmap = require("opdavies.keymap").nmap
local telescope_mapper = require "opdavies.telescope.mappings"
local buf_nnoremap = function(opts)
opts.buffer = 0
nmap(opts)
end
local buf_inoremap = function(opts)
opts.buffer = 0
imap(opts)
end
local custom_attach = function(client)
local filetype = vim.api.nvim_buf_get_option(0, "filetype")
nvim_status.on_attach(client)
buf_inoremap { "<C-k>", vim.lsp.buf.signature_help }
buf_nnoremap { "<leader>ca", vim.lsp.buf.code_action }
buf_nnoremap { "<leader>d", vim.diagnostic.open_float }
buf_nnoremap { "<leader>rn", vim.lsp.buf.rename }
buf_nnoremap { "<leader>rr", "<cmd>LspRestart<cr>" }
buf_nnoremap { "[d", vim.diagnostic.goto_prev }
buf_nnoremap { "]d", vim.diagnostic.goto_next }
local handlers = require "opdavies.lsp.handlers"
buf_nnoremap { "gD", vim.lsp.buf.declaration }
buf_nnoremap { "gd", handlers.definition }
buf_nnoremap { "gi", vim.lsp.buf.implementation }
buf_nnoremap { "gT", vim.lsp.buf.type_definition }
if filetype ~= "lua" then
buf_nnoremap { "K", vim.lsp.buf.hover }
end
telescope_mapper("<leader>dl", "diagnostics", nil, true)
-- Set autocommands conditional on server_capabilities
if client.server_capabilities.document_highlight then
vim.cmd [[
augroup lsp_document_highlight
autocmd! * <buffer>
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
augroup END
]]
end
-- Attach any filetype specific options to the client
-- filetype_attach[filetype](client)
end
require("neodev").setup {}
local servers = {
@ -128,12 +68,11 @@ for server_name, config in pairs(servers) do
config = {}
end
lspconfig[server_name].setup {
config = vim.tbl_deep_extend("force", {}, {
capabilities = capabilities,
filetypes = config.filetypes,
on_attach = custom_attach,
settings = config.settings,
}
}, config)
lspconfig[server_name].setup(config)
end
vim.diagnostic.config {
@ -143,3 +82,31 @@ vim.diagnostic.config {
update_in_insert = false,
virtual_text = { spacing = 2 },
}
vim.api.nvim_create_autocmd("LspAttach", {
callback = function()
local builtin = require "telescope.builtin"
-- buf_inoremap { "<C-k>", vim.lsp.buf.signature_help }
-- buf_nnoremap { "<leader>ca", vim.lsp.buf.code_action }
-- buf_nnoremap { "<leader>d", vim.diagnostic.open_float }
-- buf_nnoremap { "<leader>rn", vim.lsp.buf.rename }
-- buf_nnoremap { "<leader>rr", "<cmd>LspRestart<cr>" }
-- buf_nnoremap { "[d", vim.diagnostic.goto_prev }
-- buf_nnoremap { "]d", vim.diagnostic.goto_next }
-- buf_nnoremap { "gD", vim.lsp.buf.declaration }
-- buf_nnoremap { "gd", handlers.definition }
-- buf_nnoremap { "gi", vim.lsp.buf.implementation }
-- buf_nnoremap { "gT", vim.lsp.buf.type_definition }
vim.keymap.set("n", "gd", builtin.lsp_definitions, { buffer = 0 })
vim.keymap.set("n", "gr", builtin.lsp_references, { buffer = 0 })
vim.keymap.set("n", "gD", vim.lsp.buf.declaration, { buffer = 0 })
vim.keymap.set("n", "gT", vim.lsp.buf.type_definition, { buffer = 0 })
vim.keymap.set("n", "K", vim.lsp.buf.hover, { buffer = 0 })
vim.keymap.set("n", "<space>cr", vim.lsp.buf.rename, { buffer = 0 })
vim.keymap.set("n", "<space>ca", vim.lsp.buf.code_action, { buffer = 0 })
end,
})

View file

@ -1,161 +0,0 @@
SHOULD_RELOAD_TELESCOPE = true
local reloader = function()
if SHOULD_RELOAD_TELESCOPE then
RELOAD "plenary"
RELOAD "telescope"
RELOAD "opdavies.telescope.setup"
end
end
local themes = require "telescope.themes"
local M = {}
function M.current_buf()
local opts = {
sorting_strategy = "ascending",
previewer = false,
}
require("telescope.builtin").current_buffer_fuzzy_find(opts)
end
M.diagnostics = function()
local theme = require("telescope.themes").get_dropdown {
previewer = false,
}
require("telescope.builtin").diagnostics(theme)
end
function M.lsp_document_symbols()
local theme = require("telescope.themes").get_dropdown {
previewer = false,
}
require("telescope.builtin").lsp_document_symbols(theme)
end
function M.edit_neovim()
local opts = {
cwd = "~/Code/github.com/opdavies/dotfiles/config/neovim",
find_command = { "rg", "--no-ignore", "--files", "--follow" },
path_display = { "shorten" },
prompt_title = "~ dotfiles ~",
no_ignore = true,
layout_strategy = "flex",
layout_config = {
height = 0.8,
prompt_position = "top",
width = 0.9,
horizontal = {
width = { padding = 0.15 },
},
vertical = {
preview_height = 0.75,
},
},
}
require("telescope.builtin").find_files(opts)
end
function M.edit_zsh()
local opts = {
cwd = "~/.config/zsh",
path_display = { "shorten" },
prompt_title = "~ zsh ~",
no_ignore = true,
layout_strategy = "flex",
layout_config = {
height = 0.8,
prompt_position = "top",
width = 0.9,
horizontal = {
width = { padding = 0.15 },
},
vertical = {
preview_height = 0.75,
},
},
}
require("telescope.builtin").find_files(opts)
end
function M.find_all_files()
local opts = {
file_ignore_patterns = { ".direnv", ".git" },
no_ignore = true,
prompt_title = "All Files",
}
require("telescope.builtin").find_files(opts)
end
function M.git_files()
local opts = {
hidden = true,
no_ignore = true,
}
require("telescope.builtin").git_files(opts)
end
function M.grep_prompt()
require("telescope.builtin").grep_string {
path_display = { "shorten" },
search = vim.fn.input "Grep String > ",
}
end
function M.live_grep()
require("telescope").extensions.live_grep_args.live_grep_args {
file_ignore_patterns = { ".git/" },
hidden = true,
no_ignore = true,
sorting_strategy = "ascending",
}
end
function M.oldfiles()
local opts = {
prompt_title = "History",
}
require("telescope").extensions.frecency.frecency(opts)
end
function M.search_todos()
local opts = {
search = "TODO",
search_dirs = {
"app",
"docroot/modules/custom",
"docroot/themes/custom",
"src",
"web/modules/custom",
"web/themes/custom",
},
prompt_title = "TODOs",
}
require("telescope.builtin").grep_string(opts)
end
return setmetatable({}, {
__index = function(_, k)
reloader()
if M[k] then
return M[k]
else
return require("telescope.builtin")[k]
end
end,
})

View file

@ -1,76 +0,0 @@
TelescopeMapArgs = TelescopeMapArgs or {}
local telescope_mapper = function(key, f, options, buffer)
local map_key = vim.api.nvim_replace_termcodes(key .. f, true, true, true)
TelescopeMapArgs[map_key] = options or {}
local mode = "n"
local rhs = string.format("<cmd>lua R('opdavies.telescope')['%s'](TelescopeMapArgs['%s'])<CR>", f, map_key)
local map_options = {
noremap = true,
silent = true,
}
if not buffer then
vim.api.nvim_set_keymap(mode, key, rhs, map_options)
else
vim.api.nvim_buf_set_keymap(0, mode, key, rhs, map_options)
end
end
-- Based on https://github.com/nvim-lua/kickstart.nvim
-- TODO: refactor telescope_mapper and add descriptions.
telescope_mapper("<leader>/", "current_buf") -- [/] Fuzzily search in current buffer
telescope_mapper("<leader><leader>", "buffers") -- [ ] Find existing buffers
telescope_mapper("<leader>s.", "oldfiles") -- '[S]earch Recent Files ("." for repeat)
telescope_mapper("<leader>sF", "find_all_files") -- [S]earch all [F]iles
telescope_mapper("<leader>sf", "find_files") -- [S]earch [F]iles
telescope_mapper("<leader>sg", "git_files") -- [S]earch [G]it Files
telescope_mapper("<leader>sh", "help_tags") -- [S]earch [H]elp
telescope_mapper("<leader>sl", "live_grep") -- [S]earch using [L]ive grep
telescope_mapper("<leader>sp", "grep_prompt") -- [S]earch using grep [P]rompt
telescope_mapper("<leader>st", "search_todos") -- [S]earch using grep [P]rompt
local builtin = require "telescope.builtin"
local telescope = require "telescope"
local nmap = require("opdavies.keymap").nmap
nmap {
"<leader>s/",
function()
builtin.live_grep {
grep_open_files = true,
prompt_title = "Live Grep in Open Files",
}
end,
{ desc = "[S]earch [/] in Open Files" },
}
nmap {
"<leader>sw",
function()
local word = vim.fn.expand "<cword>"
builtin.grep_string { search = word }
end,
{ desc = "[S]earch for the current [W]ord" },
}
nmap {
"<leader>sW",
function()
local word = vim.fn.expand "<cWORD>"
builtin.grep_string { search = word }
end,
{ desc = "[S]earch for the current [W]ord object" },
}
telescope_mapper("<leader>ds", "lsp_document_symbols")
telescope_mapper("<leader>dl", "diagnostics")
telescope_mapper("<leader>en", "edit_neovim")
telescope_mapper("<leader>ez", "edit_zsh")
return telescope_mapper

View file

@ -1,108 +0,0 @@
local status_ok, telescope = pcall(require, "telescope")
if not status_ok then
return
end
local Job = require "plenary.job"
local previewers = require "telescope.previewers"
-- Create a new maker that won't preview binary files
-- https://github.com/nvim-telescope/telescope.nvim/wiki/Configuration-Recipes#dont-preview-binaries
local new_maker = function(filepath, bufnr, opts)
filepath = vim.fn.expand(filepath)
Job:new({
command = "file",
args = { "--mime-type", "-b", filepath },
on_exit = function(j)
local mime_type = vim.split(j:result()[1], "/")[1]
if mime_type == "text" then
previewers.buffer_previewer_maker(filepath, bufnr, opts)
else
vim.schedule(function()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { "BINARY" })
end)
end
end,
}):sync()
end
local action_layout = require "telescope.actions.layout"
local actions = require "telescope.actions"
local lga_actions = require "telescope-live-grep-args.actions"
telescope.setup {
defaults = {
buffer_previewer_maker = new_maker,
file_ignore_patterns = {
"%/contrib",
"%/core",
"%/libraries",
".direnv",
".git/",
"composer.json.save",
"composer.lock",
"flake.lock",
"node_modules",
"package-lock.json",
"pnpm-lock.yaml",
"result",
"vendor",
"yarn.lock",
},
layout_config = { prompt_position = "top" },
mappings = {
i = {
["<C-d>"] = actions.delete_buffer,
["<C-h>"] = actions.which_key,
["<M-p>"] = action_layout.toggle_preview,
["<M-m>"] = action_layout.toggle_mirror,
},
},
no_ignore = true,
path_display = { truncate = 1 },
preview = { hide_on_startup = true },
prompt_prefix = "$ ",
sorting_strategy = "ascending",
},
pickers = {
find_files = {
hidden = true,
},
buffers = {
previewer = false,
layout_config = {
width = 80,
},
},
lsp_references = {
previewer = false,
},
},
extensions = {
live_grep_args = {
auto_quoting = true,
mappings = {
i = {
["<C-k>"] = lga_actions.quote_prompt {},
["<C-i>"] = lga_actions.quote_prompt { postfix = " --iglob " },
},
},
},
["ui-select"] = {
require("telescope.themes").get_dropdown {},
},
},
}
telescope.load_extension "fzf"
telescope.load_extension "refactoring"
telescope.load_extension "ui-select"

36
plugin/telescope.lua Normal file
View file

@ -0,0 +1,36 @@
local telescope = require "telescope"
telescope.setup {
defaults = {
layout_config = { prompt_position = "top" },
path_display = { truncate = 1 },
prompt_prefix = "$ ",
sorting_strategy = "ascending",
},
pickers = {
lsp_references = {
previewer = false,
},
},
extensions = {
["ui-select"] = {
require("telescope.themes").get_dropdown {},
},
},
}
telescope.load_extension "fzf"
telescope.load_extension "refactoring"
telescope.load_extension "ui-select"
local builtin = require "telescope.builtin"
vim.keymap.set("n", "<space>fd", builtin.find_files)
vim.keymap.set("n", "<space>ft", builtin.git_files)
vim.keymap.set("n", "<space>fh", builtin.help_tags)
vim.keymap.set("n", "<space>fg", builtin.live_grep)
vim.keymap.set("n", "<space>/", builtin.current_buffer_fuzzy_find)
vim.keymap.set("n", "<space>gw", builtin.grep_string)