@@ -90,15 +90,44 @@ M.read_dynamic_variables = function()
90
90
return dynamic_variables
91
91
end
92
92
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
+
93
125
M .read_variables = function ()
94
126
local first = M .read_env_file ()
95
127
local second = M .read_dynamic_variables ()
128
+ local third = M .read_document_variables ()
96
129
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 )
102
131
end
103
132
104
133
-- replace_vars replaces the env variables fields in the provided string
0 commit comments