This commit is contained in:
NotAShelf 2024-04-23 17:43:49 +00:00 committed by GitHub
commit aa6727ce71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 273 additions and 116 deletions

View file

@ -9,6 +9,7 @@
binds = import ./binds.nix {inherit lib;}; binds = import ./binds.nix {inherit lib;};
dag = import ./dag.nix {inherit lib;}; dag = import ./dag.nix {inherit lib;};
languages = import ./languages.nix {inherit lib;}; languages = import ./languages.nix {inherit lib;};
lists = import ./lists.nix {inherit lib;};
lua = import ./lua.nix {inherit lib;}; lua = import ./lua.nix {inherit lib;};
vim = import ./vim.nix; vim = import ./vim.nix;
} }

34
lib/lists.nix Normal file
View file

@ -0,0 +1,34 @@
{lib}: let
inherit (lib.lists) elem all;
in {
/*
Checks if all values are present in the list.
Type:
listContainsValues :: { list :: [a], values :: [a] } -> Bool
Arguments:
list - A list of elements.
values - A list of values to check for presence in the list.
Returns:
True if all values are present in the list, otherwise False.
Example:
```nix
listContainsValues { list = [1 2 3]; values = [2 3]; }
=> True
listContainsValues { list = [1 2 3]; values = [2 4]; }
=> False
```
*/
listContainsValues = {
list,
values,
}: let
# Check if all values are present in the list
containsValue = value: elem value list;
in
all containsValue values;
}

View file

