Refactored to use the nvf library toLuaObject as well as other improvements and cleanups

This commit is contained in:
isaacST08 2025-01-26 18:42:54 -07:00
commit 1bebf495dc
3 changed files with 119 additions and 129 deletions

View file

@ -3,15 +3,14 @@
pkgs, pkgs,
lib, lib,
... ...
}: }: let
let
inherit (lib.options) mkOption; inherit (lib.options) mkOption;
inherit (lib.types) enum listOf package str; inherit (lib.types) enum listOf package str;
inherit (builtins) attrNames; inherit (lib.nvim.config) mkBool;
inherit (builtins) attrNames filter isAttrs hasAttr elemAt length;
cfg = config.vim.languages.tex; cfg = config.vim.languages.tex;
in in {
{
imports = [ imports = [
./latexmk.nix ./latexmk.nix
./tectonic.nix ./tectonic.nix
@ -27,6 +26,8 @@ in
This is just the default custom option. By setting any of the This is just the default custom option. By setting any of the
builders to true, this will be overwritten by that builder's builders to true, this will be overwritten by that builder's
parameters. parameters.
Setting this parameter to the name of a declared builder will
not automatically enable that builder.
''; '';
}; };
args = mkOption { args = mkOption {
@ -45,7 +46,7 @@ in
}; };
package = mkOption { package = mkOption {
type = package; type = package;
default = (pkgs.texlive.withPackages (ps: [ ps.latexmk ])); default = pkgs.texlive.withPackages (ps: [ps.latexmk]);
description = '' description = ''
The tex builder package to use. The tex builder package to use.
@ -67,4 +68,3 @@ in
}; };
}; };
} }

View file

