plugins/treesitter: re-organize treesitter plugins

This commit is contained in:
Gaetan Lepage 2023-04-06 15:10:08 +02:00 committed by Gaétan Lepage
parent f79dccdc2e
commit ff791d20c8
7 changed files with 12 additions and 8 deletions

View file

@ -0,0 +1,60 @@
{
pkgs,
lib,
config,
...
}:
with lib; let
helpers = import ../../helpers.nix {inherit lib;};
in {
options.plugins.treesitter-context = {
enable = mkEnableOption "nvim-treesitter-context";
package = helpers.mkPackageOption "treesitter-context" pkgs.vimPlugins.nvim-treesitter-context;
maxLines = mkOption {
type = types.nullOr types.ints.positive;
default = null;
description = "How many lines the window should span. Null means no limit";
};
trimScope = mkOption {
type = types.enum ["outer" "inner"];
default = "outer";
description = "Which context lines to discard if `max_lines` is exceeded";
};
maxWindowHeight = mkOption {
type = types.nullOr types.ints.positive;
default = null;
description = "Minimum editor window height to enable context";
};
patterns = mkOption {
type = types.attrsOf (types.listOf types.str);
default = {};
description = ''
Patterns to use for context delimitation. The 'default' key matches all filetypes
'';
};
exactPatterns = mkOption {
type = types.attrsOf types.bool;
default = {};
description = "Treat the coresponding entry in patterns as an exact match";
};
};
config = let
cfg = config.plugins.treesitter-context;
in
mkIf cfg.enable {
extraPlugins = [cfg.package];
plugins.treesitter.moduleConfig.context = {
max_lines = cfg.maxLines;
trim_scope = cfg.trimScope;
min_window_height = cfg.maxWindowHeight;
};
};
}

View file

@ -0,0 +1,67 @@
{
pkgs,
lib,
config,
...
}:
with lib; let
helpers = import ../../helpers.nix {inherit lib;};
inherit (helpers) mkPackageOption mkCompositeOption;
inherit (helpers.defaultNullOpts) mkInt mkBool mkStr mkNullable;
in {
options.plugins.treesitter-playground = {
enable = mkEnableOption "nvim-treesitter-playground";
package = mkPackageOption "treesitter-playground" pkgs.vimPlugins.playground;
disabledLanguages = mkNullable (types.listOf types.str) "[]" "A list of languages where this module should be disabled";
updateTime = mkInt 25 "Debounced time for highlighting nodes in the playground from source code";
persistQueries = mkBool false "Whether the query persists across vim sessions";
keybindings = mkCompositeOption "Keybindings inside the Playground" {
toggleQueryEditor = mkStr "o" "Toggle query editor";
toggleHlGroups = mkStr "i" "Toggle highlight groups";
toggleInjectedLanguages = mkStr "t" "Toggle injected languages";
toggleAnonymousNodes = mkStr "a" "Toggle anonymous nodes";
toggleLanguageDisplay = mkStr "I" "Toggle language display";
focusLanguage = mkStr "f" "Focus language";
unfocusLanguage = mkStr "F" "Unfocus language";
update = mkStr "R" "Update";
gotoNode = mkStr "<cr>" "Go to node";
showHelp = mkStr "?" "Show help";
};
};
config = let
cfg = config.plugins.treesitter-playground;
in
mkIf cfg.enable {
warnings = mkIf (!config.plugins.treesitter.enable) [
"Nixvim: treesitter-playground needs treesitter to function as intended"
];
extraPlugins = [cfg.package];
plugins.treesitter.moduleConfig.playground = {
enable = true;
disable = cfg.disabledLanguages;
updatetime = cfg.updateTime;
persist_queries = cfg.persistQueries;
keybindings = helpers.ifNonNull' cfg.keybindings {
toggle_query_editor = cfg.keybindings.toggleQueryEditor;
toggle_hl_groups = cfg.keybindings.toggleHlGroups;
toggle_injected_languages = cfg.keybindings.toggleInjectedLanguages;
toggle_anonymous_nodes = cfg.keybindings.toggleAnonymousNodes;
toggle_language_display = cfg.keybindings.toggleLanguageDisplay;
focus_language = cfg.keybindings.focusLanguage;
unfocus_language = cfg.keybindings.unfocusLanguage;
update = cfg.keybindings.update;
goto_node = cfg.keybindings.gotoNode;
show_help = cfg.keybindings.showHelp;
};
};
};
}

View file

@ -0,0 +1,138 @@
{
pkgs,
config,
lib,
...
}:
with lib; let
helpers = import ../../helpers.nix {inherit lib;};
in {
options.plugins.treesitter-refactor = let
disable = mkOption {
type = types.listOf types.str;
default = [];
description = "List of languages to disable the module on";
};
in {
enable =
mkEnableOption
"treesitter-refactor (requires plugins.treesitter.enable to be true)";
package = helpers.mkPackageOption "treesitter-refactor" pkgs.vimPlugins.nvim-treesitter-refactor;
highlightDefinitions = {
inherit disable;
enable =
mkEnableOption
"Highlights definition and usages of the current symbol under the cursor.";
clearOnCursorMove = mkOption {
type = types.bool;
default = true;
description = ''
Controls if highlights should be cleared when the cursor is moved. If your 'updatetime'
is around `100` you can set this to false to have a less laggy experience.
'';
};
};
highlightCurrentScope = {
inherit disable;
enable = mkEnableOption "highlights the block from the current scope where the cursor is.";
};
smartRename = {
inherit disable;
enable =
mkEnableOption
"Renames the symbol under the cursor within the current scope (and current file).";
keymaps = {
smartRename = mkOption {
type = types.nullOr types.str;
default = "grr";
description = "rename symbol under the cursor";
};
};
};
navigation = {
inherit disable;
enable = mkEnableOption ''
Provides "go to definition" for the symbol under the cursor,
and lists the definitions from the current file.
'';
keymaps = {
gotoDefinition = mkOption {
type = types.nullOr types.str;
default = "gnd";
description = "go to the definition of the symbol under the cursor";
};
gotoDefinitionLspFallback = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
go to the definition of the symbol under the cursor or use vim.lsp.buf.definition if
the symbol can not be resolved. You can use your own fallback function if create a
mapping fo `lua require'nvim-treesitter.refactor.navigation(nil, fallback_function)<cr>`.
'';
};
listDefinitons = mkOption {
type = types.nullOr types.str;
default = "gnD";
description = "list all definitions from the current file";
};
listDefinitonsToc = mkOption {
type = types.nullOr types.str;
default = "gO";
description = ''
list all definitions from the current file like a table of contents (similar to the one
you see when pressing |gO| in help files).
'';
};
gotoNextUsage = mkOption {
type = types.nullOr types.str;
default = "<a-*>";
description = "go to next usage of identifier under the cursor";
};
gotoPreviousUsage = mkOption {
type = types.nullOr types.str;
default = "<a-#>";
description = "go to previous usage of identifier";
};
};
};
};
config = let
cfg = config.plugins.treesitter-refactor;
in
mkIf cfg.enable {
warnings = mkIf (!config.plugins.treesitter.enable) [
"Nixvim: treesitter-refactor needs treesitter to function as intended"
];
extraPlugins = [cfg.package];
plugins.treesitter.moduleConfig.refactor = {
highlight_definitions = {
inherit (cfg.highlightDefinitions) enable disable;
clear_on_cursor_move = cfg.highlightDefinitions.clearOnCursorMove;
};
highlight_current_scope = cfg.highlightCurrentScope;
smart_rename = {
inherit (cfg.smartRename) enable disable;
keymaps = {smart_rename = cfg.smartRename.keymaps.smartRename;};
};
navigation = {
inherit (cfg.navigation) enable disable;
keymaps = let
cfgK = cfg.navigation.keymaps;
in {
goto_definition = cfgK.gotoDefinition;
goto_definition_lsp_fallback = cfgK.gotoDefinitionLspFallback;
list_definitions = cfgK.listDefinitons;
list_definitions_toc = cfgK.listDefinitonsToc;
goto_next_usage = cfgK.gotoNextUsage;
goto_previous_usage = cfgK.gotoPreviousUsage;
};
};
};
};
}

View file

@ -0,0 +1,162 @@
{
pkgs,
lib,
config,
...
}:
with lib; let
cfg = config.plugins.treesitter;
helpers = import ../../helpers.nix {inherit lib;};
in {
options = {
plugins.treesitter = {
enable = mkEnableOption "tree-sitter syntax highlighting";
package = mkOption {
type = types.package;
default = pkgs.vimPlugins.nvim-treesitter;
description = "Plugin to use for nvim-treesitter. If using nixGrammars, it should include a `withPlugins` function";
};
nixGrammars = mkOption {
type = types.bool;
default = true;
description = "Install grammars with Nix";
};
ensureInstalled = mkOption {
type = with types; oneOf [(enum ["all"]) (listOf str)];
default = "all";
description = "Either \"all\" or a list of languages";
};
parserInstallDir = mkOption {
type = types.nullOr types.str;
default =
if cfg.nixGrammars
then null
else "$XDG_DATA_HOME/nvim/treesitter";
description = ''
Location of the parsers to be installed by the plugin (only needed when nixGrammars is disabled).
This default might not work on your own install, please make sure that $XDG_DATA_HOME is set if you want to use the default. Otherwise, change it to something that will work for you!
'';
};
ignoreInstall = mkOption {
type = types.listOf types.str;
default = [];
description = "List of parsers to ignore installing (for \"all\")";
};
disabledLanguages = mkOption {
type = types.listOf types.str;
default = [];
description = "A list of languages to disable";
};
customCaptures = mkOption {
type = types.attrsOf types.str;
default = {};
description = "Custom capture group highlighting";
};
incrementalSelection = let
keymap = default:
mkOption {
type = types.str;
inherit default;
};
in {
enable = mkEnableOption "incremental selection based on the named nodes from the grammar";
keymaps = {
initSelection = keymap "gnn";
nodeIncremental = keymap "grn";
scopeIncremental = keymap "grc";
nodeDecremental = keymap "grm";
};
};
indent = mkEnableOption "tree-sitter based indentation";
folding = mkEnableOption "tree-sitter based folding";
grammarPackages = mkOption {
type = with types; listOf package;
default = cfg.package.passthru.allGrammars;
description = "Grammar packages to install";
};
moduleConfig = mkOption {
type = types.attrsOf types.anything;
default = {};
description = "This is the configuration for extra modules. It should not be used directly";
};
};
};
config = let
tsOptions =
{
highlight = {
enable = cfg.enable;
disable =
if (cfg.disabledLanguages != [])
then cfg.disabledLanguages
else null;
custom_captures =
if (cfg.customCaptures != {})
then cfg.customCaptures
else null;
};
incremental_selection =
if cfg.incrementalSelection.enable
then {
enable = true;
keymaps = {
init_selection = cfg.incrementalSelection.keymaps.initSelection;
node_incremental = cfg.incrementalSelection.keymaps.nodeIncremental;
scope_incremental = cfg.incrementalSelection.keymaps.scopeIncremental;
node_decremental = cfg.incrementalSelection.keymaps.nodeDecremental;
};
}
else null;
indent =
if cfg.indent
then {
enable = true;
}
else null;
ensure_installed =
if cfg.nixGrammars
then []
else cfg.ensureInstalled;
ignore_install = cfg.ignoreInstall;
parser_install_dir = cfg.parserInstallDir;
}
// cfg.moduleConfig;
in
mkIf cfg.enable {
extraConfigLua =
(optionalString (cfg.parserInstallDir != null) ''
vim.opt.runtimepath:append("${cfg.parserInstallDir}")
'')
+ ''
require('nvim-treesitter.configs').setup(${helpers.toLuaObject tsOptions})
'';
extraPlugins = with pkgs;
if cfg.nixGrammars
then [(cfg.package.withPlugins (_: cfg.grammarPackages))]
else [cfg.package];
extraPackages = [pkgs.tree-sitter pkgs.nodejs pkgs.gcc];
options = mkIf cfg.folding {
foldmethod = "expr";
foldexpr = "nvim_treesitter#foldexpr()";
};
};
}