re-organised config

This commit is contained in:
Fergus Molloy
2025-06-19 14:31:13 +01:00
parent 309b47ceb5
commit ec9a456c74
18 changed files with 91 additions and 183 deletions

View File

@ -0,0 +1,60 @@
return {
{
'stevearc/conform.nvim',
event = 'LspAttach',
ft = { 'lua', 'elixir', 'go', 'erlang', 'typescriptreact', 'typescript', 'json', 'nix' },
cmd = 'Format',
keys = {
{
'<leader>fm',
function()
require('conform').format { async = true, lsp_format = 'fallback' }
end,
desc = 'Format buffer',
},
},
config = function()
require('conform').setup {
notify_on_error = true,
format_on_save = function(bufnr)
local cwd = vim.fn.getcwd()
if cwd:match '/cashout' or cwd:match '/cbe' then
vim.notify_once('Detected cashout repo, auto format disabled', vim.log.levels.WARN)
return
elseif vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
return { timeout = 500, lsp_fallback = true }
end,
formatters_by_ft = {
lua = { 'stylua' },
go = { 'gofmt', 'goimports' },
typescript = { 'prettier' },
typescriptreact = { 'prettier' },
json = { 'prettier' },
nix = { 'nixfmt' },
},
}
vim.api.nvim_create_user_command('Format', function(args)
require('conform').format { async = true, lsp_format = 'fallback' }
end, { desc = 'Format buffer' })
vim.api.nvim_create_user_command('FormatDisable', function(args)
if args.bang then
-- FormatDisable! will disable formatting just for this buffer
vim.b.disable_autoformat = true
else
vim.g.disable_autoformat = true
end
end, {
desc = 'Disable autoformat-on-save',
bang = true,
})
vim.api.nvim_create_user_command('FormatEnable', function()
vim.b.disable_autoformat = false
vim.g.disable_autoformat = false
end, {
desc = 'Re-enable autoformat-on-save',
})
end,
},
}

View File

@ -0,0 +1,19 @@
return {
enable = function() -- disable lsp if wrong version installed
local enable = true
vim
.system({ 'golangci-lint', '--version' }, { text = true }, function(out)
if out.code ~= 0 then
enable = false
return
end
if out.stdout:match ' 1%.%d+%.%d+' then
enable = false
return
end
end)
:wait()
return enable
end,
}

48
lua/plugins/lsp/init.lua Normal file
View File

@ -0,0 +1,48 @@
return TableConcat(require 'plugins.lsp.conform', {
{
'neovim/nvim-lspconfig',
dependencies = {
'hrsh7th/nvim-cmp',
'saghen/blink.cmp',
{
'SmiteshP/nvim-navbuddy',
dependencies = {
'SmiteshP/nvim-navic',
'MunifTanjim/nui.nvim',
},
opts = { lsp = { auto_attach = true } },
keys = {
{ '<leader>n', '<cmd>Navbuddy<cr>', desc = 'Open Navbuddy' },
},
},
},
ft = { 'lua', 'elixir', 'go', 'erlang', 'typescript', 'typescriptreact', 'rust' },
config = function()
require('plugins.lsp.setup').setup()
end,
},
{
-- `lazydev` configures Lua LSP for your Neovim config, runtime and plugins
-- used for completion, annotations and signatures of Neovim apis
'folke/lazydev.nvim',
ft = 'lua',
opts = {
library = {
-- Load luvit types when the `vim.uv` word is found
{ path = '${3rd}/luv/library', words = { 'vim%.uv' } },
},
},
},
{
'windwp/nvim-ts-autotag',
ft = { 'javascriptreact', 'typescriptreact', 'html' },
opts = {
opts = {
-- Defaults
enable_close = true, -- Auto close tags
enable_rename = true, -- Auto rename pairs of tags
enable_close_on_slash = false, -- Auto close on trailing </
},
},
},
})

View File

