nvim: refactor config
This commit is contained in:
parent
61578424eb
commit
0caff8f78a
|
@ -6,6 +6,7 @@ end
|
||||||
-- Setup default options
|
-- Setup default options
|
||||||
require 'config.options'
|
require 'config.options'
|
||||||
require 'config.keymaps'
|
require 'config.keymaps'
|
||||||
|
require 'config.autocmd'
|
||||||
|
|
||||||
-- Are we inside Neovide?
|
-- Are we inside Neovide?
|
||||||
if vim.g.neovide then
|
if vim.g.neovide then
|
||||||
|
|
45
nvim/lua/config/autocmd.lua
Normal file
45
nvim/lua/config/autocmd.lua
Normal 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
|
||||||
|
})
|
|
@ -1,14 +1,16 @@
|
||||||
---@alias Tag
|
---Tags used for filtering certain features
|
||||||
--- | 'standalone'
|
---@alias ComposerTag
|
||||||
--- | 'linux'
|
--- | 'standalone' # True if running as a standalone application
|
||||||
|
--- | 'linux' # True if running on Linux
|
||||||
|
|
||||||
---@type table<Tag, boolean>
|
---@type table<ComposerTag, boolean>
|
||||||
local env = {
|
local env = {
|
||||||
standalone = not vim.g.vscode,
|
standalone = not vim.g.vscode,
|
||||||
linux = jit.os == 'Linux',
|
linux = jit.os == 'Linux',
|
||||||
}
|
}
|
||||||
|
|
||||||
---@param tags Tag | Tag[]
|
---@param tags ComposerTag | ComposerTag[]
|
||||||
|
---@return boolean
|
||||||
local function is(tags)
|
local function is(tags)
|
||||||
if type(tags) == 'string' then
|
if type(tags) == 'string' then
|
||||||
return env[tags] or false
|
return env[tags] or false
|
||||||
|
@ -19,17 +21,18 @@ local function is(tags)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param tags Tag | Tag[]
|
---@param tags ComposerTag | ComposerTag[]
|
||||||
local function when(tags)
|
local function when(tags)
|
||||||
local match = is(tags)
|
local match = is(tags)
|
||||||
---@generic T
|
---@generic T
|
||||||
---@param table `T`
|
---@param value `T`
|
||||||
---@return T | nil
|
---@return T | nil
|
||||||
return function(table)
|
return function(value)
|
||||||
return match and table or nil
|
return match and value or nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,7 +18,9 @@ function M.keymap(modes)
|
||||||
return function(rhs)
|
return function(rhs)
|
||||||
---@param opts string|KeymapOpts
|
---@param opts string|KeymapOpts
|
||||||
return function(opts)
|
return function(opts)
|
||||||
|
-- Either 'string' or 'table'
|
||||||
local type = type(opts)
|
local type = type(opts)
|
||||||
|
---@type table
|
||||||
local options
|
local options
|
||||||
if type == 'string' then
|
if type == 'string' then
|
||||||
options = vim.tbl_extend('force', defaults, {desc=opts})
|
options = vim.tbl_extend('force', defaults, {desc=opts})
|
||||||
|
@ -34,49 +36,6 @@ function M.keymap(modes)
|
||||||
end
|
end
|
||||||
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
|
---@param config LazyPluginSpec
|
||||||
function M.as_extendable(config)
|
function M.as_extendable(config)
|
||||||
---@param base LazyPluginSpec
|
---@param base LazyPluginSpec
|
|
@ -1,6 +1,7 @@
|
||||||
local utils = require 'config.utils'
|
local H = require 'config.helpers'
|
||||||
local map, augroup, autocmd = utils.keymap, utils.augroup, utils.autocmd
|
local n = H.keymap 'n'
|
||||||
local n, t, nv = map 'n', map 't', map {'n', 'v'}
|
local t = H.keymap 't'
|
||||||
|
local nv = H.keymap {'n', 'v'}
|
||||||
|
|
||||||
|
|
||||||
n '<leader>mn' ':Explore ~/.config/nvim/<cr>' 'Meta: open Neovim config'
|
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'
|
n '<leader>dl' (vim.diagnostic.setloclist) 'Diagnostic: list messages'
|
||||||
|
|
||||||
-- LSP
|
-- LSP
|
||||||
autocmd 'LspAttach' {'LSP actions', group=augroup 'UserLspConfig'} (function(ev)
|
do
|
||||||
-- Enable completion triggered by <c-x><c-o>
|
local group = vim.api.nvim_create_augroup('UserLspConfig', {})
|
||||||
vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'
|
|
||||||
|
|
||||||
local function workspaces_list()
|
local function setup_lsp_keymaps(ev)
|
||||||
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
|
-- 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
|
end
|
||||||
|
|
||||||
local function format()
|
vim.api.nvim_create_autocmd('LspAttach', {
|
||||||
vim.lsp.buf.format({async = true})
|
group = group,
|
||||||
end
|
callback = setup_lsp_keymaps
|
||||||
|
})
|
||||||
local buffer = ev.buf
|
end
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
local utils = require 'config.utils'
|
|
||||||
local autocmd = utils.autocmd
|
|
||||||
local g, opt = vim.g, vim.opt
|
local g, opt = vim.g, vim.opt
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,11 +15,11 @@ opt.termguicolors = true
|
||||||
|
|
||||||
-- Indentation
|
-- Indentation
|
||||||
opt.tabstop = 2 -- 1 tab = 2 spaces
|
opt.tabstop = 2 -- 1 tab = 2 spaces
|
||||||
opt.smartindent = true -- Autoindent new lines
|
|
||||||
opt.shiftwidth = 2 -- Shift 2 spaces when tab
|
opt.shiftwidth = 2 -- Shift 2 spaces when tab
|
||||||
|
opt.smartindent = true -- Autoindent new lines
|
||||||
opt.expandtab = true -- Use spaces instead of tabs
|
opt.expandtab = true -- Use spaces instead of tabs
|
||||||
|
|
||||||
-- Visual
|
-- UI
|
||||||
opt.number = true -- Show current line number
|
opt.number = true -- Show current line number
|
||||||
opt.relativenumber = true -- Show relative line numbers
|
opt.relativenumber = true -- Show relative line numbers
|
||||||
opt.colorcolumn = '80' -- Show soft char limit
|
opt.colorcolumn = '80' -- Show soft char limit
|
||||||
|
@ -33,12 +31,6 @@ opt.listchars = { -- Punctuation marks
|
||||||
leadmultispace = '│ ',
|
leadmultispace = '│ ',
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Trailing whitespace
|
|
||||||
autocmd 'BufWritePre' {} (function()
|
|
||||||
if vim.bo.filetype == 'markdown' then return end
|
|
||||||
vim.cmd [[%s/\s\+$//e]]
|
|
||||||
end)
|
|
||||||
|
|
||||||
-- Additional filetypes
|
-- Additional filetypes
|
||||||
vim.filetype.add {
|
vim.filetype.add {
|
||||||
extension = {
|
extension = {
|
||||||
|
@ -48,4 +40,3 @@ vim.filetype.add {
|
||||||
nu = 'nu',
|
nu = 'nu',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
local is, when = require 'config.composer' ()
|
local is, when = require 'config.compose' ()
|
||||||
local util = require 'config.utils'
|
local H = require 'config.helpers'
|
||||||
local n = util.keymap 'n'
|
local n = H.keymap 'n'
|
||||||
|
|
||||||
|
|
||||||
---@type LazySpec
|
---@type LazySpec
|
||||||
|
@ -194,7 +194,7 @@ return {
|
||||||
},
|
},
|
||||||
|
|
||||||
-- Completion
|
-- Completion
|
||||||
require 'plugins.nvim-cmp' {
|
{
|
||||||
'hrsh7th/nvim-cmp',
|
'hrsh7th/nvim-cmp',
|
||||||
dependencies = {
|
dependencies = {
|
||||||
'hrsh7th/cmp-cmdline',
|
'hrsh7th/cmp-cmdline',
|
||||||
|
@ -203,6 +203,7 @@ return {
|
||||||
'hrsh7th/cmp-nvim-lsp',
|
'hrsh7th/cmp-nvim-lsp',
|
||||||
'saadparwaiz1/cmp_luasnip',
|
'saadparwaiz1/cmp_luasnip',
|
||||||
},
|
},
|
||||||
|
config = require 'config.plugins.nvim-cmp'
|
||||||
},
|
},
|
||||||
|
|
||||||
-- LS configs
|
-- LS configs
|
||||||
|
@ -223,12 +224,13 @@ return {
|
||||||
},
|
},
|
||||||
|
|
||||||
-- Debugger UI
|
-- Debugger UI
|
||||||
require 'plugins.nvim-dap-ui' {
|
{
|
||||||
'rcarriga/nvim-dap-ui',
|
'rcarriga/nvim-dap-ui',
|
||||||
enabled = is { 'standalone', 'linux' },
|
enabled = is { 'standalone', 'linux' },
|
||||||
dependencies = {
|
dependencies = {
|
||||||
'mfussenegger/nvim-dap',
|
'mfussenegger/nvim-dap',
|
||||||
},
|
},
|
||||||
|
config = require 'config.plugins.nvim-dap-ui'
|
||||||
},
|
},
|
||||||
|
|
||||||
-- Mason
|
-- Mason
|
||||||
|
@ -334,12 +336,12 @@ return {
|
||||||
version = '^3',
|
version = '^3',
|
||||||
ft = { 'rust' },
|
ft = { 'rust' },
|
||||||
init = function()
|
init = function()
|
||||||
require 'plugins.rust-tools'
|
require 'config.plugins.rustaceanvim'
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
|
|
||||||
-- Tools for Haskell
|
-- Tools for Haskell
|
||||||
require 'plugins.haskell-tools' {
|
{
|
||||||
'mrcjkb/haskell-tools.nvim',
|
'mrcjkb/haskell-tools.nvim',
|
||||||
enabled = is { 'standalone', 'linux' },
|
enabled = is { 'standalone', 'linux' },
|
||||||
version = '^3',
|
version = '^3',
|
||||||
|
@ -348,6 +350,7 @@ return {
|
||||||
'nvim-lua/plenary.nvim',
|
'nvim-lua/plenary.nvim',
|
||||||
'nvim-telescope/telescope.nvim',
|
'nvim-telescope/telescope.nvim',
|
||||||
},
|
},
|
||||||
|
init = require 'config.plugins.haskell-tools'
|
||||||
},
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
local utils = require 'config.utils'
|
local H = require 'config.helpers'
|
||||||
local n = utils.keymap 'n'
|
local n = H.keymap 'n'
|
||||||
|
|
||||||
|
|
||||||
---@param buffer number
|
---@param buffer number
|
||||||
|
@ -24,11 +24,9 @@ local function on_attach(_, buffer, ht)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
return utils.as_extendable {
|
return function()
|
||||||
init = function()
|
vim.g.haskell_tools = {
|
||||||
vim.g.haskell_tools = {
|
---@type HaskellLspClientOpts`
|
||||||
---@type HaskellLspClientOpts`
|
hls = { on_attach = on_attach }
|
||||||
hls = { on_attach = on_attach }
|
}
|
||||||
}
|
end
|
||||||
end
|
|
||||||
}
|
|
|
@ -1,8 +1,5 @@
|
||||||
local M = {}
|
|
||||||
|
|
||||||
|
|
||||||
-- Reference: https://github.com/hrsh7th/nvim-cmp#recommended-configuration
|
-- Reference: https://github.com/hrsh7th/nvim-cmp#recommended-configuration
|
||||||
function M.config()
|
return function()
|
||||||
local cmp = require "cmp"
|
local cmp = require "cmp"
|
||||||
|
|
||||||
cmp.setup({
|
cmp.setup({
|
||||||
|
@ -64,10 +61,3 @@ function M.config()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@param config table
|
|
||||||
---@return table
|
|
||||||
return function(config)
|
|
||||||
return vim.tbl_extend("keep", config, M)
|
|
||||||
end
|
|
|
@ -1,7 +1,4 @@
|
||||||
local M = {}
|
return function()
|
||||||
|
|
||||||
|
|
||||||
function M.config()
|
|
||||||
local dap = require "dap"
|
local dap = require "dap"
|
||||||
local dapui = require "dapui"
|
local dapui = require "dapui"
|
||||||
|
|
||||||
|
@ -88,10 +85,3 @@ function M.config()
|
||||||
-- dapui.close()
|
-- dapui.close()
|
||||||
-- end
|
-- end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@param config table
|
|
||||||
---@return table
|
|
||||||
return function(config)
|
|
||||||
return vim.tbl_extend("keep", config, M)
|
|
||||||
end
|
|
|
@ -1,5 +1,5 @@
|
||||||
local util = require 'config.utils'
|
local H = require 'config.helpers'
|
||||||
local n = util.keymap 'n'
|
local n = H.keymap 'n'
|
||||||
|
|
||||||
|
|
||||||
vim.g.rustaceanvim = function()
|
vim.g.rustaceanvim = function()
|
Loading…
Reference in a new issue