From 33f7b7172cf5623942eefb215e7d9a440bf5dd3e Mon Sep 17 00:00:00 2001 From: aszlig Date: Wed, 9 Feb 2022 17:10:33 +0100 Subject: pkgs/vim: Add rustfmt with custom default config Since I do have a lot of projects where I use rustfmt, it gets pretty tedious to manually run "cargo fmt". Using g:rustfmt_autosave should make this less tedious but it might annoy me in the future, let's see. In addition to setting rustfmt I also added a default path for rustc, which is used whenever there is no rustc in path. This is because I usually switch between several projects which use different Rust versions and this way it will use the rustc version that is in PATH during "nix develop". For the rustfmt part I also added a small default configuration which represents my opinion on how I think Rust code should be formatted. The file is used whenever a project doesn't have a "rustfmt.toml" or ".rustfmt.toml". Unfortunately, RustFmt is broken in Vim right now[1], so I'm using the upstream project until the issue has been resolved. [1]: https://github.com/rust-lang/rust.vim/issues/446 Signed-off-by: aszlig --- pkgs/aszlig/default.nix | 3 ++- pkgs/aszlig/rustfmt/config.patch | 13 +++++++++++++ pkgs/aszlig/rustfmt/default.nix | 31 +++++++++++++++++++++++++++++++ pkgs/aszlig/vim/default.nix | 17 +++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 pkgs/aszlig/rustfmt/config.patch create mode 100644 pkgs/aszlig/rustfmt/default.nix (limited to 'pkgs/aszlig') diff --git a/pkgs/aszlig/default.nix b/pkgs/aszlig/default.nix index 58f372a9..0629ab8e 100644 --- a/pkgs/aszlig/default.nix +++ b/pkgs/aszlig/default.nix @@ -1,4 +1,4 @@ -{ callPackage, callPackage_i686, vim_configurable, gopass, mutt, xterm }: +{ callPackage, vim_configurable, gopass, mutt, xterm, rustfmt }: { aacolorize = callPackage ./aacolorize { }; @@ -12,6 +12,7 @@ nlast = callPackage ./nlast { }; psi = callPackage ./psi { }; pvolctrl = callPackage ./pvolctrl { }; + rustfmt = callPackage ./rustfmt { inherit rustfmt; }; vim = callPackage ./vim { vim = vim_configurable; }; xterm = callPackage ./xterm { inherit xterm; }; } diff --git a/pkgs/aszlig/rustfmt/config.patch b/pkgs/aszlig/rustfmt/config.patch new file mode 100644 index 00000000..0a49a585 --- /dev/null +++ b/pkgs/aszlig/rustfmt/config.patch @@ -0,0 +1,13 @@ +diff --git a/src/config/mod.rs b/src/config/mod.rs +index cd90e090..a6db6da1 100644 +--- a/src/config/mod.rs ++++ b/src/config/mod.rs +@@ -282,7 +282,7 @@ impl Config { + } + } + +- Ok(None) ++ Ok(Some(env!("DEFAULT_CONFIG_FILE").into())) + } + + match resolve_project_file(dir)? { diff --git a/pkgs/aszlig/rustfmt/default.nix b/pkgs/aszlig/rustfmt/default.nix new file mode 100644 index 00000000..cde4d010 --- /dev/null +++ b/pkgs/aszlig/rustfmt/default.nix @@ -0,0 +1,31 @@ +{ rustfmt, runCommand, remarshal }: + +let + defaultConfig = { + format_code_in_doc_comments = true; + format_macro_matchers = true; + format_strings = true; + group_imports = "StdExternalCrate"; + match_block_trailing_comma = true; + max_width = 79; + newline_style = "Unix"; + normalize_doc_attributes = true; + overflow_delimited_expr = true; + reorder_impl_items = true; + unstable_features = true; + use_field_init_shorthand = true; + use_try_shorthand = true; + wrap_comments = true; + }; + +in rustfmt.overrideAttrs (drv: { + patches = (drv.patches or []) ++ [ ./config.patch ]; + patchFlags = [ "-p1" "-d" "src/tools/rustfmt" ]; + DEFAULT_CONFIG_FILE = runCommand "rustfmt.conf" { + nativeBuildInputs = [ remarshal ]; + value = builtins.toJSON defaultConfig; + passAsFile = [ "value" ]; + } '' + json2toml "$valuePath" "$out" + ''; +}) diff --git a/pkgs/aszlig/vim/default.nix b/pkgs/aszlig/vim/default.nix index 5162dc8c..35711e00 100644 --- a/pkgs/aszlig/vim/default.nix +++ b/pkgs/aszlig/vim/default.nix @@ -1,5 +1,6 @@ { stdenv, lib, fetchurl, fetchFromGitHub, writeText, writeTextFile, writeScript , runCommand, writers, python3Packages, ledger, meson, vim, buildGoPackage +, rustc, rustfmt }: let @@ -403,6 +404,14 @@ let rev = "e38845b4042d2351c47cd63f8705fd51c97acb4f"; sha256 = "1k3val0ibriwcv5jdyq95sgxgkz54r15gpylbhns5934zvaakpj1"; }; + + # Remove once https://github.com/rust-lang/rust.vim/issues/446 is resolved. + rust = fetchFromGitHub { + owner = "rust-lang"; + repo = "rust.vim"; + rev = "4aa69b84c8a58fcec6b6dad6fe244b916b1cf830"; + sha256 = "07nh8gvkwq91i7qcz0rk5jlc8sb4d3af4zq2892kmmw576zg1wd8"; + }; }; generic = '' @@ -578,6 +587,14 @@ let let g:Hexokinase_highlighters = ['background'] let g:Hexokinase_refreshEvents = \ ['TextChanged', 'TextChangedI', 'InsertLeave', 'BufRead'] + + " rust + if !executable('rustc') + let g:rustc_path = '${rustc}/bin/rustc' + endif + let g:rustfmt_command = '${rustfmt}/bin/rustfmt' + let g:rustfmt_autosave = 1 + let g:rust_recommended_style = 0 ''; autocmd = '' -- cgit 1.4.1