More mkLuaInline

This commit is contained in:
Liyua 2025-07-01 20:39:16 +02:00
commit 592de7979a
No known key found for this signature in database
GPG key ID: AFF37010586DE14D

View file

@ -4,7 +4,8 @@
lib, lib,
inputs, inputs,
... ...
}: let }:
let
inherit (builtins) attrNames; inherit (builtins) attrNames;
inherit (lib) concatStringsSep mkLuaInline; inherit (lib) concatStringsSep mkLuaInline;
inherit (lib.meta) getExe; inherit (lib.meta) getExe;
@ -12,8 +13,7 @@
inherit (lib.modules) mkIf mkMerge; inherit (lib.modules) mkIf mkMerge;
inherit (lib.lists) isList; inherit (lib.lists) isList;
inherit (lib.strings) optionalString; inherit (lib.strings) optionalString;
inherit inherit (lib.types)
(lib.types)
anything anything
attrsOf attrsOf
enum enum
@ -32,37 +32,32 @@
noFormat = "on_attach = attach_keymaps"; noFormat = "on_attach = attach_keymaps";
defaultServer = "nil"; defaultServer = "nil";
packageToCmd = package: defaultCmd: packageToCmd =
if isList package package: defaultCmd:
then expToLua package if isList package then expToLua package else ''{"${package}/bin/${defaultCmd}"}'';
else ''{"${package}/bin/${defaultCmd}"}'';
servers = { servers = {
nil = { nil = {
package = inputs.nil.packages.${pkgs.stdenv.system}.nil; package = inputs.nil.packages.${pkgs.stdenv.system}.nil;
internalFormatter = true; internalFormatter = true;
lspConfig = ''lspconfig.nil_ls.setup ${ lspConfig = ''lspconfig.nil_ls.setup ${
toLuaObject { toLuaObject {
capabilities = mkLuaInline "capabilities"; capabilities = mkLuaInline "capabilities";
on_attach = on_attach = if cfg.format.enable then mkLuaInline "default_on_attach" else "attach_keymaps";
if cfg.format.enable cmd = mkLuaInline (packageToCmd cfg.lsp.package "nil");
then mkLuaInline "default_on_attach" settings.nil = {
else "attach_keymaps"; formatting.command =
cmd = mkLuaInline (packageToCmd cfg.lsp.package "nil"); if cfg.format.enable then
settings.nil = if cfg.format.type == "alejandra" then
{ mkLuaInline ''{"${cfg.format.package}/bin/alejandra", "--quiet"}''
formatting.command = else if cfg.format.type == "nixfmt" then
if cfg.format.enable mkLuaInline ''{"${cfg.format.package}/bin/nixfmt"}''
then else
if cfg.format.type == "alejandra" null
then ''{"${cfg.format.package}/bin/alejandra", "--quiet"}'' else
else if cfg.format.type == "nixfmt" null;
then ''{"${cfg.format.package}/bin/nixfmt"}'' } // cfg.lsp.options;
else null }
else null; }'';
}
// cfg.lsp.options;
}
}'';
# lspConfig = '' # lspConfig = ''
# lspconfig.nil_ls.setup{ # lspconfig.nil_ls.setup{
# capabilities = capabilities, # capabilities = capabilities,
@ -99,11 +94,7 @@
lspConfig = '' lspConfig = ''
lspconfig.nixd.setup{ lspconfig.nixd.setup{
capabilities = capabilities, capabilities = capabilities,
${ ${if cfg.format.enable then useFormat else noFormat},
if cfg.format.enable
then useFormat
else noFormat
},
cmd = ${packageToCmd cfg.lsp.package "nixd"}, cmd = ${packageToCmd cfg.lsp.package "nixd"},
${optionalString cfg.format.enable '' ${optionalString cfg.format.enable ''
settings = { settings = {
@ -167,25 +158,22 @@
''; '';
}; };
}; };
in { in
{
options.vim.languages.nix = { options.vim.languages.nix = {
enable = mkEnableOption "Nix language support"; enable = mkEnableOption "Nix language support";
treesitter = { treesitter = {
enable = enable = mkEnableOption "Nix treesitter" // {
mkEnableOption "Nix treesitter" default = config.vim.languages.enableTreesitter;
// { };
default = config.vim.languages.enableTreesitter;
};
package = mkGrammarOption pkgs "nix"; package = mkGrammarOption pkgs "nix";
}; };
lsp = { lsp = {
enable = enable = mkEnableOption "Nix LSP support" // {
mkEnableOption "Nix LSP support" default = config.vim.lsp.enable;
// { };
default = config.vim.lsp.enable;
};
server = mkOption { server = mkOption {
description = "Nix LSP server to use"; description = "Nix LSP server to use";
type = enum (attrNames servers); type = enum (attrNames servers);
@ -207,11 +195,9 @@ in {
}; };
format = { format = {
enable = enable = mkEnableOption "Nix formatting" // {
mkEnableOption "Nix formatting" default = config.vim.languages.enableFormat;
// { };
default = config.vim.languages.enableFormat;
};
type = mkOption { type = mkOption {
description = "Nix formatter to use"; description = "Nix formatter to use";
@ -227,11 +213,9 @@ in {
}; };
extraDiagnostics = { extraDiagnostics = {
enable = enable = mkEnableOption "extra Nix diagnostics" // {
mkEnableOption "extra Nix diagnostics" default = config.vim.languages.enableExtraDiagnostics;
// { };
default = config.vim.languages.enableExtraDiagnostics;
};
types = diagnostics { types = diagnostics {
langDesc = "Nix"; langDesc = "Nix";
@ -263,7 +247,7 @@ in {
(mkIf cfg.treesitter.enable { (mkIf cfg.treesitter.enable {
vim.treesitter.enable = true; vim.treesitter.enable = true;
vim.treesitter.grammars = [cfg.treesitter.package]; vim.treesitter.grammars = [ cfg.treesitter.package ];
}) })
(mkIf cfg.lsp.enable { (mkIf cfg.lsp.enable {
@ -274,7 +258,7 @@ in {
(mkIf (cfg.format.enable && (!cfg.lsp.enable || !servers.${cfg.lsp.server}.internalFormatter)) { (mkIf (cfg.format.enable && (!cfg.lsp.enable || !servers.${cfg.lsp.server}.internalFormatter)) {
vim.formatter.conform-nvim = { vim.formatter.conform-nvim = {
enable = true; enable = true;
setupOpts.formatters_by_ft.nix = [cfg.format.type]; setupOpts.formatters_by_ft.nix = [ cfg.format.type ];
setupOpts.formatters.${cfg.format.type} = { setupOpts.formatters.${cfg.format.type} = {
command = getExe cfg.format.package; command = getExe cfg.format.package;
}; };
@ -288,8 +272,7 @@ in {
linters = mkMerge ( linters = mkMerge (
map (name: { map (name: {
${name}.cmd = getExe diagnosticsProviders.${name}.package; ${name}.cmd = getExe diagnosticsProviders.${name}.package;
}) }) cfg.extraDiagnostics.types
cfg.extraDiagnostics.types
); );
}; };
}) })