Skip to content

Commit c3b76f2

Browse files
committed
refact: use a contains_comments function for parsing comments
1 parent fb35b85 commit c3b76f2

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lua/rest-nvim/init.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ local function get_body(bufnr, stop_line, query_line, json_body)
8585

8686
for _, json_line in ipairs(json_lines) do
8787
-- Ignore commented lines with and without indent
88-
if not (json_line:find('^#') or json_line:find('^%s+#')) then
88+
if not utils.contains_comments(json_line) then
8989
json_string = json_string .. utils.replace_env_vars(json_line)
9090
end
9191
end
@@ -148,7 +148,7 @@ local function get_headers(bufnr, query_line)
148148
and not header[1]:find('^%s+"')
149149
-- If header key doesn't contains hashes,
150150
-- so we don't get commented headers
151-
and not (header[1]:find('^#') or header[1]:find('^%s+#'))
151+
and not utils.contains_comments(header[1])
152152
-- If header key doesn't contains HTTP methods,
153153
-- so we don't get the http method/url
154154
and not utils.has_value(http_methods, header[1])
@@ -188,7 +188,7 @@ local function get_accept(bufnr, query_line)
188188

189189
for _, accept_data in pairs(accept_line) do
190190
-- Ignore commented lines with and without indent
191-
if not (accept_data:find('^#') or accept_data:find('(^%s+#)')) then
191+
if not utils.contains_comments(accept_data) then
192192
accept = utils.split(accept_data, ':')[2]
193193
end
194194
end

lua/rest-nvim/utils/init.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,16 @@ M.encode_url = function(url)
171171
return url
172172
end
173173

174+
-- contains_comments checks if the given string contains comments characters
175+
-- @param str The string that should be checked
176+
-- @return number
177+
M.contains_comments = function(str)
178+
return str:find('^#') or str:find('^%s+#')
179+
end
180+
174181
-- http_status returns the status code and the meaning, e.g. 200 OK
175182
-- see https://httpstatuses.com/ for reference
183+
-- @param code The request status code
176184
M.http_status = function(code)
177185
-- NOTE: this table does not cover all the statuses _yet_
178186
local status_meaning = {

0 commit comments

Comments
 (0)