{ config, pkgs, ... }: let javaDebug = pkgs.vscode-extensions.vscjava.vscode-java-debug; in { programs.neovim = { enable = true; viAlias = true; vimAlias = true; defaultEditor = true; initLua = '' vim.opt.number = true vim.opt.relativenumber = true vim.opt.tabstop = 2 vim.opt.softtabstop = 2 vim.opt.expandtab = true vim.opt.shiftwidth = 2 vim.opt.smartindent = true vim.opt.wrap = false vim.opt.swapfile = false vim.opt.backup = false vim.opt.undofile = false vim.opt.hlsearch = false vim.opt.incsearch = true vim.opt.termguicolors = true vim.opt.clipboard = "unnamedplus" vim.opt.scrolloff = 999 vim.opt.updatetime = 50 vim.opt.foldmethod = "indent" vim.opt.foldlevel = 99 -- nvim-tree replaces netrw; disable it up front so it never flashes. vim.g.loaded_netrw = 1 vim.g.loaded_netrwPlugin = 1 vim.g.mapleader = " " vim.keymap.set("n", "w", "w", { silent = true }) vim.keymap.set("n", "q", "q", { silent = true }) vim.keymap.set("n", "wq", "wq", { silent = true }) vim.keymap.set("n", "x", "!chmod +x %", { silent = true }) vim.keymap.set("n", "Q", "") vim.keymap.set("n", "o", "o") vim.keymap.set("n", "O", "O") vim.keymap.set("i", "jj", "") local is_dark = vim.o.background ~= "light" require("cyberdream").setup({ transparent = is_dark, variant = is_dark and "dark" or "light", terminal_colors = true, }) vim.cmd.colorscheme("cyberdream") require("nvim-tree").setup({ view = { width = 30 }, renderer = { group_empty = true }, filters = { dotfiles = false }, update_focused_file = { enable = true }, }) require("bufferline").setup({ options = { offsets = { { filetype = "NvimTree", text = "Files", highlight = "Directory", separator = true, }, }, }, }) require("lualine").setup({ options = { theme = "auto" }, }) -- e: open the explorer if closed, focus it if open elsewhere, -- close it if it already has focus. local tree = require("nvim-tree.api").tree vim.keymap.set("n", "e", function() if not tree.is_visible() then tree.open() elseif vim.bo.filetype == "NvimTree" then tree.close() else tree.focus() end end, { silent = true, desc = "Toggle/focus file explorer" }) -- o / p: previous / next buffer in bufferline order -- (left / right). From the explorer, hop back to the previous window -- first so the tree window never gets a file loaded into it. local function cycle(cmd) return function() if vim.bo.filetype == "NvimTree" then vim.cmd("wincmd p") end vim.cmd(cmd) end end vim.keymap.set("n", "o", cycle("BufferLineCyclePrev"), { silent = true, desc = "Buffer left" }) vim.keymap.set("n", "p", cycle("BufferLineCycleNext"), { silent = true, desc = "Buffer right" }) -- c: close the current bufferline tab (buffer), or quit Neovim -- when it's the last one. Switching away before deleting keeps the -- nvim-tree layout intact, and unsaved changes block the close instead -- of being discarded. vim.keymap.set("n", "c", function() if vim.bo.filetype == "NvimTree" then tree.close() return end if not vim.bo.buflisted then pcall(vim.cmd, "close") return end if vim.bo.modified then vim.notify("Unsaved changes; save first (w)", vim.log.levels.WARN) return end local buf = vim.api.nvim_get_current_buf() if #vim.fn.getbufinfo({ buflisted = 1 }) <= 1 then vim.cmd("qall") else vim.cmd("BufferLineCyclePrev") vim.cmd("bdelete " .. buf) end end, { silent = true, desc = "Close buffer / quit if last" }) require("neocord").setup({ logo = "auto", logo_tooltip = "Neovim", main_image = "language", show_time = true, global_timer = true, }) require("gitsigns").setup({ signs = { add = { text = "+" }, change = { text = "~" }, delete = { text = "_" }, }, }) -- nvim-treesitter no longer exposes a configs module; highlighting is -- started per buffer. Indent is deliberately left alone so treesitter -- doesn't fight the Eclipse-matching tab settings below. vim.api.nvim_create_autocmd("FileType", { pattern = { "java", "xml", "nix", "lua", "rs", "go", "gomod", "php", "sql", "html", "css", "scss", "javascript", }, callback = function(args) pcall(vim.treesitter.start, args.buf) end, }) -- Java indentation must match Eclipse's formatter, not the 2-space default. vim.api.nvim_create_autocmd("FileType", { pattern = "java", callback = function() vim.bo.expandtab = false vim.bo.tabstop = 4 vim.bo.shiftwidth = 4 vim.bo.softtabstop = 4 end, }) -- Go indentation must match gofmt: real tabs, displayed 4 wide. vim.api.nvim_create_autocmd("FileType", { pattern = { "go", "gomod" }, callback = function() vim.bo.expandtab = false vim.bo.tabstop = 4 vim.bo.shiftwidth = 4 vim.bo.softtabstop = 4 end, }) -- PHP indentation must match PSR-12 (php-cs-fixer): 4 spaces. vim.api.nvim_create_autocmd("FileType", { pattern = "php", callback = function() vim.bo.expandtab = true vim.bo.tabstop = 4 vim.bo.shiftwidth = 4 vim.bo.softtabstop = 4 end, }) local cmp = require("cmp") local luasnip = require("luasnip") cmp.setup({ snippet = { expand = function(a) luasnip.lsp_expand(a.body) end }, mapping = cmp.mapping.preset.insert({ [""] = cmp.mapping.complete(), [""] = cmp.mapping.confirm({ select = false }), [""] = cmp.mapping.select_next_item(), [""] = cmp.mapping.select_prev_item(), }), sources = { { name = "nvim_lsp" }, { name = "luasnip" }, { name = "buffer" }, { name = "path" }, }, }) local capabilities = require("cmp_nvim_lsp").default_capabilities() -- rustaceanvim auto-starts rust-analyzer on FileType rust, picking it up -- from PATH -- so per-project rust-analyzer pins (e.g. from a project's -- flake devShell/direnv) are used automatically without extra config here. vim.g.rustaceanvim = { server = { capabilities = capabilities, on_attach = function(_, bufnr) local map = function(k, f) vim.keymap.set("n", k, f, { buffer = bufnr, silent = true }) end map("gd", vim.lsp.buf.definition) map("gr", vim.lsp.buf.references) map("K", vim.lsp.buf.hover) map("rn", vim.lsp.buf.rename) map("ca", vim.lsp.buf.code_action) map("f", function() vim.lsp.buf.format({ async = true }) end) end, }, } -- Go: gopls (LSP) + golangci-lint-langserver (lint diagnostics) via -- Neovim's native LSP config, so no nvim-lspconfig plugin is needed. -- Like rust-analyzer above, both binaries are resolved from PATH, so a -- project's flake devShell/direnv pins are used automatically. vim.lsp.config("gopls", { cmd = { "gopls" }, filetypes = { "go", "gomod", "gowork", "gotmpl" }, root_markers = { "go.work", "go.mod", ".git" }, capabilities = capabilities, settings = { gopls = { gofumpt = true, usePlaceholders = true, completeUnimported = true, staticcheck = false, -- golangci-lint already covers this analyses = { unusedparams = true, unusedwrite = true, useany = true, }, hints = { assignVariableTypes = true, compositeLiteralFields = true, constantValues = true, parameterNames = true, rangeVariableTypes = true, }, }, }, on_attach = function(_, bufnr) local map = function(k, f) vim.keymap.set("n", k, f, { buffer = bufnr, silent = true }) end map("gd", vim.lsp.buf.definition) map("gr", vim.lsp.buf.references) map("K", vim.lsp.buf.hover) map("rn", vim.lsp.buf.rename) map("ca", vim.lsp.buf.code_action) map("f", function() vim.lsp.buf.format({ async = true }) end) map("ci", function() vim.lsp.buf.code_action({ context = { only = { "source.organizeImports" }, diagnostics = {} }, apply = true, }) end) end, }) -- golangci-lint v2 syntax (current in nixpkgs). Needs golangci-lint on -- PATH; a project .golangci.yml (with `version: "2"`) controls the rules. vim.lsp.config("golangci_lint_ls", { cmd = { "golangci-lint-langserver" }, filetypes = { "go", "gomod" }, root_markers = { ".golangci.yml", ".golangci.yaml", ".golangci.toml", ".golangci.json", "go.work", "go.mod", ".git" }, capabilities = capabilities, init_options = { command = { "golangci-lint", "run", "--output.json.path", "stdout", "--show-stats=false", "--issues-exit-code=1", }, }, }) vim.lsp.enable({ "gopls", "golangci_lint_ls" }) -- On save: organize imports via gopls, then format (gofumpt via gopls). vim.api.nvim_create_autocmd("BufWritePre", { pattern = "*.go", callback = function(args) local clients = vim.lsp.get_clients({ bufnr = args.buf, name = "gopls" }) local client = clients[1] if not client then return end local params = vim.lsp.util.make_range_params(0, client.offset_encoding) params.context = { only = { "source.organizeImports" } } local result = vim.lsp.buf_request_sync(args.buf, "textDocument/codeAction", params, 1000) for _, res in pairs(result or {}) do for _, action in pairs(res.result or {}) do if action.edit then vim.lsp.util.apply_workspace_edit(action.edit, client.offset_encoding) end end end vim.lsp.buf.format({ bufnr = args.buf, async = false }) end, }) -- PHP / SQL / HTML / CSS / JS: same approach as Go above. Every server -- and tool is resolved from PATH, so the project's flake devShell / -- direnv provides them (launch Neovim from inside that environment). local function web_attach(_, bufnr) local map = function(k, f) vim.keymap.set("n", k, f, { buffer = bufnr, silent = true }) end map("gd", vim.lsp.buf.definition) map("gr", vim.lsp.buf.references) map("K", vim.lsp.buf.hover) map("rn", vim.lsp.buf.rename) map("ca", vim.lsp.buf.code_action) -- conform runs php-cs-fixer / prettierd / sqlfluff, falling back to the LSP. map("f", function() require("conform").format({ async = true, lsp_format = "fallback" }) end) end vim.lsp.config("phpactor", { cmd = { "phpactor", "language-server" }, filetypes = { "php" }, root_markers = { "composer.json", ".phpactor.json", ".phpactor.yml", ".git" }, capabilities = capabilities, on_attach = web_attach, }) -- sqls only gives real results once a connection is defined in its -- config.yml (the root marker below); without one it attaches but is quiet. vim.lsp.config("sqls", { cmd = { "sqls" }, filetypes = { "sql", "mysql" }, root_markers = { "config.yml", ".git" }, capabilities = capabilities, on_attach = web_attach, }) vim.lsp.config("html", { cmd = { "vscode-html-language-server", "--stdio" }, filetypes = { "html", "templ" }, root_markers = { "package.json", ".git" }, capabilities = capabilities, init_options = { provideFormatter = true, embeddedLanguages = { css = true, javascript = true }, configurationSection = { "html", "css", "javascript" }, }, on_attach = web_attach, }) vim.lsp.config("cssls", { cmd = { "vscode-css-language-server", "--stdio" }, filetypes = { "css", "scss", "less" }, root_markers = { "package.json", ".git" }, capabilities = capabilities, init_options = { provideFormatter = true }, settings = { css = { validate = true }, scss = { validate = true }, less = { validate = true }, }, on_attach = web_attach, }) vim.lsp.config("ts_ls", { cmd = { "typescript-language-server", "--stdio" }, filetypes = { "javascript", "javascriptreact", "typescript", "typescriptreact" }, root_markers = { "tsconfig.json", "jsconfig.json", "package.json", ".git" }, capabilities = capabilities, init_options = { hostInfo = "neovim" }, on_attach = web_attach, }) vim.lsp.config("emmet_ls", { cmd = { "emmet-ls", "--stdio" }, filetypes = { "html", "css", "scss", "less", "javascriptreact", "typescriptreact" }, root_markers = { ".git" }, capabilities = capabilities, }) vim.lsp.enable({ "phpactor", "sqls", "html", "cssls", "ts_ls", "emmet_ls" }) -- Linters (diagnostics): run on open and save. phpstan can be slow on -- big projects, so it is deliberately not run on every keystroke. -- sqlfluff needs a dialect, set it in a project .sqlfluff file. local lint = require("lint") lint.linters_by_ft = { php = { "phpstan", "phpcs" }, sql = { "sqlfluff" }, css = { "stylelint" }, scss = { "stylelint" }, less = { "stylelint" }, javascript = { "eslint_d" }, javascriptreact = { "eslint_d" }, typescript = { "eslint_d" }, typescriptreact = { "eslint_d" }, } vim.api.nvim_create_autocmd({ "BufReadPost", "BufWritePost" }, { callback = function() lint.try_lint() end, }) -- Formatters (f via web_attach). require("conform").setup({ formatters_by_ft = { php = { "php_cs_fixer" }, sql = { "sqlfluff" }, html = { "prettierd" }, css = { "prettierd" }, scss = { "prettierd" }, less = { "prettierd" }, javascript = { "prettierd" }, javascriptreact = { "prettierd" }, typescript = { "prettierd" }, typescriptreact = { "prettierd" }, json = { "prettierd" }, }, }) local bundles = vim.fn.glob( "${javaDebug}/share/vscode/extensions/vscjava.vscode-java-debug/server/com.microsoft.java.debug.plugin-*.jar", true, true) if #bundles == 0 then vim.notify("java-debug bundle not found - Java debugging disabled", vim.log.levels.WARN) end local java_home = os.getenv("JAVA_HOME") if not java_home then local javac = vim.fn.exepath("javac") if javac ~= "" then java_home = vim.fn.fnamemodify(vim.fn.resolve(javac), ":h:h") end end if not java_home then vim.notify("No JAVA_HOME and no javac on PATH - jdtls will guess a runtime", vim.log.levels.WARN) end if not java_home then vim.notify("JAVA_HOME unset - jdtls will guess a runtime", vim.log.levels.WARN) end -- Eclipse metadata wins over build files, build files over .git, and the -- file's own directory is the last resort so bare folders still attach. local function find_project_root() local setup = require("jdtls.setup") return setup.find_root({ ".project", ".classpath" }) or setup.find_root({ "pom.xml", "build.gradle", "build.gradle.kts" }) or setup.find_root({ ".git" }) or vim.fn.expand("%:p:h") end local dap_configured = false local function start_jdtls() local jdtls = require("jdtls") local root_dir = find_project_root() local project_name = vim.fn.fnamemodify(root_dir, ":p:h:t") .. "-" .. vim.fn.sha256(root_dir):sub(1, 8) local workspace = vim.fn.stdpath("cache") .. "/jdtls-workspaces/" .. project_name local java_settings = { configuration = { updateBuildConfiguration = "automatic", }, -- Keep a stray pom.xml from overriding the Eclipse project model. -- Flip these on if a course actually requires Maven or Gradle. import = { maven = { enabled = false }, gradle = { enabled = false }, }, project = { outputPath = "bin", referencedLibraries = { "lib/**/*.jar" }, }, signatureHelp = { enabled = true }, eclipse = { downloadSources = true }, } if java_home then java_settings.configuration.runtimes = { { name = "JavaSE-21", path = java_home, default = true }, } end -- If the repo carries an exported Eclipse formatter profile, use it verbatim. local formatter = root_dir .. "/.settings/eclipse-formatter.xml" if vim.uv.fs_stat(formatter) then java_settings.format = { settings = { url = formatter } } end jdtls.start_or_attach({ cmd = { "jdtls", "-data", workspace }, root_dir = root_dir, capabilities = capabilities, settings = { java = java_settings }, init_options = { bundles = bundles }, on_attach = function(_, bufnr) require("jdtls").setup_dap({ hotcodereplace = "auto" }) if not dap_configured then dap_configured = true require("jdtls.dap").setup_dap_main_class_configs() end local map = function(k, f) vim.keymap.set("n", k, f, { buffer = bufnr, silent = true }) end map("gd", vim.lsp.buf.definition) map("gr", vim.lsp.buf.references) map("K", vim.lsp.buf.hover) map("rn", vim.lsp.buf.rename) map("ca", vim.lsp.buf.code_action) map("f", function() vim.lsp.buf.format({ async = true }) end) map("ci", require("jdtls").organize_imports) end, }) end vim.api.nvim_create_autocmd("FileType", { pattern = "java", callback = start_jdtls, }) local dap, dapui = require("dap"), require("dapui") dapui.setup() require("nvim-dap-virtual-text").setup() -- `jtest --debug` suspends the test JVM waiting on :5005; this lets -- dc attach to it directly from the config picker. dap.configurations.java = dap.configurations.java or {} table.insert(dap.configurations.java, { type = "java", request = "attach", name = "Attach to jtest (:5005)", hostName = "127.0.0.1", port = 5005, }) dap.listeners.after.event_initialized["dapui"] = function() dapui.open() end dap.listeners.before.event_terminated["dapui"] = function() dapui.close() end dap.listeners.before.event_exited["dapui"] = function() dapui.close() end vim.keymap.set("n", "db", dap.toggle_breakpoint) vim.keymap.set("n", "dB", function() dap.set_breakpoint(vim.fn.input("Condition: ")) end) vim.keymap.set("n", "dc", dap.continue) vim.keymap.set("n", "dn", dap.step_over) vim.keymap.set("n", "di", dap.step_into) vim.keymap.set("n", "du", dap.step_out) vim.keymap.set("n", "dr", dap.repl.toggle) vim.keymap.set("n", "dq", dap.terminate) vim.keymap.set({ "n", "v" }, "de", function() dapui.eval() end) ''; plugins = with pkgs.vimPlugins; [ { plugin = cyberdream-nvim; } { plugin = nvim-web-devicons; } { plugin = nvim-tree-lua; } { plugin = bufferline-nvim; } { plugin = lualine-nvim; } { plugin = neocord; } { plugin = gitsigns-nvim; } { plugin = nvim-jdtls; } { plugin = nvim-dap; } { plugin = nvim-dap-ui; } { plugin = nvim-nio; } { plugin = nvim-dap-virtual-text; } { plugin = nvim-cmp; } { plugin = cmp-nvim-lsp; } { plugin = cmp-buffer; } { plugin = cmp-path; } { plugin = luasnip; } { plugin = cmp_luasnip; } { plugin = rustaceanvim; } { plugin = nvim-lint; } { plugin = conform-nvim; } { plugin = (nvim-treesitter.withPlugins (p: [ p.java p.xml p.nix p.lua p.rust p.go p.gomod p.php p.sql p.html p.css p.javascript ])); } { plugin = vim-obsession; } ]; }; }