Skip to content

Commit

Permalink
Refactored to use the nvf library toLuaObject as well as other improv…
Browse files Browse the repository at this point in the history
…ements and cleanups
  • Loading branch information
isaacST08 committed Jan 27, 2025
1 parent ca9ab42 commit 1bebf49
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 129 deletions.
14 changes: 7 additions & 7 deletions modules/plugins/languages/tex/build/builders/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
pkgs,
lib,
...
}:
let
}: let
inherit (lib.options) mkOption;
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;
in
{
in {
imports = [
./latexmk.nix
./tectonic.nix
Expand All @@ -27,6 +26,8 @@ in
This is just the default custom option. By setting any of the
builders to true, this will be overwritten by that builder's
parameters.
Setting this parameter to the name of a declared builder will
not automatically enable that builder.
'';
};
args = mkOption {
Expand All @@ -45,7 +46,7 @@ in
};
package = mkOption {
type = package;
default = (pkgs.texlive.withPackages (ps: [ ps.latexmk ]));
default = pkgs.texlive.withPackages (ps: [ps.latexmk]);
description = ''
The tex builder package to use.
Expand All @@ -67,4 +68,3 @@ in
};
};
}

80 changes: 60 additions & 20 deletions modules/plugins/languages/tex/build/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,71 @@
...
}: let
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;

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 {
imports = [
./builders
];

options.vim.languages.tex.build = {
forwardSearchAfter = mkOption {
type = bool;
default = false;
description = "Set this property to true if you want to execute a forward search after a build.";
};
onSave = mkOption {
type = bool;
default = false;
description = "Set this property to true if you want to compile the project after saving a file.";
};
useFileList = mkOption {
type = bool;
default = false;
description = ''
When set to true, the server will use the .fls files produced by the TeX engine as an additional input for the project detection.
enable =
mkBool (
if enabledBuildersCount > 1
then throw "nvf-tex-language does not support having more than 1 builders enabled!"
else (enabledBuildersCount == 1)
) ''
Whether to enable configuring the builder.
Note that enabling this property might have an impact on performance.
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.
'';
};

forwardSearchAfter = mkBool false "Set this property to true if you want to execute a forward search after a build.";

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.
Note that enabling this property might have an impact on performance.
'';

auxDirectory = mkOption {
type = str;
default = ".";
Expand All @@ -42,6 +79,7 @@ in {
When using a latexmkrc file, texlab will automatically infer the correct setting.
'';
};

logDirectory = mkOption {
type = str;
default = ".";
Expand All @@ -52,6 +90,7 @@ in {
When using a latexmkrc file, texlab will automatically infer the correct setting.
'';
};

pdfDirectory = mkOption {
type = str;
default = ".";
Expand All @@ -62,9 +101,10 @@ in {
When using a latexmkrc file, texlab will automatically infer the correct setting.
'';
};

filename = mkOption {
type = str;
default = "";
type = nullOr str;
default = null;
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.
'';
Expand Down
154 changes: 52 additions & 102 deletions modules/plugins/languages/tex/lsp/texlab.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}: let
inherit (lib.options) mkOption;
inherit (lib.modules) mkIf;
inherit (lib.types) listOf package str;
inherit (lib.types) listOf package str attrs;
inherit
(builtins)
attrNames
Expand All @@ -36,8 +36,8 @@
inherit (lib.nvim.config) mkBool;

cfg = config.vim.languages.tex;

# --- Enable Options ---
texlabCfg = cfg.lsp.texlab;
builderCfg = cfg.build.builder;
in {
options.vim.languages.tex.lsp.texlab = {
enable = mkBool config.vim.languages.enableLSP "Whether to enable Tex LSP support (texlab)";
Expand All @@ -50,13 +50,13 @@ in {

forwardSearch = {
enable = mkBool false ''
Whether to enable forward search.
Whether to enable forward search.
Enable this option if you want to have the compiled document appear in your chosen PDF viewer.
Enable this option if you want to have the compiled document appear in your chosen PDF viewer.
For some options see [here](https://github.com/latex-lsp/texlab/wiki/Previewing).
Note this is not all the options, but can act as a guide to help you allong with custom configs.
'';
For some options see [here](https://github.com/latex-lsp/texlab/wiki/Previewing).
Note this is not all the options, but can act as a guide to help you allong with custom configs.
'';
package = mkOption {
type = package;
default = pkgs.okular;
Expand Down Expand Up @@ -91,11 +91,11 @@ in {
};

extraLuaSettings = mkOption {
type = str;
default = "";
example = ''
formatterLineLength = 80,
'';
type = attrs;
default = {};
example = {
formatterLineLength = 80;
};
description = ''
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.
Expand All @@ -119,97 +119,47 @@ in {

config = mkIf (cfg.enable && (cfg.lsp.texlab.enable)) (
let
tl = cfg.lsp.texlab;
builder = cfg.build.builder;

listToLua = list: nullOnEmpty:
if length list == 0
then
if nullOnEmpty
then "null"
else "{ }"
else "{ ${concatStringsSep ", " (map (x: ''"${toString x}"'') list)} }";

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;
# ----- Setup Config -----
# Command to start the LSP
setupConfig.cmd = ["${texlabCfg.package}/bin/texlab"];

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 = {
executable = "${tl.forwardSearch.package}/bin/${tl.forwardSearch.executable}",
args = ${listToLua tl.forwardSearch.args true}
},
${tl.extraLuaSettings}
}
# Create texlab settings section
setupConfig.settings.texlab = (
{}
# -- Forward Search --
// (
if texlabCfg.forwardSearch.enable
then {
forwardSearch = {
executable = "${texlabCfg.forwardSearch.package}/bin/${texlabCfg.forwardSearch.executable}";
args = texlabCfg.forwardSearch.args;
};
}
}
'';
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})";
}
);
}

0 comments on commit 1bebf49

Please sign in to comment.