From 8a0e8df411d66533520f797dd1341daac56aa099 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 21 Oct 2024 17:20:02 +0300 Subject: [PATCH 1/7] spellcheck: allow adding arbitrary spellfiles from name-value pairs --- modules/neovim/init/spellcheck.nix | 107 ++++++++++++++++++++++++----- 1 file changed, 90 insertions(+), 17 deletions(-) diff --git a/modules/neovim/init/spellcheck.nix b/modules/neovim/init/spellcheck.nix index d8957ef..5de7400 100644 --- a/modules/neovim/init/spellcheck.nix +++ b/modules/neovim/init/spellcheck.nix @@ -1,11 +1,14 @@ { config, + pkgs, lib, ... }: let inherit (lib.modules) mkIf mkRenamedOptionModule; - inherit (lib.options) mkOption mkEnableOption literalExpression; - inherit (lib.types) listOf str; + inherit (lib.options) mkOption mkEnableOption literalExpression literalMarkdown; + inherit (lib.strings) concatStringsSep; + inherit (lib.attrsets) mapAttrsToList; + inherit (lib.types) listOf str attrsOf; inherit (lib.nvim.lua) listToLuaTable; inherit (lib.nvim.dag) entryAfter; @@ -25,9 +28,45 @@ in { A list of languages that should be used for spellchecking. To add your own language files, you may place your `spell` - directory in either `~/.config/nvim` or the + directory in either {file}`~/.config/nvim` or the [additionalRuntimePaths](#opt-vim.additionalRuntimePaths) - directory provided by **nvf**. + directory provided by nvf. + ''; + }; + + extraSpellFiles = mkOption { + type = attrsOf (listOf str); + default = {"en.utf-8" = ["nvf" "word_you_want_to_add"];}; + example = literalExpression {"en.utf-8" = ["nvf" "word_you_want_to_add"];}; + description = literalMarkdown '' + Additional words to be used for spellchecking. The names of each key + will be used as the language code for the spell file. E.g: + + ```nix + "en.utf-8" = [ ... ]; + ``` + + will result in `en.utf-8.add.spl` being added to Neovim's runtime + in the `after/spell` ."` format for Neovim to + compile your spellfiles without mangling the resulting file names. Please + make sure that you enter the correct value, as nvf does not do any kind of + internal checking. Please see `:help mkspell` for more details. + + # Example + + ```nix + # "en" is the name, and "utf-8" is the encoding. For most use cases, utf-8 + # will be enough, however, you may change it to any encoding format Neovim + # accepts, e.g., utf-16. + "en.utf-8" = ["nvf" "word_you_want_to_add"]; + => $out/after/spell/en-utf-8.add.spl + ``` + ::: ''; }; @@ -38,7 +77,7 @@ in { description = '' A list of filetypes for which spellchecking will be disabled. - You may use `echo &filetype` in Neovim to find out the + You may use {command}`:echo &filetype` in Neovim to find out the filetype for a specific buffer. ''; }; @@ -58,18 +97,52 @@ in { }; config = mkIf cfg.enable { - vim.luaConfigRC.spellcheck = entryAfter ["basic"] '' - vim.opt.spell = true - vim.opt.spelllang = ${listToLuaTable cfg.languages} + vim = { + additionalRuntimePaths = let + spellfilesJoined = pkgs.symlinkJoin { + name = "nvf-spellfiles-joined"; + paths = mapAttrsToList (name: value: pkgs.writeTextDir "spell/${name}.add" (concatStringsSep "\n" value)) cfg.extraSpellFiles; + postBuild = '' + echo "Spellfiles joined" + ''; + }; - -- Disable spellchecking for certain filetypes - -- as configured by `vim.spellcheck.ignoredFiletypes` - vim.api.nvim_create_autocmd({ "FileType" }, { - pattern = ${listToLuaTable cfg.ignoredFiletypes}, - callback = function() - vim.opt_local.spell = false - end, - }) - ''; + compileJoinedSpellfiles = + pkgs.runCommand "nvf-compile-spellfiles" { + nativeBuildInputs = [config.vim.package]; + } '' + mkdir -p $out/after/spell + + spellfilesJoined=$(find -L ${spellfilesJoined}/spell -type f) + for spellfile in $spellfilesJoined; do + # Hacky way to ensure that the mangled extensions are omitted from the + # joined spellfiles. E.g. + local extension=".add" + local name=$(basename $spellfile "$extension") + echo "Compiling spellfile: $spellfile" + nvim --headless --clean \ + --cmd "mkspell $out/after/spell/"$name".add.spl $spellfile" -Es -n + + ls -lah $out/after/spell + done + ''; + in [ + compileJoinedSpellfiles.outPath + ]; + + luaConfigRC.spellcheck = entryAfter ["basic"] '' + vim.opt.spell = true + vim.opt.spelllang = ${listToLuaTable cfg.languages} + + -- Disable spellchecking for certain filetypes + -- as configured by `vim.spellcheck.ignoredFiletypes` + vim.api.nvim_create_autocmd({ "FileType" }, { + pattern = ${listToLuaTable cfg.ignoredFiletypes}, + callback = function() + vim.opt_local.spell = false + end, + }) + ''; + }; }; } From 94e6f49b78175bfb1cff35ad35f1e50fa378d09c Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 21 Oct 2024 18:42:27 +0300 Subject: [PATCH 2/7] fix doc errors --- modules/neovim/init/spellcheck.nix | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/neovim/init/spellcheck.nix b/modules/neovim/init/spellcheck.nix index 5de7400..18788fb 100644 --- a/modules/neovim/init/spellcheck.nix +++ b/modules/neovim/init/spellcheck.nix @@ -5,7 +5,7 @@ ... }: let inherit (lib.modules) mkIf mkRenamedOptionModule; - inherit (lib.options) mkOption mkEnableOption literalExpression literalMarkdown; + inherit (lib.options) mkOption mkEnableOption literalExpression literalMD; inherit (lib.strings) concatStringsSep; inherit (lib.attrsets) mapAttrsToList; inherit (lib.types) listOf str attrsOf; @@ -37,8 +37,8 @@ in { extraSpellFiles = mkOption { type = attrsOf (listOf str); default = {"en.utf-8" = ["nvf" "word_you_want_to_add"];}; - example = literalExpression {"en.utf-8" = ["nvf" "word_you_want_to_add"];}; - description = literalMarkdown '' + example = literalExpression ''{"en.utf-8" = ["nvf" "word_you_want_to_add"];}''; + description = '' Additional words to be used for spellchecking. The names of each key will be used as the language code for the spell file. E.g: @@ -51,13 +51,13 @@ in { after all spell directories provided by plugins and those placed in {file}`$XDG_CONFIG_HOME/nvf/spell`. - :::warning + ::: {.warning} The attribute keys must be in `"."` format for Neovim to compile your spellfiles without mangling the resulting file names. Please make sure that you enter the correct value, as nvf does not do any kind of internal checking. Please see `:help mkspell` for more details. - # Example + Example: ```nix # "en" is the name, and "utf-8" is the encoding. For most use cases, utf-8 From 62292777f15f848fcdcbb6783cd68f6e635fa136 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 21 Oct 2024 18:54:15 +0300 Subject: [PATCH 3/7] apply review suggestions --- modules/neovim/init/spellcheck.nix | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/modules/neovim/init/spellcheck.nix b/modules/neovim/init/spellcheck.nix index 18788fb..1ce30c3 100644 --- a/modules/neovim/init/spellcheck.nix +++ b/modules/neovim/init/spellcheck.nix @@ -5,7 +5,7 @@ ... }: let inherit (lib.modules) mkIf mkRenamedOptionModule; - inherit (lib.options) mkOption mkEnableOption literalExpression literalMD; + inherit (lib.options) mkOption mkEnableOption literalExpression; inherit (lib.strings) concatStringsSep; inherit (lib.attrsets) mapAttrsToList; inherit (lib.types) listOf str attrsOf; @@ -27,10 +27,9 @@ in { description = '' A list of languages that should be used for spellchecking. - To add your own language files, you may place your `spell` - directory in either {file}`~/.config/nvim` or the - [additionalRuntimePaths](#opt-vim.additionalRuntimePaths) - directory provided by nvf. + To add your own language files, you may place your `spell` directory in either + {file}`$XDG_CONFIG_HOME/nvf` or in a path that is included in the + [additionalRuntimePaths](#opt-vim.additionalRuntimePaths) list provided by nvf. ''; }; @@ -39,15 +38,15 @@ in { default = {"en.utf-8" = ["nvf" "word_you_want_to_add"];}; example = literalExpression ''{"en.utf-8" = ["nvf" "word_you_want_to_add"];}''; description = '' - Additional words to be used for spellchecking. The names of each key - will be used as the language code for the spell file. E.g: + Additional words to be used for spellchecking. The names of each key will be + used as the language code for the spell file. For example ```nix "en.utf-8" = [ ... ]; ``` - will result in `en.utf-8.add.spl` being added to Neovim's runtime - in the `after/spell` Date: Mon, 21 Oct 2024 19:18:05 +0300 Subject: [PATCH 4/7] get rid of after/ --- modules/neovim/init/spellcheck.nix | 42 +++++++++++++++--------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/modules/neovim/init/spellcheck.nix b/modules/neovim/init/spellcheck.nix index 1ce30c3..c7b495f 100644 --- a/modules/neovim/init/spellcheck.nix +++ b/modules/neovim/init/spellcheck.nix @@ -6,7 +6,7 @@ }: let inherit (lib.modules) mkIf mkRenamedOptionModule; inherit (lib.options) mkOption mkEnableOption literalExpression; - inherit (lib.strings) concatStringsSep; + inherit (lib.strings) concatLines; inherit (lib.attrsets) mapAttrsToList; inherit (lib.types) listOf str attrsOf; inherit (lib.nvim.lua) listToLuaTable; @@ -33,9 +33,9 @@ in { ''; }; - extraSpellFiles = mkOption { + extraSpellWords = mkOption { type = attrsOf (listOf str); - default = {"en.utf-8" = ["nvf" "word_you_want_to_add"];}; + default = {}; example = literalExpression ''{"en.utf-8" = ["nvf" "word_you_want_to_add"];}''; description = '' Additional words to be used for spellchecking. The names of each key will be @@ -46,15 +46,13 @@ in { ``` will result in `en.utf-8.add.spl` being added to Neovim's runtime in the - `after/spell` ."` format for Neovim to compile your spellfiles without mangling the resulting file names. Please make sure that you enter the correct value, as nvf does not do any kind of - internal checking. Please see `:help mkspell` for more details. + internal checking. Please see {command}`:help mkspell` for more details. Example: @@ -63,7 +61,7 @@ in { # will be enough, however, you may change it to any encoding format Neovim # accepts, e.g., utf-16. "en.utf-8" = ["nvf" "word_you_want_to_add"]; - => $out/after/spell/en-utf-8.add.spl + => $out/spell/en-utf-8.add.spl ``` ::: ''; @@ -76,8 +74,10 @@ in { description = '' A list of filetypes for which spellchecking will be disabled. + ::: {.tip} You may use {command}`:echo &filetype` in Neovim to find out the filetype for a specific buffer. + ::: ''; }; @@ -100,7 +100,7 @@ in { additionalRuntimePaths = let spellfilesJoined = pkgs.symlinkJoin { name = "nvf-spellfiles-joined"; - paths = mapAttrsToList (name: value: pkgs.writeTextDir "spell/${name}.add" (concatStringsSep "\n" value)) cfg.extraSpellFiles; + paths = mapAttrsToList (name: value: pkgs.writeTextDir "spell/${name}.add" (concatLines value)) cfg.extraSpellWords; postBuild = '' echo "Spellfiles joined" ''; @@ -108,25 +108,25 @@ in { compileJoinedSpellfiles = pkgs.runCommandLocal "nvf-compile-spellfiles" { + # Use the same version of Neovim as the user's configuration nativeBuildInputs = [config.vim.package]; } '' - mkdir -p $out/after/spell + mkdir -p "$out/spell" - spellfilesJoined=$(find -L ${spellfilesJoined}/spell -type f) + spellfilesJoined=$(find -L "${spellfilesJoined}/spell" -type f) for spellfile in $spellfilesJoined; do - # Hacky way to ensure that the mangled extensions are omitted from the - # joined spellfiles. E.g. - local extension=".add" - local name=$(basename $spellfile "$extension") - echo "Compiling spellfile: $spellfile" - nvim --headless --clean \ - --cmd "mkspell $out/after/spell/"$name".add.spl $spellfile" -Es -n - - ls -lah $out/after/spell + # Hacky way to ensure that the mangled extensions are omitted from the + # joined spellfiles. E.g. + local name=$(basename "$spellfile" ".add") + echo "Compiling spellfile: $spellfile" + nvim --headless --clean \ + --cmd "mkspell $out/spell/$name.add.spl $spellfile" -Es -n done ''; in - mkIf (cfg.extraSpellFiles != {}) [ + mkIf (cfg.extraSpellWords != {}) [ + # If .outPath is missing, additionalRuntimePaths receives the *function* + # instead of a path, causing errors. compileJoinedSpellfiles.outPath ]; From 2e1779fbaed2762520366cfe6fba15b65e56d11d Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 28 Oct 2024 20:05:01 +0300 Subject: [PATCH 5/7] docs: update release notes --- docs/release-notes/rl-0.7.md | 5 +++++ modules/neovim/init/spellcheck.nix | 36 +++++++++++++++--------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/docs/release-notes/rl-0.7.md b/docs/release-notes/rl-0.7.md index 0d560f8..72c35ca 100644 --- a/docs/release-notes/rl-0.7.md +++ b/docs/release-notes/rl-0.7.md @@ -181,6 +181,7 @@ To migrate to `nixfmt`, simply change `vim.languages.nix.format.type` to default. - Refactor of `nvim-cmp` and completion related modules + - Remove `autocomplete.type` in favor of per-plugin enable options such as [](#opt-vim.autocomplete.nvim-cmp.enable). - Deprecate legacy Vimsnip in favor of Luasnip, and integrate @@ -269,9 +270,13 @@ To migrate to `nixfmt`, simply change `vim.languages.nix.format.type` to configuration for [dashboard.nvim](https://github.com/nvimdev/dashboard-nvim) - Update `lualine.nvim` input and add missing themes: + - Adds `ayu`, `gruvbox_dark`, `iceberg`, `moonfly`, `onedark`, `powerline_dark` and `solarized_light` themes. +- Add [](#opt-vim.spellcheck.extraSpellWords) to allow adding arbitrary + spellfiles to Neovim's runtime with ease. + [ppenguin](https://github.com/ppenguin): - Telescope: diff --git a/modules/neovim/init/spellcheck.nix b/modules/neovim/init/spellcheck.nix index c7b495f..16af3ee 100644 --- a/modules/neovim/init/spellcheck.nix +++ b/modules/neovim/init/spellcheck.nix @@ -98,30 +98,30 @@ in { config = mkIf cfg.enable { vim = { additionalRuntimePaths = let - spellfilesJoined = pkgs.symlinkJoin { - name = "nvf-spellfiles-joined"; - paths = mapAttrsToList (name: value: pkgs.writeTextDir "spell/${name}.add" (concatLines value)) cfg.extraSpellWords; - postBuild = '' - echo "Spellfiles joined" - ''; - }; - compileJoinedSpellfiles = pkgs.runCommandLocal "nvf-compile-spellfiles" { # Use the same version of Neovim as the user's configuration nativeBuildInputs = [config.vim.package]; - } '' - mkdir -p "$out/spell" - spellfilesJoined=$(find -L "${spellfilesJoined}/spell" -type f) - for spellfile in $spellfilesJoined; do - # Hacky way to ensure that the mangled extensions are omitted from the - # joined spellfiles. E.g. - local name=$(basename "$spellfile" ".add") - echo "Compiling spellfile: $spellfile" - nvim --headless --clean \ - --cmd "mkspell $out/spell/$name.add.spl $spellfile" -Es -n + spellfilesJoined = pkgs.symlinkJoin { + name = "nvf-spellfiles-joined"; + paths = mapAttrsToList (name: value: pkgs.writeTextDir "spell/${name}.add" (concatLines value)) cfg.extraSpellWords; + postBuild = "echo Spellfiles joined"; + }; + } '' + # Fail on unset variables and non-zero exit codes + # this might be the only way to trace when `nvim --headless` + # fails in batch mode + set -eu + + mkdir -p "$out/spell" + for spellfile in "$spellfilesJoined"/spell/*.add; do + name="$(basename "$spellfile" ".add")" + echo "Compiling spellfile: $spellfile" + nvim --headless --clean \ + --cmd "mkspell $out/spell/$name.add.spl $spellfile" -Es -n done + ''; in mkIf (cfg.extraSpellWords != {}) [ From b836b399411e5df795d30de5527332002f02b4da Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Wed, 30 Oct 2024 15:27:30 +0300 Subject: [PATCH 6/7] neovim/spellcheck: add autogroup to spellcheck fts autocmd; fix vim-dirtytalk --- modules/neovim/init/spellcheck.nix | 14 ++++++------ .../spellcheck/vim-dirtytalk/config.nix | 22 ++++++++++++++----- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/modules/neovim/init/spellcheck.nix b/modules/neovim/init/spellcheck.nix index 16af3ee..ee2b511 100644 --- a/modules/neovim/init/spellcheck.nix +++ b/modules/neovim/init/spellcheck.nix @@ -81,18 +81,16 @@ in { ''; }; - /* - # FIXME: This needs to be revisited. It tries to install - # the spellfile to an user directory, but it cannot do so - # as we sanitize runtime paths. programmingWordlist.enable = mkEnableOption '' vim-dirtytalk, a wordlist for programmers containing common programming terms. - Setting this value as `true` has the same effect - as setting {option}`vim.spellCheck.enable` + ::: {.note} + Enabling this option will unconditionally set + {option}`vim.spellcheck.enable` to true as vim-dirtytalk + depends on spellchecking having been set up. + ::: ''; - */ }; config = mkIf cfg.enable { @@ -136,7 +134,9 @@ in { -- Disable spellchecking for certain filetypes -- as configured by `vim.spellcheck.ignoredFiletypes` + vim.api.nvim_create_augroup("nvf_autocmds", {clear = false}) vim.api.nvim_create_autocmd({ "FileType" }, { + group = "nvf_autocmds", pattern = ${listToLuaTable cfg.ignoredFiletypes}, callback = function() vim.opt_local.spell = false diff --git a/modules/plugins/spellcheck/vim-dirtytalk/config.nix b/modules/plugins/spellcheck/vim-dirtytalk/config.nix index 08426d1..09e404c 100644 --- a/modules/plugins/spellcheck/vim-dirtytalk/config.nix +++ b/modules/plugins/spellcheck/vim-dirtytalk/config.nix @@ -7,16 +7,26 @@ inherit (lib.nvim.dag) entryAfter; cfg = config.vim.spellcheck; in { - config = mkIf (cfg.enable && cfg.programmingWordlist.enable) { + config = mkIf cfg.programmingWordlist.enable { vim = { startPlugins = ["vim-dirtytalk"]; - # vim-dirtytalk doesn't have any setup - # but we would like to append programming to spelllang - # as soon as possible while the plugin is enabled + spellcheck.enable = true; + + # vim-dirtytalk doesn't have any setup but we would + # like to append programming to spelllangs as soon as + # possible while the plugin is enabled and the state + # directory can be found. pluginRC.vim-dirtytalk = entryAfter ["basic"] '' - -- append programming to spelllang - vim.opt.spelllang:append("programming") + -- If Neovim can find (or access) the state directory + -- then append "programming" wordlist from vim-dirtytalk + -- to spelllang table. If path cannot be found, display + -- an error and avoid appending the programming words + if vim.fn.isdirectory(vim.fn.stdpath('state')) == 1 then + vim.opt.spelllang:append("programming") + else + vim.notify("State path does not exist: " .. state_path, vim.log.levels.ERROR) + end ''; }; }; From 07b4e3f479cf7f25b2e1132353e79e91ed85d572 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Wed, 30 Oct 2024 15:43:17 +0300 Subject: [PATCH 7/7] fix stuff --- modules/neovim/init/spellcheck.nix | 5 +++++ modules/plugins/spellcheck/vim-dirtytalk/config.nix | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/neovim/init/spellcheck.nix b/modules/neovim/init/spellcheck.nix index ee2b511..5d6f5be 100644 --- a/modules/neovim/init/spellcheck.nix +++ b/modules/neovim/init/spellcheck.nix @@ -64,6 +64,11 @@ in { => $out/spell/en-utf-8.add.spl ``` ::: + + Note that while adding a new language, you will still need to add the name of + the language (e.g. "en") to the {option}`vim.spellcheck.languages` list by name + in order to enable spellchecking for the language. By default only `"en"` is in + the list. ''; }; diff --git a/modules/plugins/spellcheck/vim-dirtytalk/config.nix b/modules/plugins/spellcheck/vim-dirtytalk/config.nix index 09e404c..51ccfb8 100644 --- a/modules/plugins/spellcheck/vim-dirtytalk/config.nix +++ b/modules/plugins/spellcheck/vim-dirtytalk/config.nix @@ -17,7 +17,7 @@ in { # like to append programming to spelllangs as soon as # possible while the plugin is enabled and the state # directory can be found. - pluginRC.vim-dirtytalk = entryAfter ["basic"] '' + pluginRC.vim-dirtytalk = entryAfter ["spellcheck"] '' -- If Neovim can find (or access) the state directory -- then append "programming" wordlist from vim-dirtytalk -- to spelllang table. If path cannot be found, display