nix-community.nixvim/plugins/utils/flash.nix

595 lines
19 KiB
Nix
Raw Normal View History

2023-10-15 17:31:03 +02:00
{
lib,
helpers,
config,
pkgs,
2023-10-15 17:31:03 +02:00
...
}:
2024-05-05 19:39:35 +02:00
with lib;
let
2023-10-15 17:31:03 +02:00
cfg = config.plugins.flash;
2024-05-05 19:39:35 +02:00
in
{
options.plugins.flash =
let
configOpts = {
labels = helpers.defaultNullOpts.mkStr "asdfghjklqwertyuiopzxcvbnm" ''
Labels appear next to the matches, allowing you to quickly jump to any location. Labels are
guaranteed not to exist as a continuation of the search pattern.
'';
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
search = {
automatic = mkOption {
type = types.bool;
default = false;
description = ''
Automatically set the values according to context. Same as passing `search = {}` in lua
'';
};
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
multiWindow = helpers.defaultNullOpts.mkBool true "search/jump in all windows";
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
forward = helpers.defaultNullOpts.mkBool true "search direction";
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
wrap = helpers.defaultNullOpts.mkBool true ''
when `false`, find only matches in the given direction
2023-10-15 17:31:03 +02:00
'';
2024-05-05 19:39:35 +02:00
mode =
helpers.defaultNullOpts.mkNullable
2023-10-15 17:31:03 +02:00
(
2024-05-05 19:39:35 +02:00
with types;
either (enum [
"exact"
"search"
"fuzzy"
]) helpers.nixvimTypes.rawLua
2023-10-15 17:31:03 +02:00
)
2024-05-05 19:39:35 +02:00
''"exact"''
''
- exact: exact match
- search: regular search
- fuzzy: fuzzy search
- fun(str): custom search function that returns a pattern
For example, to only match at the beginning of a word:
function(str)
return "\\<" .. str
end
'';
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
incremental = helpers.defaultNullOpts.mkBool false "behave like `incsearch`";
exclude =
helpers.defaultNullOpts.mkListOf types.str
[
"notify"
"cmp_menu"
"noice"
"flash_prompt"
{
__raw = ''
function(win)
return not vim.api.nvim_win_get_config(win).focusable
end
'';
}
]
2024-05-05 19:39:35 +02:00
''
Excluded filetypes and custom window filters
'';
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
trigger = helpers.defaultNullOpts.mkStr "" ''
Optional trigger character that needs to be typed before a jump label can be used.
It's NOT recommended to set this, unless you know what you're doing
2023-10-15 17:31:03 +02:00
'';
2024-05-05 19:39:35 +02:00
maxLength =
helpers.defaultNullOpts.mkNullable (with types; either (enum [ false ]) types.int) false
2024-05-05 19:39:35 +02:00
''
max pattern length. If the pattern length is equal to this labels will no longer be
skipped. When it exceeds this length it will either end in a jump or terminate the search
'';
};
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
jump = {
jumplist = helpers.defaultNullOpts.mkBool true "save location in the jumplist";
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
pos = helpers.defaultNullOpts.mkEnumFirstDefault [
"start"
"end"
"range"
] "jump position";
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
history = helpers.defaultNullOpts.mkBool false "add pattern to search history";
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
register = helpers.defaultNullOpts.mkBool false "add pattern to search register";
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
nohlsearch = helpers.defaultNullOpts.mkBool false "clear highlight after jump";
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
autojump = helpers.defaultNullOpts.mkBool false ''
automatically jump when there is only one match
'';
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
inclusive = helpers.mkNullOrOption types.bool ''
You can force inclusive/exclusive jumps by setting the `inclusive` option. By default it
will be automatically set based on the mode.
'';
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
offset = helpers.mkNullOrOption types.int ''
jump position offset. Not used for range jumps.
0: default
1: when pos == "end" and pos < current position
'';
};
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
label = {
uppercase = helpers.defaultNullOpts.mkBool true "allow uppercase labels";
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
exclude = helpers.defaultNullOpts.mkStr "" ''
add any labels with the correct case here, that you want to exclude
'';
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
current = helpers.defaultNullOpts.mkBool true ''
add a label for the first match in the current window.
you can always jump to the first match with `<CR>`
'';
2023-10-15 17:31:03 +02:00
after = helpers.defaultNullOpts.mkNullable (with types; either bool (listOf int)) true ''
2024-05-05 19:39:35 +02:00
show the label after the match
'';
2023-10-15 17:31:03 +02:00
before = helpers.defaultNullOpts.mkNullable (with types; either bool (listOf int)) false ''
2024-05-05 19:39:35 +02:00
show the label before the match
'';
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
style =
helpers.defaultNullOpts.mkEnum
[
"eol"
"overlay"
"right_align"
"inline"
]
"overlay"
''
position of the label extmark
'';
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
reuse =
helpers.defaultNullOpts.mkEnumFirstDefault
[
"lowercase"
"all"
"none"
]
''
flash tries to re-use labels that were already assigned to a position,
when typing more characters. By default only lower-case labels are re-used.
'';
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
distance = helpers.defaultNullOpts.mkBool true ''
for the current window, label targets closer to the cursor first
2023-10-15 17:31:03 +02:00
'';
2024-05-05 19:39:35 +02:00
minPatternLength = helpers.defaultNullOpts.mkInt 0 ''
minimum pattern length to show labels
Ignored for custom labelers.
2023-10-15 17:31:03 +02:00
'';
2024-05-05 19:39:35 +02:00
rainbow = {
enabled = helpers.defaultNullOpts.mkBool false ''
Enable this to use rainbow colors to highlight labels
Can be useful for visualizing Treesitter ranges.
'';
2023-10-15 17:31:03 +02:00
shade = helpers.defaultNullOpts.mkNullable (types.ints.between 1 9) 5 "";
2024-05-05 19:39:35 +02:00
};
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
format =
helpers.defaultNullOpts.mkLuaFn
''
format = function(opts)
return { { opts.match.label, opts.hl_group } }
end
''
''
With `format`, you can change how the label is rendered.
Should return a list of `[text, highlight]` tuples.
@class Flash.Format
@field state Flash.State
@field match Flash.Match
@field hl_group string
@field after boolean
@type fun(opts:Flash.Format): string[][]
'';
2023-10-15 17:31:03 +02:00
};
2024-05-05 19:39:35 +02:00
highlight = {
backdrop = helpers.defaultNullOpts.mkBool true "show a backdrop with hl FlashBackdrop";
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
matches = helpers.defaultNullOpts.mkBool true "Highlight the search matches";
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
priority = helpers.defaultNullOpts.mkPositiveInt 5000 "extmark priority";
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
groups = builtins.mapAttrs (_: default: helpers.defaultNullOpts.mkStr default "") {
match = "FlashMatch";
current = "FlashCurrent";
backdrop = "FlashBackdrop";
label = "FlashLabel";
};
};
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
action = helpers.defaultNullOpts.mkLuaFn "nil" ''
action to perform when picking a label.
defaults to the jumping logic depending on the mode.
@type fun(match:Flash.Match, state:Flash.State)
2023-10-15 17:31:03 +02:00
'';
2024-05-05 19:39:35 +02:00
pattern = helpers.defaultNullOpts.mkStr "" "initial pattern to use when opening flash";
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
continue = helpers.defaultNullOpts.mkBool false ''
When `true`, flash will try to continue the last search
2023-10-15 17:31:03 +02:00
'';
2024-05-05 19:39:35 +02:00
config = helpers.defaultNullOpts.mkLuaFn "nil" ''
Set config to a function to dynamically change the config
@type fun(opts:Flash.Config)
2023-10-15 17:31:03 +02:00
'';
2024-05-05 19:39:35 +02:00
prompt = {
enabled = helpers.defaultNullOpts.mkBool true ''
options for the floating window that shows the prompt, for regular jumps
'';
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
# Not sure what is the type...
prefix = helpers.defaultNullOpts.mkListOf types.anything [
[
""
"FlashPromptIcon"
]
] "";
winConfig = helpers.defaultNullOpts.mkAttrsOf types.anything {
relative = "editor";
width = 1;
height = 1;
row = -1;
col = 0;
zindex = 1000;
} "See nvim_open_win for more details";
2024-05-05 19:39:35 +02:00
};
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
remoteOp = {
restore = helpers.defaultNullOpts.mkBool false ''
restore window views and cursor position after doing a remote operation
'';
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
motion = helpers.defaultNullOpts.mkBool false ''
For `jump.pos = "range"`, this setting is ignored.
- `true`: always enter a new motion when doing a remote operation
- `false`: use the window's cursor position and jump target
- `nil`: act as `true` for remote windows, `false` for the current window
'';
};
};
in
helpers.neovim-plugin.extraOptionsOptions
// {
enable = mkEnableOption "flash.nvim";
2023-10-15 17:31:03 +02:00
package = helpers.mkPluginPackageOption "flash.nvim" pkgs.vimPlugins.flash-nvim;
2023-10-15 17:31:03 +02:00
2024-05-05 19:39:35 +02:00
modes =
let
mkModeConfig =
{
extra ? { },
default,
description ? "",
}:
2024-05-05 19:39:35 +02:00
helpers.defaultNullOpts.mkNullable (types.submodule {
options = configOpts // extra;
}) default description;
2024-05-05 19:39:35 +02:00
in
{
search = mkModeConfig {
description = ''
options used when flash is activated through a regular search with `/` or `?`
'';
extra = {
enabled = helpers.defaultNullOpts.mkBool true ''
when `true`, flash will be activated during regular search by default.
You can always toggle when searching with `require("flash").toggle()`
2023-10-15 17:31:03 +02:00
'';
};
default = {
enabled = true;
highlight = {
backdrop = false;
};
jump = {
history = true;
register = true;
nohlsearch = true;
};
/*
forward will be automatically set to the search direction
mode is always set to 'search'
incremental is set to 'true' when 'incsearch' is enabled
*/
search.automatic = true;
};
};
char = mkModeConfig {
description = "options used when flash is activated through a regular search with `/` or `?`";
extra = {
enabled = helpers.defaultNullOpts.mkBool true "";
autohide = helpers.defaultNullOpts.mkBool false ''
hide after jump when not using jump labels
'';
jumpLabels = helpers.defaultNullOpts.mkBool false "show jump labels";
multiLine = helpers.defaultNullOpts.mkBool true ''
set to `false` to use the current line only
'';
keys =
helpers.defaultNullOpts.mkAttrsOf types.str
# FIXME can't show helper func in docs
(helpers.listToUnkeyedAttrs [
"f"
"F"
"t"
"T"
";"
","
])
''
by default all keymaps are enabled, but you can disable some of them,
by removing them from the list.
If you rather use another key, you can map them
to something else, e.g., `{ ";" = "L"; "," = "H"; }`
'';
charActions =
helpers.defaultNullOpts.mkLuaFn
''
2024-05-05 19:39:35 +02:00
function(motion)
return {
[";"] = "next", -- set to right to always go right
[","] = "prev", -- set to left to always go left
-- clever-f style
[motion:lower()] = "next",
[motion:upper()] = "prev",
-- jump2d style: same case goes next, opposite case goes prev
-- [motion] = "next",
-- [motion:match("%l") and motion:upper() or motion:lower()] = "prev",
}
end
''
''
The direction for `prev` and `next` is determined by the motion.
`left` and `right` are always left and right.
'';
};
default = {
enabled = true;
# dynamic configuration for ftFT motions
config = ''
function(opts)
-- autohide flash when in operator-pending mode
opts.autohide = vim.fn.mode(true):find("no") and vim.v.operator == "y"
-- disable jump labels when not enabled, when using a count,
-- or when recording/executing registers
opts.jump_labels = opts.jump_labels
and vim.v.count == 0
and vim.fn.reg_executing() == ""
and vim.fn.reg_recording() == ""
-- Show jump labels only in operator-pending mode
-- opts.jump_labels = vim.v.count == 0 and vim.fn.mode(true):find("o")
end
2023-10-15 17:31:03 +02:00
'';
autohide = false;
jumpLabels = false;
multiLine = false;
label = {
exclude = "hjkliardc";
};
# FIXME can't show the function call in the docs...
keys = helpers.listToUnkeyedAttrs [
"f"
"F"
"t"
"T"
";"
","
];
charActions = ''
function(motion)
return {
[";"] = "next", -- set to right to always go right
[","] = "prev", -- set to left to always go left
-- clever-f style
[motion:lower()] = "next",
[motion:upper()] = "prev",
-- jump2d style: same case goes next, opposite case goes prev
-- [motion] = "next",
-- [motion:match("%l") and motion:upper() or motion:lower()] = "prev",
}
end
'';
search = {
wrap = false;
};
highlight = {
backdrop = true;
};
jump = {
register = false;
};
};
};
treesitter = mkModeConfig {
description = ''
options used for treesitter selections `require("flash").treesitter()`
'';
default = {
labels = "abcdefghijklmnopqrstuvwxyz";
jump = {
pos = "range";
};
search = {
incremental = false;
};
label = {
before = true;
after = true;
style = "inline";
};
highlight = {
backdrop = false;
matches = false;
};
};
};
treesitterSearch = mkModeConfig {
default = {
jump = {
pos = "range";
};
search = {
multiWindow = true;
wrap = true;
incremental = false;
};
remoteOp = {
restore = true;
};
label = {
before = true;
after = true;
style = "inline";
};
};
};
remote = mkModeConfig {
default = {
remoteOp = {
restore = true;
motion = true;
};
};
description = "options used for remote flash";
};
2024-05-05 19:39:35 +02:00
};
2023-10-15 17:31:03 +02:00
}
// configOpts;
2024-05-05 19:39:35 +02:00
config =
let
mkGlobalConfig = c: {
inherit (c) labels;
search =
if c.search.automatic then
helpers.emptyTable
else
{
multi_window = c.search.multiWindow;
inherit (c.search)
forward
wrap
mode
incremental
exclude
trigger
;
max_length = c.search.maxLength;
};
jump = {
inherit (c.jump)
jumplist
pos
history
register
nohlsearch
autojump
inclusive
offset
;
2023-10-15 17:31:03 +02:00
};
2024-05-05 19:39:35 +02:00
label = {
inherit (c.label)
uppercase
exclude
current
after
before
style
reuse
distance
;
min_pattern_length = c.label.minPatternLength;
rainbow = {
inherit (c.label.rainbow) enabled shade;
};
inherit (c.label) format;
};
highlight = {
inherit (c.highlight)
backdrop
matches
priority
groups
;
};
inherit (c)
action
pattern
continue
config
;
prompt = {
inherit (c.prompt) enabled prefix;
win_config = c.prompt.winConfig;
};
remote_op = {
inherit (c.remoteOp) restore motion;
2023-10-15 17:31:03 +02:00
};
};
2024-05-05 19:39:35 +02:00
options =
(mkGlobalConfig cfg)
// {
modes =
let
mkModeConfig = c: extra: helpers.ifNonNull' c ((mkGlobalConfig c) // (extra c));
in
{
search = mkModeConfig cfg.modes.search (c: {
inherit (c) enabled;
});
char = mkModeConfig cfg.modes.char (c: {
inherit (c) enabled autohide;
jump_labels = c.jumpLabels;
multi_line = c.multiLine;
inherit (c) keys charActions;
});
treesitter = mkModeConfig cfg.modes.treesitter (c: { });
treesitter_search = mkModeConfig cfg.modes.treesitterSearch (c: { });
remote = mkModeConfig cfg.modes.remote (c: { });
};
}
// cfg.extraOptions;
in
2023-10-15 17:31:03 +02:00
mkIf cfg.enable {
2024-05-05 19:39:35 +02:00
extraPlugins = [ cfg.package ];
2023-10-15 17:31:03 +02:00
extraConfigLua = ''
require('flash').setup(${helpers.toLuaObject options})
'';
};
}