plugins/rest: switch to mkNeovimPlugin

This commit is contained in:
Gaetan Lepage 2024-04-07 14:07:31 +02:00 committed by Gaétan Lepage
parent 3a161eaec0
commit 192b404e41
2 changed files with 365 additions and 184 deletions

View file

@ -5,179 +5,320 @@
pkgs, pkgs,
... ...
}: }:
with lib; let with lib;
cfg = config.plugins.rest; helpers.neovim-plugin.mkNeovimPlugin config {
in { name = "rest";
meta.maintainers = [maintainers.GaetanLepage]; originalName = "rest.nvim";
luaName = "rest-nvim";
defaultPackage = pkgs.vimPlugins.rest-nvim;
options.plugins.rest = extraPackages = [pkgs.curl];
helpers.neovim-plugin.extraOptionsOptions
// {
enable = mkEnableOption "rest.nvim";
package = helpers.mkPackageOption "rest.nvim" pkgs.vimPlugins.rest-nvim; maintainers = [maintainers.GaetanLepage];
resultSplitHorizontal = helpers.defaultNullOpts.mkBool false '' # TODO introduced 2024-04-07: remove 2024-06-07
Open request results in a horizontal split (default opens on vertical). deprecateExtraOptions = true;
optionsRenamedToSettings = [
"envFile"
"encodeUrl"
"skipSslVerification"
"customDynamicVariables"
["highlight" "timeout"]
];
imports = let
basePluginPath = ["plugins" "rest"];
settingsPath = basePluginPath ++ ["settings"];
in [
(
mkRenamedOptionModule
(basePluginPath ++ ["resultSplitHorizontal"])
(settingsPath ++ ["result" "split" "horizontal"])
)
(
mkRenamedOptionModule
(basePluginPath ++ ["resultSplitInPlace"])
(settingsPath ++ ["result" "split" "in_place"])
)
(
mkRenamedOptionModule
(basePluginPath ++ ["stayInCurrentWindowAfterSplit"])
(settingsPath ++ ["result" "split" "stay_in_current_window_after_split"])
)
(
mkRenamedOptionModule
(basePluginPath ++ ["result" "showUrl"])
(settingsPath ++ ["result" "behavior" "show_info" "url"])
)
(
mkRenamedOptionModule
(basePluginPath ++ ["result" "showHeaders"])
(settingsPath ++ ["result" "behavior" "show_info" "headers"])
)
(
mkRenamedOptionModule
(basePluginPath ++ ["result" "showHttpInfo"])
(settingsPath ++ ["result" "behavior" "show_info" "http_info"])
)
(
mkRenamedOptionModule
(basePluginPath ++ ["result" "showCurlCommand"])
(settingsPath ++ ["result" "behavior" "show_info" "curl_command"])
)
(
mkRemovedOptionModule
(basePluginPath ++ ["result" "showStatistics"])
''
Use `plugins.rest.settings.result.behavior.statistics.{enable,stats}` instead.
Refer to the documentation for more information.
''
)
(
mkRenamedOptionModule
(basePluginPath ++ ["result" "formatters"])
(settingsPath ++ ["result" "behavior" "formatters"])
)
(
mkRenamedOptionModule
(basePluginPath ++ ["highlight" "enabled"])
(settingsPath ++ ["highlight" "enable"])
)
(
mkRemovedOptionModule
(basePluginPath ++ ["jumpToRequest"])
''
This option has been deprecated upstream.
''
)
(
mkRemovedOptionModule
(basePluginPath ++ ["yankDryRun"])
''
This option has been deprecated upstream.
''
)
(
mkRemovedOptionModule
(basePluginPath ++ ["searchBack"])
''
This option has been deprecated upstream.
''
)
];
settingsOptions = {
client = helpers.defaultNullOpts.mkStr "curl" ''
The HTTP client to be used when running requests.
''; '';
resultSplitInPlace = helpers.defaultNullOpts.mkBool false '' env_file = helpers.defaultNullOpts.mkStr ".env" ''
Keep the http file buffer above|left when split horizontal|vertical. Environment variables file to be used for the request variables in the document.
''; '';
stayInCurrentWindowAfterSplit = helpers.defaultNullOpts.mkBool false '' env_pattern = helpers.defaultNullOpts.mkStr "\\.env$" ''
Stay in current windows (`.http` file) or change to results window (default). Environment variables file pattern for `telescope.nvim`.
''; '';
skipSslVerification = helpers.defaultNullOpts.mkBool false '' env_edit_command = helpers.defaultNullOpts.mkStr "tabedit" ''
Skip SSL verification, useful for unknown certificates. Neovim command to edit an environment file.
''; '';
encodeUrl = helpers.defaultNullOpts.mkBool true '' encode_url = helpers.defaultNullOpts.mkBool true ''
Encode URL before making request. Encode URL before making request.
''; '';
highlight = { skip_ssl_verification = helpers.defaultNullOpts.mkBool false ''
enabled = helpers.defaultNullOpts.mkBool true '' Skip SSL verification, useful for unknown certificates.
Enable the highlighting of the selected request when send. '';
custom_dynamic_variables = mkOption {
type = with helpers.nixvimTypes;
nullOr (
maybeRaw (
attrsOf strLuaFn
)
);
default = null;
example = {
"$timestamp" = "os.time";
"$randomInt" = ''
function()
return math.random(0, 1000)
end
'';
};
description = ''
Custom dynamic variables. Keys are variable names and values are lua functions.
default: `{}`
'';
apply = v:
if isAttrs v
then mapAttrs (_: helpers.mkRaw) v
else v;
};
logs = {
level = helpers.defaultNullOpts.mkNullable helpers.nixvimTypes.logLevel "info" ''
The logging level name, see `:h vim.log.levels`.
''; '';
timeout = helpers.defaultNullOpts.mkUnsignedInt 150 '' save = helpers.defaultNullOpts.mkBool true ''
Timeout for highlighting. Whether to save log messages into a `.log` file.
''; '';
}; };
result = { result = {
showUrl = helpers.defaultNullOpts.mkBool true '' split = {
Toggle showing URL, HTTP info, headers at top the of result window. horizontal = helpers.defaultNullOpts.mkBool false ''
''; Open request results in a horizontal split.
showCurlCommand = helpers.defaultNullOpts.mkBool true ''
Show the generated curl command in case you want to launch the same request via the
terminal (can be verbose).
'';
showHttpInfo = helpers.defaultNullOpts.mkBool true ''
Show HTTP information.
'';
showHeaders = helpers.defaultNullOpts.mkBool true ''
Show headers information.
'';
showStatistics =
helpers.defaultNullOpts.mkNullable
(
with types;
either
(enum [false])
(
listOf
(
either
str
(listOf str)
)
)
)
"false"
''
Table of curl `--write-out` variables or false if disabled.
For more granular control see [Statistics Spec](https://github.com/rest-nvim/rest.nvim?tab=readme-ov-file#statistics-spec).
''; '';
formatters = in_place = helpers.defaultNullOpts.mkBool false ''
helpers.defaultNullOpts.mkAttrsOf Keep the HTTP file buffer above|left when split horizontal|vertical.
(
with types;
either str (enum [false])
)
''
{
json = "jq";
html.__raw = \'\'
function(body)
if vim.fn.executable("tidy") == 0 then
return body
end
-- stylua: ignore
return vim.fn.system({
"tidy", "-i", "-q",
"--tidy-mark", "no",
"--show-body-only", "auto",
"--show-errors", "0",
"--show-warnings", "0",
"-",
}, body):gsub("\n$", "")
end
\'\';
}
''
''
Executables or functions for formatting response body [optional].
Set them to false if you want to disable them.
''; '';
stay_in_current_window_after_split = helpers.defaultNullOpts.mkBool true ''
Stay in the current window (HTTP file) or change the focus to the results window.
'';
};
behavior = {
show_info = {
url = helpers.defaultNullOpts.mkBool true ''
Display the request URL.
'';
headers = helpers.defaultNullOpts.mkBool true ''
Display the request headers.
'';
http_info = helpers.defaultNullOpts.mkBool true ''
Display the request HTTP information.
'';
curl_command = helpers.defaultNullOpts.mkBool true ''
Display the cURL command that was used for the request.
'';
};
decode_url = helpers.defaultNullOpts.mkBool true ''
Whether to decode the request URL query parameters to improve readability.
'';
statistics = {
enable = helpers.defaultNullOpts.mkBool true ''
Whether to enable statistics or not.
'';
stats =
helpers.defaultNullOpts.mkListOf (with types; attrsOf str)
''
[
{
__unkeyed = "total_time";
title = "Time taken:";
}
{
__unkeyed = "size_download_t";
title = "Download size:";
}
]
''
"See https://curl.se/libcurl/c/curl_easy_getinfo.html.";
};
formatters = {
json = helpers.defaultNullOpts.mkStr "jq" ''
JSON formatter.
'';
html =
helpers.defaultNullOpts.mkStr
''
{
__raw = \'\'
function(body)
if vim.fn.executable("tidy") == 0 then
return body, { found = false, name = "tidy" }
end
local fmt_body = vim.fn.system({
"tidy",
"-i",
"-q",
"--tidy-mark", "no",
"--show-body-only", "auto",
"--show-errors", "0",
"--show-warnings", "0",
"-",
}, body):gsub("\n$", "")
return fmt_body, { found = true, name = "tidy" }
end
\'\';
}
''
"HTML formatter.";
};
};
}; };
jumpToRequest = helpers.defaultNullOpts.mkBool false '' highlight = {
Moves the cursor to the selected request line when send. enable = helpers.defaultNullOpts.mkBool true ''
''; Whether current request highlighting is enabled or not.
'';
envFile = helpers.defaultNullOpts.mkStr ".env" ''
Specifies file name that consist environment variables. timeout = helpers.defaultNullOpts.mkUnsignedInt 750 ''
''; Duration time of the request highlighting in milliseconds.
customDynamicVariables = mkOption {
type = with helpers.nixvimTypes; nullOr (attrsOf strLuaFn);
default = null;
description = ''
Allows to extend or overwrite built-in dynamic variable functions.
''; '';
apply = v: helpers.ifNonNull' v (mapAttrs (_: helpers.mkRaw) v);
}; };
yankDryRun = helpers.defaultNullOpts.mkBool true ""; keybinds =
helpers.defaultNullOpts.mkListOf (with types; listOf str)
searchBack = helpers.defaultNullOpts.mkBool true ""; ''
[
[
"<localleader>rr"
"<cmd>Rest run<cr>"
"Run request under the cursor"
]
[
"<localleader>rl"
"<cmd>Rest run last<cr>"
"Re-run latest request"
]
]
''
''
Declare some keybindings.
Format: list of 3 strings lists: key, action and description.
'';
}; };
config = mkIf cfg.enable { settingsExample = {
extraPlugins = [cfg.package]; client = "curl";
env_file = ".env";
extraPackages = [pkgs.curl]; logs = {
level = "info";
extraConfigLua = let save = true;
setupOptions = with cfg; };
{ result = {
result_split_horizontal = resultSplitHorizontal; split = {
result_split_in_place = resultSplitInPlace; horizontal = false;
stay_in_current_window_after_split = stayInCurrentWindowAfterSplit; in_place = false;
skip_ssl_verification = skipSslVerification; stay_in_current_window_after_split = true;
encode_url = encodeUrl; };
highlight = with highlight; { };
inherit keybinds = [
(highlight) [
enabled "<localleader>rr"
timeout "<cmd>Rest run<cr>"
; "Run request under the cursor"
}; ]
result = with result; { [
show_url = showUrl; "<localleader>rl"
show_curl_command = showCurlCommand; "<cmd>Rest run last<cr>"
show_http_info = showHttpInfo; "Re-run latest request"
show_headers = showHeaders; ]
show_statistics = showStatistics; ];
inherit formatters; };
}; }
jump_to_request = jumpToRequest;
env_file = envFile;
custom_dynamic_variables = customDynamicVariables;
yank_dry_run = yankDryRun;
search_back = searchBack;
}
// cfg.extraOptions;
in ''
require('rest-nvim').setup(${helpers.toLuaObject setupOptions})
'';
};
}

