generated from ellisonleao/nvim-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 6
feat: action block highlight and jump #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d455e89
feat: add action highlight
qvalentin 97da0c4
feat: jump between action block start/end
qvalentin 005655c
fix: support multiple if else
qvalentin 48aa3c0
chore: cleanup
qvalentin f230b19
chore: mr comments
qvalentin 6d6f49d
docs: clarify action types
qvalentin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,2 +1,10 @@ | ||
| -- set up the gotmpl commentstring | ||
| vim.opt_local.commentstring = "{{/* %s */}}" | ||
|
|
||
| vim.keymap.set("n", "%", function() | ||
| local jumped = require("helm-ls.matchparen").jump_to_matching_keyword() | ||
| if not jumped then | ||
| -- Fallback to default % behavior | ||
| vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("%", true, false, true), "n", false) | ||
| end | ||
| end, { buffer = true, noremap = true, silent = true, desc = "Jump to matching keyword" }) |
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| ---@class ActionHighlightModule | ||
| local M = {} | ||
|
|
||
| local queries = require("helm-ls.queries") | ||
|
|
||
| local ns_id = vim.api.nvim_create_namespace("helm-ls-action-highlight") | ||
|
|
||
| local function highlight_node(bufnr, node) | ||
| if not node then | ||
| return | ||
| end | ||
| local start_row, start_col, end_row, end_col = node:range() | ||
| vim.api.nvim_buf_set_extmark(bufnr, ns_id, start_row, start_col, { | ||
| end_row = end_row, | ||
| end_col = end_col, | ||
| hl_group = "Visual", | ||
| }) | ||
| end | ||
|
|
||
| local function highlight_keywords() | ||
| local bufnr = vim.api.nvim_get_current_buf() | ||
| vim.api.nvim_buf_clear_namespace(bufnr, ns_id, 0, -1) | ||
|
|
||
| local parser = vim.treesitter.get_parser(bufnr, "helm") | ||
| if not parser then | ||
| return | ||
| end | ||
|
|
||
| -- Make sure tree is parsed. parse() is idempotent. | ||
| parser:parse() | ||
|
|
||
| local cursor_node = vim.treesitter.get_node({ bufnr = bufnr, include_anonymous = true }) | ||
| if not cursor_node then | ||
| return | ||
| end | ||
qvalentin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| -- 1. Find the containing action node by traversing up from cursor | ||
| local action_node | ||
| local current_node = cursor_node | ||
| local action_types = { "range_action", "if_action", "with_action", "define_action", "block_action" } | ||
| while current_node do | ||
| if vim.tbl_contains(action_types, current_node:type()) then | ||
| action_node = current_node | ||
| break | ||
| end | ||
| current_node = current_node:parent() | ||
| end | ||
|
|
||
| if not action_node then | ||
| return -- No action found at cursor | ||
| end | ||
|
|
||
| -- 2. Find parts within that action node and highlight them | ||
| local parts_query = vim.treesitter.query.parse("helm", queries.action_parts) | ||
| if not parts_query then | ||
| return | ||
| end | ||
|
|
||
| -- Get the visible range of lines in the current window | ||
| local start_line = vim.fn.line("w0") - 1 | ||
| local end_line = vim.fn.line("w$") - 1 | ||
|
|
||
| for id, node_to_highlight in parts_query:iter_captures(action_node, bufnr, start_line, end_line) do | ||
| local is_nested = false | ||
| local parent = node_to_highlight:parent() | ||
| -- Check if the capture is inside a nested action block | ||
| while parent and parent:id() ~= action_node:id() do | ||
| if vim.tbl_contains(action_types, parent:type()) then | ||
| is_nested = true | ||
| break | ||
| end | ||
| parent = parent:parent() | ||
| end | ||
|
|
||
| if not is_nested then | ||
| highlight_node(bufnr, node_to_highlight) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| function M.setup(config) | ||
| -- Not needed for now | ||
| end | ||
|
|
||
| function M.highlight_current_block() | ||
| highlight_keywords() | ||
| end | ||
|
|
||
| return M | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| ---@class MatchParenModule | ||
| local M = {} | ||
|
|
||
| local queries = require("helm-ls.queries") | ||
|
|
||
| local function is_cursor_on_node(cursor_row, cursor_col, node) | ||
| local start_row, start_col, _, end_col = node:range() | ||
| if cursor_row == start_row and cursor_col >= start_col and cursor_col < end_col then | ||
| return true | ||
| end | ||
| return false | ||
| end | ||
qvalentin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| function M.jump_to_matching_keyword() | ||
| local bufnr = vim.api.nvim_get_current_buf() | ||
| local parser = vim.treesitter.get_parser(bufnr, "helm") | ||
| if not parser then | ||
| return false | ||
| end | ||
| parser:parse() | ||
|
|
||
qvalentin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| local cursor_row, cursor_col = unpack(vim.api.nvim_win_get_cursor(0)) | ||
| cursor_row = cursor_row - 1 | ||
|
|
||
| local cursor_node = vim.treesitter.get_node({ bufnr = bufnr, include_anonymous = true }) | ||
| if not cursor_node then | ||
| return false | ||
| end | ||
|
|
||
qvalentin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| -- 1. Find the containing action node | ||
| local action_node | ||
| local current_node = cursor_node | ||
| local action_types = { "range_action", "if_action", "with_action", "define_action", "block_action" } | ||
| while current_node do | ||
| if vim.tbl_contains(action_types, current_node:type()) then | ||
| action_node = current_node | ||
| break | ||
| end | ||
| current_node = current_node:parent() | ||
| end | ||
|
|
||
| if not action_node then | ||
| return false | ||
| end | ||
|
|
||
| -- 2. Collect parts and identify node under cursor in a single pass | ||
| local parts_query = vim.treesitter.query.parse("helm", queries.action_parts) | ||
| if not parts_query then | ||
| return false | ||
| end | ||
|
|
||
qvalentin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| local start_nodes, middle_nodes, end_nodes = {}, {}, {} | ||
| local cursor_on_node, cursor_on_node_type | ||
|
|
||
| local start_row, _, end_row, _ = action_node:range() | ||
| for id, node in parts_query:iter_captures(action_node, bufnr, start_row, end_row + 1) do | ||
| local is_nested = false | ||
| local parent = node:parent() | ||
| while parent and parent:id() ~= action_node:id() do | ||
| if vim.tbl_contains(action_types, parent:type()) then | ||
| is_nested = true | ||
| break | ||
| end | ||
| parent = parent:parent() | ||
| end | ||
|
|
||
| if not is_nested then | ||
| local capture_name = parts_query.captures[id] | ||
| if is_cursor_on_node(cursor_row, cursor_col, node) then | ||
| cursor_on_node = node | ||
| cursor_on_node_type = capture_name | ||
| end | ||
| if capture_name == "start" then | ||
| table.insert(start_nodes, node) | ||
| elseif capture_name == "middle" then | ||
| table.insert(middle_nodes, node) | ||
| elseif capture_name == "end" then | ||
| table.insert(end_nodes, node) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| if not cursor_on_node then | ||
| return false | ||
| end | ||
|
|
||
| -- Sort nodes by position | ||
| local function by_pos(a, b) | ||
| local ar, ac = a:start() | ||
| local br, bc = b:start() | ||
| return ar == br and ac < bc or ar < br | ||
| end | ||
| table.sort(start_nodes, by_pos) | ||
| table.sort(middle_nodes, by_pos) | ||
| table.sort(end_nodes, by_pos) | ||
|
|
||
| -- 3. Jump based on the type of node under the cursor | ||
| local function jump_to(node) | ||
| if not node then | ||
| return false | ||
| end | ||
| local r, c = node:start() | ||
| vim.api.nvim_win_set_cursor(0, { r + 1, c }) | ||
| return true | ||
| end | ||
|
|
||
| if cursor_on_node_type == "start" then | ||
| return jump_to(middle_nodes[1]) or jump_to(end_nodes[1]) | ||
| elseif cursor_on_node_type == "middle" then | ||
| -- To find the next middle node, we must find the index of the current one | ||
| for i, node in ipairs(middle_nodes) do | ||
| if node:id() == cursor_on_node:id() then | ||
| return jump_to(middle_nodes[i + 1]) or jump_to(end_nodes[1]) | ||
| end | ||
| end | ||
| elseif cursor_on_node_type == "end" then | ||
| return jump_to(start_nodes[1]) | ||
| end | ||
|
|
||
| return false | ||
| end | ||
|
|
||
| return M | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| local M = {} | ||
|
|
||
| M.action_parts = [[ | ||
| ("range" @start) | ||
| ("if" @start) | ||
| ("with" @start) | ||
| ("define" @start) | ||
| ("block" @start) | ||
| (["else" "else if"] @middle) | ||
| ("end" @end) | ||
| ]] | ||
|
|
||
| return M |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.