Skip to content

Commit d5da1a1

Browse files
authored
languages/formatters: allow multiple formatters (#1103)
* language/python: allow multiple formatters * package: add prettier-plugin-astro * language/astro: fix prettier, remove prettierd * package: add prettier-plugin-svelte * language/svelte: fix prettier, remove prettierd * languages: allow listOf format.type * deprecations: add warnings for removed format options * ruby: remove unused variable * nix: fix outdated references to format.package * docs: update release note * treewide: warn deprecation in singleOrListOf * conform: add definitions for setupOpts.formatters
1 parent a87439e commit d5da1a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+641
-488
lines changed

docs/release-notes/rl-0.8.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@
143143
- Use conform over custom autocmds for LSP format on save
144144
- Move LSP keybinds and other related plugin integrations into an LspAttach
145145
event.
146+
- Allow multiple formatters in language modules.
147+
- Fixed `prettier` in astro and svelte, and removed `prettierd` due to high
148+
complexity that would be needed to support it.
146149

147150
[diniamo](https://github.com/diniamo):
148151

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
stdenv,
3+
fetchFromGitHub,
4+
nodejs,
5+
pnpm_9,
6+
pins,
7+
}: let
8+
pin = pins.prettier-plugin-astro;
9+
in
10+
stdenv.mkDerivation (finalAttrs: {
11+
pname = "prettier-plugin-astro";
12+
version = pin.version or pin.revision;
13+
14+
src = fetchFromGitHub {
15+
inherit (pin.repository) owner repo;
16+
rev = finalAttrs.version;
17+
sha256 = pin.hash;
18+
};
19+
20+
pnpmDeps = pnpm_9.fetchDeps {
21+
inherit (finalAttrs) pname src;
22+
fetcherVersion = 2;
23+
hash = "sha256-K7pIWLkIIbUKDIcysfEtcf/eVMX9ZgyFHdqcuycHCNE=";
24+
};
25+
26+
nativeBuildInputs = [
27+
nodejs
28+
pnpm_9.configHook
29+
];
30+
31+
buildPhase = ''
32+
runHook preBuild
33+
34+
pnpm run build
35+
36+
runHook postBuild
37+
'';
38+
39+
installPhase = ''
40+
runHook preInstall
41+
42+
# mkdir -p $out/dist
43+
cp -r dist/ $out
44+
cp -r node_modules $out
45+
46+
runHook postInstall
47+
'';
48+
})
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
buildNpmPackage,
3+
fetchFromGitHub,
4+
pins,
5+
}: let
6+
pin = pins.prettier-plugin-svelte;
7+
in
8+
buildNpmPackage (finalAttrs: {
9+
pname = "prettier-plugin-svelte";
10+
version = pin.version or pin.revision;
11+
12+
src = fetchFromGitHub {
13+
inherit (pin.repository) owner repo;
14+
rev = finalAttrs.version;
15+
sha256 = pin.hash;
16+
};
17+
18+
npmDepsHash = "sha256-D+gDdKiIG38jV+M/BqTKf0yYj1KXpbIodtQFdzocpn8=";
19+
})

lib/types/custom.nix

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{lib}: let
2+
inherit (builtins) warn toJSON;
23
inherit (lib.options) mergeEqualOption;
34
inherit (lib.lists) singleton;
45
inherit (lib.strings) isString stringLength match;
@@ -27,5 +28,14 @@ in {
2728
check = v: isString v && (match "#?[0-9a-fA-F]{6}" v) != null;
2829
};
2930

30-
singleOrListOf = t: coercedTo t singleton (listOf t);
31+
# no compound types please
32+
deprecatedSingleOrListOf = option: t:
33+
coercedTo
34+
t
35+
(x:
36+
warn ''
37+
${option} no longer accepts non-list values, use [${toJSON x}] instead
38+
''
39+
(singleton x))
40+
(listOf t);
3141
}

lib/types/default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ in {
1010
inherit (typesDag) dagOf;
1111
inherit (typesPlugin) pluginsOpt extraPluginType mkPluginSetupOption luaInline pluginType borderType;
1212
inherit (typesLanguage) diagnostics mkGrammarOption;
13-
inherit (customTypes) char hexColor mergelessListOf singleOrListOf;
13+
inherit (customTypes) char hexColor mergelessListOf deprecatedSingleOrListOf;
1414
}

