@@ -2,11 +2,13 @@ local api = vim.api
2
2
local bit = require (' bit' )
3
3
local ms = require (' vim.lsp.protocol' ).Methods
4
4
local util = require (' vim.lsp.util' )
5
+ local Range = require (' vim.treesitter._range' )
5
6
local uv = vim .uv
6
7
7
8
--- @class (private ) STTokenRange
8
9
--- @field line integer line number 0-based
9
10
--- @field start_col integer start column 0-based
11
+ --- @field end_line integer end line number 0-based
10
12
--- @field end_col integer end column 0-based
11
13
--- @field type string token type as string
12
14
--- @field modifiers table<string,boolean> token modifiers as a set. E.g. , { static = true , readonly = true }
@@ -44,7 +46,7 @@ local STHighlighter = { active = {} }
44
46
local function lower_bound (tokens , line , lo , hi )
45
47
while lo < hi do
46
48
local mid = bit .rshift (lo + hi , 1 ) -- Equivalent to floor((lo + hi) / 2).
47
- if tokens [mid ].line < line then
49
+ if tokens [mid ].end_line < line then
48
50
lo = mid + 1
49
51
else
50
52
hi = mid
@@ -102,6 +104,8 @@ local function tokens_to_ranges(data, bufnr, client, request)
102
104
local token_modifiers = legend .tokenModifiers
103
105
local encoding = client .offset_encoding
104
106
local lines = api .nvim_buf_get_lines (bufnr , 0 , - 1 , false )
107
+ -- For all encodings, \r\n takes up two code points, and \n (or \r) takes up one.
108
+ local eol_offset = vim .bo .fileformat [bufnr ] == ' dos' and 2 or 1
105
109
local ranges = {} --- @type STTokenRange[]
106
110
107
111
local start = uv .hrtime ()
@@ -141,11 +145,23 @@ local function tokens_to_ranges(data, bufnr, client, request)
141
145
if token_type then
142
146
local modifiers = modifiers_from_number (data [i + 4 ], token_modifiers )
143
147
local end_char = start_char + data [i + 2 ] --- @type integer LuaLS bug
144
- local buf_line = lines and lines [line + 1 ] or ' '
148
+ local buf_line = lines [line + 1 ] or ' '
149
+ local end_line = line --- @type integer
145
150
local start_col = vim .str_byteindex (buf_line , encoding , start_char , false )
146
151
local end_col = vim .str_byteindex (buf_line , encoding , end_char , false )
152
+
153
+ end_char = end_char - vim .str_utfindex (buf_line , encoding ) - eol_offset
154
+ -- While end_char goes past the given line, extend the token range to the next line
155
+ while end_char > 0 do
156
+ end_line = end_line + 1
157
+ buf_line = lines [end_line + 1 ] or ' '
158
+ end_col = vim .str_byteindex (buf_line , encoding , end_char , false )
159
+ end_char = end_char - vim .str_utfindex (buf_line , encoding ) - eol_offset
160
+ end
161
+
147
162
ranges [# ranges + 1 ] = {
148
163
line = line ,
164
+ end_line = end_line ,
149
165
start_col = start_col ,
150
166
end_col = end_col ,
151
167
type = token_type ,
398
414
local function set_mark (bufnr , ns , token , hl_group , priority )
399
415
vim .api .nvim_buf_set_extmark (bufnr , ns , token .line , token .start_col , {
400
416
hl_group = hl_group ,
417
+ end_line = token .end_line ,
401
418
end_col = token .end_col ,
402
419
priority = priority ,
403
420
strict = false ,
692
709
--- the following fields:
693
710
--- - line (integer) line number, 0-based
694
711
--- - start_col (integer) start column, 0-based
712
+ --- - end_line (integer) end line number, 0-based
695
713
--- - end_col (integer) end column, 0-based
696
714
--- - type (string) token type as string, e.g. "variable"
697
715
--- - modifiers (table) token modifiers as a set. E.g., { static = true, readonly = true }
@@ -709,6 +727,8 @@ function M.get_at_pos(bufnr, row, col)
709
727
row , col = cursor [1 ] - 1 , cursor [2 ]
710
728
end
711
729
730
+ local position = { row , col , row , col }
731
+
712
732
local tokens = {} --- @type STTokenRangeInspect[]
713
733
for client_id , client in pairs (highlighter .client_state ) do
714
734
local highlights = client .current_result .highlights
@@ -722,7 +742,9 @@ function M.get_at_pos(bufnr, row, col)
722
742
break
723
743
end
724
744
725
- if token .start_col <= col and token .end_col > col then
745
+ if
746
+ Range .contains ({ token .line , token .start_col , token .end_line , token .end_col }, position )
747
+ then
726
748
token .client_id = client_id
727
749
tokens [# tokens + 1 ] = token
728
750
end
0 commit comments