From e6f397f4be82f976f1ee00f41e1a26eb614883a2 Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Mon, 4 Dec 2023 06:25:00 +0100 Subject: [PATCH] Set up conform.nvim --- devshells/default.nix | 1 + devshells/lua.nix | 8 ++++ devshells/purescript.nix | 2 +- home/features/neovim/config/lazy-lock.json | 2 +- .../config/lua/my/abbreviations/unicode.lua | 5 ++- .../config/lua/my/plugins/format-nvim.lua | 42 ------------------- .../config/lua/my/plugins/lspconfig.lua | 26 +++++++----- .../neovim/config/lua/my/plugins/null-ls.lua | 14 ------- home/features/neovim/default.nix | 21 +++++++++- modules/common/neovim.nix | 22 ++++++++-- shell.nix | 2 +- 11 files changed, 68 insertions(+), 77 deletions(-) create mode 100644 devshells/lua.nix delete mode 100644 home/features/neovim/config/lua/my/plugins/format-nvim.lua diff --git a/devshells/default.nix b/devshells/default.nix index 54d9d58..f3e6733 100644 --- a/devshells/default.nix +++ b/devshells/default.nix @@ -3,5 +3,6 @@ args: { purescript = import ./purescript.nix args; rwtw = import ./rwtw.nix args; typst = import ./typst.nix args; + lua = import ./lua.nix args; } diff --git a/devshells/lua.nix b/devshells/lua.nix new file mode 100644 index 0000000..2552eaf --- /dev/null +++ b/devshells/lua.nix @@ -0,0 +1,8 @@ +{ pkgs, ... }: +pkgs.mkShell { + pacakges = with pkgs; [ + stylua + lua-language-server + lua + ]; +} diff --git a/devshells/purescript.nix b/devshells/purescript.nix index 02b7e1a..1bb8897 100644 --- a/devshells/purescript.nix +++ b/devshells/purescript.nix @@ -1,5 +1,5 @@ # shell containing the tools i most commonly use for purescript work! { pkgs, ... }: pkgs.mkShell { - nativebuildinputs = with pkgs; [ purescript spago typescript nodejs ]; + packages = with pkgs; [ purescript spago typescript nodejs ]; } diff --git a/home/features/neovim/config/lazy-lock.json b/home/features/neovim/config/lazy-lock.json index 1e85622..4eee3ff 100644 --- a/home/features/neovim/config/lazy-lock.json +++ b/home/features/neovim/config/lazy-lock.json @@ -9,12 +9,12 @@ "cmp-nvim-lsp": { "branch": "main", "commit": "44b16d11215dce86f253ce0c30949813c0a90765" }, "cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" }, "cmp_luasnip": { "branch": "master", "commit": "18095520391186d634a0045dacaa346291096566" }, + "conform": { "branch": "master", "commit": "5bf1405fd234d469243ea6f394e0aeec9ea53bd8" }, "crates.nvim": { "branch": "main", "commit": "d5caf28aba49e81ac4099426231f3cf3c151013a" }, "dhall-vim": { "branch": "master", "commit": "68500ef46ff3706f46c99db3be7a0c8abcf6a3ae" }, "dressing.nvim": { "branch": "master", "commit": "6bde51adabba06f7fd4a469885a85f36d78a5f52" }, "fidget.nvim": { "branch": "main", "commit": "0ba1e16d07627532b6cae915cc992ecac249fb97" }, "flash": { "branch": "main", "commit": "48817af25f51c0590653bbc290866e4890fe1cbe" }, - "formatter.nvim": { "branch": "master", "commit": "44c89f09dcc220dc2a9b056e93c3a87c86e79804" }, "github-actions-yaml.vim": { "branch": "master", "commit": "f2f16243447cea174daa6b4a9ffd3ff9213814ef" }, "gitlinker.nvim": { "branch": "master", "commit": "cc59f732f3d043b626c8702cb725c82e54d35c25" }, "gitsigns.nvim": { "branch": "main", "commit": "af0f583cd35286dd6f0e3ed52622728703237e50" }, diff --git a/home/features/neovim/config/lua/my/abbreviations/unicode.lua b/home/features/neovim/config/lua/my/abbreviations/unicode.lua index 6b4c59c..6a103e9 100644 --- a/home/features/neovim/config/lua/my/abbreviations/unicode.lua +++ b/home/features/neovim/config/lua/my/abbreviations/unicode.lua @@ -6,7 +6,10 @@ local M = {} M.unicode = { -- {{{ Logic { "frl", "∀" }, -- [f]o[r]al[l] - { "exs", "∃" }, -- [e][x]ist[s] + + + + { "exs", "∃" }, -- [e][x]ist[s] { "land", "∧" }, -- [l]ogical [and] { "Land", "⋀" }, -- arbitrary [l]ogical [and] { "lor", "∨" }, -- [l]ogical [or] diff --git a/home/features/neovim/config/lua/my/plugins/format-nvim.lua b/home/features/neovim/config/lua/my/plugins/format-nvim.lua deleted file mode 100644 index 0f1b6b5..0000000 --- a/home/features/neovim/config/lua/my/plugins/format-nvim.lua +++ /dev/null @@ -1,42 +0,0 @@ -local env = require("my.helpers.env") -local K = require("my.keymaps") - -local M = { - "mhartington/formatter.nvim", - event = "BufReadPre", - cond = env.vscode.not_active(), -} - -function M.config() - local any = require("formatter.filetypes.any") - local formatters = { - markdown = { - require("formatter.filetypes.markdown").prettier, - }, - lua = { - require("formatter.filetypes.lua").stylua, - }, - ["*"] = { - any.remove_trailing_whitespace, - }, - } - - require("formatter").setup({ filetype = formatters }) - - local format = function() - if formatters[vim.bo.filetype] ~= nil then - vim.cmd([[Format]]) - elseif next(vim.lsp.get_active_clients({ bufnr = 0 })) == nil then - vim.lsp.buf.format() - end - end - - K.nmap("F", format, "[F]ormat file") - - vim.api.nvim_create_autocmd("BufWritePre", { - group = vim.api.nvim_create_augroup("Formatting", { clear = false }), - callback = format, - }) -end - -return M diff --git a/home/features/neovim/config/lua/my/plugins/lspconfig.lua b/home/features/neovim/config/lua/my/plugins/lspconfig.lua index e900194..5fa7ced 100644 --- a/home/features/neovim/config/lua/my/plugins/lspconfig.lua +++ b/home/features/neovim/config/lua/my/plugins/lspconfig.lua @@ -42,14 +42,14 @@ function M.on_attach(client, bufnr) vim.lsp.buf.format({ async = false, bufnr = bufnr }) end - if client.supports_method("textDocument/formatting") then - nmap("F", format, "[F]ormat") - vim.api.nvim_create_autocmd("BufWritePre", { - group = vim.api.nvim_create_augroup("LspFormatting", { clear = false }), - buffer = bufnr, - callback = format, - }) - end + -- if client.supports_method("textDocument/formatting") then + -- nmap("F", format, "[F]ormat") + -- vim.api.nvim_create_autocmd("BufWritePre", { + -- group = vim.api.nvim_create_augroup("LspFormatting", { clear = false }), + -- buffer = bufnr, + -- callback = format, + -- }) + -- end -- }}} -- {{{ Go to declaration / references / implementation nmap("gd", vim.lsp.buf.definition, "[G]o to [d]efinition") @@ -129,6 +129,10 @@ local servers = { settings = { ---@diagnostic disable-next-line: missing-fields Lua = { + ---@diagnostic disable-next-line: missing-fields + format = { + enable = true + }, -- Do not send telemetry data containing a randomized but unique identifier telemetry = { enable = false, @@ -211,9 +215,9 @@ function lspconfig.config() diagnostics_icons() -- -- {{{ Change on-hover borders vim.lsp.handlers["textDocument/hover"] = - vim.lsp.with(vim.lsp.handlers.hover, { border = "single" }) + vim.lsp.with(vim.lsp.handlers.hover, { border = "single" }) vim.lsp.handlers["textDocument/signatureHelp"] = - vim.lsp.with(vim.lsp.handlers.signature_help, { border = "single" }) + vim.lsp.with(vim.lsp.handlers.signature_help, { border = "single" }) -- -- }}} local capabilities = M.capabilities() @@ -226,7 +230,7 @@ function lspconfig.config() require("lspconfig")[lsp].setup({ on_attach = details.on_attach, - settings = details.settings, -- Specific per-language settings + settings = details.settings, -- Specific per-language settings flags = { debounce_text_changes = 150, -- This will be the default in neovim 0.7+ }, diff --git a/home/features/neovim/config/lua/my/plugins/null-ls.lua b/home/features/neovim/config/lua/my/plugins/null-ls.lua index 0f417fe..c48e316 100644 --- a/home/features/neovim/config/lua/my/plugins/null-ls.lua +++ b/home/features/neovim/config/lua/my/plugins/null-ls.lua @@ -13,21 +13,7 @@ function M.config() local null_ls = require("null-ls") local sources = { - -- {{{ Typescript formatting - -- null_ls.builtins.formatting.prettierd, -- format ts files - null_ls.builtins.formatting.prettier, -- format ts files - -- }}} - -- {{{ Lua formatting - -- null_ls.builtins.formatting, -- format lua code - null_ls.builtins.formatting.stylua, -- format lua code - -- }}} -- {{{ Python - -- Formatting: - -- null_ls.builtins.formatting.black, - -- null_ls.builtins.formatting.isort, - null_ls.builtins.formatting.yapf.with({ - extra_args = { [[--style="{ indent_width: 2 }"]] }, - }), -- Diagnostics null_ls.builtins.diagnostics.ruff, -- Linting -- null_ls.builtins.diagnostics.mypy, -- Type checking diff --git a/home/features/neovim/default.nix b/home/features/neovim/default.nix index 6b6b2d0..2e5a48c 100644 --- a/home/features/neovim/default.nix +++ b/home/features/neovim/default.nix @@ -73,7 +73,7 @@ let # Required by magma-nvim: # python310Packages.pynvim # python310Packages.jupyter - ]; + ] ++ config.satellite.neovim.generated.dependencies; # }}} # {{{ extraRuntime extraRuntimePaths = env: [ @@ -268,7 +268,7 @@ in package = "folke/flash.nvim"; name = "flash"; - cond = nlib.blacklistEnv [ "vscode" "firenvim" ]; + cond = nlib.blacklistEnv [ "vscode" ]; keys = let keybind = mode: mapping: action: desc: { inherit mapping desc mode; @@ -287,5 +287,22 @@ in opts.modes.char.enabled = false; }; # }}} + # {{{ Conform.nvim + satellite.neovim.lazy.conform = { + package = "stevearc/conform.nvim"; + name = "conform"; + + cond = nlib.blacklistEnv [ "vscode" ]; + event = "BufReadPost"; + + opts.log_level = nlib.lua "vim.log.levels.DEBUG"; + opts.format_on_save.lsp_fallback = true; + opts.formatters_by_ft = { + lua = [ "stylua" ]; + python = [ "ruff_format" ]; + javascript = [ [ "prettierd" "prettier" ] ]; + }; + }; + # }}} # }}} } diff --git a/modules/common/neovim.nix b/modules/common/neovim.nix index fd97414..c0a8500 100644 --- a/modules/common/neovim.nix +++ b/modules/common/neovim.nix @@ -215,13 +215,13 @@ let numberOr = luaEncoders.conditional (e: lib.isFloat e || lib.isInt e) luaEncoders.number; nullOr = luaEncoders.conditional (e: e == null) luaEncoders.nil; anything = lib.pipe (luaEncoders.fail (v: "Cannot figure out how to encode value ${builtins.toJSON v}")) [ - luaEncoders.luaCodeOr + (luaEncoders.attrsetOfOr luaEncoders.anything) + (luaEncoders.listOfOr luaEncoders.anything) luaEncoders.nullOr luaEncoders.boolOr luaEncoders.numberOr luaEncoders.stringOr - (luaEncoders.listOfOr luaEncoders.anything) - (luaEncoders.attrsetOfOr luaEncoders.anything) + luaEncoders.luaCodeOr # Lua code expressions have priority over attrsets ]; # }}} # {{{ Lua code @@ -299,7 +299,7 @@ let # Format and write a lua file to disk writeLuaFile = path: name: text: let - directory = "lua/${path}"; + directory = "lua/${path}"; destination = "${directory}/${name}.lua"; unformatted = pkgs.writeText "raw-lua-${name}" text; in @@ -340,6 +340,12 @@ in type = types.package; description = "Derivation building all the given nix modules"; }; + + dependencies = lib.mkOption { + default = [ ]; + type = types.listOf types.package; + description = "List of packages to give neovim access to"; + }; }; lib = { @@ -432,4 +438,12 @@ in name = "lazy-nvim-modules"; paths = lib.attrsets.mapAttrsToList (_: m: m.module) cfg.generated.lazy; }; + + config.satellite.neovim.generated.dependencies = + lib.pipe cfg.lazy + [ + (lib.attrsets.mapAttrsToList (_: m: m.dependencies.nix)) + lib.lists.flatten + ] + ; } diff --git a/shell.nix b/shell.nix index 9b21a27..338e511 100644 --- a/shell.nix +++ b/shell.nix @@ -4,5 +4,5 @@ pkgs.mkShell { # Enable experimental features without having to specify the argument NIX_CONFIG = "experimental-features = nix-command flakes"; - nativeBuildInputs = with pkgs; [ nix home-manager git ]; + packages = with pkgs; [ nix home-manager git ]; }