From a791b41455f4575fc9b42b8adcbca8d5ca885afc Mon Sep 17 00:00:00 2001 From: Grimmauld Date: Wed, 15 May 2024 23:24:35 +0200 Subject: [PATCH] fmt support --- common/tooling/lsp.nix | 120 +++++++++++++++++++++++++++++--------- common/tooling/nix.nix | 11 +++- common/tooling/nvim.nix | 1 - common/tooling/python.nix | 17 ++++-- common/tooling/rust.nix | 16 ++--- 5 files changed, 123 insertions(+), 42 deletions(-) diff --git a/common/tooling/lsp.nix b/common/tooling/lsp.nix index 96ab557..179fc97 100644 --- a/common/tooling/lsp.nix +++ b/common/tooling/lsp.nix @@ -12,18 +12,46 @@ let mkIf getName mkEnableOption + getExe filter + optionalString + concatLines ; + + conf_def = + fmt: + ( + '' + [formatter.${getName fmt.package}] + command = "${getExe fmt.package}" + includes = ${builtins.toJSON fmt.includes} + '' + + (optionalString (fmt.options != [ ]) "options = ${builtins.toJSON fmt.options}\n") + ); + + treefmt_conf = pkgs.writeText "treefmt.toml" ( + concatLines (map (v: (conf_def v.fmt)) (filter (v: !isNull v.fmt) tooling.lang_servers)) + ); in { config = mkIf (enable && tooling.enable) { - environment.systemPackages = map (v: v.package) tooling.lang_servers; + environment.systemPackages = + [ + pkgs.treefmt + pkgs.findup + pkgs.gnused + ] + ++ (map (v: v.lsp.package) (filter (v: !isNull v.lsp) tooling.lang_servers)) + ++ (map (v: v.fmt.package) (filter (v: !isNull v.fmt) tooling.lang_servers)); + + environment.shellAliases."treefmt" = "${getExe pkgs.treefmt} --config-file ${treefmt_conf}"; grimmShared.tooling.nvim.extraLuaRC = [ "vim.g.coq_settings = { auto_start = 'shut-up' }" ] ++ (map ( - v: "require'lspconfig'.${v.name}.setup(require('coq').lsp_ensure_capabilities(${v.config}))" - ) (filter (v: v.vimIntegration) tooling.lang_servers)); + v: + "require'lspconfig'.${v.lsp.lspconf_mod_name}.setup(require('coq').lsp_ensure_capabilities(${v.lsp.lspconf_config}))" + ) (filter (v: !isNull v.lsp && v.lsp.vimIntegration) tooling.lang_servers)); grimmShared.tooling.nvim.plugins = with pkgs.vimPlugins; [ nvim-lspconfig @@ -34,33 +62,73 @@ in options.grimmShared.tooling.lang_servers = mkOption { type = types.listOf ( - types.submodule ( - { config, ... }: - { - options = { - name = mkOption { - type = types.nullOr types.nonEmptyStr; - default = getName config.package; - description = "lspconfig module name"; - }; + types.submodule { + options = { + lsp = mkOption { + type = types.nullOr ( + types.submodule ( + { config, ... }: + { + options = { + lspconf_mod_name = mkOption { + type = types.nullOr types.nonEmptyStr; + default = getName config.package; + description = "lspconfig module name"; + }; - config = mkOption { - type = types.nonEmptyStr; - default = "{}"; - description = "options to pass to lspconfig"; - }; + lspconf_config = mkOption { + type = types.nonEmptyStr; + default = "{}"; + description = "options to pass to lspconfig"; + }; - vimIntegration = mkEnableOption "Enable coq/nvim-lspconfig integration" // { - default = true; - }; + package = mkOption { + type = types.package; + default = null; + description = "LSP package"; + }; - package = mkOption { - type = types.package; - description = "LSP package"; - }; + vimIntegration = mkEnableOption "Enable coq/nvim-lspconfig integration" // { + default = true; + }; + }; + } + ) + ); }; - } - ) + + fmt = mkOption { + type = types.nullOr ( + types.submodule ( + { config, ... }: + { + options = { + package = mkOption { + type = types.package; + description = "FMT package"; + }; + + options = mkOption { + type = types.listOf types.nonEmptyStr; + default = [ ]; + }; + + includes = mkOption { + type = types.listOf types.nonEmptyStr; + default = [ ]; + }; + + command = mkOption { + type = types.nonEmptyStr; + default = getExe config.package; + }; + }; + } + ) + ); + }; + }; + } ); default = { }; description = "Language servers available on the system"; diff --git a/common/tooling/nix.nix b/common/tooling/nix.nix index 01b8346..6eb0a98 100644 --- a/common/tooling/nix.nix +++ b/common/tooling/nix.nix @@ -6,7 +6,6 @@ (writeShellScriptBin "rebuild" "bash -c \"nixos-rebuild switch |& nom\"") nixpkgs-review - nixpkgs-fmt nixfmt-rfc-style nixd nixpkgs-hammering @@ -22,7 +21,15 @@ plugins = with pkgs.vimPlugins; [ vim-nix ]; }; - grimmShared.tooling.lang_servers = [ { package = pkgs.nixd; } ]; + grimmShared.tooling.lang_servers = [ + { + lsp.package = pkgs.nixd; + fmt = { + package = pkgs.nixpkgs-fmt; + includes = [ "*.nix" ]; + }; + } + ]; nix.settings = { experimental-features = [ diff --git a/common/tooling/nvim.nix b/common/tooling/nvim.nix index 3cf6be3..ac1ded8 100644 --- a/common/tooling/nvim.nix +++ b/common/tooling/nvim.nix @@ -18,7 +18,6 @@ in config = mkIf (enable && tooling.enable) { environment.systemPackages = with pkgs; [ dos2unix - treefmt neovim-remote ]; diff --git a/common/tooling/python.nix b/common/tooling/python.nix index 5bbb88b..0c35703 100644 --- a/common/tooling/python.nix +++ b/common/tooling/python.nix @@ -19,22 +19,27 @@ in config = lib.mkIf (enable && tooling.enable) { environment.systemPackages = [ (pkgs.python3.withPackages pyLibs) - pkgs.yapf - pkgs.pyright ] ++ lib.optionals graphical (with pkgs; [ jetbrains.pycharm-community ]); programs.xonsh = { enable = true; config = lib.concatLines ( lib.mapAttrsToList ( - name: value: ''aliases["${name}"] = "${value}"'' + name: value: "aliases[\"${name}\"] = '''${value}'''" ) config.environment.shellAliases ); package = pkgs.xonsh.wrapper.override { extraPackages = pyLibs; }; }; - grimmShared.tooling.nvim.extraLuaRC = lib.singleton '' - require'lspconfig'.pyright.setup{} - ''; + grimmShared.tooling.lang_servers = [ + { + lsp.package = pkgs.pyright; + fmt = { + package = pkgs.yapf; + includes = [ "*.py" ]; + options = [ "-i" ]; + }; + } + ]; }; } diff --git a/common/tooling/rust.nix b/common/tooling/rust.nix index 5297a71..29a6ea9 100644 --- a/common/tooling/rust.nix +++ b/common/tooling/rust.nix @@ -11,16 +11,18 @@ in config = lib.mkIf (enable && tooling.enable) { environment.systemPackages = with pkgs; - [ - rustfmt - pkg-config - ] - ++ lib.optionals graphical [ jetbrains.clion ]; + [ pkg-config ] ++ lib.optionals graphical [ jetbrains.clion ]; grimmShared.tooling.lang_servers = [ { - package = pkgs.rust-analyzer; - name = "rust_analyzer"; + lsp = { + package = pkgs.rust-analyzer; + lspconf_mod_name = "rust_analyzer"; + }; + fmt = { + package = pkgs.rustfmt; + includes = [ "*.rs" ]; + }; } ]; };