huge refactor

- tidy up
- shuffle code around
- use same abstractions for all usecases
This commit is contained in:
Vladan Popovic 2023-07-30 04:26:25 +02:00
parent 9570523e75
commit 721bc03588
12 changed files with 116 additions and 178 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
plugin/packer_compiled.lua plugin/packer_compiled.lua
*.spl

View file

@ -1,86 +1,12 @@
-- LSP -- remap leader to <space>
-- Map leader to space
vim.g.mapleader = ' ' vim.g.mapleader = ' '
local fn = vim.fn
local execute = vim.api.nvim_command
-- Sensible defaults -- Sensible defaults
require('settings') require('settings')
require('packer_setup')
-- Auto install packer.nvim if not exists
local install_path = fn.stdpath('data')..'/site/pack/packer/opt/packer.nvim'
if fn.empty(fn.glob(install_path)) > 0 then
execute('!git clone https://github.com/wbthomason/packer.nvim '..install_path)
end
vim.cmd [[packadd packer.nvim]]
vim.cmd 'autocmd BufWritePost plugins.lua PackerCompile' -- Auto compile when there are changes in plugins.lua
vim.opt.autoindent = true
vim.opt.background = 'light'
vim.opt.errorformat:prepend('%f|%l col %c|%m')
vim.opt.expandtab = true
vim.opt.fileformat = 'unix'
vim.opt.number = true
vim.opt.pastetoggle = '<F2>'
vim.opt.shiftwidth = 4
vim.opt.softtabstop = 4
vim.opt.tabstop = 4
vim.opt.textwidth = 79
vim.opt.undofile = true
-- Enable vimrc files per project and disable unsafe commands in project vimrc
vim.opt.exrc = true
vim.opt.secure = true
-- Toggles on different plugins/modes
vim.cmd 'nmap <F3> :set nonumber!<CR>'
vim.cmd 'nmap <F5> :NvimTreeToggle<CR>'
--
-- Highlight trailing whitespace (darker red) and remove all with Ctrl+k
vim.cmd 'highlight WhitespaceEOL ctermbg=red guibg=#ab0d0d'
vim.cmd 'match WhitespaceEOL /\\s\\+\\%#\\@<!$/'
vim.cmd 'nnoremap <C-k> :let _s=@/<Bar>:%s/\\s\\+$//e<Bar>:let @/=_s<Bar><CR>'
vim.cmd 'highlight Visual cterm=bold ctermbg=Grey ctermfg=NONE'
vim.cmd 'highlight Search ctermfg=Black'
vim.cmd 'highlight Comment ctermfg=DarkGrey'
vim.cmd 'highlight SignColumn ctermbg=0'
vim.cmd 'highlight LineNr ctermbg=0 ctermfg=248'
vim.cmd 'highlight Folded ctermbg=Green ctermfg=Black'
vim.cmd 'highlight Pmenu ctermbg=253 guibg=253'
vim.cmd 'highlight Pmenu ctermfg=232 guifg=232'
vim.cmd 'let g:airline_theme=\'papercolor\''
local keymap = vim.api.nvim_set_keymap
local default_opts = { noremap = true, silent = true }
-- Use tab and shift+tab to cycle buffers
keymap("n", "<tab>", "<cmd>bn<cr>", default_opts)
keymap("n", "<S-tab>", "<cmd>bp<cr>", default_opts)
-- Find files, buffers, grep in folder using Telescope command-line sugar.
keymap("n", "<leader>ff", "<cmd>Telescope find_files<cr>", default_opts)
keymap("n", "<leader>fg", "<cmd>Telescope live_grep<cr>", default_opts)
keymap("n", "<leader>fb", "<cmd>Telescope buffers<cr>", default_opts)
keymap("n", "<leader>fh", "<cmd>Telescope help_tags<cr>", default_opts)
keymap("n", "<leader>fl", "<cmd>Telescope git_files<cr>", default_opts)
-- Turn spellchecker on
keymap("n", "<leader>s", "<cmd>setlocal spell spelllang=en_us<cr>", default_opts)
-- Install plugins
require('plugins') require('plugins')
-- Key mappings
require('keymappings') require('keymappings')
require('lsp')
-- Setup Lua language server using submodule require('config')
require('lsp_lua')
-- OR you can invoke them individually here
--require('colorscheme') -- color scheme
require('config.compe') -- completion
--require('fugitive') -- fugitive
require('lang')
require('filetypes') require('filetypes')
require('theme')

