nvim: refactor config

This commit is contained in:
Maciej Jur 2024-01-23 22:23:58 +01:00
parent 61578424eb
commit 0caff8f78a
Signed by: kamov
GPG key ID: 191CBFF5F72ECAFD
11 changed files with 121 additions and 132 deletions

View file

@ -6,6 +6,7 @@ end
-- Setup default options
require 'config.options'
require 'config.keymaps'
require 'config.autocmd'
-- Are we inside Neovide?
if vim.g.neovide then

View file

@ -0,0 +1,45 @@
local group = vim.api.nvim_create_augroup('UserGroup', {})
-- Remove trailing whitespace on write
vim.api.nvim_create_autocmd('BufWritePre', {
group = group,
pattern = '*',
callback = function()
if vim.bo.filetype == 'markdown' then return end
vim.cmd [[%s/\s\+$//e]]
end
})
-- Highlight yank
vim.api.nvim_create_autocmd('TextYankPost', {
group = group,
pattern = '*',
callback = function()
vim.highlight.on_yank { timeout = 250 }
end
})
-- Restore cursor position
vim.api.nvim_create_autocmd('BufReadPost', {
group = group,
callback = function(args)
if
vim.fn.line [['"]] >= 1 and
vim.fn.line [['"]] < vim.fn.line '$' and
vim.b[args.buf].filetype ~= 'commit'
then
vim.cmd [[normal! g`"]]
end
end
})
-- Set indentation for Rust
vim.api.nvim_create_autocmd('FileType', {
pattern = 'rust',
callback = function()
vim.opt_local.tabstop = 4
vim.opt_local.shiftwidth = 4
vim.opt_local.listchars:append { leadmultispace = '' }
end
})

View file

@ -1,14 +1,16 @@
---@alias Tag
--- | 'standalone'
--- | 'linux'
---Tags used for filtering certain features
---@alias ComposerTag
--- | 'standalone' # True if running as a standalone application
--- | 'linux' # True if running on Linux
---@type table<Tag, boolean>
---@type table<ComposerTag, boolean>
local env = {
standalone = not vim.g.vscode,
linux = jit.os == 'Linux',
}
---@param tags Tag | Tag[]
---@param tags ComposerTag | ComposerTag[]
---@return boolean
local function is(tags)
if type(tags) == 'string' then
return env[tags] or false
@ -19,17 +21,18 @@ local function is(tags)
return false
end
end
return true
end
---@param tags Tag | Tag[]
---@param tags ComposerTag | ComposerTag[]
local function when(tags)
local match = is(tags)
---@generic T
---@param table `T`
---@param value `T`
---@return T | nil
return function(table)
return match and table or nil
return function(value)
return match and value or nil
end
end

View file

@ -18,7 +18,9 @@ function M.keymap(modes)
return function(rhs)
---@param opts string|KeymapOpts
return function(opts)
-- Either 'string' or 'table'
local type = type(opts)
---@type table
local options
if type == 'string' then
options = vim.tbl_extend('force', defaults, {desc=opts})
@ -34,49 +36,6 @@ function M.keymap(modes)
end
end
---@class AugroupOpts
---@field [1] string Name
---@field clear? boolean Clear existing commands if the group already exists
---Wrapper around `vim.api.nvim_create_augroup`
---@param opts string|AugroupOpts
---@return number
function M.augroup(opts)
local type = type(opts)
local name, options
if type == 'string' then
name, options = opts, {}
elseif type == 'table' then
name, options = opts[1], opts
options[1] = nil
end
return vim.api.nvim_create_augroup(name, options)
end
---@class AutocmdOpts
---@field [1]? string Shorthand desc
---@field desc? string Description for this autocommand
---@field group? string|number
---@field callback? function
---Wrapper around `vim.api.nvim_create_autocmd`
---@param event string|string[]
function M.autocmd(event)
---@param opts AutocmdOpts
return function(opts)
opts.desc = opts[1] or opts.desc
opts[1] = nil
---@param callback? function
return function(callback)
opts.callback = callback
vim.api.nvim_create_autocmd(event, opts)
end
end
end
---@param config LazyPluginSpec
function M.as_extendable(config)
---@param base LazyPluginSpec

View file

@ -1,6 +1,7 @@
local utils = require 'config.utils'
local map, augroup, autocmd = utils.keymap, utils.augroup, utils.autocmd
local n, t, nv = map 'n', map 't', map {'n', 'v'}
local H = require 'config.helpers'
local n = H.keymap 'n'
local t = H.keymap 't'
local nv = H.keymap {'n', 'v'}
n '<leader>mn' ':Explore ~/.config/nvim/<cr>' 'Meta: open Neovim config'
@ -29,31 +30,39 @@ n ']d' (vim.diagnostic.goto_next) 'Next diagnostic message'
n '<leader>dl' (vim.diagnostic.setloclist) 'Diagnostic: list messages'
-- LSP
autocmd 'LspAttach' {'LSP actions', group=augroup 'UserLspConfig'} (function(ev)
-- Enable completion triggered by <c-x><c-o>
vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'
do
local group = vim.api.nvim_create_augroup('UserLspConfig', {})
local function workspaces_list()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
local function setup_lsp_keymaps(ev)
-- Enable completion triggered by <c-x><c-o>
vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'
local function workspaces_list()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end
local function format()
vim.lsp.buf.format({async = true})
end
local buffer = ev.buf
n 'K' (vim.lsp.buf.hover) {'Show hover help', buffer}
n 'gD' (vim.lsp.buf.declaration) {'Go to declaration', buffer}
n 'gd' (vim.lsp.buf.definition) {'Go to definition', buffer}
n 'gi' (vim.lsp.buf.implementation) {'Go to implementation', buffer}
n 'gr' (vim.lsp.buf.references) {'Show references', buffer}
n '<leader>D' (vim.lsp.buf.type_definition) {'Go to type definition', buffer}
n '<C-k>' (vim.lsp.buf.signature_help) {'Signature help', buffer}
n '<leader>wa' (vim.lsp.buf.add_workspace_folder) {'Add workspace folder', buffer}
n '<leader>wr' (vim.lsp.buf.remove_workspace_folder) {'Remove workspace folder', buffer}
n '<leader>wl' (workspaces_list) {'List workspace folders', buffer}
n '<leader>lr' (vim.lsp.buf.rename) {'Rename identifier', buffer}
nv '<leader>la' (vim.lsp.buf.code_action) {'Show code actions', buffer}
n '<leader>lf' (format) {'Format file', buffer}
end
local function format()
vim.lsp.buf.format({async = true})
end
local buffer = ev.buf
n 'K' (vim.lsp.buf.hover) {'Show hover help', buffer}
n 'gD' (vim.lsp.buf.declaration) {'Go to declaration', buffer}
n 'gd' (vim.lsp.buf.definition) {'Go to definition', buffer}
n 'gi' (vim.lsp.buf.implementation) {'Go to implementation', buffer}
n 'gr' (vim.lsp.buf.references) {'Show references', buffer}
n '<leader>D' (vim.lsp.buf.type_definition) {'Go to type definition', buffer}
n '<C-k>' (vim.lsp.buf.signature_help) {'Signature help', buffer}
n '<leader>wa' (vim.lsp.buf.add_workspace_folder) {'Add workspace folder', buffer}
n '<leader>wr' (vim.lsp.buf.remove_workspace_folder) {'Remove workspace folder', buffer}
n '<leader>wl' (workspaces_list) {'List workspace folders', buffer}
n '<leader>lr' (vim.lsp.buf.rename) {'Rename identifier', buffer}
nv '<leader>la' (vim.lsp.buf.code_action) {'Show code actions', buffer}
n '<leader>lf' (format) {'Format file', buffer}
end)
vim.api.nvim_create_autocmd('LspAttach', {
group = group,
callback = setup_lsp_keymaps
})
end

View file

@ -1,5 +1,3 @@
local utils = require 'config.utils'
local autocmd = utils.autocmd
local g, opt = vim.g, vim.opt
@ -17,11 +15,11 @@ opt.termguicolors = true
-- Indentation
opt.tabstop = 2 -- 1 tab = 2 spaces
opt.smartindent = true -- Autoindent new lines
opt.shiftwidth = 2 -- Shift 2 spaces when tab
opt.smartindent = true -- Autoindent new lines
opt.expandtab = true -- Use spaces instead of tabs
-- Visual
-- UI
opt.number = true -- Show current line number
opt.relativenumber = true -- Show relative line numbers
opt.colorcolumn = '80' -- Show soft char limit
@ -33,12 +31,6 @@ opt.listchars = { -- Punctuation marks
leadmultispace = '',
}
-- Trailing whitespace
autocmd 'BufWritePre' {} (function()
if vim.bo.filetype == 'markdown' then return end
vim.cmd [[%s/\s\+$//e]]
end)
-- Additional filetypes
vim.filetype.add {
extension = {
@ -48,4 +40,3 @@ vim.filetype.add {
nu = 'nu',
}
}

View file

@ -1,6 +1,6 @@
local is, when = require 'config.composer' ()
local util = require 'config.utils'
local n = util.keymap 'n'
local is, when = require 'config.compose' ()
local H = require 'config.helpers'
local n = H.keymap 'n'
---@type LazySpec
@ -194,7 +194,7 @@ return {
},
-- Completion
require 'plugins.nvim-cmp' {
{
'hrsh7th/nvim-cmp',
dependencies = {
'hrsh7th/cmp-cmdline',
@ -203,6 +203,7 @@ return {
'hrsh7th/cmp-nvim-lsp',
'saadparwaiz1/cmp_luasnip',
},
config = require 'config.plugins.nvim-cmp'
},
-- LS configs
@ -223,12 +224,13 @@ return {
},
-- Debugger UI
require 'plugins.nvim-dap-ui' {
{
'rcarriga/nvim-dap-ui',
enabled = is { 'standalone', 'linux' },
dependencies = {
'mfussenegger/nvim-dap',
},
config = require 'config.plugins.nvim-dap-ui'
},
-- Mason
@ -334,12 +336,12 @@ return {
version = '^3',
ft = { 'rust' },
init = function()
require 'plugins.rust-tools'
require 'config.plugins.rustaceanvim'
end
},
-- Tools for Haskell
require 'plugins.haskell-tools' {
{
'mrcjkb/haskell-tools.nvim',
enabled = is { 'standalone', 'linux' },
version = '^3',
@ -348,6 +350,7 @@ return {
'nvim-lua/plenary.nvim',
'nvim-telescope/telescope.nvim',
},
init = require 'config.plugins.haskell-tools'
},
}

View file

@ -1,5 +1,5 @@
local utils = require 'config.utils'
local n = utils.keymap 'n'
local H = require 'config.helpers'
local n = H.keymap 'n'
---@param buffer number
@ -24,11 +24,9 @@ local function on_attach(_, buffer, ht)
end
return utils.as_extendable {
init = function()
vim.g.haskell_tools = {
---@type HaskellLspClientOpts`
hls = { on_attach = on_attach }
}
end
}
return function()
vim.g.haskell_tools = {
---@type HaskellLspClientOpts`
hls = { on_attach = on_attach }
}
end

View file

@ -1,8 +1,5 @@
local M = {}
-- Reference: https://github.com/hrsh7th/nvim-cmp#recommended-configuration
function M.config()
return function()
local cmp = require "cmp"
cmp.setup({
@ -64,10 +61,3 @@ function M.config()
})
})
end
---@param config table
---@return table
return function(config)
return vim.tbl_extend("keep", config, M)
end

View file

@ -1,7 +1,4 @@
local M = {}
function M.config()
return function()
local dap = require "dap"
local dapui = require "dapui"
@ -88,10 +85,3 @@ function M.config()
-- dapui.close()
-- end
end
---@param config table
---@return table
return function(config)
return vim.tbl_extend("keep", config, M)
end

View file

@ -1,5 +1,5 @@
local util = require 'config.utils'
local n = util.keymap 'n'
local H = require 'config.helpers'
local n = H.keymap 'n'
vim.g.rustaceanvim = function()