Skip to content

Commit

Permalink
feat: implement document vars, fix #68
Browse files Browse the repository at this point in the history
  • Loading branch information
klen authored and NTBBloodbath committed Nov 30, 2021
1 parent e1c3417 commit 7e45caf
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions lua/rest-nvim/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,44 @@ M.read_dynamic_variables = function()
return dynamic_variables
end

M.get_node_value = function(node, bufnr)
local start_row, start_col, _, end_col = node:range()
local line = api.nvim_buf_get_lines(bufnr, start_row, start_row + 1, false)[1]
return line and string.sub(line, start_col + 1, end_col):gsub('"', '') or nil
end

M.read_document_variables = function()
local variables = {}
local bufnr = vim.api.nvim_get_current_buf()
local parser = vim.treesitter.get_parser(bufnr)
if not parser then return variables end

local first_tree = parser:trees()[1]
if not first_tree then return variables end

local root = first_tree:root()
if not root then return variables end

for node in root:iter_children() do
local type = node:type()
if type == 'header' then
local name = node:named_child(0)
local value = node:named_child(1)
variables[M.get_node_value(name, bufnr)] = M.get_node_value(value, bufnr)
elseif type ~= 'comment' then
break
end
end
return variables

end

M.read_variables = function()
local first = M.read_env_file()
local second = M.read_dynamic_variables()
local third = M.read_document_variables()

for k, v in pairs(second) do
first[k] = v
end

return first
return vim.tbl_extend('force', first, second, third)
end

-- replace_vars replaces the env variables fields in the provided string
Expand Down

0 comments on commit 7e45caf

Please sign in to comment.