diff --git a/init.lua b/init.lua index 513f850..e563a3b 100644 --- a/init.lua +++ b/init.lua @@ -21,6 +21,17 @@ vim.o.termguicolors = true -- vim.cmd "let g:everforest_background = 'hard'" vim.cmd.colorscheme('nordfox') +-- LSP -- +vim.o.winborder = 'rounded' +vim.lsp.enable({'intelephense'}) +vim.lsp.enable({'drupal-lsp'}) +vim.lsp.enable({'rust_analyzer'}) +vim.lsp.enable({'elixirls'}) +vim.lsp.enable({'lexical'}) +vim.keymap.set("n", "K", vim.lsp.buf.hover, {}) +vim.keymap.set("n", "gd", vim.lsp.buf.definition, {}) +vim.keymap.set({ "n" }, "ca", vim.lsp.buf.code_action, {}) + -- XDEBUG -- local dap = require('dap') require('telescope').load_extension('dap') @@ -50,13 +61,6 @@ dap.configurations.php = { } } --- vim.cmd [[ --- highlight Normal guibg=none --- highlight NonText guibg=none --- highlight Normal ctermbg=none --- highlight NonText ctermbg=none --- ]] - vim.fn.sign_define('DapBreakpoint',{ text ='🟥', texthl ='', linehl ='', numhl =''}) vim.fn.sign_define('DapStopped',{ text ='▶️', texthl ='', linehl ='', numhl =''}) vim.keymap.set('n', '?', function() dap.continue() end) @@ -72,17 +76,17 @@ vim.keymap.set('n', 'db', function() widgets.centered_float(widgets.scopes) end) -local lspconfig = require 'lspconfig' -local configs = require 'lspconfig.configs' -if not configs.drupal then - configs.drupal = { - default_config = { - cmd = {'drupal-lsp'}, - filetypes = {'php', 'module', 'inc', 'theme'}, - root_dir = function(fname) - return lspconfig.util.root_pattern('composer.json', '.git')(fname) - end - }; - } -end -lspconfig.drupal.setup{ autostart = true } +-- local lspconfig = require 'lspconfig' +-- local configs = require 'lspconfig.configs' +-- if not configs.drupal then +-- configs.drupal = { +-- default_config = { +-- cmd = {'drupal-lsp'}, +-- filetypes = {'php', 'module', 'inc', 'theme'}, +-- root_dir = function(fname) +-- return lspconfig.util.root_pattern('composer.json', '.git')(fname) +-- end +-- }; +-- } +-- end +-- lspconfig.drupal.setup{ autostart = true } diff --git a/lazy-lock.json b/lazy-lock.json index 197b8c4..2119422 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -20,7 +20,6 @@ "nvim-dap": { "branch": "master", "commit": "8df427aeba0a06c6577dc3ab82de3076964e3b8d" }, "nvim-dap-ui": { "branch": "master", "commit": "73a26abf4941aa27da59820fd6b028ebcdbcf932" }, "nvim-dap-virtual-text": { "branch": "master", "commit": "df66808cd78b5a97576bbaeee95ed5ca385a9750" }, - "nvim-lspconfig": { "branch": "master", "commit": "ac1dfbe3b60e5e23a2cff90e3bd6a3bc88031a57" }, "nvim-nio": { "branch": "master", "commit": "21f5324bfac14e22ba26553caf69ec76ae8a7662" }, "nvim-treesitter": { "branch": "master", "commit": "066fd6505377e3fd4aa219e61ce94c2b8bdb0b79" }, "nvim-web-devicons": { "branch": "master", "commit": "1fb58cca9aebbc4fd32b086cb413548ce132c127" }, diff --git a/lsp/drupal.lua b/lsp/drupal.lua new file mode 100644 index 0000000..7fba41a --- /dev/null +++ b/lsp/drupal.lua @@ -0,0 +1,12 @@ +return { + cmd = {'drupal-lsp'}, + filetypes = {'php', 'module', 'inc', 'theme'}, + root_dir = function(bufnr, on_dir) + local fname = vim.api.nvim_buf_get_name(bufnr) + local cwd = assert(vim.uv.cwd()) + local root = vim.fs.root(fname, { 'composer.json', '.git' }) + + -- prefer cwd if root is a descendant + on_dir(root and vim.fs.relpath(cwd, root) and cwd) + end, +} diff --git a/lsp/elixirls.lua b/lsp/elixirls.lua new file mode 100644 index 0000000..f377116 --- /dev/null +++ b/lsp/elixirls.lua @@ -0,0 +1,11 @@ +return { + filetypes = { 'elixir', 'eelixir', 'heex', 'surface' }, + root_dir = function(bufnr, on_dir) + local fname = vim.api.nvim_buf_get_name(bufnr) + local matches = vim.fs.find({ 'mix.exs' }, { upward = true, limit = 2, path = fname }) + local child_or_root_path, maybe_umbrella_path = unpack(matches) + local root_dir = vim.fs.dirname(maybe_umbrella_path or child_or_root_path) + + on_dir(root_dir) + end, +} diff --git a/lsp/intelephense.lua b/lsp/intelephense.lua new file mode 100644 index 0000000..a98bf8c --- /dev/null +++ b/lsp/intelephense.lua @@ -0,0 +1,12 @@ +return { + cmd = { 'intelephense', '--stdio' }, + filetypes = { 'php' }, + root_dir = function(bufnr, on_dir) + local fname = vim.api.nvim_buf_get_name(bufnr) + local cwd = assert(vim.uv.cwd()) + local root = vim.fs.root(fname, { 'composer.json', '.git' }) + + -- prefer cwd if root is a descendant + on_dir(root and vim.fs.relpath(cwd, root) and cwd) + end, +} diff --git a/lsp/lexical.lua b/lsp/lexical.lua new file mode 100644 index 0000000..25f9c3f --- /dev/null +++ b/lsp/lexical.lua @@ -0,0 +1,4 @@ +return { + filetypes = { 'elixir', 'eelixir', 'heex', 'surface' }, + root_markers = { 'mix.exs', '.git' }, +} diff --git a/lsp/rust_analyzer.lua b/lsp/rust_analyzer.lua new file mode 100644 index 0000000..d42eebe --- /dev/null +++ b/lsp/rust_analyzer.lua @@ -0,0 +1,96 @@ +local function reload_workspace(bufnr) + local clients = vim.lsp.get_clients { bufnr = bufnr, name = 'rust_analyzer' } + for _, client in ipairs(clients) do + vim.notify 'Reloading Cargo Workspace' + client.request('rust-analyzer/reloadWorkspace', nil, function(err) + if err then + error(tostring(err)) + end + vim.notify 'Cargo workspace reloaded' + end, 0) + end +end + +local function is_library(fname) + local user_home = vim.fs.normalize(vim.env.HOME) + local cargo_home = os.getenv 'CARGO_HOME' or user_home .. '/.cargo' + local registry = cargo_home .. '/registry/src' + local git_registry = cargo_home .. '/git/checkouts' + + local rustup_home = os.getenv 'RUSTUP_HOME' or user_home .. '/.rustup' + local toolchains = rustup_home .. '/toolchains' + + for _, item in ipairs { toolchains, registry, git_registry } do + if vim.fs.relpath(item, fname) then + local clients = vim.lsp.get_clients { name = 'rust_analyzer' } + return #clients > 0 and clients[#clients].config.root_dir or nil + end + end +end + +return { + cmd = { 'rust-analyzer' }, + filetypes = { 'rust' }, + root_dir = function(bufnr, on_dir) + local fname = vim.api.nvim_buf_get_name(bufnr) + local reused_dir = is_library(fname) + if reused_dir then + on_dir(reused_dir) + return + end + + local cargo_crate_dir = vim.fs.root(fname, { 'Cargo.toml' }) + local cargo_workspace_root + + if cargo_crate_dir == nil then + on_dir( + vim.fs.root(fname, { 'rust-project.json' }) + or vim.fs.dirname(vim.fs.find('.git', { path = fname, upward = true })[1]) + ) + return + end + + local cmd = { + 'cargo', + 'metadata', + '--no-deps', + '--format-version', + '1', + '--manifest-path', + cargo_crate_dir .. '/Cargo.toml', + } + + vim.system(cmd, { text = true }, function(output) + if output.code == 0 then + if output.stdout then + local result = vim.json.decode(output.stdout) + if result['workspace_root'] then + cargo_workspace_root = vim.fs.normalize(result['workspace_root']) + end + end + + on_dir(cargo_workspace_root or cargo_crate_dir) + else + vim.schedule(function() + vim.notify(('[rust_analyzer] cmd failed with code %d: %s\n%s'):format(output.code, cmd, output.stderr)) + end) + end + end) + end, + capabilities = { + experimental = { + serverStatusNotification = true, + }, + }, + before_init = function(init_params, config) + -- See https://github.com/rust-lang/rust-analyzer/blob/eb5da56d839ae0a9e9f50774fa3eb78eb0964550/docs/dev/lsp-extensions.md?plain=1#L26 + if config.settings and config.settings['rust-analyzer'] then + init_params.initializationOptions = config.settings['rust-analyzer'] + end + end, + on_attach = function() + vim.api.nvim_buf_create_user_command(0, 'LspCargoReload', function() + reload_workspace(0) + end, { desc = 'Reload current cargo workspace' }) + end, +} diff --git a/lua/dan/core.lua b/lua/dan/core.lua index b1268a1..fe2f90c 100644 --- a/lua/dan/core.lua +++ b/lua/dan/core.lua @@ -33,10 +33,6 @@ vim.keymap.set("n", "tt", function() job_id = vim.bo.channel end) -vim.keymap.set("n", "build", function() - vim.fn.chansend(job_id, { "ls -lah" }) -end) - -- General config vim.cmd("set expandtab") vim.cmd("set tabstop=2") @@ -45,12 +41,7 @@ vim.cmd("set shiftwidth=2") vim.cmd("set syntax=on") vim.cmd("set number") vim.cmd("set nu") --- vim.cmd("set relativenumber") --- vim.cmd("set rnu") --- vim.cmd("set number relativenumber") --- vim.cmd("set nu rnu") --- vim.cmd("set guicursor=n-v-c-i:block") --- vim.cmd("set guicursor=a:blinkon100") +-- vim.cmd("set guicursor=n-v-c:block") vim.api.nvim_set_option("clipboard","unnamed") -- Command to turn off diagnostics, if crowding screen diff --git a/lua/plugins/lsp-config.lua b/lua/plugins/lsp-config.lua index ea8bb9c..7b87e72 100644 --- a/lua/plugins/lsp-config.lua +++ b/lua/plugins/lsp-config.lua @@ -9,27 +9,8 @@ return { "williamboman/mason-lspconfig.nvim", config = function() require("mason-lspconfig").setup({ - ensure_installed = { "lua_ls", "phpactor", "rust_analyzer" }, + ensure_installed = { "lua_ls", "intelephense", "rust_analyzer", "elixirls" }, }) end, }, - { - "neovim/nvim-lspconfig", - config = function() - local capabilities = require("cmp_nvim_lsp").default_capabilities() - local lspconfig = require("lspconfig") - lspconfig.lua_ls.setup({ - capabilities = capabilities, - }) - lspconfig.phpactor.setup({ - capabilities = capabilities, - }) - lspconfig.rust_analyzer.setup({ - capabilities = capabilities, - }) - vim.keymap.set("n", "K", vim.lsp.buf.hover, {}) - vim.keymap.set("n", "gd", vim.lsp.buf.definition, {}) - vim.keymap.set({ "n" }, "ca", vim.lsp.buf.code_action, {}) - end, - }, } diff --git a/plugin/danterm.lua b/plugin/danterm.lua deleted file mode 100644 index 25103e2..0000000 --- a/plugin/danterm.lua +++ /dev/null @@ -1,54 +0,0 @@ -local t_opts = {silent = true} -vim.keymap.set('t', '', '', t_opts) -vim.keymap.set('t', '', 'Danterm') -vim.keymap.set('n', 'dt', 'Danterm') - -local state = { - floating = { - buf = -1, - win = -1, - } -} - -local function dan_term(opts) - opts = opts or {} - local width = opts.width or math.floor(vim.o.columns * 0.8) - local height = opts.height or math.floor(vim.o.lines * 0.8) - local screen_width = vim.o.columns - local screen_height = vim.o.lines - local col = math.floor((screen_width - width) / 2) - local row = math.floor((screen_height - height) / 2) - - local buf = nil - if vim.api.nvim_buf_is_valid(opts.buf) then - buf = opts.buf - else - buf = vim.api.nvim_create_buf(false, true) - end - - local win_config = { - relative = 'editor', - width = width, - height = height, - col = col, - row = row, - style = 'minimal', - border = 'rounded', -- optional, choose 'none', 'single', 'double', etc. - } - - local win = vim.api.nvim_open_win(buf, true, win_config) - - return { buf = buf, win = win } -end - -vim.api.nvim_create_user_command("Danterm", function() - if not vim.api.nvim_win_is_valid(state.floating.win) then - state.floating = dan_term { buf = state.floating.buf } - if vim.bo[state.floating.buf].buftype ~= "terminal" then - vim.cmd.term() - vim.api.nvim_input("i") - end - else - vim.api.nvim_win_hide(state.floating.win) - end -end, {})