Skip to content

Commit

Permalink
Merge pull request #47 from mistweaverco/feat-complete-rewrite-parser
Browse files Browse the repository at this point in the history
Feat complete rewrite parser
  • Loading branch information
gorillamoe committed Jul 11, 2024
2 parents d8f81d5 + 93add89 commit 28d8586
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 401 deletions.
2 changes: 1 addition & 1 deletion docs/_coverpage.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# kulala.nvim <small>1.6.3</small>
# kulala.nvim <small>2.0.0</small>

> A minimal 🤏 HTTP-client 🐼 interface 🖥️ for Neovim ❤️.
Expand Down
18 changes: 18 additions & 0 deletions lua/kulala/client_pipe/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local FS = require("kulala.utils.fs")
local M = {}

M.pipe = function(cmdstring, contents)
-- build the command
local cmd = {}
for token in string.gmatch(cmdstring, "[^%s]+") do
table.insert(cmd, token)
end
local cmd_exists = FS.command_exists(cmd[1])
if not cmd_exists then
vim.notify("Pipe --> Command not found: " .. cmd[1] .. ". Returning plain contents..", vim.log.levels.ERROR)
return contents
end
return vim.system(cmd, { stdin = contents, text = true }):wait().stdout
end

return M
18 changes: 13 additions & 5 deletions lua/kulala/cmd/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local GLOBALS = require("kulala.globals")
local CONFIG = require("kulala.config")
local FORMATTER = require("kulala.formatter")
local CLIENT_PIPE = require("kulala.client_pipe")
local FS = require("kulala.utils.fs")

local M = {}
Expand All @@ -9,10 +10,7 @@ local M = {}
M.run = function(result, callback)
vim.fn.jobstart(result.cmd, {
on_stdout = function(_, datalist)
if result.ft then
local body = FS.read_file(GLOBALS.BODY_FILE)
FS.write_file(GLOBALS.BODY_FILE, FORMATTER.format(result.ft, body))
end
-- do nothing
end,
on_stderr = function(_, datalist)
if callback then
Expand All @@ -22,8 +20,18 @@ M.run = function(result, callback)
end
end,
on_exit = function(_, code)
local success = code == 0
if success then
local body = FS.read_file(GLOBALS.BODY_FILE)
if result.ft ~= "text" and not result.client_pipe then
FS.write_file(GLOBALS.BODY_FILE, FORMATTER.format(result.ft, body))
end
if result.client_pipe then
body = CLIENT_PIPE.pipe(result.client_pipe, body)
end
end
if callback then
callback(code == 0)
callback(success)
end
end,
})
Expand Down
1 change: 1 addition & 0 deletions lua/kulala/formatter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ M.format = function(ft, contents)
local cmd = {}
local cmd_exists = false
if ft == "json" then
vim.inspect("formatting for json")
cmd = cfg.formatters.json
cmd_exists = FS.command_exists("jq")
elseif ft == "xml" then
Expand Down
2 changes: 1 addition & 1 deletion lua/kulala/globals/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local FS = require("kulala.utils.fs")

local M = {}

M.VERSION = "1.6.3"
M.VERSION = "2.0.0"
M.UI_ID = "kulala://ui"
M.HEADERS_FILE = FS.get_plugin_tmp_dir() .. "/headers.txt"
M.BODY_FILE = FS.get_plugin_tmp_dir() .. "/body.txt"
Expand Down
6 changes: 6 additions & 0 deletions lua/kulala/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local UI = require("kulala.ui")
local SELECTOR = require("kulala.ui.selector")
local ENV_PARSER = require("kulala.parser.env")
local GLOBAL_STORE = require("kulala.global_store")
local GLOBALS = require("kulala.globals")
local CONFIG = require("kulala.config")
local JUMPS = require("kulala.jumps")
local M = {}
Expand All @@ -17,6 +18,11 @@ M.run = function()
UI:open()
end

