Skip to content

Commit 0438864

Browse files
feat: prompt variables (close #238)
1 parent 4729132 commit 0438864

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lua/rest-nvim/parser/init.lua

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,23 @@ function parser.parse(node, source, ctx)
391391
end
392392
elseif child_type == "request_separator" then
393393
name = get_node_field_text(child, "value", source)
394-
elseif child_type == "comment" and get_node_field_text(child, "name", source) == "name" then
395-
name = get_node_field_text(child, "value", source) or name
394+
elseif child_type == "comment" and child:field("name")[1] then
395+
local comment_name = get_node_field_text(child, "name", source)
396+
local comment_value = get_node_field_text(child, "value", source)
397+
if comment_name == "name" then
398+
name = comment_value or name
399+
elseif comment_name == "prompt" and comment_value then
400+
local var_name, var_description = comment_value:match("(%S+)%s*(.*)")
401+
if var_description == "" then
402+
var_description = nil
403+
end
404+
vim.ui.input({
405+
prompt = (var_description or ("Enter value for `%s`"):format(var_name)) .. ": ",
406+
default = ctx:resolve(var_name),
407+
}, function (input)
408+
ctx:set_local(var_name, input)
409+
end)
410+
end
396411
elseif child_type == "variable_declaration" then
397412
parser.parse_variable_declaration(child, source, ctx)
398413
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
### basic get statement
2+
3+
# Basic syntax of prompt variable:
4+
5+
# @prompt user Username to search from github
6+
# ---- ------------------------------
7+
# | |
8+
# | `- variable description (optional)
9+
# |
10+
# `- variable name
11+
12+
# If variable with same key already exists, existing value will be the default value
13+
14+
@host = https://api.github.com
15+
16+
# @prompt host Host to send the request
17+
18+
GET /users/{{user}}
19+
Host: {{host}}
20+
user-agent: neovim

0 commit comments

Comments
 (0)