From 664b27cb2c64d854facc022740c11540fb4e378c Mon Sep 17 00:00:00 2001 From: isaacST08 Date: Sat, 3 May 2025 18:51:58 -0600 Subject: [PATCH] Refactored pdfViewer and fixed infrec error --- modules/plugins/languages/tex/default.nix | 3 +- modules/plugins/languages/tex/lsp/default.nix | 12 +- modules/plugins/languages/tex/lsp/texlab.nix | 9 +- .../languages/tex/pdfViewer/custom.nix | 43 ---- .../languages/tex/pdfViewer/default.nix | 208 +++++++----------- .../tex/pdfViewer/getEnabledPdfViewer.nix | 22 ++ .../languages/tex/pdfViewer/okular.nix | 41 ---- .../tex/pdfViewer/premadePdfViewers.nix | 80 +++++++ .../languages/tex/pdfViewer/qpdfview.nix | 41 ---- .../languages/tex/pdfViewer/sioyek.nix | 55 ----- .../tex/pdfViewer/viewerTemplate.nix | 68 ------ .../languages/tex/pdfViewer/zathura.nix | 42 ---- 12 files changed, 194 insertions(+), 430 deletions(-) delete mode 100644 modules/plugins/languages/tex/pdfViewer/custom.nix create mode 100644 modules/plugins/languages/tex/pdfViewer/getEnabledPdfViewer.nix delete mode 100644 modules/plugins/languages/tex/pdfViewer/okular.nix create mode 100644 modules/plugins/languages/tex/pdfViewer/premadePdfViewers.nix delete mode 100644 modules/plugins/languages/tex/pdfViewer/qpdfview.nix delete mode 100644 modules/plugins/languages/tex/pdfViewer/sioyek.nix delete mode 100644 modules/plugins/languages/tex/pdfViewer/viewerTemplate.nix delete mode 100644 modules/plugins/languages/tex/pdfViewer/zathura.nix diff --git a/modules/plugins/languages/tex/default.nix b/modules/plugins/languages/tex/default.nix index 7b1df4cd..7dbe7e3a 100644 --- a/modules/plugins/languages/tex/default.nix +++ b/modules/plugins/languages/tex/default.nix @@ -44,6 +44,7 @@ in { `vim.g.tex_flavor = ` line from your lua config entirely (unless you manually set it elsewhere of course). ''; + flavor = mkOption { type = enum [ "plaintex" @@ -71,7 +72,7 @@ in { config = mkIf cfg.enable (mkMerge [ # Extra Lua config options (mkIf cfg.extraOpts.texFlavor.enable { - vim.globals.tex_flavor = "${cfg.extraOpts.texFlavor.flavor}"; + vim.globals.tex_flavor = lib.mkDefault "${cfg.extraOpts.texFlavor.flavor}"; }) ]); } diff --git a/modules/plugins/languages/tex/lsp/default.nix b/modules/plugins/languages/tex/lsp/default.nix index affb272a..244829d0 100644 --- a/modules/plugins/languages/tex/lsp/default.nix +++ b/modules/plugins/languages/tex/lsp/default.nix @@ -12,7 +12,13 @@ in { ./texlab.nix ]; - config = mkIf (cfg.enable && (any (x: x.enable) (attrValues cfg.lsp))) { - vim.lsp.lspconfig.enable = true; # Enable lspconfig when any of the lsps are enabled - }; + config = + mkIf + ( + cfg.enable # Check if nvf is enabled. + && (any (x: x.enable) (attrValues cfg.lsp)) # Check if any of the LSPs have been enabled. + ) + { + vim.lsp.lspconfig.enable = lib.mkDefault true; # Enable lspconfig when any of the lsps are enabled + }; } diff --git a/modules/plugins/languages/tex/lsp/texlab.nix b/modules/plugins/languages/tex/lsp/texlab.nix index 963d280a..339fb40c 100644 --- a/modules/plugins/languages/tex/lsp/texlab.nix +++ b/modules/plugins/languages/tex/lsp/texlab.nix @@ -26,6 +26,9 @@ cfg = config.vim.languages.tex; texlabCfg = cfg.lsp.texlab; builderCfg = cfg.build.builder; + + # Get the enabled pdf viewer. + pdfViewer = import ../pdfViewer/getEnabledPdfViewer.nix {inherit lib config;}; in { options.vim.languages.tex.lsp.texlab = { enable = mkBool config.vim.languages.enableLSP '' @@ -271,7 +274,7 @@ in { package = mkOption { type = package; - default = cfg.pdfViewer.package; + default = pdfViewer.package; description = '' The package to use as your PDF viewer. This viewer needs to support Synctex. @@ -282,7 +285,7 @@ in { executable = mkOption { type = str; - default = cfg.pdfViewer.executable; + default = pdfViewer.executable; description = '' Defines the executable of the PDF previewer. The previewer needs to support SyncTeX. @@ -293,7 +296,7 @@ in { args = mkOption { type = listOf str; - default = cfg.pdfViewer.args; + default = pdfViewer.args; description = '' Defines additional arguments that are passed to the configured previewer to perform the forward search. diff --git a/modules/plugins/languages/tex/pdfViewer/custom.nix b/modules/plugins/languages/tex/pdfViewer/custom.nix deleted file mode 100644 index 7b8a9de4..00000000 --- a/modules/plugins/languages/tex/pdfViewer/custom.nix +++ /dev/null @@ -1,43 +0,0 @@ -{ - pkgs, - lib, - ... -} @ moduleInheritancePackage: let - # The name of the pdf viewer - name = "custom"; - - # The viewer template - template = import ./viewerTemplate.nix; - - inherit (lib.options) mkOption mkEnableOption mkPackageOption; - inherit (lib.types) str listOf; -in ( - template { - inherit name moduleInheritancePackage; - - options = { - enable = mkEnableOption "enable using a custom pdf viewer."; - - package = mkPackageOption pkgs "okular" { - extraDescription = "custom viewer package"; - }; - - executable = mkOption { - type = str; - example = "okular"; - description = "The executable name to call the viewer."; - }; - - args = mkOption { - type = listOf str; - example = [ - "--unique" - "file:%p#src:%l%f" - ]; - description = "Arguments to pass to the viewer."; - }; - }; - - argsFunction = viewerCfg: (viewerCfg.args); - } -) diff --git a/modules/plugins/languages/tex/pdfViewer/default.nix b/modules/plugins/languages/tex/pdfViewer/default.nix index 6d6dcd7e..ffa946af 100644 --- a/modules/plugins/languages/tex/pdfViewer/default.nix +++ b/modules/plugins/languages/tex/pdfViewer/default.nix @@ -1,162 +1,104 @@ { config, lib, + pkgs, ... }: let - defaultPdfViewerName = "okular"; - - inherit - (builtins) - filter - isAttrs - hasAttr - attrNames - length - elemAt - ; - inherit (lib.modules) mkIf; inherit (lib.options) mkOption; inherit (lib.types) str package listOf; cfg = config.vim.languages.tex; - viewerCfg = cfg.pdfViewer; - enabledPdfViewersInfo = let - # This function will sort through the pdf viewer options and count how many - # pdf viewers have been enabled. - # If no viewers have been enabled, the count will be 0 and the name of the - # enabled viewer will be the default pdf viewer defined above. - getEnabledPdfViewersInfo = { - enabledPdfViewersCount ? 0, - index ? 0, - pdfViewerNamesList ? ( - filter ( - x: let - y = viewerCfg."${x}"; - in ( - isAttrs y && hasAttr "enable" y && hasAttr "package" y && hasAttr "executable" y && hasAttr "args" y - ) - ) (attrNames viewerCfg) - ), - currentEnabledPdfViewerName ? defaultPdfViewerName, - }: let - # Get the name of the current pdf viewer being checked if it is enabled - currentPdfViewerName = elemAt pdfViewerNamesList index; + pdfViewer = {name, ...}: { + options = { + enable = lib.mkEnableOption "${builtins.toString name} pdf viewer"; - # Get the current pdf viewer object - currentPdfViewer = viewerCfg."${currentPdfViewerName}"; + name = mkOption { + type = str; + example = "okular"; + description = '' + The name of the pdf viewer to use. - # Get the index that will be used for the next iteration - nextIndex = index + 1; + This value will be automatically set when any of the viewers are + enabled. - # Increment the count that is recording the number of enabled pdf viewers - # if this viewer is enabled, otherwise leave it as is. - newEnabledPdfViewersCount = - if currentPdfViewer.enable - then enabledPdfViewersCount + 1 - else enabledPdfViewersCount; + This value will be automatically set to the value of the parent + attribute set. ex. `...tex.pdfViewer..name = "$${name}"` + This value cannot and should not be changed to be different from this + parent value. - # If this pdf viewer is enabled, set is as the enabled viewer. - newEnabledPdfViewerName = - if currentPdfViewer.enable - then currentPdfViewerName - else currentEnabledPdfViewerName; - in - # Check that the end of the list of viewers has not been reached - if length pdfViewerNamesList > nextIndex - # If the end of the viewers list has not been reached, call the next iteration - # of the function to process the next viewer - then - getEnabledPdfViewersInfo { - inherit pdfViewerNamesList; - enabledPdfViewersCount = newEnabledPdfViewersCount; - index = nextIndex; - currentEnabledPdfViewerName = newEnabledPdfViewerName; - } - # If the end of the viewers list has been reached, then return the total number - # of viewers that have been enabled and the name of the last viewer that was enabled. - else { - count = newEnabledPdfViewersCount; - enabledViewerName = newEnabledPdfViewerName; + Default values already exist such as `...tex.pdfViewer.okular` but + you can override the default values or created completely custom + pdf viewers should you wish. + ''; }; - in (getEnabledPdfViewersInfo {}); - enabledPdfViewerCfg = viewerCfg."${enabledPdfViewersInfo.enabledViewerName}"; + package = mkOption { + type = package; + example = pkgs.kdePackages.okular; + description = "The package of the pdf viewer to use."; + }; + + executable = mkOption { + type = str; + default = "${builtins.toString name}"; + description = '' + The executable for the pdf viewer to use. + + It will be called as `/bin/`. + + By default, the name of the pdf viewer will be used. + ''; + }; + + args = mkOption { + type = listOf str; + default = []; + description = '' + The command line arguments to use when calling the pdf viewer command. + + These will be called as + `/bin/ ...`. + ''; + }; + }; + + # The name of the pdf viewer must be set to the parent attribute set name. + config.name = lib.mkForce name; + }; in { imports = [ - ./custom.nix - ./okular.nix - ./qpdfview.nix - ./sioyek.nix - ./zathura.nix + ./premadePdfViewers.nix ]; - options.vim.languages.tex.pdfViewer = { - name = mkOption { - type = str; - default = enabledPdfViewerCfg.name; - description = '' - The name of the pdf viewer to use. + options.vim.languages.tex.pdfViewer = mkOption { + type = with lib.types; attrsOf (submodule pdfViewer); + default = {}; + example = { + zathura.enable = true; - This value will be automatically set when any of the viewers are - enabled. - - Setting this option option manually is not recommended but can be used - for some very technical nix-ing. If you wish to use a custom viewer, - please use the `custom` entry provided under `viewers`. - ''; - }; - - package = mkOption { - type = package; - default = enabledPdfViewerCfg.package; - description = '' - The package of the pdf viewer to use. - - This value will be automatically set when any of the viewers are - enabled. - - Setting this option option manually is not recommended but can be used - for some very technical nix-ing. If you wish to use a custom viewer, - please use the `custom` entry provided under `viewers`. - ''; - }; - - executable = mkOption { - type = str; - default = enabledPdfViewerCfg.executable; - description = '' - The executable for the pdf viewer to use. - - This value will be automatically set when any of the viewers are - enabled. - - Setting this option option manually is not recommended but can be used - for some very technical nix-ing. If you wish to use a custom viewer, - please use the `custom` entry provided under `viewers`. - ''; - }; - - args = mkOption { - type = listOf str; - default = enabledPdfViewerCfg.args; - description = '' - The command line arguments to use when calling the pdf viewer command. - - This value will be automatically set when any of the viewers are - enabled. - - Setting this option option manually is not recommended but can be used - for some very technical nix-ing. If you wish to use a custom viewer, - please use the `custom` entry provided under `viewers`. - ''; + customOkular = { + enable = false; + package = pkgs.kdePackages.okular; + executable = "okular"; + args = [ + "--unique" + "file:%p#src:%l%f" + ]; + }; }; }; - config = mkIf (enabledPdfViewersInfo.count > 0) { + config = let + # List form of all pdf viewers. + pdfViewers = builtins.attrValues cfg.pdfViewer; + + countPdfViewers = viewers: (lib.lists.count (x: x.enable) viewers); + in { assertions = [ { - assertion = enabledPdfViewersInfo.count < 2; + # Assert that there is only one enabled pdf viewer. + assertion = (countPdfViewers pdfViewers) < 2; message = '' The nvf-tex-language implementation does not support having more than 1 pdf viewers enabled. diff --git a/modules/plugins/languages/tex/pdfViewer/getEnabledPdfViewer.nix b/modules/plugins/languages/tex/pdfViewer/getEnabledPdfViewer.nix new file mode 100644 index 00000000..e8790c84 --- /dev/null +++ b/modules/plugins/languages/tex/pdfViewer/getEnabledPdfViewer.nix @@ -0,0 +1,22 @@ +{ + config, + lib, + ... +}: let + # The attribute set of pdf viewers in this configuration. + pdfViewers = config.vim.languages.tex.pdfViewer; + + # The list of pdf viewers in this configuration. + pdfViewersList = builtins.attrValues pdfViewers; + + # The list of enabled pdf viewers. + enabledPdfViewersList = builtins.filter (x: x.enable) pdfViewersList; + + # The number of enabled pdf viewers. + enabledPdfViewersCount = lib.lists.count (x: x.enable) pdfViewersList; +in + if (enabledPdfViewersCount == 0) + # Use the fallback if no pdf viewer was enabled. + then pdfViewers.fallback + # Otherwise get the first enabled viewer. + else builtins.head enabledPdfViewersList diff --git a/modules/plugins/languages/tex/pdfViewer/okular.nix b/modules/plugins/languages/tex/pdfViewer/okular.nix deleted file mode 100644 index 0c3d7f8d..00000000 --- a/modules/plugins/languages/tex/pdfViewer/okular.nix +++ /dev/null @@ -1,41 +0,0 @@ -{ - pkgs, - lib, - ... -} @ moduleInheritancePackage: let - # The name of the pdf viewer - name = "okular"; - - # The viewer template - template = import ./viewerTemplate.nix; - - inherit (lib.options) mkOption mkEnableOption mkPackageOption; - inherit (lib.types) str listOf; -in ( - template { - inherit name moduleInheritancePackage; - - options = { - enable = mkEnableOption "enable okular as the pdf file previewer."; - - package = mkPackageOption pkgs "okular" {}; - - executable = mkOption { - type = str; - default = "okular"; - description = "The executable name to call the viewer."; - }; - - args = mkOption { - type = listOf str; - default = [ - "--unique" - "file:%p#src:%l%f" - ]; - description = "Arguments to pass to the viewer."; - }; - }; - - argsFunction = viewerCfg: (viewerCfg.args); - } -) diff --git a/modules/plugins/languages/tex/pdfViewer/premadePdfViewers.nix b/modules/plugins/languages/tex/pdfViewer/premadePdfViewers.nix new file mode 100644 index 00000000..622a058e --- /dev/null +++ b/modules/plugins/languages/tex/pdfViewer/premadePdfViewers.nix @@ -0,0 +1,80 @@ +{ + pkgs, + lib, + ... +}: let + inherit (lib) mkDefault mkForce; + + mkPdfViewerDefaults = { + package, + executable, + args ? [], + }: { + package = mkDefault package; + executable = mkDefault executable; + args = mkDefault args; + }; +in { + config.vim.languages.tex.pdfViewer = { + okular = mkPdfViewerDefaults { + package = pkgs.kdePackages.okular; + executable = "okular"; + args = [ + "--unique" + "file:%p#src:%l%f" + ]; + }; + + sioyek = mkPdfViewerDefaults { + package = pkgs.sioyek; + executable = "sioyek"; + args = [ + "--reuse-window" + "--execute-command" + "toggle_synctex" + "--inverse-search" + "texlab inverse-search -i \"%%1\" -l %%2" + "--forward-search-file" + "%f" + "--forward-search-line" + "%l" + "%p" + ]; + }; + + qpdfview = mkPdfViewerDefaults { + package = pkgs.qpdfview; + executable = "qpdfview"; + args = [ + "--unique" + "%p#src:%f:%l:1" + ]; + }; + + zathura = mkPdfViewerDefaults { + package = pkgs.zathura; + executable = "zathura"; + args = [ + "--synctex-forward" + "%l:1:%f" + "%p" + ]; + }; + + # This is a special pdf viewer. It is force set to a basic and known + # working configuration of okular and is used where needed in the + # rest of the tex language configuration encase no other pdf viewer + # was enabled. + # It cannot be enabled on its own and exists purely as a fallback + # option for internal use. + fallback = { + enable = mkForce false; + package = mkForce pkgs.kdePackages.okular; + executable = mkForce "okular"; + args = mkForce [ + "--unique" + "file:%p#src:%l%f" + ]; + }; + }; +} diff --git a/modules/plugins/languages/tex/pdfViewer/qpdfview.nix b/modules/plugins/languages/tex/pdfViewer/qpdfview.nix deleted file mode 100644 index 1938fb94..00000000 --- a/modules/plugins/languages/tex/pdfViewer/qpdfview.nix +++ /dev/null @@ -1,41 +0,0 @@ -{ - pkgs, - lib, - ... -} @ moduleInheritancePackage: let - # The name of the pdf viewer - name = "qpdfview"; - - # The viewer template - template = import ./viewerTemplate.nix; - - inherit (lib.options) mkOption mkEnableOption mkPackageOption; - inherit (lib.types) str listOf; -in ( - template { - inherit name moduleInheritancePackage; - - options = { - enable = mkEnableOption "enable qpdfview as the pdf file previewer."; - - package = mkPackageOption pkgs "qpdfview" {}; - - executable = mkOption { - type = str; - default = "qpdfview"; - description = "The executable name to call the viewer."; - }; - - args = mkOption { - type = listOf str; - default = [ - "--unique" - "%p#src:%f:%l:1" - ]; - description = "Arguments to pass to the viewer."; - }; - }; - - argsFunction = viewerCfg: (viewerCfg.args); - } -) diff --git a/modules/plugins/languages/tex/pdfViewer/sioyek.nix b/modules/plugins/languages/tex/pdfViewer/sioyek.nix deleted file mode 100644 index ad0b4943..00000000 --- a/modules/plugins/languages/tex/pdfViewer/sioyek.nix +++ /dev/null @@ -1,55 +0,0 @@ -{ - pkgs, - lib, - ... -} @ moduleInheritancePackage: let - # The name of the pdf viewer - name = "sioyek"; - - # The viewer template - template = import ./viewerTemplate.nix; - - inherit (lib.options) mkOption mkEnableOption mkPackageOption; - inherit (lib.types) str listOf; -in ( - template { - inherit name moduleInheritancePackage; - - options = { - enable = mkEnableOption "sioyek as the pdf file previewer."; - - package = mkPackageOption pkgs "sioyek" {}; - - executable = mkOption { - type = str; - default = "sioyek"; - description = "The executable name to call the viewer."; - }; - - args = mkOption { - type = listOf str; - default = [ - "--reuse-window" - "--execute-command" - "toggle_synctex" - "--inverse-search" - "texlab inverse-search -i \"%%1\" -l %%2" - "--forward-search-file" - "%f" - "--forward-search-line" - "%l" - "%p" - ]; - description = '' - Arguments to pass to the viewer. - - By default, this is the only viewer that supports the inverse search - feature by command line arguments and doesn't explicitly require extra - tinkering else where in your config. - ''; - }; - }; - - argsFunction = viewerCfg: (viewerCfg.args); - } -) diff --git a/modules/plugins/languages/tex/pdfViewer/viewerTemplate.nix b/modules/plugins/languages/tex/pdfViewer/viewerTemplate.nix deleted file mode 100644 index 72c52877..00000000 --- a/modules/plugins/languages/tex/pdfViewer/viewerTemplate.nix +++ /dev/null @@ -1,68 +0,0 @@ -# This function acts as a template for creating new pdf viewers. -# It enforces providing all the parameters required for creating -# a new pdf viewer for it to be able to work in the existing code. -# -# The first layer requirements are as follows: -{ - # This is the name of the pdf viewer, it will only be used internally and - # MUST match the .nix file that the pdf viewer is implemented in. - name, - # - # Module attribute set. This is the attribute set that the module that is - # defining a pdf viewer is passed as its input. - moduleInheritancePackage, - # - # These are the standard options for the pdf viewer just like creating any - # other module. Some options are required and are described below but - # it will also accept any other options that are provided to it. - options, - # - # These are the command line arguments that will accompany the executable - # when the view command is called. - # This is a function that will take in the cfg of its own pdf viewer. - # i.e. it will be called as "args cfg.pdfViewer.${name}" - argsFunction, - ... -}: let - # Inherit the necessary variables available to any module. - inherit (moduleInheritancePackage) lib config; - # - # Inherit other useful functions. - inherit (lib.modules) mkIf; - # - # Set the cfg variable - cfg = config.vim.languages.tex; - # - # Set the cfg of the viewer itself - viewerCfg = cfg.pdfViewer.${name}; -in { - # These are the options for the pdf viewer. It will accept any options - # provided to it but some options are mandatory: - options.vim.languages.tex.pdfViewer.${name} = ({ - # The enable option. This one is self explanatory. - enable, - # - # This is the package option for the pdf viewer. - package, - # - # This is the executable that will be used to call the pdf viewer. - # It, along with package will result in: - # "/bin/" - executable, - # - # Any other options provided are accepted. - ... - } @ opts: - opts) - options; - - # Check that the language and this pdf viewer have been enabled before making - # any config. - config = mkIf (cfg.enable && viewerCfg.enable) { - vim.languages.tex.pdfViewer = { - inherit name; - inherit (viewerCfg) package executable; - args = argsFunction viewerCfg; - }; - }; -} diff --git a/modules/plugins/languages/tex/pdfViewer/zathura.nix b/modules/plugins/languages/tex/pdfViewer/zathura.nix deleted file mode 100644 index f1fd16d5..00000000 --- a/modules/plugins/languages/tex/pdfViewer/zathura.nix +++ /dev/null @@ -1,42 +0,0 @@ -{ - pkgs, - lib, - ... -} @ moduleInheritancePackage: let - # The name of the pdf viewer - name = "zathura"; - - # The viewer template - template = import ./viewerTemplate.nix; - - inherit (lib.options) mkOption mkEnableOption mkPackageOption; - inherit (lib.types) str listOf; -in ( - template { - inherit name moduleInheritancePackage; - - options = { - enable = mkEnableOption "enable zathura as the pdf file previewer."; - - package = mkPackageOption pkgs "zathura" {}; - - executable = mkOption { - type = str; - default = "zathura"; - description = "The executable name to call the viewer."; - }; - - args = mkOption { - type = listOf str; - default = [ - "--synctex-forward" - "%l:1:%f" - "%p" - ]; - description = "Arguments to pass to the viewer."; - }; - }; - - argsFunction = viewerCfg: (viewerCfg.args); - } -)