From 8885e2f216d0bdd8b24a2de494342bd4d80de02c Mon Sep 17 00:00:00 2001 From: "Dung Duc Huynh (Kaka)" <870029+jellydn@users.noreply.github.com> Date: Thu, 14 Mar 2024 11:07:10 +0800 Subject: [PATCH] Fix: HurlRunnterToEntry using treesitter * Fix HurlRunnterToEntry using treesitter (#116) * chore(doc): auto generate docs * chore(test): add API tests for dog breeds * Fix find entry via treesitter (#124) * Fix HurlRunnterToEntry using treesitter * chore(doc): auto generate docs * Fix find entry method returing two nodes range. Lua index starts on 1 so we were accidentally returing the line range of the current node plus one line of the next node. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: github-actions[bot] Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * refactor(http_utils): make find_hurl_entry_positions_in_buffer a module function --------- Co-authored-by: Horacio Sanson <900716+hsanson@users.noreply.github.com> Co-authored-by: github-actions[bot] Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- lua/hurl/http_utils.lua | 120 ++++++++++++---------------------------- lua/hurl/main.lua | 2 +- 2 files changed, 36 insertions(+), 86 deletions(-) diff --git a/lua/hurl/http_utils.lua b/lua/hurl/http_utils.lua index 7755e91..b12aaf6 100644 --- a/lua/hurl/http_utils.lua +++ b/lua/hurl/http_utils.lua @@ -1,103 +1,53 @@ local M = {} ---- Find the HTTP verb in the given line ----@param line string ----@param current_line_number number -local function find_http_verb(line, current_line_number) - if not line then - return nil - end - - local verbs = { 'GET', 'POST', 'PUT', 'DELETE', 'PATCH' } - local verb_start, verb_end, verb - - for _, v in ipairs(verbs) do - verb_start, verb_end = line:find(v) - if verb_start then - verb = v - break - end - end - - if verb_start then - return { - line_number = current_line_number, - start_pos = verb_start, - end_pos = verb_end, - method = verb, - } - else - return nil - end -end - --- Find the closest HURL entry at the current cursor position. -local function find_hurl_entry_positions_in_buffer() +M.find_hurl_entry_positions_in_buffer = function() local ts = vim.treesitter - local node = ts.get_node() - while node and node:type() ~= 'entry' do - node = node:parent() + -- Look for closest `entry` node to cursor position. + local current_node = ts.get_node() + while current_node and current_node:type() ~= 'entry' do + current_node = current_node:parent() end - if not node then + if not current_node then + local cursor_line = vim.api.nvim_win_get_cursor(0)[1] return { current = 0, - start_line = nil, - end_line = nil, + start_line = cursor_line, + end_line = cursor_line, } else - local r1, _, _ = node:start() - local r2, _, _ = node:end_() - return { - current = r1 + 1, - start_line = r1 + 1, - end_line = r2 + 1, - } - end -end - ---- Find the HTTP verbs in the current buffer ----@return table -local function find_http_verb_positions_in_buffer() - local buf = vim.api.nvim_get_current_buf() - local total_lines = vim.api.nvim_buf_line_count(buf) - local cursor = vim.api.nvim_win_get_cursor(0) - local current_line_number = cursor[1] - - local next_entry = 0 - local current_index = 0 - local current_verb = nil - local end_line = total_lines -- Default to the last line of the buffer - - for i = 1, total_lines do - local line = vim.api.nvim_buf_get_lines(buf, i - 1, i, false)[1] - local result = find_http_verb(line, i) - if result then - next_entry = next_entry + 1 - if i == current_line_number then - current_index = next_entry - current_verb = result - elseif current_verb and i > current_verb.line_number then - end_line = i - 1 -- The end line of the current verb is the line before the next verb starts - break -- No need to continue once the end line of the current verb is found + local r1, _, _ = current_node:start() + local r2, _, _ = current_node:end_() + + local hurl_file = current_node:parent() + + local current_node_idx = 1 + if hurl_file and hurl_file:type() == 'hurl_file' then + -- Find the current node index + for node in hurl_file:iter_children() do + if node:id() == current_node:id() then + break + end + current_node_idx = current_node_idx + 1 end + else + -- Parent node is not a hurl_file, HURL file must have errors. + local cursor_line = vim.api.nvim_win_get_cursor(0)[1] + return { + current = 0, + start_line = cursor_line, + end_line = cursor_line, + } end - end - if current_verb and current_index == next_entry then - -- If the current verb is the last one, the end line is the last line of the buffer - end_line = total_lines + return { + current = current_node_idx, + start_line = r1 + 1, + end_line = r2, + } end - - return { - current = current_index, - start_line = current_verb and current_verb.line_number or nil, - end_line = end_line, - } end -M.find_http_verb_positions_in_buffer = find_http_verb_positions_in_buffer -M.find_hurl_entry_positions_in_buffer = find_hurl_entry_positions_in_buffer - return M diff --git a/lua/hurl/main.lua b/lua/hurl/main.lua index a4f79c2..3cd0290 100644 --- a/lua/hurl/main.lua +++ b/lua/hurl/main.lua @@ -369,7 +369,7 @@ function M.setup() -- Run request to current entry if there is a HTTP method utils.create_cmd('HurlRunnerToEntry', function(opts) - local result = http.find_http_verb_positions_in_buffer() + local result = http.find_hurl_entry_positions_in_buffer() if result.current > 0 then opts.fargs = opts.fargs or {} opts.fargs = vim.list_extend(opts.fargs, { '--to-entry', result.current })