modules/extra/deprecations.nix

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
"Obsolete option `vim.languages.${lang}.lsp.server` used, use `vim.languages.${lang}.lsp.servers` instead."
4343
(head x);
4444
};
45+
46+
mkRemovedFormatPackage = lang: (mkRemovedOptionModule ["vim" "languages" lang "format" "package"] ''
47+
`vim.languages.${lang}.format.package` is removed, please use `vim.formatter.conform-nvim.formatters.<formatter_name>.command` instead.
48+
'');
4549
in {
4650
imports = concatLists [
4751
[
@@ -269,8 +273,38 @@ in {
269273
270274
<https://github.com/Saecki/crates.nvim/wiki/Documentation-v0.7.1#in-process-language-server>
271275
'')
276+
277+
(mkRemovedOptionModule ["vim" "language" "astro" "format"] ''
278+
This option has been removed due to being broken for a long time.
279+
'')
280+
(mkRemovedOptionModule ["vim" "language" "svelte" "format"] ''
281+
This option has been removed due to being broken for a long time.
282+
'')
272283
]
273284

285+
(map mkRemovedFormatPackage [
286+
"bash"
287+
"css"
288+
"elixir"
289+
"fsharp"
290+
"go"
291+
"hcl"
292+
"html"
293+
"json"
294+
"lua"
295+
"markdown"
296+
"nim"
297+
"nix"
298+
"ocaml"
299+
"python"
300+
"qml"
301+
"r"
302+
"ruby"
303+
"rust"
304+
"sql"
305+
"ts"
306+
"typst"
307+
])
274308
# Migrated via batchRenameOptions. Further batch renames must be below this line.
275309
renamedVimOpts
276310
];

modules/plugins/formatter/conform-nvim/conform-nvim.nix

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,58 @@
11
{lib, ...}: let
22
inherit (lib.generators) mkLuaInline;
33
inherit (lib.options) mkOption mkEnableOption literalMD;
4-
inherit (lib.types) attrs either nullOr;
4+
inherit (lib.types) attrs either nullOr listOf submodule str;
55
inherit (lib.nvim.lua) toLuaObject;
66
inherit (lib.nvim.types) luaInline mkPluginSetupOption;
7+
8+
formattersType = submodule {
9+
freeformType = attrs;
10+
options = {
11+
command = mkOption {
12+
type = nullOr (either str luaInline);
13+
default = null;
14+
description = "The command to run.";
15+
};
16+
17+
args = mkOption {
18+
type = nullOr (either (listOf str) luaInline);
19+
default = null;
20+
description = ''
21+
A list of strings, or a lua function that returns a list of strings.
22+
23+
Return a single string instead of a list to run the command in a
24+
shell.
25+
'';
26+
};
27+
28+
prepend_args = mkOption {
29+
type = nullOr (either (listOf str) luaInline);
30+
default = null;
31+
description = ''
32+
When inherit = true, add additional arguments to the beginning of
33+
args. Can also be a function, like args.
34+
'';
35+
};
36+
37+
append_args = mkOption {
38+
type = nullOr (either (listOf str) luaInline);
39+
default = null;
40+
description = ''
41+
When inherit = true, add additional arguments to the end of args.
42+
Can also be a function, like args.
43+
'';
44+
};
45+
};
46+
};
747
in {
848
options.vim.formatter.conform-nvim = {
949
enable = mkEnableOption "lightweight yet powerful formatter plugin for Neovim [conform-nvim]";
1050
setupOpts = mkPluginSetupOption "conform.nvim" {
51+
formatters = mkOption {
52+
type = formattersType;
53+
default = {};
54+
description = "Custom formatters and overrides for built-in formatters.";
55+
};
1156
formatters_by_ft = mkOption {
1257
type = attrs;
1358
default = {};

modules/plugins/languages/asm.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
inherit (lib.options) mkEnableOption mkOption;
99
inherit (lib.modules) mkIf mkMerge;
1010
inherit (lib.types) enum;
11-
inherit (lib.nvim.types) mkGrammarOption singleOrListOf;
11+
inherit (lib.nvim.types) mkGrammarOption deprecatedSingleOrListOf;
1212
inherit (lib.meta) getExe;
1313
inherit (lib.nvim.attrsets) mapListToAttrs;
1414

@@ -34,7 +34,7 @@ in {
3434
lsp = {
3535
enable = mkEnableOption "Assembly LSP support" // {default = config.vim.lsp.enable;};
3636
servers = mkOption {
37-
type = singleOrListOf (enum (attrNames servers));
37+
type = deprecatedSingleOrListOf "vim.language.asm.lsp.servers" (enum (attrNames servers));
3838
default = defaultServers;
3939
description = "Assembly LSP server to use";
4040
};

modules/plugins/languages/astro.nix

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
self,
23
config,
34
pkgs,
45
lib,
@@ -8,9 +9,9 @@
89
inherit (lib.options) mkEnableOption mkOption;
910
inherit (lib.modules) mkIf mkMerge;
1011
inherit (lib.meta) getExe;
11-
inherit (lib.types) enum package;
12+
inherit (lib.types) enum coercedTo;
1213
inherit (lib.nvim.attrsets) mapListToAttrs;
13-
inherit (lib.nvim.types) mkGrammarOption diagnostics singleOrListOf;
14+
inherit (lib.nvim.types) mkGrammarOption diagnostics deprecatedSingleOrListOf;
1415
inherit (lib.generators) mkLuaInline;
1516

1617
cfg = config.vim.languages.astro;
@@ -40,23 +41,21 @@
4041
};
4142
};
4243

43-
# TODO: specify packages
44-
defaultFormat = "prettier";
45-
formats = {
44+
defaultFormat = ["prettier"];
45+
formats = let
46+
parser = "${self.packages.${pkgs.stdenv.system}.prettier-plugin-astro}/index.js";
47+
in {
4648
prettier = {
47-
package = pkgs.prettier;
48-
};
49-
50-
prettierd = {
51-
package = pkgs.prettierd;
49+
command = getExe pkgs.nodePackages.prettier;
50+
options.ft_parsers.astro = "astro";
51+
prepend_args = ["--plugin=${parser}"];
5252
};
5353

5454
biome = {
55-
package = pkgs.biome;
55+
command = getExe pkgs.biome;
5656
};
5757
};
5858

59-
# TODO: specify packages
6059
defaultDiagnosticsProvider = ["eslint_d"];
6160
diagnosticsProviders = {
6261
eslint_d = let
@@ -76,6 +75,15 @@
7675
};
7776
};
7877
};
78+
79+
formatType =
80+
deprecatedSingleOrListOf
81+
"vim.languages.astro.format.type"
82+
(coercedTo (enum ["prettierd"]) (_:
83+
lib.warn
84+
"vim.languages.astro.format.type: prettierd is deprecated, use prettier instead"
85+
"prettier")
86+
(enum (attrNames formats)));
7987
in {
8088
options.vim.languages.astro = {
8189
enable = mkEnableOption "Astro language support";
@@ -89,7 +97,7 @@ in {
8997
lsp = {
9098
enable = mkEnableOption "Astro LSP support" // {default = config.vim.lsp.enable;};
9199
servers = mkOption {
92-
type = singleOrListOf (enum (attrNames servers));
100+
type = deprecatedSingleOrListOf "vim.language.astro.lsp.servers" (enum (attrNames servers));
93101
default = defaultServers;
94102
description = "Astro LSP server to use";
95103
};
@@ -99,16 +107,10 @@ in {
99107
enable = mkEnableOption "Astro formatting" // {default = config.vim.languages.enableFormat;};
100108

101109
type = mkOption {
102-
type = enum (attrNames formats);
110+
type = formatType;
103111
default = defaultFormat;
104112
description = "Astro formatter to use";
105113
};
106-
107-
package = mkOption {
108-
type = package;
109-
default = formats.${cfg.format.type}.package;
110-
description = "Astro formatter package";
111-
};
112114
};
113115

114116
extraDiagnostics = {
@@ -140,9 +142,14 @@ in {
140142
(mkIf cfg.format.enable {
141143
vim.formatter.conform-nvim = {
142144
enable = true;
143-
setupOpts.formatters_by_ft.astro = [cfg.format.type];
144-
setupOpts.formatters.${cfg.format.type} = {
145-
command = getExe cfg.format.package;
145+
setupOpts = {
146+
formatters_by_ft.astro = cfg.format.type;
147+
formatters =
148+
mapListToAttrs (name: {
149+
inherit name;
150+
value = formats.${name};
151+
})
152+
cfg.format.type;
146153
};
147154
};
148155
})

0 commit comments

Comments
 (0)