View file

@ -1,2 +1,3 @@
-- nvim-compe -- nvim-compe
require('config.compe') require('config.compe')
require('config.treesitter')

19
lua/config/treesitter.lua Normal file
View file

@ -0,0 +1,19 @@
require('nvim-treesitter.configs').setup {
ensure_installed = { "lua", "rust", "elm", "python", "yaml", "toml" },
auto_install = true,
highlight = {
enable = true,
additional_vim_regex_highlighting=false,
},
ident = {
enable = true,
},
rainbow = {
enable = true,
extended_mode = false,
max_file_lines = nil,
}
}
vim.api.nvim_set_hl(0, "@variable", { })
vim.api.nvim_set_hl(0, "@parameter", { })

View file

@ -1,2 +1,20 @@
local utils = require('utils') local utils = require('utils')
utils.map('n', '<C-h>', '<cmd>noh<CR>') -- Clear highlights utils.map('n', '<C-h>', '<cmd>noh<CR>') -- Clear highlights
-- Use tab and shift+tab to cycle buffers
utils.map("n", "<tab>", "<cmd>bn<cr>")
utils.map("n", "<S-tab>", "<cmd>bp<cr>")
-- Find files, buffers, grep in folder using Telescope command-line sugar.
utils.map("n", "<leader>ff", "<cmd>Telescope find_files<cr>")
utils.map("n", "<leader>fg", "<cmd>Telescope live_grep<cr>")
utils.map("n", "<leader>fb", "<cmd>Telescope buffers<cr>")
utils.map("n", "<leader>fh", "<cmd>Telescope help_tags<cr>")
utils.map("n", "<leader>fl", "<cmd>Telescope git_files<cr>")
-- Turn spellchecker on
utils.map("n", "<leader>s", "<cmd>setlocal spell spelllang=en_us<cr>")
utils.map("n", "<C-k>", "<cmd>let _s=@/<Bar>:%s/\\s\\+$//e<Bar>:let @/=_s<Bar><CR>")
-- Toggles on different plugins/modes
utils.map("n", "<F3>", "<cmd>set nonumber!<CR>")
utils.map("n", "<F5>", "<cmd>NvimTreeToggle<CR>")

View file