M.version = function()
local neovim_version = vim.fn.execute("version")
vim.notify("Kulala version: " .. GLOBALS.VERSION .. "\n\n" .. "Neovim version: " .. neovim_version, "info", { title = "Kulala" })
end

M.jump_next = function()
JUMPS.jump_next()
end
Expand Down
73 changes: 9 additions & 64 deletions lua/kulala/jumps/init.lua
Original file line number Diff line number Diff line change
@@ -1,78 +1,23 @@
local Ts = vim.treesitter
local PARSER = require("kulala.parser")

local M = {}

-- Function to find all "request" nodes in the tree
local function find_all_request_nodes(node)
local request_nodes = {}

local function recursive_search(node)
if not node then return end

for child in node:iter_children() do
if child:type() == "request" then
table.insert(request_nodes, child)
end
recursive_search(child)
end
end

recursive_search(node)
return request_nodes
end

-- Function to move to the next "request" node
M.jump_next = function()
local bufnr = vim.api.nvim_get_current_buf()
local parser = Ts.get_parser(bufnr)
local tree = parser:parse()[1]
local root = tree:root()

local request_nodes = find_all_request_nodes(root)

-- Get the current cursor position
local cursor = vim.api.nvim_win_get_cursor(0)
local current_row = cursor[1] - 1
local current_col = cursor[2]

for _, node in ipairs(request_nodes) do
local start_row, start_col, _, _ = node:range()
if start_row > current_row or (start_row == current_row and start_col > current_col) then
vim.api.nvim_win_set_cursor(0, {start_row + 1, start_col})
return
end
local _, reqs = PARSER.get_document()
local next = PARSER.get_next_request(reqs)
if next then
vim.api.nvim_win_set_cursor(0, {next.start_line + 1, 0})
end

print("No next 'request' node found")
end

-- Function to move to the previous "request" node
M.jump_prev = function()
local bufnr = vim.api.nvim_get_current_buf()
local parser = Ts.get_parser(bufnr)
local tree = parser:parse()[1]
local root = tree:root()

local request_nodes = find_all_request_nodes(root)

-- Get the current cursor position
local cursor = vim.api.nvim_win_get_cursor(0)
local current_row = cursor[1] - 1
local current_col = cursor[2]

for i = #request_nodes, 1, -1 do
local node = request_nodes[i]
local start_row, start_col, _, _ = node:range()
if start_row < current_row or (start_row == current_row and start_col < current_col) then
vim.api.nvim_win_set_cursor(0, {start_row + 1, start_col})
return
end
local _, reqs = PARSER.get_document()
local prev = PARSER.get_previous_request(reqs)
if prev then
vim.api.nvim_win_set_cursor(0, {prev.start_line + 1, 0})
end

print("No previous 'request' node found")
end




return M
12 changes: 12 additions & 0 deletions lua/kulala/parser/dynamic_vars.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
local FS = require("kulala.utils.fs")
local GLOBALS = require("kulala.globals")
local CONFIG = require("kulala.config")
local STRING_UTILS = require("kulala.utils.string")
local M = {}

local random = math.random
Expand All @@ -15,6 +18,14 @@ local function uuid()
end)
end

local previous_response_body = function()
local previous_response = FS.read_file(GLOBALS.BODY_FILE)
if not previous_response then
return ""
end
return STRING_UTILS.trim(previous_response)
end

---Retrieve all dynamic variables from both rest.nvim and the ones declared by
---the user on his configuration
---@return { [string]: fun():string }[] An array-like table of tables which contains dynamic variables definition
Expand All @@ -26,6 +37,7 @@ function M.retrieve_all()
return os.date("%Y-%m-%d")
end,
["$timestamp"] = os.time,
["$previousResponseBody"] = previous_response_body,
["$randomInt"] = function()
return math.random(0, 1000)
end,
Expand Down
Loading

0 comments on commit 28d8586

Please sign in to comment.