Skip to content

Commit d0377fd

Browse files
authored
Merge pull request #260 from Rasmus-Bertell/feature/refactor-writing-to-buffer
2 parents c186d3e + 0d22f4a commit d0377fd

2 files changed

Lines changed: 130 additions & 39 deletions

File tree

lua/rest-nvim/curl/init.lua

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,14 @@ M.get_or_create_buf = function()
7676
local existing_bufnr = vim.fn.bufnr(tmp_name)
7777
if existing_bufnr ~= -1 then
7878
-- Set modifiable
79-
vim.api.nvim_set_option_value("modifiable", true, { buf = existing_bufnr})
79+
vim.api.nvim_set_option_value("modifiable", true, { buf = existing_bufnr })
8080
-- Prevent modified flag
81-
vim.api.nvim_set_option_value("buftype", "nofile", { buf = existing_bufnr})
81+
vim.api.nvim_set_option_value("buftype", "nofile", { buf = existing_bufnr })
8282
-- Delete buffer content
83-
vim.api.nvim_buf_set_lines(
84-
existing_bufnr,
85-
0,
86-
vim.api.nvim_buf_line_count(existing_bufnr) - 1,
87-
false,
88-
{}
89-
)
83+
vim.api.nvim_buf_set_lines(existing_bufnr, 0, -1, false, {})
9084

9185
-- Make sure the filetype of the buffer is httpResult so it will be highlighted
92-
vim.api.nvim_set_option_value("ft", "httpResult", { buf = existing_bufnr } )
86+
vim.api.nvim_set_option_value("ft", "httpResult", { buf = existing_bufnr })
9387

9488
return existing_bufnr
9589
end
@@ -116,9 +110,28 @@ local function create_callback(curl_cmd, opts)
116110
return
117111
end
118112
local res_bufnr = M.get_or_create_buf()
119-
local header_lines = res.headers
113+
114+
local headers = utils.filter(res.headers, function(value)
115+
return value ~= ""
116+
end, false)
117+
118+
headers = utils.map(headers, function(value)
119+
local _, _, http, status = string.find(value, "^(HTTP.*)%s+(%d+)%s*$")
120+
121+
if http and status then
122+
return http .. " " .. utils.http_status(tonumber(status))
123+
end
124+
125+
return value
126+
end)
127+
128+
headers = utils.split_list(headers, function(value)
129+
return string.find(value, "^HTTP.*$")
130+
end)
131+
120132
res.headers = parse_headers(res.headers)
121-
local content_type = res.headers[utils.key(res.headers,'content-type')]
133+
134+
local content_type = res.headers[utils.key(res.headers, "content-type")]
122135
if content_type then
123136
content_type = content_type:match("application/([-a-z]+)") or content_type:match("text/(%l+)")
124137
end
@@ -139,45 +152,32 @@ local function create_callback(curl_cmd, opts)
139152
end
140153
end
141154

142-
-- This can be quite verbose so let user control it
143-
if config.get("result").show_curl_command then
144-
vim.api.nvim_buf_set_lines(res_bufnr, 0, 0, false, { "Command: " .. curl_cmd })
145-
end
146-
147155
if config.get("result").show_url then
148156
--- Add metadata into the created buffer (status code, date, etc)
149157
-- Request statement (METHOD URL)
150-
vim.api.nvim_buf_set_lines(res_bufnr, 0, 0, false, { method:upper() .. " " .. url })
158+
utils.write_block(res_bufnr, { method:upper() .. " " .. url }, false)
159+
end
160+
161+
-- This can be quite verbose so let user control it
162+
if config.get("result").show_curl_command then
163+
utils.write_block(res_bufnr, { "Command: " .. curl_cmd }, true)
151164
end
152165

153166
if config.get("result").show_http_info then
154-
local line_count = vim.api.nvim_buf_line_count(res_bufnr)
155-
local separator = config.get("result").show_url and 0 or 1
156167
-- HTTP version, status code and its meaning, e.g. HTTP/1.1 200 OK
157-
vim.api.nvim_buf_set_lines(
158-
res_bufnr,
159-
line_count - separator,
160-
line_count - separator,
161-
false,
162-
{ "HTTP/1.1 " .. utils.http_status(res.status) }
163-
)
168+
utils.write_block(res_bufnr, { "HTTP/1.1 " .. utils.http_status(res.status) }, false)
164169
end
165170

166171
if config.get("result").show_headers then
167-
local line_count = vim.api.nvim_buf_line_count(res_bufnr)
168172
-- Headers, e.g. Content-Type: application/json
169-
vim.api.nvim_buf_set_lines(
170-
res_bufnr,
171-
line_count + 1,
172-
line_count + 1 + #header_lines,
173-
false,
174-
header_lines
175-
)
173+
for _, header_block in ipairs(headers) do
174+
utils.write_block(res_bufnr, header_block, true)
175+
end
176176
end
177177

