generated from nvimdev/nvim-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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] <github-actions[bot]@users.noreply.github.com> 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] <github-actions[bot]@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
a6bae81
commit 8885e2f
Showing
2 changed files
with
36 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters