2024-01-26 13:18:16 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
nixvimCfg = config.nixvimConfigs.fiesta;
|
|
|
|
cfg = nixvimCfg.setups.treesitter;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.nixvimConfigs.fiesta.setups.treesitter.enable =
|
|
|
|
lib.mkEnableOption "tree-sitter setup for Fiesta NixVim";
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
# The main star of the show.
|
|
|
|
plugins.treesitter = {
|
|
|
|
enable = true;
|
|
|
|
|
|
|
|
# Install all of the grammars with Nix. We can easily replace it if we
|
|
|
|
# want to.
|
|
|
|
nixGrammars = true;
|
2024-01-30 10:05:54 +00:00
|
|
|
nixvimInjections = true;
|
2024-01-26 13:18:16 +00:00
|
|
|
|
2024-02-12 09:41:26 +00:00
|
|
|
# We'll just use the common languages out of the bat.
|
2024-02-14 10:58:21 +00:00
|
|
|
grammarPackages = with config.plugins.treesitter.package.builtGrammars; [
|
|
|
|
bash
|
|
|
|
c
|
|
|
|
cpp
|
|
|
|
css
|
|
|
|
dockerfile
|
|
|
|
go
|
|
|
|
graphql
|
|
|
|
html
|
|
|
|
http
|
|
|
|
javascript
|
|
|
|
json
|
|
|
|
json5
|
|
|
|
lua
|
|
|
|
nix
|
|
|
|
rust
|
|
|
|
toml
|
|
|
|
tsx
|
|
|
|
typescript
|
|
|
|
yaml
|
2024-02-12 09:41:26 +00:00
|
|
|
];
|
|
|
|
|
2024-01-26 13:18:16 +00:00
|
|
|
# Enable all of its useful features.
|
|
|
|
folding = true;
|
|
|
|
indent = true;
|
2024-01-30 10:05:54 +00:00
|
|
|
incrementalSelection.enable = true;
|
2024-01-26 13:18:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# Enable some more context for me.
|
|
|
|
plugins.treesitter-context = {
|
|
|
|
enable = true;
|
|
|
|
lineNumbers = true;
|
2024-02-04 11:48:21 +00:00
|
|
|
maxLines = 7;
|
2024-01-30 10:05:54 +00:00
|
|
|
mode = "cursor";
|
2024-01-29 12:35:10 +00:00
|
|
|
separator = "*";
|
2024-01-26 13:18:16 +00:00
|
|
|
};
|
|
|
|
|
2024-01-30 10:05:54 +00:00
|
|
|
# Some niceties for refactoring.
|
|
|
|
plugins.treesitter-refactor = {
|
|
|
|
enable = true;
|
|
|
|
highlightCurrentScope.enable = true;
|
|
|
|
highlightDefinitions.enable = true;
|
|
|
|
navigation.enable = true;
|
|
|
|
smartRename.enable = true;
|
|
|
|
};
|
|
|
|
|
2024-01-26 13:18:16 +00:00
|
|
|
# Show me your moves.
|
|
|
|
plugins.treesitter-textobjects = {
|
|
|
|
enable = true;
|
2024-01-29 12:35:10 +00:00
|
|
|
lspInterop = {
|
|
|
|
enable = true;
|
|
|
|
border = "none";
|
|
|
|
peekDefinitionCode =
|
|
|
|
let
|
|
|
|
bindingPrefix = "<leader>d";
|
|
|
|
|
|
|
|
mkQueryMappings = query: binding:
|
|
|
|
lib.nameValuePair "${bindingPrefix}${binding}" {
|
|
|
|
desc = "Peek definition of ${query}";
|
|
|
|
query = "@${query}.outer";
|
|
|
|
};
|
|
|
|
in
|
2024-02-02 04:40:16 +00:00
|
|
|
lib.mapAttrs' mkQueryMappings {
|
|
|
|
"function" = "f";
|
|
|
|
"class" = "F";
|
|
|
|
};
|
2024-01-29 12:35:10 +00:00
|
|
|
};
|
|
|
|
move = lib.mkMerge ([{
|
|
|
|
enable = true;
|
|
|
|
setJumps = true;
|
|
|
|
}]
|
|
|
|
++ (
|
|
|
|
let
|
|
|
|
motions = lib.cartesianProductOfSets {
|
|
|
|
region = [ "Start" "End" ];
|
|
|
|
jumpDirection = [ "Previous" "Next" ];
|
|
|
|
variant = [ "outer" "inner" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
motionMap = {
|
|
|
|
outerPrevious = "[";
|
|
|
|
outerNext = "]";
|
|
|
|
innerPrevious = "[[";
|
|
|
|
innerNext = "]]";
|
|
|
|
};
|
|
|
|
|
|
|
|
actionDesc = variant: jumpDirection: query:
|
|
|
|
if variant == "inner"
|
|
|
|
then "Jump to inner part of the ${jumpDirection} ${query}"
|
|
|
|
else "Jump to ${jumpDirection} ${query}";
|
|
|
|
|
|
|
|
mkQueryMappings =
|
|
|
|
# The accumulator. Should be a list where it contains all of the
|
|
|
|
# modules to be merged.
|
|
|
|
acc:
|
|
|
|
|
|
|
|
# The query object of the treesitter node. All queries are
|
|
|
|
# assumed to be "@$QUERY.outer".
|
|
|
|
query:
|
|
|
|
|
|
|
|
# A set of bindings to be used for each jump direction.
|
|
|
|
bindings:
|
2024-02-02 04:40:16 +00:00
|
|
|
let
|
|
|
|
mappings = builtins.map
|
|
|
|
(motion:
|
2024-01-29 12:35:10 +00:00
|
|
|
let
|
|
|
|
inherit (motion) region jumpDirection variant;
|
|
|
|
jumpDirection' = lib.strings.toLower jumpDirection;
|
|
|
|
binding' = bindings.${jumpDirection'};
|
|
|
|
bindingPrefix = motionMap."${variant}${jumpDirection}";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
"goto${jumpDirection}${region}" = {
|
|
|
|
"${bindingPrefix}${binding'}" = {
|
|
|
|
desc = actionDesc variant jumpDirection' query;
|
|
|
|
query = "@${query}.${variant}";
|
|
|
|
};
|
|
|
|
};
|
2024-02-02 04:40:16 +00:00
|
|
|
})
|
|
|
|
motions;
|
|
|
|
in
|
|
|
|
acc ++ mappings;
|
2024-01-29 12:35:10 +00:00
|
|
|
in
|
|
|
|
lib.foldlAttrs mkQueryMappings [ ] {
|
2024-02-02 04:40:16 +00:00
|
|
|
"function" = { previous = "M"; next = "m"; };
|
2024-01-29 12:35:10 +00:00
|
|
|
"block" = { previous = "B"; next = "b"; };
|
|
|
|
"call" = { previous = "F"; next = "f"; };
|
|
|
|
"class" = { previous = "C"; next = "c"; };
|
|
|
|
"conditional" = { previous = "D"; next = "d"; };
|
|
|
|
"statement" = { previous = "S"; next = "s"; };
|
|
|
|
"loop" = { previous = "L"; next = "l"; };
|
2024-02-02 04:40:16 +00:00
|
|
|
}
|
|
|
|
));
|
2024-01-29 12:35:10 +00:00
|
|
|
select = {
|
|
|
|
enable = true;
|
|
|
|
lookahead = true;
|
|
|
|
selectionModes = {
|
|
|
|
"@function.outer" = "V";
|
|
|
|
"@class.outer" = "<c-v>";
|
|
|
|
"@block.outer" = "<c-v>";
|
|
|
|
};
|
|
|
|
keymaps =
|
|
|
|
let
|
|
|
|
prefixMap = {
|
|
|
|
"outer" = {
|
|
|
|
key = "a";
|
|
|
|
desc = query: "Select around the ${query} region";
|
|
|
|
};
|
|
|
|
"inner" = {
|
|
|
|
key = "i";
|
|
|
|
desc = query: "Select inner part of the ${query} region";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# A function that creates a pair of keymaps: one for the outer and
|
|
|
|
# inner part of the query. As such, it assumes the query has an
|
|
|
|
# outer and inner variant.
|
|
|
|
mkQueryMappings =
|
|
|
|
# The textobject query, assumed as "@$QUERY.$VARIANT".
|
|
|
|
query:
|
|
|
|
|
|
|
|
# The keymap sequence to affix for the mapping pair.
|
|
|
|
binding:
|
|
|
|
|
|
|
|
let
|
|
|
|
mappingsList =
|
2024-02-02 04:40:16 +00:00
|
|
|
builtins.map
|
|
|
|
(variant:
|
|
|
|
let
|
|
|
|
prefixMap' = prefixMap.${variant};
|
|
|
|
in
|
|
|
|
lib.nameValuePair "${prefixMap'.key}${binding}" {
|
|
|
|
query = "@${query}.${variant}";
|
|
|
|
desc = prefixMap'.desc query;
|
|
|
|
}) [ "outer" "inner" ];
|
2024-01-29 12:35:10 +00:00
|
|
|
in
|
2024-02-02 04:40:16 +00:00
|
|
|
lib.listToAttrs mappingsList;
|
2024-01-29 12:35:10 +00:00
|
|
|
in
|
|
|
|
lib.concatMapAttrs mkQueryMappings {
|
|
|
|
"function" = "m";
|
|
|
|
"call" = "f";
|
|
|
|
"class" = "c";
|
|
|
|
"block" = "b";
|
|
|
|
"loop" = "l";
|
|
|
|
"statement" = "s";
|
|
|
|
"attribute" = "a";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
swap = lib.mkMerge (
|
|
|
|
[{ enable = true; }]
|
|
|
|
++ (
|
|
|
|
let
|
|
|
|
motions = lib.cartesianProductOfSets {
|
|
|
|
jumpDirection = [ "Previous" "Next" ];
|
|
|
|
variant = [ "outer" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
motionMap = {
|
|
|
|
"outerPrevious" = "<leader>S";
|
|
|
|
"outerNext" = "<leader>s";
|
|
|
|
};
|
|
|
|
|
|
|
|
actionDesc = variant: jumpDirection: query:
|
|
|
|
if variant == "inner"
|
|
|
|
then "Jump to inner part of the ${jumpDirection} ${query}"
|
|
|
|
else "Jump to ${jumpDirection} ${query}";
|
|
|
|
|
|
|
|
mkQueryMappings = acc: query: bindings:
|
|
|
|
let
|
2024-02-02 04:40:16 +00:00
|
|
|
mappings = builtins.map
|
|
|
|
(motion:
|
|
|
|
let
|
|
|
|
inherit (motion) jumpDirection variant;
|
|
|
|
jumpDirection' = lib.strings.toLower jumpDirection;
|
|
|
|
binding' = bindings.${jumpDirection'};
|
|
|
|
bindingPrefix = motionMap."${variant}${jumpDirection}";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
"swap${jumpDirection}" = {
|
|
|
|
"${bindingPrefix}${binding'}" = {
|
|
|
|
desc = actionDesc variant jumpDirection' query;
|
|
|
|
query = "@${query}.${variant}";
|
|
|
|
};
|
2024-01-29 12:35:10 +00:00
|
|
|
};
|
2024-02-02 04:40:16 +00:00
|
|
|
})
|
|
|
|
motions;
|
2024-01-29 12:35:10 +00:00
|
|
|
in
|
2024-02-02 04:40:16 +00:00
|
|
|
acc ++ mappings;
|
2024-01-29 12:35:10 +00:00
|
|
|
in
|
2024-02-02 04:40:16 +00:00
|
|
|
lib.foldlAttrs mkQueryMappings [ ] {
|
|
|
|
"function" = { next = "f"; previous = "F"; };
|
|
|
|
"parameter" = { next = "a"; previous = "A"; };
|
|
|
|
"conditional" = { next = "d"; previous = "D"; };
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
2024-01-26 13:18:16 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|