nix-community.nixvim/plugins/utils/auto-save.nix

143 lines
4 KiB
Nix
Raw Normal View History

2023-06-02 13:13:27 +02:00
{
lib,
helpers,
config,
pkgs,
2023-06-02 13:13:27 +02:00
...
}:
2024-05-05 19:39:35 +02:00
with lib;
let
2023-06-02 13:13:27 +02:00
cfg = config.plugins.auto-save;
2024-05-05 19:39:35 +02:00
in
{
options.plugins.auto-save = helpers.neovim-plugin.extraOptionsOptions // {
enable = mkEnableOption "auto-save";
2023-06-02 13:13:27 +02:00
package = helpers.mkPluginPackageOption "auto-save" pkgs.vimPlugins.auto-save-nvim;
2024-05-05 19:39:35 +02:00
keymaps = {
silent = mkOption {
type = types.bool;
description = "Whether auto-save keymaps should be silent.";
default = false;
2023-06-02 13:13:27 +02:00
};
2024-05-05 19:39:35 +02:00
toggle = helpers.mkNullOrOption types.str "Keymap for running auto-save.";
};
enableAutoSave = helpers.defaultNullOpts.mkBool true ''
Whether to start auto-save when the plugin is loaded.
'';
2023-06-02 13:13:27 +02:00
2024-05-05 19:39:35 +02:00
executionMessage = {
message =
helpers.defaultNullOpts.mkNullable (with types; either str helpers.nixvimTypes.rawLua)
2023-06-02 13:13:27 +02:00
''
{
__raw = \'\'
function()
return ("AutoSave: saved at " .. vim.fn.strftime("%H:%M:%S"))
end
\'\';
}
''
''
The message to print en save.
This can be a lua function that returns a string.
'';
2024-05-05 19:39:35 +02:00
dim = helpers.defaultNullOpts.mkNullable (types.numbers.between 0
1
) "0.18" "Dim the color of `message`.";
2023-06-02 13:13:27 +02:00
2024-05-05 19:39:35 +02:00
cleaningInterval = helpers.defaultNullOpts.mkInt 1250 ''
Time (in milliseconds) to wait before automatically cleaning MsgArea after displaying
`message`.
See `:h MsgArea`.
'';
};
2023-06-02 13:13:27 +02:00
2024-05-05 19:39:35 +02:00
triggerEvents =
helpers.defaultNullOpts.mkNullable (with types; listOf str) ''["InsertLeave" "TextChanged"]''
2023-06-02 13:13:27 +02:00
''
Vim events that trigger auto-save.
See `:h events`.
'';
2024-05-05 19:39:35 +02:00
condition =
helpers.defaultNullOpts.mkLuaFn
2023-06-02 13:13:27 +02:00
''
function(buf)
local fn = vim.fn
local utils = require("auto-save.utils.data")
if
fn.getbufvar(buf, "&modifiable") == 1 and utils.not_in(fn.getbufvar(buf, "&filetype"), {}) then
return true -- met condition(s), can save
end
return false -- can't save
end
''
''
Function that determines whether to save the current buffer or not.
- return true: if buffer is ok to be saved
- return false: if it's not ok to be saved
'';
2024-05-05 19:39:35 +02:00
writeAllBuffers = helpers.defaultNullOpts.mkBool false ''
Write all buffers when the current one meets `condition`.
'';
2023-06-02 13:13:27 +02:00
2024-05-05 19:39:35 +02:00
debounceDelay = helpers.defaultNullOpts.mkInt 135 ''
Saves the file at most every `debounce_delay` milliseconds.
'';
2023-06-02 13:13:27 +02:00
2024-05-05 19:39:35 +02:00
callbacks =
mapAttrs (name: desc: helpers.mkNullOrLuaFn "The code of the function that runs ${desc}.")
2023-06-02 13:13:27 +02:00
{
enabling = "when enabling auto-save";
disabling = "when disabling auto-save";
beforeAssertingSave = "before checking `condition`";
beforeSaving = "before doing the actual save";
afterSaving = "after doing the actual save";
};
2024-05-05 19:39:35 +02:00
};
2023-06-02 13:13:27 +02:00
2024-05-05 19:39:35 +02:00
config =
let
options = {
2023-06-02 13:13:27 +02:00
enabled = cfg.enableAutoSave;
execution_message = with cfg.executionMessage; {
inherit message dim;
cleaning_interval = cleaningInterval;
};
trigger_events = cfg.triggerEvents;
inherit (cfg) condition;
2023-06-02 13:13:27 +02:00
write_all_buffers = cfg.writeAllBuffers;
debounce_delay = cfg.debounceDelay;
callbacks = with cfg.callbacks; {
inherit enabling disabling;
before_asserting_save = beforeAssertingSave;
before_saving = beforeSaving;
after_saving = afterSaving;
};
2024-05-05 19:39:35 +02:00
} // cfg.extraOptions;
in
2023-06-02 13:13:27 +02:00
mkIf cfg.enable {
2024-05-05 19:39:35 +02:00
extraPlugins = [ cfg.package ];
2023-06-02 13:13:27 +02:00
extraConfigLua = ''
require('auto-save').setup(${helpers.toLuaObject options})
'';
2024-05-05 19:39:35 +02:00
keymaps =
with cfg.keymaps;
optional (toggle != null) {
mode = "n";
key = toggle;
action = ":ASToggle<CR>";
options.silent = cfg.keymaps.silent;
};
2023-06-02 13:13:27 +02:00
};
}