Skip to content

Commit 89e5a4e

Browse files
committed
Set default formatter to nixfmt-rfc-style
It's supposed to be the official formatting tool. See: <NixOS/rfcs#166>
1 parent 8c10ad2 commit 89e5a4e

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

crates/nil/src/handler.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ use std::process;
1616
use std::sync::Arc;
1717
use text_size::TextRange;
1818

19+
const DEFAULT_FORMATTER: &str = match option_env!("CFG_DEFAULT_FORMATTER") {
20+
Some(cmd) => cmd,
21+
None => "nixfmt",
22+
};
23+
1924
pub(crate) fn goto_definition(
2025
snap: StateSnapshot,
2126
params: GotoDefinitionParams,
@@ -219,19 +224,26 @@ pub(crate) fn formatting(
219224
Ok(stdout)
220225
}
221226

222-
let cmd =
223-
snap.config.formatting_command.as_ref().context(
224-
"No formatter configured. Set the nil.formatting.command LSP server setting.",
225-
)?;
227+
let cmd = match &snap.config.formatting_command {
228+
Some(cmd) => cmd,
229+
None => &vec![DEFAULT_FORMATTER.to_owned()],
230+
};
226231

227232
let (file_content, line_map) = {
228233
let vfs = snap.vfs();
229234
let (file, line_map) = convert::from_file(&vfs, &params.text_document)?;
230235
(vfs.content_for_file(file), line_map)
231236
};
232237

233-
let new_content = run_with_stdin(cmd, <Arc<[u8]>>::from(file_content.clone()))
234-
.with_context(|| format!("Failed to run formatter {cmd:?}"))?;
238+
let new_content =
239+
run_with_stdin(cmd, <Arc<[u8]>>::from(file_content.clone())).with_context(|| {
240+
let note = if snap.config.formatting_command.is_none() {
241+
", set `nil.formatting.command` in LSP setting to override the default"
242+
} else {
243+
""
244+
};
245+
format!("Failed to run formatter {cmd:?}{note}")
246+
})?;
235247

236248
if new_content == *file_content {
237249
return Ok(None);

dev/nvim-lsp.nix

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ let
8989
settings = {
9090
['nil'] = {
9191
testSetting = 42,
92-
formatting = {
93-
command = { "nixfmt" },
94-
},
9592
},
9693
},
9794
}

dev/vim-coc.nix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ let
9999
nil.server.path = pkgs.writeShellScript "nil" ''
100100
exec "$NIL_PATH" "$@"
101101
'';
102-
nil.formatting.command = [ (pkgs.lib.getExe pkgs.nixfmt-rfc-style) ];
103102
nil.diagnostics.excludedFiles = [ "generated.nix" ];
104103
nil.nix.flake.autoEvalInputs = true;
105104
nil.nix.maxMemoryMB = 2048;

flake.nix

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ rec {
2323
rev = self.rev or (lib.warn "Git changes are not committed" (self.dirtyRev or "dirty"));
2424

2525
mkNil =
26-
{ rustPlatform, nixVersions, ... }:
26+
{
27+
rustPlatform,
28+
nixVersions,
29+
nixfmt-rfc-style,
30+
...
31+
}:
2732
rustPlatform.buildRustPackage {
2833
pname = "nil";
2934
version = "unstable-${date}";
@@ -37,6 +42,7 @@ rec {
3742
nativeBuildInputs = [ (nixVersions.latest or nixVersions.unstable) ];
3843

3944
CFG_RELEASE = "git-${rev}";
45+
CFG_DEFAULT_FORMATTER = lib.getExe nixfmt-rfc-style;
4046

4147
meta = {
4248
inherit description;

0 commit comments

Comments
 (0)