nvim: better goimports, treesitter, etc.
Anirudh Oppiliappan x@icyphox.sh
Wed, 13 Apr 2022 16:51:12 +0530
10 files changed,
90 insertions(+),
60 deletions(-)
M
config/nvim/_init.lua
→
config/nvim/_init.lua
@@ -2,13 +2,15 @@ -- impatient.nvim
-- TODO: remove this once it's merged require 'impatient' -require('settings') -require('maps') -require('statusline.line') -require('treesitter') -require('fzy/fzy') +require 'settings' +require 'maps' +require 'statusline.line' +require 'treesitter' +require 'fzy/fzy' -- lsp setup -require('lsp.config') -require('lsp.python') -require('lsp.go') +require 'lsp.config' +require 'lsp.python' +require 'lsp.go' +require 'lsp.lua' +require 'lsp.json'
M
config/nvim/ftplugin/go.vim
→
config/nvim/ftplugin/go.vim
@@ -5,5 +5,26 @@ setlocal smarttab
setlocal formatoptions=croql setlocal tabstop=4 -autocmd BufWritePre *.go lua vim.lsp.buf.formatting() -autocmd BufWritePre *.go lua goimports(1000) + +lua <<EOF + function org_imports(wait_ms) + local params = vim.lsp.util.make_range_params() + params.context = {only = {"source.organizeImports"}} + local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, wait_ms) + for _, res in pairs(result or {}) do + for _, r in pairs(res.result or {}) do + if r.edit then + vim.lsp.util.apply_workspace_edit(r.edit, "utf-16") + else + vim.lsp.buf.execute_command(r.command) + end + end + end + end +EOF + +augroup go_lsp + autocmd! + autocmd BufWritePre *.go :silent! lua vim.lsp.buf.formatting() + autocmd BufWritePre *.go :silent! lua org_imports(3000) +augroup END
M
config/nvim/lua/lsp/go.lua
→
config/nvim/lua/lsp/go.lua
@@ -10,34 +10,3 @@ staticcheck = true,
}, on_attach = require('maps').on_attach, } - - -function goimports(timeout) - local context = { source = { organizeImports = true } } - vim.validate { context = { context, "t", true } } - - local params = vim.lsp.util.make_range_params() - params.context = context - - -- See the implementation of the textDocument/codeAction callback - -- (lua/vim/lsp/handler.lua) for how to do this properly. - local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, timeout_ms) - if not result or next(result) == nil then return end - local actions = result[1].result - if not actions then return end - local action = actions[1] - - -- textDocument/codeAction can return either Command[] or CodeAction[]. If it - -- is a CodeAction, it can have either an edit, a command or both. Edits - -- should be executed first. - if action.edit or type(action.command) == "table" then - if action.edit then - vim.lsp.util.apply_workspace_edit(action.edit) - end - if type(action.command) == "table" then - vim.lsp.buf.execute_command(action.command) - end - else - vim.lsp.buf.execute_command(action) - end -end
M
config/nvim/lua/lsp/lua.lua
→
config/nvim/lua/lsp/lua.lua
@@ -1,31 +1,23 @@
--- only on macOS -util = require('lspconfig.util') - - -local root_path = '/Users/icy/Leet/lua-language-server' -local bin_path = root_path .. '/bin/macOS/lua-language-server' +local runtime_path = vim.split(package.path, ';') +table.insert(runtime_path, "lua/?.lua") +table.insert(runtime_path, "lua/?/init.lua") require'lspconfig'.sumneko_lua.setup { - cmd = { bin_path, "-E", root_path .. '/main.lua' }, settings = { Lua = { runtime = { version = 'LuaJIT', - path = vim.split(package.path, ';'), + path = runtime_path, }, diagnostics = { globals = {'vim'}, }, workspace = { - library = { - [vim.fn.expand('$VIMRUNTIME/lua')] = true, - [vim.fn.expand('$VIMRUNTIME/lua/vim/lsp')] = true, - }, + library = vim.api.nvim_get_runtime_file("", true), }, telemetry = { enable = false, - } + }, }, }, - on_attach = require('maps').on_attach, }
M
config/nvim/lua/maps.lua
→
config/nvim/lua/maps.lua
@@ -1,5 +1,6 @@
local cmd = vim.cmd local map = vim.api.nvim_set_keymap +local u = require 'utils' local M = {} -- map the leader key@@ -77,8 +78,8 @@ buf_set_keymap('n', 'gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', options)
buf_set_keymap('n', 'ga', '<Cmd>lua vim.lsp.buf.code_action()<CR>', options) buf_set_keymap('n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', options) buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', options) - buf_set_keymap('n', '[d', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>', options) - buf_set_keymap('n', ']d', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>', options) + buf_set_keymap('n', '[d', '<cmd>lua vim.diagnostic.goto_prev()<CR>', options) + buf_set_keymap('n', ']d', '<cmd>lua vim.diagnostic.goto_next()<CR>', options) buf_set_keymap('n', '<leader>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', options) if client.resolved_capabilities.document_formatting then@@ -100,5 +101,14 @@ " Avoid showing message extra message when using completion
set shortmess+=c ]], false) + +-- abbreviations +local star = '★' +local stars = {} +for n = 1, 5 +do + table.insert(stars, star) + u.iabbrev(n .. '*', table.concat(stars)) +end return M
M
config/nvim/lua/settings.lua
→
config/nvim/lua/settings.lua
@@ -57,9 +57,6 @@ g.gitgutter_sign_removed = '-'
g.gitgutter_sign_removed_first_line = '^' g.gitgutter_sign_modified_removed = '#' --- use a python3 venv -g.python3_host_prog = vim.env.HOME .. '/leet/vim-python3/bin/python3' - -- window-local options o.number = false o.list = true
M
config/nvim/lua/treesitter.lua
→
config/nvim/lua/treesitter.lua
@@ -4,5 +4,37 @@ enable = true,
}, indent = { enable = true, + }, + textobjects = { + select = { + enable = true, + lookahead = true, + keymaps = { + ["af"] = "@function.outer", + ["if"] = "@function.inner", + ["ac"] = "@class.outer", + ["ic"] = "@class.inner", + }, + }, + move = { + enable = true, + set_jumps = true, + goto_next_start = { + ["]m"] = "@function.outer", + ["]]"] = "@class.outer", + }, + goto_next_end = { + ["]M"] = "@function.outer", + ["]["] = "@class.outer", + }, + goto_previous_start = { + ["[m"] = "@function.outer", + ["[["] = "@class.outer", + }, + goto_previous_end = { + ["[M"] = "@function.outer", + ["[]"] = "@class.outer", + }, + }, } }
M
config/nvim/lua/utils.lua
→
config/nvim/lua/utils.lua
@@ -10,4 +10,9 @@ end
cmd('augroup END') end +function M.iabbrev(a, b) + cmd(string.format('iabbrev %s %s', a, b)) +end + return M +