@ -1,17 +1,21 @@
{ {
config, config,
pkgs,
lib, lib,
... ...
}: let }: let
inherit (lib.modules) mkIf mkMerge; inherit (lib.modules) mkIf mkMerge;
inherit (lib.lists) optional; inherit (lib.lists) optional optionals;
inherit (lib.trivial) boolToString;
inherit (lib.nvim.lists) listContainsValues;
inherit (lib.nvim.binds) mkSetBinding addDescriptionsToMappings; inherit (lib.nvim.binds) mkSetBinding addDescriptionsToMappings;
inherit (lib.nvim.lua) toLuaObject;
inherit (lib.nvim.dag) entryBefore entryAnywhere; inherit (lib.nvim.dag) entryBefore entryAnywhere;
cfg = config.vim.treesitter; cfg = config.vim.treesitter;
usingNvimCmp = config.vim.autocomplete.enable && config.vim.autocomplete.type == "nvim-cmp"; usingNvimCmp = config.vim.autocomplete.enable && config.vim.autocomplete.type == "nvim-cmp";
self = import ./treesitter.nix {inherit lib;}; self = import ./treesitter.nix {inherit pkgs lib;};
mappingDefinitions = self.options.vim.treesitter.mappings; mappingDefinitions = self.options.vim.treesitter.mappings;
mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions; mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions;
in { in {
@ -20,6 +24,7 @@ in {
startPlugins = ["nvim-treesitter"] ++ optional usingNvimCmp "cmp-treesitter"; startPlugins = ["nvim-treesitter"] ++ optional usingNvimCmp "cmp-treesitter";
autocomplete.sources = {"treesitter" = "[Treesitter]";}; autocomplete.sources = {"treesitter" = "[Treesitter]";};
treesitter.grammars = optionals cfg.addDefaultGrammars cfg.defaultGrammars;
maps = { maps = {
# HACK: Using mkSetLuaBinding and putting the lua code does not work for some reason: It just selects the whole file. # HACK: Using mkSetLuaBinding and putting the lua code does not work for some reason: It just selects the whole file.
@ -41,17 +46,31 @@ in {
''); '');
luaConfigRC.treesitter = entryAnywhere '' luaConfigRC.treesitter = entryAnywhere ''
require'nvim-treesitter.configs'.setup { require('nvim-treesitter.configs').setup {
highlight = { -- Disable imperative treesitter options that would attempt to fetch
-- grammars into the read-only Nix store. To add additional grammars here
-- you must use the `config.vim.treesitter.grammars` option.
auto_install = false,
sync_install = false,
ensure_installed = {},
-- Indentation module for Treesitter
indent = {
enable = true, enable = true,
disable = {}, disable = {},
}, },
auto_install = false, -- Highlight module for Treesitter
ensure_installed = {}, highlight = {
enable = ${boolToString cfg.highlight.enable},
disable = ${toLuaObject cfg.highlight.disable},
additional_vim_regex_highlighting = false,
},
-- Indentation module for Treesitter
incremental_selection = { incremental_selection = {
enable = true, enable = true,
disable = {},
keymaps = { keymaps = {
init_selection = false, init_selection = false,
node_incremental = false, node_incremental = false,

View file

@ -1,7 +1,9 @@
{ {
imports = [ imports = [
# treesitter extras
./ts-context
./treesitter.nix ./treesitter.nix
./context.nix
./config.nix ./config.nix
]; ];
} }

View file

@ -1,20 +1,15 @@
{lib, ...}: let {
inherit (lib.options) mkOption mkEnableOption; pkgs,
lib,
...
}: let
inherit (lib.options) mkOption mkEnableOption literalMD;
inherit (lib.types) listOf package str either bool;
inherit (lib.nvim.binds) mkMappingOption; inherit (lib.nvim.binds) mkMappingOption;
inherit (lib.types) listOf package; inherit (lib.nvim.types) luaInline mkGrammarOption;
in { in {
options.vim.treesitter = { options.vim.treesitter = {
enable = mkEnableOption "treesitter, also enabled automatically through language options"; enable = mkEnableOption "treesitter, also enabled automatically through language options";
fold = mkEnableOption "fold with treesitter";
autotagHtml = mkEnableOption "autoclose and rename html tag";
grammars = mkOption {
type = listOf package;
default = [];
description = ''
List of treesitter grammars to install. For supported languages
use the `vim.language.<lang>.treesitter` option
'';
};
mappings.incrementalSelection = { mappings.incrementalSelection = {
init = mkMappingOption "Init selection [treesitter]" "gnn"; init = mkMappingOption "Init selection [treesitter]" "gnn";
@ -22,5 +17,85 @@ in {
incrementByScope = mkMappingOption "Increment selection by scope [treesitter]" "grc"; incrementByScope = mkMappingOption "Increment selection by scope [treesitter]" "grc";
decrementByNode = mkMappingOption "Decrement selection by node [treesitter]" "grm"; decrementByNode = mkMappingOption "Decrement selection by node [treesitter]" "grm";
}; };
fold = mkEnableOption "fold with treesitter";
autotagHtml = mkEnableOption "autoclose and rename html tag";
grammars = mkOption {
type = listOf package;
default = [];
description = ''
List of treesitter grammars to install.
For languages already supported by neovim-flake, you may
use the {option}`vim.language.<lang>.treesitter` options, which
will automatically add the required grammars to this.
'';
};
highlight = {
enable = mkEnableOption "highlighting with treesitter";
disable = mkOption {
type = either (listOf str) luaInline;
default = [];
example = literalMD ''
```lua
-- Disable slow treesitter highlight for large files
disable = function(lang, buf)
local max_filesize = 1000 * 1024 -- 1MB
local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(buf))
if ok and stats and stats.size > max_filesize then
return true
end
end
```
'';
description = ''
List of treesitter grammars to disable highlighting for.
This option can be either a list, in which case it will be
converted to a Lua table containing grammars to disable
highlighting for, or a string containing a **lua function**
that will be read as is.
::: {.warning}
A comma will be added at the end of your function, so you
do not need to add it yourself. Doing so will cause in
syntax errors within your Neovim configuration.
:::
'';
};
};
addDefaultGrammars = mkOption {
type = bool;
default = true;
description = ''
Whether to add the default grammars to the list of grammars
to install.
This option is only relevant if treesitter has been enabled.
'';
};
defaultGrammars = mkOption {
internal = true;
readOnly = true;
type = listOf package;
default = with pkgs.vimPlugins.nvim-treesitter.builtGrammars; [c lua vim vimdoc query];
description = ''
A list of treesitter grammars that will be installed by default
if treesitter has been enabled and {option}`vim.treeesitter.addDefaultGrammars`
has been set to true.
::: {.note}
Regardless of which language module options you enable, Neovim
depends on those grammars to be enabled while treesitter is enabled.
This list cannot be modified, but if you would like to bring your own
parsers instead of those provided here, you can set `addDefaultGrammars`
to false
:::
'';
};
}; };
} }

View file

@ -0,0 +1,31 @@
{
config,
lib,
...
}: let
inherit (lib.modules) mkIf;
inherit (lib.trivial) boolToString;
inherit (lib.nvim.lua) nullString;
inherit (lib.nvim.dag) entryAfter;
inherit (config.vim) treesitter;
cfg = treesitter.context;
in {
config = mkIf (treesitter.enable && cfg.enable) {
vim.startPlugins = ["nvim-treesitter-context"];
vim.luaConfigRC.treesitter-context = entryAfter ["treesitter"] ''
require'treesitter-context'.setup {
enable = true,
max_lines = ${toString cfg.maxLines},
min_window_height = ${toString cfg.minWindowHeight},
line_numbers = ${boolToString cfg.lineNumbers},
multiline_threshold = ${toString cfg.multilineThreshold},
trim_scope = '${cfg.trimScope}',
mode = '${cfg.mode}',
separator = ${nullString cfg.separator},
max_lines = ${toString cfg.zindex},
}
'';
};
}

View file

@ -1,89 +1,60 @@
{ {lib, ...}: let
config,
lib,
...
}: let
inherit (lib.options) mkOption mkEnableOption; inherit (lib.options) mkOption mkEnableOption;
inherit (lib.types) int bool str nullOr enum; inherit (lib.types) int bool str nullOr enum;
inherit (lib.modules) mkIf;
inherit (lib.trivial) boolToString;
inherit (lib.nvim.lua) nullString;
inherit (lib.nvim.dag) entryAnywhere;
inherit (config.vim) treesitter;
cfg = treesitter.context;
in { in {
options.vim.treesitter.context = { options.vim.treesitter.context = {
enable = mkEnableOption "context of current buffer contents [nvim-treesitter-context] "; enable = mkEnableOption "context of current buffer contents [nvim-treesitter-context] ";
maxLines = mkOption { maxLines = mkOption {
description = "How many lines the window should span. Values &lt;=0 mean no limit.";
type = int; type = int;
default = 0; default = 0;
description = "How many lines the window should span. Values &lt;=0 mean no limit.";
}; };
minWindowHeight = mkOption { minWindowHeight = mkOption {
description = "Minimum editor window height to enable context. Values &lt;= 0 mean no limit.";
type = int; type = int;
default = 0; default = 0;
description = "Minimum editor window height to enable context. Values &lt;= 0 mean no limit.";
}; };
lineNumbers = mkOption { lineNumbers = mkOption {
description = "";
type = bool; type = bool;
default = true; default = true;
description = "";
}; };
multilineThreshold = mkOption { multilineThreshold = mkOption {
description = "Maximum number of lines to collapse for a single context line.";
type = int; type = int;
default = 20; default = 20;
description = "Maximum number of lines to collapse for a single context line.";
}; };
trimScope = mkOption { trimScope = mkOption {
description = "Which context lines to discard if [](#opt-vim.treesitter.context.maxLines) is exceeded.";
type = enum ["inner" "outer"]; type = enum ["inner" "outer"];
default = "outer"; default = "outer";
description = "Which context lines to discard if [](#opt-vim.treesitter.context.maxLines) is exceeded.";
}; };
mode = mkOption { mode = mkOption {
description = "Line used to calculate context.";
type = enum ["cursor" "topline"]; type = enum ["cursor" "topline"];
default = "cursor"; default = "cursor";
description = "Line used to calculate context.";
}; };
separator = mkOption { separator = mkOption {
type = nullOr str;
default = null;
description = '' description = ''
Separator between context and content. Should be a single character string, like '-'. Separator between context and content. Should be a single character string, like '-'.
When separator is set, the context will only show up when there are at least 2 lines above cursorline. When separator is set, the context will only show up when there are at least 2 lines above cursorline.
''; '';
type = nullOr str;
default = null;
}; };
zindex = mkOption { zindex = mkOption {
description = "The Z-index of the context window.";
type = int; type = int;
default = 20; default = 20;
description = "The Z-index of the context window.";
}; };
}; };
config = mkIf (treesitter.enable && cfg.enable) {
vim.startPlugins = ["nvim-treesitter-context"];
vim.luaConfigRC.treesitter-context = entryAnywhere ''
require'treesitter-context'.setup {
enable = true,
max_lines = ${toString cfg.maxLines},
min_window_height = ${toString cfg.minWindowHeight},
line_numbers = ${boolToString cfg.lineNumbers},
multiline_threshold = ${toString cfg.multilineThreshold},
trim_scope = '${cfg.trimScope}',
mode = '${cfg.mode}',
separator = ${nullString cfg.separator},
max_lines = ${toString cfg.zindex},
}
'';
};
} }

View file

@ -0,0 +1,6 @@
{
imports = [
./context.nix
./config.nix
];
}

View file

@ -1,75 +1,93 @@
{ {
config, config,
pkgs,
lib, lib,
... ...
}: let }: let
inherit (lib.modules) mkIf; inherit (lib.modules) mkIf;
inherit (lib.lists) optionals;
inherit (lib.strings) optionalString;
inherit (lib.trivial) boolToString; inherit (lib.trivial) boolToString;
inherit (lib.nvim.dag) entryAnywhere; inherit (lib.nvim.dag) entryAnywhere;
cfg = config.vim.ui.noice; cfg = config.vim.ui.noice;
tscfg = config.vim.treesitter;
cmptype = config.vim.autocomplete.type;
defaultGrammars = with pkgs.vimPlugins.nvim-treesitter.builtGrammars; [vim regex lua bash markdown];
in { in {
config = mkIf cfg.enable { config = mkIf cfg.enable {
vim.startPlugins = [ vim = {
"noice-nvim" startPlugins = [
"nui-nvim" "noice-nvim"
]; "nui-nvim"
];
vim.luaConfigRC.noice-nvim = entryAnywhere '' treesitter.grammars = optionals tscfg.addDefaultGrammars defaultGrammars;
require("noice").setup({
lsp = {
override = {
["vim.lsp.util.convert_input_to_markdown_lines"] = true,
["vim.lsp.util.stylize_markdown"] = true,
["cmp.entry.get_documentation"] = true,
},
signature = { luaConfigRC.noice-nvim = entryAnywhere ''
enabled = false, -- FIXME: enabling this file throws an error which I couldn't figure out require("noice").setup({
}, lsp = {
}, override = {
["vim.lsp.util.convert_input_to_markdown_lines"] = true,
presets = { ["vim.lsp.util.stylize_markdown"] = true,
bottom_search = true, -- use a classic bottom cmdline for search ${optionalString (cmptype == "nvim-cmp") "[\"cmp.entry.get_documentation\"] = true,"}
command_palette = true, -- position the cmdline and popupmenu together },
long_message_to_split = true, -- long messages will be sent to a split
inc_rename = false, -- enables an input dialog for inc-rename.nvim signature = {
lsp_doc_border = ${boolToString config.vim.ui.borders.enable}, -- add a border to hover docs and signature help enabled = false, -- FIXME: enabling this file throws an error which I couldn't figure out
},
format = {
cmdline = { pattern = "^:", icon = "", lang = "vim" },
search_down = { kind = "search", pattern = "^/", icon = " ", lang = "regex" },
search_up = { kind = "search", pattern = "^%?", icon = " ", lang = "regex" },
filter = { pattern = "^:%s*!", icon = "", lang = "bash" },
lua = { pattern = "^:%s*lua%s+", icon = "", lang = "lua" },
help = { pattern = "^:%s*he?l?p?%s+", icon = "󰋖" },
input = {},
},
messages = {
-- NOTE: If you enable messages, then the cmdline is enabled automatically.
-- This is a current Neovim limitation.
enabled = false, -- enables the Noice messages UI
view = "notify", -- default view for messages
view_error = "notify", -- view for errors
view_warn = "notify", -- view for warnings
view_history = "messages", -- view for :messages
view_search = "virtualtext", -- view for search count messages. Set to `false` to disable
},
-- Hide written messages
routes = {
{
filter = {
event = "msg_show",
kind = "",
find = "written",
}, },
opts = { skip = true },
}, },
},
}) hover = {
''; enabled = true,
silent = false, -- set to true to not show a message if hover is not available
view = nil, -- when nil, use defaults from documentation
opts = {}, -- merged with defaults from documentation
},
presets = {
bottom_search = true, -- use a classic bottom cmdline for search
command_palette = true, -- position the cmdline and popupmenu together
long_message_to_split = true, -- long messages will be sent to a split
inc_rename = false, -- enables an input dialog for inc-rename.nvim
lsp_doc_border = ${boolToString config.vim.ui.borders.enable}, -- add a border to hover docs and signature help
},
format = {
cmdline = { pattern = "^:", icon = "", lang = "vim" },
search_down = { kind = "search", pattern = "^/", icon = " ", lang = "regex" },
search_up = { kind = "search", pattern = "^%?", icon = " ", lang = "regex" },
filter = { pattern = "^:%s*!", icon = "", lang = "bash" },
lua = { pattern = "^:%s*lua%s+", icon = "", lang = "lua" },
help = { pattern = "^:%s*he?l?p?%s+", icon = "󰋖" },
input = {},
},
messages = {
-- NOTE: If you enable messages, then the cmdline is enabled automatically.
-- This is a current Neovim limitation.
enabled = true, -- enables the Noice messages UI
view = "notify", -- default view for messages
view_error = "notify", -- view for errors
view_warn = "notify", -- view for warnings
view_history = "messages", -- view for :messages
view_search = "virtualtext", -- view for search count messages. Set to `false` to disable
},
-- Hide written messages
routes = {
{
filter = {
event = "msg_show",
kind = "",
find = "written",
},
opts = { skip = true },
},
},
})
'';
};
}; };
} }