@ -0,0 +1,34 @@
return {
on_init = function(client)
if client.workspace_folders then
local path = client.workspace_folders[1].name
if path ~= vim.fn.stdpath 'config' and (vim.uv.fs_stat(path .. '/.luarc.json') or vim.uv.fs_stat(path .. '/.luarc.jsonc')) then
return
end
end
client.config.settings.Lua = vim.tbl_deep_extend('force', client.config.settings.Lua, {
runtime = {
-- Tell the language server which version of Lua you're using
-- (most likely LuaJIT in the case of Neovim)
version = 'LuaJIT',
},
-- Make the server aware of Neovim runtime files
workspace = {
checkThirdParty = false,
library = {
vim.env.VIMRUNTIME,
-- Depending on the usage, you might want to add additional paths here.
-- "${3rd}/luv/library"
-- "${3rd}/busted/library",
},
-- or pull in all of 'runtimepath'. NOTE: this is a lot slower and will cause issues when working on your own configuration (see https://github.com/neovim/nvim-lspconfig/issues/3189)
-- library = vim.api.nvim_get_runtime_file("", true)
},
})
end,
settings = {
Lua = {},
},
filetypes = { 'lua' },
}

View File

@ -0,0 +1,29 @@
return function(client, bufnr)
require('nvim-navbuddy').attach(client, bufnr)
local nmap = function(keys, func, desc)
if desc then
desc = 'LSP: ' .. desc
end
vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
end
nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition')
nmap('gi', vim.lsp.buf.implementation, '[G]oto [I]mplementation')
nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition')
nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[N]ame')
nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction')
nmap('K', vim.lsp.buf.hover, 'Hover docs')
-- Lesser used LSP functionality
nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
nmap('<leader>wl', function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, '[W]orkspace [L]ist Folders')
nmap('<leader>e', vim.diagnostic.open_float, 'Open floating diagnostics')
nmap('<leader>q', vim.diagnostic.setloclist, 'Open diagnostics list')
end

50
lua/plugins/lsp/setup.lua Normal file
View File

@ -0,0 +1,50 @@
local M = {}
function M.setup()
local servers = {
ts_ls = { filetypes = { 'javascript', 'typescript', 'javascriptreact', 'typescriptreact' } },
gopls = { filetypes = { 'go', 'gomod' } },
rust_analyzer = {
filetypes = { 'rust' },
settings = {
['rust-analyzer'] = {
check = {
command = 'clippy',
extraArgs = {
'--',
'-Dclippy::correctness',
'-Wclippy::complexity',
'-Wclippy::pedantic',
'-Wclippy::perf',
},
},
},
},
},
golangci_lint_ls = require 'plugins.lsp.golangci_lint_ls',
erlangls = { cmd = { '/home/fergusmolloy/.local/bin/erlang_ls' }, filetypes = { 'erlang' } },
elixirls = { cmd = { 'elixir-ls' }, filetypes = { 'elixir' } },
lua_ls = require 'plugins.lsp.lua_ls',
}
local on_attach = require 'plugins.lsp.on_attach'
local lspconfig = require 'lspconfig'
for server, config in pairs(servers) do
if config['enable'] then
if not config.enable() then
vim.notify('Disabling language server ' .. server, vim.log.levels.WARN)
goto continue
end
end
config.capabilities = require('blink.cmp').get_lsp_capabilities(config.capabilities)
config.on_attach = require 'plugins.lsp.on_attach'
vim.lsp.enable(server)
vim.lsp.config(server, config)
::continue::
end
end
return M

15
lua/plugins/lsp/utils.lua Normal file
View File

@ -0,0 +1,15 @@
local M = {}
function M.check_ft(server, config)
if config.filetypes == nil or next(config.filetypes) == nil then
return true -- if no filetypes specified then enable
else
config.server = server
for _, ft in ipairs(config.filetypes) do
if vim.bo[0].filetype == ft then
return true
end
end
end
return false
end
return M