Skip to content

Commit 49c3d35

Browse files
committed
fix: Ignore Java annotations
1 parent 59f318a commit 49c3d35

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

lua/treesitter-context/context.lua

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -377,18 +377,19 @@ function M.get(winid)
377377
local range0 = context_range(parent, bufnr, query)
378378
if range0 and range_is_valid(range0) then
379379
local range, lines = get_text_for_range(range0, bufnr)
380-
if range_is_valid(range) then
381-
local last_context = context_ranges[#context_ranges]
382-
if last_context and parent_start_row == last_context[1] then
383-
-- If there are multiple contexts on the same row, then prefer the inner
384-
contexts_height = contexts_height - util.get_range_height(last_context)
385-
context_ranges[#context_ranges] = nil
386-
context_lines[#context_lines] = nil
380+
if range[1] < contexts_end_row then
381+
if range_is_valid(range) then
382+
local last_context = context_ranges[#context_ranges]
383+
if last_context and parent_start_row == last_context[1] then
384+
-- If there are multiple contexts on the same row, then prefer the inner
385+
contexts_height = contexts_height - util.get_range_height(last_context)
386+
context_ranges[#context_ranges] = nil
387+
context_lines[#context_lines] = nil
388+
end
389+
contexts_height = contexts_height + util.get_range_height(range)
390+
context_ranges[#context_ranges + 1] = range
391+
context_lines[#context_lines + 1] = lines
387392
end
388-
389-
contexts_height = contexts_height + util.get_range_height(range)
390-
context_ranges[#context_ranges + 1] = range
391-
context_lines[#context_lines + 1] = lines
392393
end
393394
end
394395
end

queries/java/context.scm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
) @context
44

55
(method_declaration
6+
type: (_) @context.start
7+
body: (_) @context.end
8+
) @context
9+
10+
(constructor_declaration
11+
name: (_) @context.start
612
body: (_) @context.end
713
) @context
814

@@ -15,6 +21,7 @@
1521
) @context
1622

1723
(class_declaration
24+
name: (_) @context.start
1825
body: (_) @context.end
1926
) @context
2027

test/lang/test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import stuff1;
44
import stuff2;
55

6-
@class_annot_1 // {{CONTEXT}}
7-
@class_annot_2 // {{CONTEXT}}
6+
@class_annot_1
7+
@class_annot_2
88
public class MyClass { // {{CONTEXT}}
99

1010

1111

1212
// {{CURSOR}}
13-
@method_annot_1 // {{CONTEXT}}
14-
@method_annot_2 // {{CONTEXT}}
13+
@method_annot_1
14+
@method_annot_2
1515
public void my_method(int param) { // {{CONTEXT}}
1616

1717

0 commit comments

Comments
 (0)