@ -1,48 +1,34 @@
USER = vim.fn.expand('$USER')
-- Language specific key mappings
--require('lang.keymappings')
local on_attach = function(client, bufnr) local on_attach = function(client, bufnr)
local utils = require('utils')
require'lsp_signature'.on_attach(client) require'lsp_signature'.on_attach(client)
local function buf_set_keymap(...) utils.opt("o", "omnifunc", "v:lua.vim.lsp.omnifunc")
vim.api.nvim_buf_set_keymap(bufnr, ...)
end
local function buf_set_option(...)
vim.api.nvim_buf_set_option(bufnr, ...)
end
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
-- Mappings. -- Mappings.
local opts = {noremap = true, silent = true} utils.map("n", "gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>")
buf_set_keymap('n', 'gD', '<Cmd>lua vim.lsp.buf.declaration()<CR>', opts) utils.map("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>")
buf_set_keymap('n', 'gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', opts) utils.map("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>")
buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts) utils.map("n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>")
buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts) utils.map("n", "K", "<Cmd>lua vim.lsp.buf.hover()<CR>")
buf_set_keymap('n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', opts) utils.map("n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>")
buf_set_keymap('n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts) utils.map("n", "[d", "<cmd>lua vim.diagnostic.goto_prev()<CR>")
buf_set_keymap('n', '[d', '<cmd>lua vim.diagnostic.goto_prev()<CR>', opts) utils.map("n", "]d", "<cmd>lua vim.diagnostic.goto_next()<CR>")
buf_set_keymap('n', ']d', '<cmd>lua vim.diagnostic.goto_next()<CR>', opts) utils.map("n", "[l", "<cmd>lua vim.diagnostic.show_line_diagnostics()<CR>")
buf_set_keymap('n', '[l', '<cmd>lua vim.diagnostic.show_line_diagnostics()<CR>', opts) utils.map("n", "]l", "<cmd>lua vim.diagnostic.set_loclist()<CR>")
buf_set_keymap('n', ']l', '<cmd>lua vim.diagnostic.set_loclist()<CR>', opts) utils.map("n", "<M-CR>", "<cmd>lua vim.lsp.buf.code_action()<CR>")
buf_set_keymap('n', '<leader>law', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts) utils.map("n", "<M-r>", "<cmd>lua vim.lsp.buf.rename()()<CR>")
buf_set_keymap('n', '<leader>lrw', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts) utils.map("n", "<M-t>", "<cmd>lua vim.lsp.buf.type_definition()<CR>")
buf_set_keymap('n', '<leader>llw', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
buf_set_keymap('n', '<leader>lt', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
buf_set_keymap('n', '<leader>lrn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
buf_set_keymap('n', '<M-CR>', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
-- list/add/remove workspace folders, not that I use them often
utils.map("n", "<leader>wl", "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>")
utils.map("n", "<leader>wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>")
utils.map("n", "<leader>wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>")
-- Set some keybinds conditional on server capabilities -- Set formatting keybinds conditional on server capabilities
if client.server_capabilities.document_formatting then if client.server_capabilities.document_formatting then
buf_set_keymap("n", "<leader>lf", utils.map('n', '<leader>lf', '<cmd>lua vim.lsp.buf.formatting()<CR>')
"<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
elseif client.server_capabilities.document_range_formatting then elseif client.server_capabilities.document_range_formatting then
buf_set_keymap("n", "<leader>lf", utils.map('n', '<leader>lf', '<cmd>lua vim.lsp.buf.range_formatting()<CR>')
"<cmd>lua vim.lsp.buf.range_formatting()<CR>", opts)
end end
client.server_capabilities.semanticTokensProvider = nil client.server_capabilities.semanticTokensProvider = nil
@ -62,47 +48,18 @@ local on_attach = function(client, bufnr)
end end
end end
require('nvim-treesitter.configs').setup {
ensure_installed = { "lua", "rust", "elm", "python", "toml" },
auto_install = true,
highlight = {
enable = true,
additional_vim_regex_highlighting=false,
},
ident = {
enable = true,
},
rainbow = {
enable = true,
extended_mode = false,
max_file_lines = nil,
}
}
vim.api.nvim_set_hl(0, "@variable", { })
vim.api.nvim_set_hl(0, "@parameter", { })
local nvim_lsp = require('lspconfig') local nvim_lsp = require('lspconfig')
local capabilities = vim.lsp.protocol.make_client_capabilities() local capabilities = vim.lsp.protocol.make_client_capabilities()
-- Code actions -- show snippets in code completion popup
capabilities.textDocument.codeAction = {
dynamicRegistration = true,
codeActionLiteralSupport = {
codeActionKind = {
valueSet = (function()
local res = vim.tbl_values(vim.lsp.protocol.CodeActionKind)
table.sort(res)
return res
end)()
}
}
}
capabilities.textDocument.completion.completionItem.snippetSupport = true; capabilities.textDocument.completion.completionItem.snippetSupport = true;
-- LSPs -- LSPs
local servers = {"vimls", "ocamllsp",} local servers = {
"lua_ls",
"ocamllsp",
"vimls",
}
for _, lsp in ipairs(servers) do for _, lsp in ipairs(servers) do
nvim_lsp[lsp].setup {capabilities = capabilities, on_attach = on_attach} nvim_lsp[lsp].setup {capabilities = capabilities, on_attach = on_attach}
end end
@ -148,6 +105,8 @@ nvim_lsp.rust_analyzer.setup({
} }
}) })
vim.g.code_action_menu_window_border = 'single'
-- symbols-outline.nvim -- symbols-outline.nvim
vim.g.symbols_outline = { vim.g.symbols_outline = {
highlight_hovered_item = true, highlight_hovered_item = true,
@ -192,6 +151,5 @@ do
table.insert(qflist, d) table.insert(qflist, d)
end end
end end
-- setqflist(qflist)
end end
end end

View file

@ -1,18 +0,0 @@
-- Your custom attach function for nvim-lspconfig goes here.
local on_attach = function(client, bufnr)
require('completion').on_attach()
local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
-- Mappings
local opts = { noremap=true, silent=true }
end
-- To get builtin LSP running, do something like:
-- NOTE: This replaces the calls where you would have before done `require('nvim_lsp').sumneko_lua.setup()`
--require('nlua.lsp.nvim').setup(require('lspconfig'), {
-- on_attach = on_attach,
--})

9
lua/packer_setup.lua Normal file
View file

@ -0,0 +1,9 @@
-- Auto install packer.nvim if not exists
local install_path = vim.fn.stdpath('data')..'/site/pack/packer/opt/packer.nvim'
if vim.fn.empty(vim.fn.glob(install_path)) > 0 then
vim.api.nvim_command('!git clone https://github.com/wbthomason/packer.nvim '..install_path)
end
vim.cmd [[packadd packer.nvim]]
-- Auto compile when there are changes in plugins.lua
vim.cmd 'autocmd BufWritePost plugins.lua PackerCompile'

View file

@ -106,7 +106,6 @@ return require('packer').startup(function()
-- Fugitive for Git -- Fugitive for Git
use { 'tpope/vim-fugitive' } use { 'tpope/vim-fugitive' }
-- Fugitive for Git
use { 'ray-x/lsp_signature.nvim' } use { 'ray-x/lsp_signature.nvim' }
-- Airline status bar -- Airline status bar

View file

@ -1,14 +1,17 @@
local utils = require('utils') local utils = require('utils')
local cmd = vim.cmd
local indent = 4 local indent = 4
cmd 'syntax enable' vim.cmd 'syntax enable'
cmd 'filetype plugin indent on' vim.cmd 'filetype plugin indent on'
--
-- Highlight on yank
vim.cmd 'au TextYankPost * lua vim.highlight.on_yank {on_visual = false}'
utils.opt('b', 'expandtab', true) utils.opt('b', 'expandtab', true)
utils.opt('b', 'shiftwidth', indent) utils.opt('b', 'shiftwidth', indent)
utils.opt('b', 'smartindent', true)
utils.opt('b', 'tabstop', indent) utils.opt('b', 'tabstop', indent)
utils.opt('b', 'softtabstop', indent)
utils.opt('b', 'smartindent', true)
utils.opt('o', 'hidden', true) utils.opt('o', 'hidden', true)
utils.opt('o', 'ignorecase', true) utils.opt('o', 'ignorecase', true)
utils.opt('o', 'scrolloff', 4 ) utils.opt('o', 'scrolloff', 4 )
@ -20,5 +23,14 @@ utils.opt('o', 'wildmode', 'list:longest')
utils.opt('w', 'number', true) utils.opt('w', 'number', true)
utils.opt('o', 'clipboard','unnamed,unnamedplus') utils.opt('o', 'clipboard','unnamed,unnamedplus')
-- Highlight on yank -- see smartindent
vim.cmd 'au TextYankPost * lua vim.highlight.on_yank {on_visual = false}' -- vim.opt.autoindent = true
vim.opt.background = 'light'
vim.opt.errorformat:prepend('%f|%l col %c|%m')
vim.opt.fileformat = 'unix'
vim.opt.pastetoggle = '<F2>'
vim.opt.textwidth = 79
vim.opt.undofile = true
-- Enable vimrc files per project and disable unsafe commands in project vimrc
vim.opt.exrc = true
vim.opt.secure = true

13
lua/theme.lua Normal file
View file

@ -0,0 +1,13 @@
-- Highlight trailing whitespace (darker red) and remove all with Ctrl+k
vim.cmd 'highlight WhitespaceEOL ctermbg=red guibg=#ab0d0d'
vim.cmd 'match WhitespaceEOL /\\s\\+\\%#\\@<!$/'
vim.cmd 'highlight Visual cterm=bold ctermbg=Grey ctermfg=NONE'
vim.cmd 'highlight Search ctermfg=Black'
vim.cmd 'highlight Comment ctermfg=DarkGrey'
vim.cmd 'highlight SignColumn ctermbg=0'
vim.cmd 'highlight LineNr ctermbg=0 ctermfg=248'
vim.cmd 'highlight Folded ctermbg=Green ctermfg=Black'
vim.cmd 'highlight Pmenu ctermbg=253 guibg=253'
vim.cmd 'highlight Pmenu ctermfg=232 guifg=232'
vim.cmd 'let g:airline_theme=\'papercolor\''

View file

@ -8,7 +8,7 @@ function utils.opt(scope, key, value)
end end
function utils.map(mode, lhs, rhs, opts) function utils.map(mode, lhs, rhs, opts)
local options = {noremap = true} local options = { noremap = true, silent = true }
if opts then options = vim.tbl_extend('force', options, opts) end if opts then options = vim.tbl_extend('force', options, opts) end
vim.api.nvim_set_keymap(mode, lhs, rhs, options) vim.api.nvim_set_keymap(mode, lhs, rhs, options)
end end