178178
--- Add the curl command results into the created buffer
179179
local formatter = config.get("result").formatters[content_type]
180-
-- formate response body
180+
-- format response body
181181
if type(formatter) == "function" then
182182
local ok, out = pcall(formatter, res.body)
183183
-- check if formatter ran successfully
@@ -220,8 +220,8 @@ local function create_callback(curl_cmd, opts)
220220
buf_content = buf_content .. "\n#+END"
221221

222222
local lines = utils.split(buf_content, "\n")
223-
local line_count = vim.api.nvim_buf_line_count(res_bufnr) - 1
224-
vim.api.nvim_buf_set_lines(res_bufnr, line_count, line_count + #lines, false, lines)
223+
224+
utils.write_block(res_bufnr, lines)
225225

226226
-- Only open a new split if the buffer is not loaded into the current window
227227
if vim.fn.bufwinnr(res_bufnr) == -1 then

lua/rest-nvim/utils/init.lua

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ M.get_variables = function()
182182
if variables[name]:match(oname) then
183183
-- Add that into the variable
184184
-- I.E if @url={{path}}:{{port}}/{{source}}
185-
-- Substitue in path, port and source
185+
-- Substitute in path, port and source
186186
variables[name] = variables[name]:gsub("{{" .. oname .. "}}", ovalue)
187187
end
188188
end
@@ -417,6 +417,97 @@ M.contains_comments = function(str)
417417
return str:find("^#") or str:find("^%s+#")
418418
end
419419

420+
--- Filter a table and return filtered copy
421+
---
422+
--- @param tbl table The table to filter
423+
--- @param filter function The filtering function, parameters are value, key and table
424+
--- @param preserve_keys boolean? Should the copied table preserve keys or not, default true
425+
---
426+
--- @return List|table
427+
M.filter = function(tbl, filter, preserve_keys)
428+
local out = {}
429+
430+
preserve_keys = preserve_keys and true
431+
432+
for key, val in ipairs(tbl) do
433+
if filter(val, key, tbl) then
434+
if preserve_keys then
435+
out[key] = val
436+
else
437+
table.insert(out, val)
438+
end
439+
end
440+
end
441+
442+
return out
443+
end
444+
445+
--- Make a copy of the table applying the transformation function to each element.
446+
--- Does not preserve the keys of the original table.
447+
---
448+
--- @param tbl table The table to filter
449+
--- @param transform function The transformation function, parameters are value, key and table
450+
---
451+
--- @return List
452+
M.map = function(tbl, transform)
453+
local out = {}
454+
455+
for key, val in ipairs(tbl) do
456+
table.insert(out, transform(val, key, tbl))
457+
end
458+
459+
return out
460+
end
461+
462+
--- Wrapper around nvim_buf_set_lines
463+
---
464+
--- @param buffer integer The target buffer
465+
--- @param block List The list of lines to write
466+
--- @param newline boolean? Add a newline to the end, default false
467+
---
468+
--- @return nil
469+
M.write_block = function(buffer, block, newline)
470+
local content = vim.api.nvim_buf_get_lines(buffer, 0, -1, false)
471+
local first_line = false
472+
473+
if #content == 1 and content[1] == "" then
474+
first_line = true
475+
end
476+
477+
vim.api.nvim_buf_set_lines(buffer, first_line and 0 or -1, -1, false, block)
478+
479+
if newline then
480+
vim.api.nvim_buf_set_lines(buffer, -1, -1, false, { "" })
481+
end
482+
end
483+
484+
--- Split table on the elements where the function returns true
485+
---
486+
--- @param tbl List
487+
--- @param index function
488+
--- @param inclusive boolean? If true the split value is in the first table, default false
489+
---
490+
--- @return List[]
491+
M.split_list = function(tbl, index, inclusive)
492+
local out = { {} }
493+
494+
for key, val in ipairs(tbl) do
495+
if index(val, key, tbl) then
496+
table.insert(out, {})
497+
498+
if inclusive then
499+
table.insert(out[#out - 1], val)
500+
else
501+
table.insert(out[#out], val)
502+
end
503+
else
504+
table.insert(out[#out], val)
505+
end
506+
end
507+
508+
return out
509+
end
510+
420511
-- http_status returns the status code and the meaning, e.g. 200 OK
421512
-- see https://httpstatuses.com/ for reference
422513
-- @param code The request status code

0 commit comments

Comments
 (0)