Skip to content

Commit b58d498

Browse files
YaroSpacehuantrinh1802tamton-aquib
authored
Weekly updates (#648)
* feat: allow using variables in redirect response (#647) * fix(lsp): return "" if no item.value * feat(cmd): do not display big responses * fix(formatter): graphql deindentation * feat(cmd): format json response on redirect * fix(graphql): support multiline arguments * feat: Mouse support for winbar ui (#653) * feat: Mouse support for winbar ui * feat: Mouse support for winbar ui (pr review) * refact(ui): update icons * fix(grammar): remove comments from body --------- Co-authored-by: YaroSpace <5670940+YaroSpace@users.noreply.github.com> Co-authored-by: BenTriByte <6076614+huantrinh1802@users.noreply.github.com> Co-authored-by: Aquib Javed <77913442+tamton-aquib@users.noreply.github.com>
1 parent 65d102f commit b58d498

File tree

29 files changed

+5786
-6118
lines changed

29 files changed

+5786
-6118
lines changed

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Kulala News
22

3+
## Version 5.3.3
4+
5+
### Feature: support variables in redirect response path
6+
### Feature: do not display big responses + `max_response_size` config option
7+
### Feature: format json response on redirect
8+
39
## Version 5.3.2
410

511
### Enhancement: support url encoding in scheme, authority, path

docs/docs/getting-started/configuration-options.mdx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,12 @@ Generate a bug report on error.
448448

449449
Default: `false`
450450

451+
### format_json_on_redirect
452+
453+
Format json response when redirecting to file
454+
455+
Default: `true`
456+
451457
## Scripts
452458

453459
### scripts.node_path_resolver
@@ -531,8 +537,8 @@ Example:
531537
width = 80,
532538
height = 20,
533539
split = "vertical",
534-
bo = { foldmethod = "indent" }, -- buffer options
535-
wo = { number = true, wrap = true }, -- window options
540+
bo = { number = true, wrap = true }, -- buffer options
541+
wo = { foldmethod = "indent" }, -- window options
536542
},
537543
},
538544
},
@@ -777,6 +783,12 @@ Example:
777783
}
778784
```
779785

786+
### ui.max_response_size
787+
788+
Do not show responses over maximum size, in bytes
789+
790+
Default: `32000`
791+
780792
### ui.show_request_summary
781793

782794
Enable/disable request summary in the output window.

docs/docs/getting-started/requirements.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ List of requirements for using kulala.
3232

3333
(Only required for formatted JSON responses)
3434

35+
## prettier
36+
37+
- [prettier](https://prettier.io)
38+
39+
(Only required for formatting GraphQL)
40+
3541
## xmllint
3642

3743
- [xmllint][xmllint] (tested with libxml v20914)
@@ -54,5 +60,20 @@ vim.filetype.add({
5460

5561
This will make Neovim recognize files with the `.http` extension as HTTP files.
5662

57-
[ts]: https://github.com/nvim-treesitter/nvim-treesitter?tab=readme-ov-file#supported-languages
63+
[ts]: https://github.com/nvim-treesitter/nvim-treesitter
64+
65+
Kulala.nvim comes with a parser compiled with the latest version of treesitter.
66+
67+
If you have Neovim `0.10.x`, you might get an error `ABI version mismatch for kulala_http.so: supported between 13 and 14, found 15`.
68+
69+
You need to install `tree-sitter CLI` and recompile the parser:
70+
71+
1. Delete the existing parser at `nvim-treesitter/parser/kulala_http`
72+
2. Install the `tree-sitter CLI` (if not installed already):
73+
- from distribution repositories
74+
- or from https://github.com/tree-sitter/tree-sitter/tree/master/crates/cli
75+
3. Recompile the parser:
76+
- Open a `http` file in Neovim (this will load Kulala)
77+
- Run `:TSInstallFromGrammar kulala_http`
78+
5879
[xmllint]: https://packages.ubuntu.com/noble/libxml2-utils

lua/kulala/cmd/export.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ M.export_requests = function(action)
482482
local file = vim.fn.fnamemodify(path, ":t:r") .. ".json"
483483
file = (export_type == "folder" and path or vim.fn.fnamemodify(path, ":p:h")) .. "/" .. file
484484

485-
if Fs.write_json(file, collection, true, true) then Logger.info("Exported collection: " .. file) end
485+
if Fs.write_json(file, collection, { escape = true }) then Logger.info("Exported collection: " .. file) end
486486

487487
return collection
488488
end

lua/kulala/cmd/formatter.lua

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ local function capitalize(str)
1313
return str:lower():gsub("^%l", string.upper):gsub("%-%l", string.upper)
1414
end
1515

16-
local function trim(str)
17-
str = str or ""
18-
str = str:gsub("^%s+", ""):gsub("%s+$", ""):gsub("[ \t]+", " ")
19-
return str
16+
local function trim(str, collapse)
17+
str = (str or ""):gsub("^%s+", ""):gsub("%s+$", "")
18+
return collapse and str:gsub("[ \t]+", " ") or str
2019
end
2120

22-
local function get_text(node)
23-
return trim(ts.get_node_text(node, buf))
21+
local function get_text(node, collapse)
22+
collapse = collapse == nil and true or collapse
23+
local text = ts.get_node_text(node, buf)
24+
return trim(text, collapse) or text
2425
end
2526

2627
local function get_fields(node, names)
@@ -267,7 +268,7 @@ format_rules = {
267268
end,
268269

269270
["raw_body"] = function(node)
270-
local body = get_text(node)
271+
local body = get_text(node, false)
271272

272273
local request = current_section().request
273274
request.body = #request.body > 0 and (request.body .. "\n\n" .. body) or body
@@ -276,7 +277,7 @@ format_rules = {
276277
end,
277278

278279
["multipart_form_data"] = function(node)
279-
local body = get_text(node)
280+
local body = get_text(node, false)
280281

281282
local request = current_section().request
282283
request.body = #request.body > 0 and (request.body .. "\n\n" .. body) or body
@@ -285,7 +286,7 @@ format_rules = {
285286
end,
286287

287288
["xml_body"] = function(node)
288-
local body = get_text(node)
289+
local body = get_text(node, false)
289290

290291
local formatted = Formatter.xml(body) or body
291292
formatted = formatted:gsub("\n*$", "")
@@ -297,7 +298,7 @@ format_rules = {
297298
end,
298299

299300
["json_body"] = function(node)
300-
local json = get_text(node)
301+
local json = get_text(node, false)
301302

302303
if format_opts.quote_json_variables then
303304
local lcurly, rcurly = "X7BX7B", "X7DX7D"
@@ -324,7 +325,8 @@ format_rules = {
324325
end,
325326

326327
["graphql_data"] = function(node)
327-
local body = get_text(node)
328+
local body = get_text(node, false)
329+
328330
local formatted = Formatter.graphql(body, { sort = format_opts.sort.json }) or body
329331
formatted = formatted:gsub("\n*$", "")
330332

@@ -344,16 +346,16 @@ format_rules = {
344346
end,
345347

346348
["pre_request_script"] = function(node)
347-
local script = get_text(node)
348-
script = script:gsub("\n", "\n "):gsub("\n %%}", "\n%%}")
349+
local script = get_text(node, false)
350+
script = script:gsub("\n", "\n "):gsub("\n%s+%%}", "\n%%}")
349351

350352
table.insert(current_section().request.pre_request_script, script)
351353
return script
352354
end,
353355

354356
["res_handler_script"] = function(node)
355-
local script = get_text(node)
356-
script = script:gsub("\n", "\n "):gsub("\n %%}", "\n%%}")
357+
local script = get_text(node, false)
358+
script = script:gsub("\n", "\n "):gsub("\n%s+%%}", "\n%%}")
357359

358360
table.insert(current_section().request.res_handler_script, script)
359361
return script

lua/kulala/cmd/init.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ local function inject_payload(errors, request)
176176
return table.concat(lines, "\n")
177177
end
178178

179+
local function get_body()
180+
local max_size = CONFIG.get().ui.max_response_size
181+
if vim.fn.getfsize(GLOBALS.BODY_FILE) > max_size then
182+
FS.write_file(GLOBALS.HEADERS_FILE, "Content-Type: text/plain", true)
183+
return "The size of response is > " .. max_size / 1000 .. "Kb.\nPath to response: " .. GLOBALS.BODY_FILE
184+
else
185+
return FS.read_file(GLOBALS.BODY_FILE) or ""
186+
end
187+
end
188+
179189
local function save_response(request_status, parsed_request)
180190
local buf = DB.get_current_buffer()
181191
local line = parsed_request.show_icon_line_number or 0
@@ -202,7 +212,7 @@ local function save_response(request_status, parsed_request)
202212
time = vim.fn.localtime(),
203213
duration = request_status.duration or 0,
204214
body = "",
205-
body_raw = FS.read_file(GLOBALS.BODY_FILE) or "",
215+
body_raw = get_body(),
206216
json = {},
207217
filtered = nil,
208218
headers = FS.read_file(GLOBALS.HEADERS_FILE) or "",

lua/kulala/cmd/lsp.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ local trigger_chars = { "@", "#", "-", ":", "{", "$", ">", "<", ".", "(", '"' }
2626

2727
local function make_item(label, description, kind, detail, documentation, text, format, score)
2828
return {
29-
label = label,
29+
label = label or "",
3030
labelDetails = {
31-
description = description,
31+
description = description or "",
3232
},
33-
kind = kind,
34-
detail = detail,
33+
kind = kind or "",
34+
detail = detail or "",
3535
documentation = {
36-
value = documentation,
36+
value = documentation or "",
3737
kind = vim.lsp.protocol.MarkupKind.Markdown,
3838
},
39-
insertText = text,
39+
insertText = text or "",
4040
insertTextFormat = format or lsp_format.PlainText,
4141
sortText = score or tostring(1.02), -- fix for blink.cmp
4242
}

lua/kulala/config/defaults.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ local M = {
6464
},
6565
},
6666

67+
-- format json response when redirecting to file
68+
format_json_on_redirect = true,
69+
6770
scripts = {
6871
-- Resolves "NODE_PATH" environment variable for node scripts. Defaults to the first "node_modules" directory found upwards from "script_file_dir".
6972
node_path_resolver = nil, ---@type fun(http_file_dir: string, script_file_dir: string, script_data: ScriptData): string|nil
@@ -116,6 +119,9 @@ local M = {
116119
-- disable notifications of script output
117120
disable_script_print_output = false,
118121

122+
-- do not show responses over maximum size, in bytes
123+
max_response_size = 32000,
124+
119125
report = {
120126
-- possible values: true | false | "on_error"
121127
show_script_output = true,

lua/kulala/config/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ M.options = M.defaults
1717

1818
local function set_signcolumn_icons()
1919
vim.fn.sign_define {
20-
{ name = "kulala.done", text = M.options.icons.inlay.done },
21-
{ name = "kulala.error", text = M.options.icons.inlay.error },
20+
{ name = "kulala.done", text = M.options.icons.inlay.done, texthl = "String" },
21+
{ name = "kulala.error", text = M.options.icons.inlay.error, texthl = "ErrorMsg" },
2222
{ name = "kulala.loading", text = M.options.icons.inlay.loading },
2323
{ name = "kulala.space", text = " " },
2424
}

lua/kulala/formatter/init.lua

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
local Config = require("kulala.config")
2-
local Graphql = require("kulala.parser.graphql")
32
local Logger = require("kulala.logger")
43
local Shell = require("kulala.cmd.shell_utils")
54

65
local M = {}
76

87
M.format = function(formatter, contents, opts)
98
opts = vim.tbl_deep_extend("keep", opts or {}, {
9+
escape = true,
1010
verbose = true,
1111
})
1212

@@ -29,7 +29,7 @@ M.format = function(formatter, contents, opts)
2929

3030
if not result or result.code ~= 0 or result.stderr ~= "" or result.stdout == "" then return contents end
3131

32-
return result.stdout
32+
return opts.escape and result.stdout or result.stdout:gsub("\\/", "/"):gsub('\\"', '"')
3333
end
3434

3535
M.json = function(contents, opts)
@@ -50,16 +50,7 @@ M.graphql = function(contents, opts)
5050
local formatter = Config.get().contenttypes["application/graphql"]
5151
if not (formatter and type(formatter.formatter) == "table") then return contents end
5252

53-
local _, json = Graphql.get_json(contents)
54-
if not json then return contents end
55-
56-
local formatted = M.format(formatter.formatter, json.query, opts)
57-
58-
if json.variables and next(json.variables) then
59-
formatted = formatted .. "\n" .. M.json(json.variables, { sort = opts.sort })
60-
end
61-
62-
return formatted
53+
return M.format(formatter.formatter, contents, opts)
6354
end
6455

6556
M.html = function(contents, opts)

0 commit comments

Comments
 (0)