Skip to content

Commit

Permalink
ref(lsp)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjeusel committed Mar 18, 2023
1 parent de0b822 commit 7436011
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 140 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@

- Use [zsh/zprof](https://stevenvanbael.com/profiling-zsh-startup) module
- Use `set -x` at top of zshrc to echo line by line

#### Neovim:

- [Lua Type Hint](https://github.com/LuaLS/lua-language-server/wiki/Annotations)
32 changes: 32 additions & 0 deletions dotfiles/.config/nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ require("lazy").setup({ import = "wax.plugins" }, {
custom_keys = false,
checker = { enabled = false }, -- background plugin update checker
change_detection = { enabled = true, notify = false },
ui = {
border = "rounded",
icons = {
cmd = "",
config = "",
event = "",
ft = "",
init = "",
import = "",
keys = "",
lazy = "󰒲 ",
loaded = "",
not_loaded = "",
plugin = "",
runtime = "",
source = "",
start = "",
task = "",
list = {
"",
"",
"",
"",
},
},
},
performance = {
rtp = {
disabled_plugins = {
Expand All @@ -60,6 +86,12 @@ require("lazy").setup({ import = "wax.plugins" }, {
},
},
},
-- custom_keys = {
-- -- open lazy git ui
-- ["<leader>fl"] = function()
-- require("lazy").home()
-- end,
-- },
})

safe_require("wax.folds")
91 changes: 31 additions & 60 deletions dotfiles/.config/nvim/lua/wax/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,76 +117,47 @@ vim.api.nvim_create_autocmd("LspAttach", {
end,
})

local function generate_handlers()
local default_on_attach = lsp_status.on_attach

local default_handler = function(server_name)
require("lspconfig")[server_name].setup({
capabilities = capabilities,
on_attach = default_on_attach,
})
end

local handlers = { default_handler }
local default_on_attach = lsp_status.on_attach

local function lspconfig_setup()
local scan = require("plenary.scandir")

local server_with_custom_config = vim.tbl_map(function(server_file)
return vim.fn.fnamemodify(server_file, ":t:r")
end, scan.scan_dir(lua_waxdir .. "/lsp/servers", { depth = 1 }))

for _, server_name in ipairs(server_with_custom_config) do
handlers[server_name] = function()
local server_opts = require(("wax.lsp.servers.%s"):format(server_name))
local on_attach = default_on_attach
if server_opts.on_attach then
on_attach = function(client, bufnr)
default_on_attach(client)
server_opts.on_attach(client, bufnr)
end
local server_opts = require(("wax.lsp.servers.%s"):format(server_name))

-- Maybe combine both on_attach
local on_attach = default_on_attach
if server_opts.on_attach then
on_attach = function(client, bufnr)
default_on_attach(client)
server_opts.on_attach(client, bufnr)
end
require("lspconfig")[server_name].setup(
vim.tbl_deep_extend(
"keep",
{ capabilities = capabilities, on_attach = on_attach },
server_opts
)
)
end
end

return handlers
-- log.warn("Setting up", server_name, "with", server_opts)
require("lspconfig")[server_name].setup(
vim.tbl_deep_extend(
"keep",
{ capabilities = capabilities, on_attach = on_attach },
server_opts
)
)
end
end

local handlers = generate_handlers()

-- -- make sure to configure lspconfig before actual setup_handlers of mason-lspconfig
-- -- so we define our custom servers, and make the on_new_config works
-- vim.tbl_map(function(handler)
-- if type(handler) == "function" then
-- handler()
-- end
-- end,
-- handlers
-- )

-- Register homemade LSP servers (mypygls):
local lspconfig = require("lspconfig")
local configs = require("lspconfig.configs")

configs.mypygls = {
default_config = {
cmd = { "mypygls" },
filetypes = { "python" },
root_dir = function(fname)
return lspconfig.util.find_git_ancestor(fname)
end,
settings = {},
},
}

-- map it in mason-lspconfig
local server_mapping = require("mason-lspconfig.mappings.server")
server_mapping.lspconfig_to_package["mypygls"] = "mypygls"

require("mason-lspconfig").setup_handlers(handlers)
-- call lspconfig setup with our custom servers configs
lspconfig_setup()

-- -- Don't call the setup_handlers as it messes everything
-- require("mason-lspconfig").setup_handlers({
-- function(server_name)
-- require("lspconfig")[server_name].setup({
-- capabilities = capabilities,
-- on_attach = default_on_attach,
-- })
-- end,
-- })
34 changes: 4 additions & 30 deletions dotfiles/.config/nvim/lua/wax/lsp/null-ls.lua
Original file line number Diff line number Diff line change
@@ -1,41 +1,16 @@
local u = require("null-ls.utils")
local s = require("null-ls.state")
local builtins = require("null-ls.builtins")
local cmd_resolver = require("null-ls.helpers.command_resolver")

-- local methods = require("null-ls.methods")

local python_utils = require("wax.lsp.python-utils")

local function from_python_env(params)
local resolved = s.get_cache(params.bufnr, params.command)
if resolved then
if resolved.command then
log.debug(
string.format(
"Using cached value [%s] as the resolved command for [%s]",
resolved.command,
params.command
)
)
end
return resolved.command
end

local from_python_env = wax_cache_fn(function(params)
local workspace = u.get_root()
local cmd_for_env = python_utils.get_python_path(workspace, params.command)
resolved = { command = cmd_for_env, cwd = workspace }

s.set_cache(params.bufnr, params.command, resolved)
return resolved.command
end

local function from_node_modules(params)
local base_fn = cmd_resolver.from_node_modules()
local result = base_fn(params)
dumpf(result)
return result
end
local cmd = python_utils.get_python_path(workspace, params.command)
return cmd
end, { arg_table_key = "command" })

local eslint_filetypes = {
"javascript",
Expand All @@ -48,7 +23,6 @@ local eslint_filetypes = {
local eslint_cfg = {
filetypes = eslint_filetypes,
dynamic_command = cmd_resolver.from_node_modules(),
-- dynamic_command = from_node_modules,
}

local prettier_filetypes = vim.list_extend(vim.deepcopy(eslint_filetypes), { "yaml" })
Expand Down
7 changes: 6 additions & 1 deletion dotfiles/.config/nvim/lua/wax/lsp/python-utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,14 @@ local function find_python_cmd(workspace, cmd)
return cmd
end

M.get_python_path = function(workspace, cmd)
function M.get_python_path(workspace, cmd)
workspace = workspace or find_workspace_name(vim.api.nvim_buf_get_name(0))
cmd = cmd or "python"

if workspace == nil then
return cmd
end

local python_path = nil

if string.find(workspace, M.basepath_poetry_venv) then
Expand Down
3 changes: 1 addition & 2 deletions dotfiles/.config/nvim/lua/wax/lsp/servers/html.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
return {
filetypes = { "html", "markdown" },
settings = { documentFormatting = false },
init_options = { provideFormatter = false },
settings = { documentFormattingProvider = false },
}
39 changes: 38 additions & 1 deletion dotfiles/.config/nvim/lua/wax/lsp/servers/mypygls.lua
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
return {}
-- Register homemade LSP servers (mypygls):
local lspconfig = require("lspconfig")
local configs = require("lspconfig.configs")

configs.mypygls = {
default_config = {
cmd = { "mypygls" },
filetypes = { "python" },
root_dir = function(fname)
return lspconfig.util.find_git_ancestor(fname)
end,
settings = {},
},
}

-- map it in mason-lspconfig
require("mason-lspconfig.mappings.server").lspconfig_to_package["mypygls"] = "mypygls"

local python_utils = require("wax.lsp.python-utils")

return {
on_new_config = function(config, new_workspace)
local python_path = python_utils.get_python_path(new_workspace)
local new_workspace_name = to_workspace_name(new_workspace)

if python_path == "python" then
local msg = "LSP python (mypygls) - keeping previous python path '%s' for new_root_dir '%s'"
log.debug(msg:format(config.cmd[1], new_workspace))
return config
else
local msg = "LSP python (mypygls) - '%s' using path %s"
log.info(msg:format(new_workspace_name, python_path))

config.cmd = { python_path, "-m", "mypygls" }
return config
end
end,
}
2 changes: 1 addition & 1 deletion dotfiles/.config/nvim/lua/wax/lsp/servers/pylsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ local function to_pylsp_cmd(python_path)
end

return {
-- cmd = to_pylsp_cmd(python_utils.get_python_path()),
-- if python format by efm, disable formatting capabilities for pylsp
on_attach = function(client, _)
-- formatting is done by null-ls
client.server_capabilities.documentFormattingProvider = false
client.server_capabilities.documentRangeFormattingProvider = false
end,
-- cmd = to_pylsp_cmd(python_utils.get_python_path(nil, "python")),
settings = {
pylsp = {
-- https://github.com/python-lsp/python-lsp-server/blob/develop/CONFIGURATION.md
Expand Down
1 change: 0 additions & 1 deletion dotfiles/.config/nvim/lua/wax/lsp/servers/pyright.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ return {
settings = {
python = {
-- https://github.com/microsoft/pyright/blob/main/docs/settings.md
-- pythonPath = python_utils.get_python_path(),
disableOrganizeImports = true,
analysis = {
-- diagnosticMode = "workspace",
Expand Down
2 changes: 1 addition & 1 deletion dotfiles/.config/nvim/lua/wax/lsp/servers/ruff_lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ return {
filetypes = { "python" },
settings = {
-- https://github.com/charliermarsh/ruff-lsp#settings
-- interpreter = {},
interpreter = { python_utils.get_python_path() },
organizeImports = false,
},
on_new_config = function(config, new_workspace)
Expand Down
13 changes: 0 additions & 13 deletions dotfiles/.config/nvim/lua/wax/lsp/servers/sqls.lua

This file was deleted.

3 changes: 3 additions & 0 deletions dotfiles/.config/nvim/lua/wax/lsp/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ vim.diagnostic.config({
update_in_insert = true,
severity_sort = true,
})

-- change border of LspInfo:
require("lspconfig.ui.windows").default_options.border = "rounded"
7 changes: 6 additions & 1 deletion dotfiles/.config/nvim/lua/wax/plugcfg/grapple.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
local grapple = require("grapple")

local loglevel = waxopts.loglevel
if loglevel == "trace" then
loglevel = "debug"
end

grapple.setup({
---@type "debug" | "info" | "warn" | "error"
log_level = waxopts.loglevel,
log_level = loglevel,
-- log_level = "debug",

---The scope used when creating, selecting, and deleting tags
Expand Down
23 changes: 4 additions & 19 deletions dotfiles/.config/nvim/lua/wax/plugcfg/lualine.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
local function fn_cache(fn)
local cache = {}

local function cached_fn()
local abspath = vim.api.nvim_buf_get_name(0)
if vim.tbl_contains(vim.tbl_keys(cache), abspath) then
return cache[abspath]
end
local result = fn()
cache[abspath] = result
end

return cached_fn
end

local workspace_name = fn_cache(function()
local workspace_name = wax_cache_fn(function()
return find_workspace_name() or ""
end)
end, {})

local relative_path = fn_cache(function()
local relative_path = wax_cache_fn(function()
local abspath = vim.api.nvim_buf_get_name(0)
local workspace = find_root_dir(abspath)
local Path = require("plenary.path")
Expand All @@ -30,7 +15,7 @@ local relative_path = fn_cache(function()
end)

local function diagnostics()
if #vim.lsp.buf_get_clients() > 0 then
if #vim.lsp.get_active_clients() > 0 then
return require("lsp-status").status()
else
return ""
Expand Down
Loading

0 comments on commit 7436011

Please sign in to comment.