Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions lua/treesitter-context/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -377,18 +377,19 @@ function M.get(winid)
local range0 = context_range(parent, bufnr, query)
if range0 and range_is_valid(range0) then
local range, lines = get_text_for_range(range0, bufnr)
if range_is_valid(range) then
local last_context = context_ranges[#context_ranges]
if last_context and parent_start_row == last_context[1] then
-- If there are multiple contexts on the same row, then prefer the inner
contexts_height = contexts_height - util.get_range_height(last_context)
context_ranges[#context_ranges] = nil
context_lines[#context_lines] = nil
if range[1] < contexts_end_row then
if range_is_valid(range) then
Comment on lines 377 to +381
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_text_for_range() calls nvim_buf_get_text, but the new visibility guard (range[1] < contexts_end_row) can skip adding the context afterwards. Since the start row doesn’t change between range0 and range, consider checking range0[1] < contexts_end_row (and/or range_is_valid(range0)) before calling get_text_for_range() to avoid unnecessary buffer reads when @context.start is still in view (e.g., hidden annotations/attributes).

Copilot uses AI. Check for mistakes.
local last_context = context_ranges[#context_ranges]
if last_context and parent_start_row == last_context[1] then
-- If there are multiple contexts on the same row, then prefer the inner
contexts_height = contexts_height - util.get_range_height(last_context)
context_ranges[#context_ranges] = nil
context_lines[#context_lines] = nil
end
contexts_height = contexts_height + util.get_range_height(range)
context_ranges[#context_ranges + 1] = range
context_lines[#context_lines + 1] = lines
end

contexts_height = contexts_height + util.get_range_height(range)
context_ranges[#context_ranges + 1] = range
context_lines[#context_lines + 1] = lines
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions queries/java/context.scm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
) @context

(method_declaration
type: (_) @context.start
body: (_) @context.end
) @context

(constructor_declaration
name: (_) @context.start
body: (_) @context.end
) @context
Comment on lines +10 to 13
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds support for constructor_declaration contexts, but there’s no corresponding fixture coverage in test/lang/test.java to ensure constructors are correctly detected (and that annotations on constructors are ignored as intended). Adding a small constructor example with // {{CONTEXT}}/// {{CURSOR}} markers would prevent regressions.

Copilot uses AI. Check for mistakes.

Expand All @@ -15,6 +21,7 @@
) @context

(class_declaration
name: (_) @context.start
body: (_) @context.end
) @context

Expand Down
8 changes: 4 additions & 4 deletions test/lang/test.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import stuff1;
import stuff2;

@class_annot_1 // {{CONTEXT}}
@class_annot_2 // {{CONTEXT}}
@class_annot_1
@class_annot_2
public class MyClass { // {{CONTEXT}}



// {{CURSOR}}
@method_annot_1 // {{CONTEXT}}
@method_annot_2 // {{CONTEXT}}
@method_annot_1
@method_annot_2
public void my_method(int param) { // {{CONTEXT}}


Expand Down
Loading