@ -4,34 +4,71 @@
... ...
}: let }: let
inherit (lib.options) mkOption; inherit (lib.options) mkOption;
inherit (lib.types) bool str; inherit (lib.types) str nullOr;
inherit (builtins) filter isAttrs hasAttr attrNames length elemAt;
inherit (lib.nvim.config) mkBool;
cfg = config.vim.languages.tex; cfg = config.vim.languages.tex;
enabledBuildersCount = let
# This function will sort through the builder options and count how many
# builders have been enabled.
getEnabledBuildersCount = {
enabledBuildersCount ? 0,
index ? 0,
builderNamesList ? (
filter (
x: let
y = cfg.build.builders.${x};
in (isAttrs y && hasAttr "enable" y)
) (attrNames cfg.build.builders)
),
}: let
currentBuilderName = elemAt builderNamesList index;
currentBuilder = cfg.build.builders.${currentBuilderName};
nextIndex = index + 1;
newEnabledBuildersCount =
if currentBuilder.enable
then enabledBuildersCount + 1
else enabledBuildersCount;
in
if length builderNamesList > nextIndex
then
getEnabledBuildersCount {
inherit builderNamesList;
enabledBuildersCount = newEnabledBuildersCount;
index = nextIndex;
}
else newEnabledBuildersCount;
in (getEnabledBuildersCount {});
in { in {
imports = [ imports = [
./builders ./builders
]; ];
options.vim.languages.tex.build = { options.vim.languages.tex.build = {
forwardSearchAfter = mkOption { enable =
type = bool; mkBool (
default = false; if enabledBuildersCount > 1
description = "Set this property to true if you want to execute a forward search after a build."; then throw "nvf-tex-language does not support having more than 1 builders enabled!"
}; else (enabledBuildersCount == 1)
onSave = mkOption { ) ''
type = bool; Whether to enable configuring the builder.
default = false;
description = "Set this property to true if you want to compile the project after saving a file."; By enabling any of the builders, this option will be automatically set.
}; If you enable more than one builder then an error will be thrown.
useFileList = mkOption { '';
type = bool;
default = false; forwardSearchAfter = mkBool false "Set this property to true if you want to execute a forward search after a build.";
description = ''
onSave = mkBool false "Set this property to true if you want to compile the project after saving a file.";
useFileList = mkBool false ''
When set to true, the server will use the .fls files produced by the TeX engine as an additional input for the project detection. When set to true, the server will use the .fls files produced by the TeX engine as an additional input for the project detection.
Note that enabling this property might have an impact on performance. Note that enabling this property might have an impact on performance.
''; '';
};
auxDirectory = mkOption { auxDirectory = mkOption {
type = str; type = str;
default = "."; default = ".";
@ -42,6 +79,7 @@ in {
When using a latexmkrc file, texlab will automatically infer the correct setting. When using a latexmkrc file, texlab will automatically infer the correct setting.
''; '';
}; };
logDirectory = mkOption { logDirectory = mkOption {
type = str; type = str;
default = "."; default = ".";
@ -52,6 +90,7 @@ in {
When using a latexmkrc file, texlab will automatically infer the correct setting. When using a latexmkrc file, texlab will automatically infer the correct setting.
''; '';
}; };
pdfDirectory = mkOption { pdfDirectory = mkOption {
type = str; type = str;
default = "."; default = ".";
@ -62,9 +101,10 @@ in {
When using a latexmkrc file, texlab will automatically infer the correct setting. When using a latexmkrc file, texlab will automatically infer the correct setting.
''; '';
}; };
filename = mkOption { filename = mkOption {
type = str; type = nullOr str;
default = ""; default = null;
description = '' description = ''
Allows overriding the default file name of the build artifact. This setting is used to find the correct PDF file to open during forward search. Allows overriding the default file name of the build artifact. This setting is used to find the correct PDF file to open during forward search.
''; '';

View file

@ -19,7 +19,7 @@
}: let }: let
inherit (lib.options) mkOption; inherit (lib.options) mkOption;
inherit (lib.modules) mkIf; inherit (lib.modules) mkIf;
inherit (lib.types) listOf package str; inherit (lib.types) listOf package str attrs;
inherit inherit
(builtins) (builtins)
attrNames attrNames
@ -36,8 +36,8 @@
inherit (lib.nvim.config) mkBool; inherit (lib.nvim.config) mkBool;
cfg = config.vim.languages.tex; cfg = config.vim.languages.tex;
texlabCfg = cfg.lsp.texlab;
# --- Enable Options --- builderCfg = cfg.build.builder;
in { in {
options.vim.languages.tex.lsp.texlab = { options.vim.languages.tex.lsp.texlab = {
enable = mkBool config.vim.languages.enableLSP "Whether to enable Tex LSP support (texlab)"; enable = mkBool config.vim.languages.enableLSP "Whether to enable Tex LSP support (texlab)";
@ -91,11 +91,11 @@ in {
}; };
extraLuaSettings = mkOption { extraLuaSettings = mkOption {
type = str; type = attrs;
default = ""; default = {};
example = '' example = {
formatterLineLength = 80, formatterLineLength = 80;
''; };
description = '' description = ''
For any options that do not have options provided through nvf this can be used to add them. For any options that do not have options provided through nvf this can be used to add them.
Options already declared in nvf config will NOT be overridden. Options already declared in nvf config will NOT be overridden.
@ -119,97 +119,47 @@ in {
config = mkIf (cfg.enable && (cfg.lsp.texlab.enable)) ( config = mkIf (cfg.enable && (cfg.lsp.texlab.enable)) (
let let
tl = cfg.lsp.texlab; # ----- Setup Config -----
builder = cfg.build.builder; # Command to start the LSP
setupConfig.cmd = ["${texlabCfg.package}/bin/texlab"];
listToLua = list: nullOnEmpty: # Create texlab settings section
if length list == 0 setupConfig.settings.texlab = (
then {}
if nullOnEmpty # -- Forward Search --
then "null" // (
else "{ }" if texlabCfg.forwardSearch.enable
else "{ ${concatStringsSep ", " (map (x: ''"${toString x}"'') list)} }"; then {
stringToLua = string: nullOnEmpty:
if string == ""
then
if nullOnEmpty
then "null"
else ""
else ''"${string}"'';
boolToLua = boolean:
if boolean
then "true"
else "false";
# -- Build --
buildConfig = let
# This function will sort through the builder options and count how many
# builders have been enabled.
getEnabledBuildersCount = {
enabledBuildersCount ? 0,
index ? 0,
builderNamesList ? (
filter (
x: let
y = cfg.build.builders.${x};
in (isAttrs y && hasAttr "enable" y)
) (attrNames cfg.build.builders)
),
}: let
currentBuilderName = elemAt builderNamesList index;
currentBuilder = cfg.build.builders.${currentBuilderName};
nextIndex = index + 1;
newEnabledBuildersCount =
if currentBuilder.enable
then enabledBuildersCount + 1
else enabledBuildersCount;
in
if length builderNamesList > nextIndex
then
getEnabledBuildersCount {
inherit builderNamesList;
enabledBuildersCount = newEnabledBuildersCount;
index = nextIndex;
}
else newEnabledBuildersCount;
enabledBuildersCount = getEnabledBuildersCount {};
in
if enabledBuildersCount == 0
then ""
else if enabledBuildersCount > 1
then throw "Texlab does not support having more than 1 builders enabled!"
else ''
build = {
executable = "${builder.package}/bin/${builder.executable}",
args = ${listToLua builder.args false},
forwardSearchAfter = ${boolToLua cfg.build.forwardSearchAfter},
onSave = ${boolToLua cfg.build.onSave},
useFileList = ${boolToLua cfg.build.useFileList},
auxDirectory = ${stringToLua cfg.build.auxDirectory true},
logDirectory = ${stringToLua cfg.build.logDirectory true},
pdfDirectory = ${stringToLua cfg.build.pdfDirectory true},
filename = ${stringToLua cfg.build.filename true},
},
'';
in {
vim.lsp.lspconfig.sources.texlab = ''
lspconfig.texlab.setup {
cmd = { "${tl.package}/bin/texlab" },
settings = {
texlab = {
${buildConfig}
forwardSearch = { forwardSearch = {
executable = "${tl.forwardSearch.package}/bin/${tl.forwardSearch.executable}", executable = "${texlabCfg.forwardSearch.package}/bin/${texlabCfg.forwardSearch.executable}";
args = ${listToLua tl.forwardSearch.args true} args = texlabCfg.forwardSearch.args;
}, };
${tl.extraLuaSettings}
} }
else {}
)
# -- Build --
// (
if cfg.build.enable
then {
build = {
executable = "${builderCfg.package}/bin/${builderCfg.executable}";
args = builderCfg.args;
forwardSearchAfter = cfg.build.forwardSearchAfter;
onSave = cfg.build.onSave;
useFileList = cfg.build.useFileList;
auxDirectory = cfg.build.auxDirectory;
logDirectory = cfg.build.logDirectory;
pdfDirectory = cfg.build.pdfDirectory;
filename = cfg.build.filename;
};
} }
} else {}
''; )
# -- Extra --
// texlabCfg.extraLuaSettings
);
in {
vim.lsp.lspconfig.sources.texlab = "lspconfig.texlab.setup(${lib.nvim.lua.toLuaObject setupConfig})";
} }
); );
} }