format mapping, code autoformat

neovim_0_5
ray-x 3 years ago
parent 23ea6b94de
commit 28f8407f4f

@ -1,24 +1,21 @@
-- todo allow config passed in
local lspconfig = nil
if packer_plugins ~= nil then
if not packer_plugins["neovim/nvim-lspconfig"] or not packer_plugins["neovim/nvim-lspconfig"].loaded then
vim.cmd [[packadd nvim-lspconfig]]
end
if not packer_plugins["ray-x/guihua.lua"] or not packer_plugins["guihua.lua"].loaded then
vim.cmd [[packadd guihua.lua]]
-- if lazyloading
end
end
if package.loaded["lspconfig"] then
lspconfig = require "lspconfig"
if not packer_plugins["neovim/nvim-lspconfig"] or
not packer_plugins["neovim/nvim-lspconfig"].loaded then
vim.cmd [[packadd nvim-lspconfig]]
end
if not packer_plugins["ray-x/guihua.lua"] or
not packer_plugins["guihua.lua"].loaded then
vim.cmd [[packadd guihua.lua]]
-- if lazyloading
end
end
if package.loaded["lspconfig"] then lspconfig = require "lspconfig" end
local highlight = require "navigator.lspclient.highlight"
if lspconfig == nil then
error("loading lsp config")
end
local config = require "navigator".config_values()
if lspconfig == nil then error("loading lsp config") end
local config = require"navigator".config_values()
local cap = vim.lsp.protocol.make_client_capabilities()
local on_attach = require("navigator.lspclient.attach").on_attach
@ -26,205 +23,164 @@ local on_attach = require("navigator.lspclient.attach").on_attach
-- gopls["ui.completion.usePlaceholders"] = true
local golang_setup = {
on_attach = on_attach,
capabilities = cap,
-- init_options = {
-- useplaceholders = true,
-- completeunimported = true
-- },
message_level = vim.lsp.protocol.MessageType.Error,
cmd = {
"gopls"
on_attach = on_attach,
capabilities = cap,
-- init_options = {
-- useplaceholders = true,
-- completeunimported = true
-- },
message_level = vim.lsp.protocol.MessageType.Error,
cmd = {
"gopls"
-- share the gopls instance if there is one already
-- "-remote=auto",
-- share the gopls instance if there is one already
-- "-remote=auto",
--[[ debug options ]]
--
--"-logfile=auto",
--"-debug=:0",
--"-remote.debug=:0",
--"-rpc.trace",
},
settings = {
gopls = {
analyses = {
unusedparams = true,
unreachable = false
},
codelenses = {
generate = true, -- show the `go generate` lens.
gc_details = true -- // Show a code lens toggling the display of gc's choices.
},
usePlaceholders = true,
completeUnimported = true,
staticcheck = true,
matcher = "fuzzy",
symbolMatcher = "fuzzy",
gofumpt = true,
buildFlags = {"-tags", "integration"}
-- buildFlags = {"-tags", "functional"}
}
},
root_dir = function(fname)
local util = require("lspconfig").util
return util.root_pattern("go.mod", ".git")(fname) or util.path.dirname(fname)
end
--[[ debug options ]]
--
-- "-logfile=auto",
-- "-debug=:0",
-- "-remote.debug=:0",
-- "-rpc.trace",
},
settings = {
gopls = {
analyses = {unusedparams = true, unreachable = false},
codelenses = {
generate = true, -- show the `go generate` lens.
gc_details = true -- // Show a code lens toggling the display of gc's choices.
},
usePlaceholders = true,
completeUnimported = true,
staticcheck = true,
matcher = "fuzzy",
symbolMatcher = "fuzzy",
gofumpt = true,
buildFlags = {"-tags", "integration"}
-- buildFlags = {"-tags", "functional"}
}
},
root_dir = function(fname)
local util = require("lspconfig").util
return util.root_pattern("go.mod", ".git")(fname) or
util.path.dirname(fname)
end
}
local clang_cfg = {
cmd = {
"clangd",
"--background-index",
"--suggest-missing-includes",
"--clang-tidy",
"--header-insertion=iwyu"
},
on_attach = function(client)
client.resolved_capabilities.document_formatting = true
on_attach(client)
end
cmd = {
"clangd", "--background-index", "--suggest-missing-includes",
"--clang-tidy", "--header-insertion=iwyu"
},
on_attach = function(client)
client.resolved_capabilities.document_formatting = true
on_attach(client)
end
}
local rust_cfg = {
settings = {
["rust-analyzer"] = {
assist = {
importMergeBehavior = "last",
importPrefix = "by_self"
},
cargo = {
loadOutDirsFromCheck = true
},
procMacro = {
enable = true
}
settings = {
["rust-analyzer"] = {
assist = {importMergeBehavior = "last", importPrefix = "by_self"},
cargo = {loadOutDirsFromCheck = true},
procMacro = {enable = true}
}
}
}
}
local sqls_cfg = {
on_attach = function(client, bufnr)
client.resolved_capabilities.execute_command = true
highlight.diagnositc_config_sign()
require "sqls".setup {picker = "telescope"} -- or default
end,
settings = {
cmd = {"sqls", "-config", "$HOME/.config/sqls/config.yml"}
-- alterantively:
-- connections = {
-- {
-- driver = 'postgresql',
-- datasourcename = 'host=127.0.0.1 port=5432 user=postgres password=password dbname=user_db sslmode=disable',
-- },
-- },
}
on_attach = function(client, bufnr)
client.resolved_capabilities.execute_command = true
highlight.diagnositc_config_sign()
require"sqls".setup {picker = "telescope"} -- or default
end,
settings = {
cmd = {"sqls", "-config", "$HOME/.config/sqls/config.yml"}
-- alterantively:
-- connections = {
-- {
-- driver = 'postgresql',
-- datasourcename = 'host=127.0.0.1 port=5432 user=postgres password=password dbname=user_db sslmode=disable',
-- },
-- },
}
}
-- lua setup
local sumneko_root_path = config.sumneko_root_path
local sumneko_binary = config.sumneko_binary
local lua_cfg = {
cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"},
on_attach = on_attach,
settings = {
Lua = {
runtime = {
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
version = "LuaJIT",
-- Setup your lua path
path = vim.split(package.path, ";")
},
diagnostics = {
enable = true,
-- Get the language server to recognize the `vim` global
globals = {
"vim",
"describe",
"it",
"before_each",
"after_each",
"teardown",
"pending"
}
},
workspace = {
-- Make the server aware of Neovim runtime files
library = {
[vim.fn.expand("$VIMRUNTIME/lua")] = true,
[vim.fn.expand("$VIMRUNTIME/lua/vim")] = true,
[vim.fn.expand("$VIMRUNTIME/lua/vim/lsp")] = true
-- [vim.fn.expand("~/repos/nvim/lua")] = true
cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"},
on_attach = on_attach,
settings = {
Lua = {
runtime = {
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
version = "LuaJIT",
-- Setup your lua path
path = vim.split(package.path, ";")
},
diagnostics = {
enable = true,
-- Get the language server to recognize the `vim` global
globals = {
"vim", "describe", "it", "before_each", "after_each",
"teardown", "pending"
}
},
workspace = {
-- Make the server aware of Neovim runtime files
library = {
[vim.fn.expand("$VIMRUNTIME/lua")] = true,
[vim.fn.expand("$VIMRUNTIME/lua/vim")] = true,
[vim.fn.expand("$VIMRUNTIME/lua/vim/lsp")] = true
-- [vim.fn.expand("~/repos/nvim/lua")] = true
}
}
}
}
}
}
}
local servers = {
"gopls",
"tsserver",
"flow",
"bashls",
"dockerls",
"pyls",
"jedi_language_server",
"jdtls",
"sumneko_lua",
"vimls",
"html",
"jsonls",
"cssls",
"yamlls",
"clangd",
"sqls",
"denols",
"dartls",
"dotls",
"kotlin_language_server",
"nimls",
"phpactor",
"r_language_server",
"rust_analyzer",
"terraformls"
"gopls", "tsserver", "flow", "bashls", "dockerls", "pyls",
"jedi_language_server", "jdtls", "sumneko_lua", "vimls", "html", "jsonls",
"cssls", "yamlls", "clangd", "sqls", "denols", "dartls", "dotls",
"kotlin_language_server", "nimls", "phpactor", "r_language_server",
"rust_analyzer", "terraformls"
}
local function setup(user_opts)
if lspconfig == nil then
error("lsp-config need installed and enabled")
return
end
highlight.diagnositc_config_sign()
highlight.add_highlight()
for _, lspclient in ipairs(servers) do
if lspconfig[lspclient] == nil then
print("not supported", lspclient)
goto continue
if lspconfig == nil then
error("lsp-config need installed and enabled")
return
end
local lspft = lspconfig[lspclient].filetypes
if lspft ~= nil and #lspft > 0 then
local ft = vim.bo.filetype
local should_load = false
for _, value in ipairs(lspft) do
if ft == value then
should_load = true
highlight.diagnositc_config_sign()
highlight.add_highlight()
for _, lspclient in ipairs(servers) do
if lspconfig[lspclient] == nil then
print("not supported", lspclient)
goto continue
end
local lspft = lspconfig[lspclient].filetypes
if lspft ~= nil and #lspft > 0 then
local ft = vim.bo.filetype
local should_load = false
for _, value in ipairs(lspft) do
if ft == value then should_load = true end
end
if not should_load then goto continue end
end
end
if not should_load then
goto continue
end
end
lspconfig[lspclient].setup {
message_level = vim.lsp.protocol.MessageType.error,
log_level = vim.lsp.protocol.MessageType.error,
on_attach = on_attach
}
::continue::
end
lspconfig[lspclient].setup {
message_level = vim.lsp.protocol.MessageType.error,
log_level = vim.lsp.protocol.MessageType.error,
on_attach = on_attach
}
::continue::
end
lspconfig.gopls.setup(golang_setup)
lspconfig.sqls.setup(sqls_cfg)
lspconfig.sumneko_lua.setup(lua_cfg)
lspconfig.clangd.setup(clang_cfg)
lspconfig.rust_analyzer.setup(rust_cfg)
lspconfig.gopls.setup(golang_setup)
lspconfig.sqls.setup(sqls_cfg)
lspconfig.sumneko_lua.setup(lua_cfg)
lspconfig.clangd.setup(clang_cfg)
lspconfig.rust_analyzer.setup(rust_cfg)
end
return {setup = setup, cap = cap}

@ -1,148 +1,166 @@
local log = require "navigator.util".log
local function set_keymap(...)
vim.api.nvim_set_keymap(...)
end
local log = require"navigator.util".log
local function set_keymap(...) vim.api.nvim_set_keymap(...) end
local event_hdlrs = {
{ev = "BufWritePre", func = "diagnostic.set_loclist({open_loclist = false})"},
{ev = "CursorHold", func = "document_highlight()"},
{ev = "CursorHoldI", func = "document_highlight()"},
{ev = "CursorMoved", func = "clear_references()"}
{
ev = "BufWritePre",
func = "diagnostic.set_loclist({open_loclist = false})"
}, {ev = "CursorHold", func = "document_highlight()"},
{ev = "CursorHoldI", func = "document_highlight()"},
{ev = "CursorMoved", func = "clear_references()"}
}
local key_maps = {
{key = "gr", func = "references()"},
{mode = "i", key = "<M-k>", func = "signature_help()"},
{key = "gs", func = "signature_help()"},
{key = "g0", func = "document_symbol()"},
{key = "gW", func = "workspace_symbol()"},
{key = "<c-]>", func = "definition()"},
{key = "gD", func = "declaration()"},
{key = "gp", func = "require('navigator.definition').definition_preview()"},
{key = "gT", func = "require('navigator.treesitter').buf_ts()"},
{key = "GT", func = "require('navigator.treesitter').bufs_ts()"},
{key = "K", func = "hover()"},
{key = "ga", mode = 'n', func = "code_action()"},
{key = "ga", mode = 'v', func = "range_code_action()"},
{key = "<Leader>re", func = "rename()"},
{key = "<Leader>gi", func = "incoming_calls()"},
{key = "<Leader>go", func = "outgoing_calls()"},
{key = "gi", func = "implementation()"},
{key = "gt", func = "type_definition()"},
{key = "gL", func = "diagnostic.show_line_diagnostics()"},
{key = "gG", func = "require('navigator.diagnostics').show_diagnostic()"},
{key = "]d", func = "diagnostic.goto_next()"},
{key = "[d", func = "diagnostic.goto_prev()"},
{key = "<C-LeftMouse>", func = "definition()"},
{key = "g<LeftMouse>", func = "implementation()"}
{key = "gr", func = "references()"},
{mode = "i", key = "<M-k>", func = "signature_help()"},
{key = "gs", func = "signature_help()"},
{key = "g0", func = "document_symbol()"},
{key = "gW", func = "workspace_symbol()"},
{key = "<c-]>", func = "definition()"},
{key = "gD", func = "declaration()"},
{key = "gp", func = "require('navigator.definition').definition_preview()"},
{key = "gT", func = "require('navigator.treesitter').buf_ts()"},
{key = "GT", func = "require('navigator.treesitter').bufs_ts()"},
{key = "K", func = "hover()"},
{key = "ga", mode = 'n', func = "code_action()"},
{key = "ga", mode = 'v', func = "range_code_action()"},
{key = "<Leader>re", func = "rename()"},
{key = "<Leader>gi", func = "incoming_calls()"},
{key = "<Leader>go", func = "outgoing_calls()"},
{key = "gi", func = "implementation()"},
{key = "gt", func = "type_definition()"},
{key = "gL", func = "diagnostic.show_line_diagnostics()"},
{key = "gG", func = "require('navigator.diagnostics').show_diagnostic()"},
{key = "]d", func = "diagnostic.goto_next()"},
{key = "[d", func = "diagnostic.goto_prev()"},
{key = "<C-LeftMouse>", func = "definition()"},
{key = "g<LeftMouse>", func = "implementation()"}
}
local function set_mapping(user_opts)
local opts = {noremap = true, silent = true}
user_opts = user_opts or {}
local user_key = user_opts.keymaps or {}
local bufnr = user_opts.bufnr or 0
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
for _, v in pairs(user_key) do
local exists = false
for _, default in pairs(key_maps) do
if v.func == default.func then
default.key, exists = v.key, true
break
end
local opts = {noremap = true, silent = true}
user_opts = user_opts or {}
local user_key = user_opts.keymaps or {}
local bufnr = user_opts.bufnr or 0
local function buf_set_keymap(...)
vim.api.nvim_buf_set_keymap(bufnr, ...)
end
if not exists then
table.insert(key_maps, v)
-- local function buf_set_option(...)
-- vim.api.nvim_buf_set_option(bufnr, ...)
-- end
for _, v in pairs(user_key) do
local exists = false
for _, default in pairs(key_maps) do
if v.func == default.func then
default.key, exists = v.key, true
break
end
end
if not exists then table.insert(key_maps, v) end
end
end
-- local key_opts = {vim.tbl_deep_extend("force", key_maps, unpack(result))}
for _, value in pairs(key_maps) do
local f = "<Cmd>lua vim.lsp.buf." .. value.func .. "<CR>"
if string.find(value.func, 'require') then
f = "<Cmd>lua " .. value.func .. "<CR>"
elseif string.find(value.func, 'diagnostic') then
f = "<Cmd>lua vim.lsp." .. value.func .. "<CR>"
-- local key_opts = {vim.tbl_deep_extend("force", key_maps, unpack(result))}
for _, value in pairs(key_maps) do
local f = "<Cmd>lua vim.lsp.buf." .. value.func .. "<CR>"
if string.find(value.func, 'require') then
f = "<Cmd>lua " .. value.func .. "<CR>"
elseif string.find(value.func, 'diagnostic') then
f = "<Cmd>lua vim.lsp." .. value.func .. "<CR>"
end
local k = value.key
local m = value.mode or "n"
buf_set_keymap(m, k, f, opts)
end
local k = value.key
local m = value.mode or "n"
buf_set_keymap(m, k, f, opts)
end
-- format setup
-- format setup
if user_opts.cap.document_formatting then
buf_set_keymap("n", "<space>f", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
-- if user_opts.cap.document_formatting then
buf_set_keymap("n", "<space>ff", "<cmd>lua vim.lsp.buf.formatting()<CR>",
opts)
vim.cmd([[autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting()]])
end
if user_opts.cap.document_range_formatting then
buf_set_keymap("v", "<space>f", "<cmd>lua vim.lsp.buf.range_formatting()<CR>", opts)
end
-- if user_opts.cap.document_range_formatting then
buf_set_keymap("v", "<space>ff",
"<cmd>lua vim.lsp.buf.range_formatting()<CR>", opts)
end
local function set_event_handler(user_opts)
user_opts = user_opts or {}
local file_types = "c,cpp,h,go,python,vim,sh,javascript,html,css,lua,typescript,rust"
-- local format_files = "c,cpp,h,go,python,vim,javascript,typescript" --html,css,
vim.api.nvim_command [[augroup nvim_lsp_autos]]
vim.api.nvim_command [[autocmd!]]
for _, value in pairs(event_hdlrs) do
local f = ""
if string.find(value.func, "diagnostic") then
f = "lua vim.lsp." .. value.func
else
f = "lua vim.lsp.buf." .. value.func
user_opts = user_opts or {}
local file_types =
"c,cpp,h,go,python,vim,sh,javascript,html,css,lua,typescript,rust"
-- local format_files = "c,cpp,h,go,python,vim,javascript,typescript" --html,css,
vim.api.nvim_command [[augroup nvim_lsp_autos]]
vim.api.nvim_command [[autocmd!]]
for _, value in pairs(event_hdlrs) do
local f = ""
if string.find(value.func, "diagnostic") then
f = "lua vim.lsp." .. value.func
else
f = "lua vim.lsp.buf." .. value.func
end
local cmd = "autocmd FileType " .. file_types ..
" autocmd nvim_lsp_autos " .. value.ev ..
" <buffer> silent! " .. f
vim.api.nvim_command(cmd)
end
local cmd = "autocmd FileType " .. file_types .. " autocmd nvim_lsp_autos " .. value.ev .. " <buffer> silent! " .. f
vim.api.nvim_command(cmd)
end
vim.api.nvim_command([[augroup END]])
vim.api.nvim_command([[augroup END]])
end
local M = {}
function M.setup(user_opts)
set_mapping(user_opts)
set_event_handler(user_opts)
local cap = user_opts.cap or {}
if cap.call_hierarchy then
vim.lsp.handlers["callHierarchy/incomingCalls"] = require "navigator.hierarchy".incoming_calls_handler
vim.lsp.handlers["callHierarchy/outgoingCalls"] = require "navigator.hierarchy".outgoing_calls_handler
end
vim.lsp.handlers["textDocument/references"] = require "navigator.reference".reference_handler
vim.lsp.handlers["textDocument/codeAction"] = require "navigator.codeAction".code_action_handler
vim.lsp.handlers["textDocument/definition"] = require "navigator.definition".definition_handler
if cap.declaration then
vim.lsp.handlers["textDocument/declaration"] = require "navigator.definition".declaration_handler
end
vim.lsp.handlers["textDocument/typeDefinition"] = require "navigator.definition".typeDefinition_handler
vim.lsp.handlers["textDocument/implementation"] = require "navigator.implementation".implementation_handler
vim.lsp.handlers["textDocument/documentSymbol"] = require "navigator.symbols".document_symbol_handler
vim.lsp.handlers["workspace/symbol"] = require "navigator.symbols".workspace_symbol_handler
vim.lsp.handlers["textDocument/publishDiagnostics"] = require'navigator.diagnostics'.diagnostic_handler
vim.lsp.handlers["textDocument/publishDiagnostics"] = require'navigator.diagnostics'.diagnostic_handler
local hassig, sig = pcall(require, "lsp_signature")
if not hassig then
vim.lsp.handlers["textDocument/signatureHelp"] = require'navigator.signature'.signature_handler
end
-- vim.lsp.handlers["textDocument/hover"] = require 'navigator.hover'.hover_handler
user_opts = user_opts or {}
user_opts.cap = vim.lsp.protocol.make_client_capabilities()
set_mapping(user_opts)
set_event_handler(user_opts)
local cap = user_opts.cap or {}
if cap.call_hierarchy then
vim.lsp.handlers["callHierarchy/incomingCalls"] =
require"navigator.hierarchy".incoming_calls_handler
vim.lsp.handlers["callHierarchy/outgoingCalls"] =
require"navigator.hierarchy".outgoing_calls_handler
end
vim.lsp.handlers["textDocument/references"] =
require"navigator.reference".reference_handler
vim.lsp.handlers["textDocument/codeAction"] =
require"navigator.codeAction".code_action_handler
vim.lsp.handlers["textDocument/definition"] =
require"navigator.definition".definition_handler
if cap.declaration then
vim.lsp.handlers["textDocument/declaration"] =
require"navigator.definition".declaration_handler
end
vim.lsp.handlers["textDocument/typeDefinition"] =
require"navigator.definition".typeDefinition_handler
vim.lsp.handlers["textDocument/implementation"] =
require"navigator.implementation".implementation_handler
vim.lsp.handlers["textDocument/documentSymbol"] =
require"navigator.symbols".document_symbol_handler
vim.lsp.handlers["workspace/symbol"] =
require"navigator.symbols".workspace_symbol_handler
vim.lsp.handlers["textDocument/publishDiagnostics"] =
require'navigator.diagnostics'.diagnostic_handler
vim.lsp.handlers["textDocument/publishDiagnostics"] =
require'navigator.diagnostics'.diagnostic_handler
local hassig, sig = pcall(require, "lsp_signature")
if not hassig then
vim.lsp.handlers["textDocument/signatureHelp"] =
require'navigator.signature'.signature_handler
end
-- vim.lsp.handlers["textDocument/hover"] = require 'navigator.hover'.hover_handler
end
return M

Loading…
Cancel
Save