View file

@ -7,46 +7,86 @@
plugins.rest = { plugins.rest = {
enable = true; enable = true;
resultSplitHorizontal = false; settings = {
resultSplitInPlace = false; client = "curl";
stayInCurrentWindowAfterSplit = false; env_file = ".env";
skipSslVerification = false; env_pattern = "\\.env$";
encodeUrl = true; env_edit_command = "tabedit";
highlight = { encode_url = true;
enabled = true; skip_ssl_verification = false;
timeout = 150; custom_dynamic_variables = {};
}; logs = {
result = { level = "info";
showUrl = true; save = true;
showCurlCommand = true;
showHttpInfo = true;
showHeaders = true;
showStatistics = false;
formatters = {
json = "jq";
html.__raw = ''
function(body)
if vim.fn.executable("tidy") == 0 then
return body
end
-- stylua: ignore
return vim.fn.system({
"tidy", "-i", "-q",
"--tidy-mark", "no",
"--show-body-only", "auto",
"--show-errors", "0",
"--show-warnings", "0",
"-",
}, body):gsub("\n$", "")
end
'';
}; };
result = {
split = {
horizontal = false;
in_place = false;
stay_in_current_window_after_split = true;
};
behavior = {
show_info = {
url = true;
headers = true;
http_info = true;
curl_command = true;
};
decode_url = true;
statistics = {
enable = true;
stats = [
{
__unkeyed = "total_time";
title = "Time taken:";
}
{
__unkeyed = "size_download_t";
title = "Download size:";
}
];
};
formatters = {
json = "jq";
html.__raw = ''
function(body)
if vim.fn.executable("tidy") == 0 then
return body, { found = false, name = "tidy" }
end
local fmt_body = vim.fn.system({
"tidy",
"-i",
"-q",
"--tidy-mark", "no",
"--show-body-only", "auto",
"--show-errors", "0",
"--show-warnings", "0",
"-",
}, body):gsub("\n$", "")
return fmt_body, { found = true, name = "tidy" }
end
'';
};
};
};
highlight = {
enable = true;
timeout = 750;
};
keybinds = [
[
"<localleader>rr"
"<cmd>Rest run<cr>"
"Run request under the cursor"
]
[
"<localleader>rl"
"<cmd>Rest run last<cr>"
"Re-run latest request"
]
];
}; };
jumpToRequest = false;
envFile = ".env";
customDynamicVariables = null;
yankDryRun = true;
searchBack = true;
}; };
}; };
} }