Skip to content

Commit 7e45caf

Browse files
klenNTBBloodbath
authored andcommitted
feat: implement document vars, fix #68
1 parent e1c3417 commit 7e45caf

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

lua/rest-nvim/utils/init.lua

+34-5
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,44 @@ M.read_dynamic_variables = function()
9090
return dynamic_variables
9191
end
9292

93+
M.get_node_value = function(node, bufnr)
94+
local start_row, start_col, _, end_col = node:range()
95+
local line = api.nvim_buf_get_lines(bufnr, start_row, start_row + 1, false)[1]
96+
return line and string.sub(line, start_col + 1, end_col):gsub('"', '') or nil
97+
end
98+
99+
M.read_document_variables = function()
100+
local variables = {}
101+
local bufnr = vim.api.nvim_get_current_buf()
102+
local parser = vim.treesitter.get_parser(bufnr)
103+
if not parser then return variables end
104+
105+
local first_tree = parser:trees()[1]
106+
if not first_tree then return variables end
107+
108+
local root = first_tree:root()
109+
if not root then return variables end
110+
111+
for node in root:iter_children() do
112+
local type = node:type()
113+
if type == 'header' then
114+
local name = node:named_child(0)
115+
local value = node:named_child(1)
116+
variables[M.get_node_value(name, bufnr)] = M.get_node_value(value, bufnr)
117+
elseif type ~= 'comment' then
118+
break
119+
end
120+
end
121+
return variables
122+
123+
end
124+
93125
M.read_variables = function()
94126
local first = M.read_env_file()
95127
local second = M.read_dynamic_variables()
128+
local third = M.read_document_variables()
96129

97-
for k, v in pairs(second) do
98-
first[k] = v
99-
end
100-
101-
return first
130+
return vim.tbl_extend('force', first, second, third)
102131
end
103132

104133
-- replace_vars replaces the env variables fields in the provided string

0 commit comments

Comments
 (0)