1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
local M = {}
---@class quicker.TSHighlight
---@field [1] integer start_col
---@field [2] integer end_col
---@field [3] string highlight group
local _cached_queries = {}
---@param lang string
---@return vim.treesitter.Query?
local function get_highlight_query(lang)
local query = _cached_queries[lang]
if query == nil then
query = vim.treesitter.query.get(lang, "highlights") or false
_cached_queries[lang] = query
end
if query then
return query
end
end
---@param bufnr integer
---@param lnum integer
---@return quicker.TSHighlight[]
function M.buf_get_ts_highlights(bufnr, lnum)
local filetype = vim.bo[bufnr].filetype
if not filetype or filetype == "" then
filetype = vim.filetype.match({ buf = bufnr }) or ""
end
local lang = vim.treesitter.language.get_lang(filetype) or filetype
if lang == "" then
return {}
end
local ok, parser = pcall(vim.treesitter.get_parser, bufnr, lang)
if not ok or not parser then
return {}
end
local row = lnum - 1
if not parser:is_valid() then
parser:parse(true)
end
local highlights = {}
parser:for_each_tree(function(tstree, tree)
if not tstree then
return
end
local root_node = tstree:root()
local root_start_row, _, root_end_row, _ = root_node:range()
-- Only worry about trees within the line range
if root_start_row > row or root_end_row < row then
return
end
local query = get_highlight_query(tree:lang())
-- Some injected languages may not have highlight queries.
if not query then
return
end
for capture, node, metadata in query:iter_captures(root_node, bufnr, row, root_end_row + 1) do
if capture == nil then
break
end
local range = vim.treesitter.get_range(node, bufnr, metadata[capture])
local start_row, start_col, _, end_row, end_col, _ = unpack(range)
if start_row > row then
break
end
local capture_name = query.captures[capture]
local hl = string.format("@%s.%s", capture_name, tree:lang())
if end_row > start_row then
end_col = -1
end
table.insert(highlights, { start_col, end_col, hl })
end
end)
return highlights
end
---@class quicker.LSPHighlight
---@field [1] integer start_col
---@field [2] integer end_col
---@field [3] string highlight group
---@field [4] integer priority modifier
-- We're accessing private APIs here. This could break in the future.
local STHighlighter = vim.lsp.semantic_tokens.__STHighlighter
--- Copied from Neovim semantic_tokens.lua
--- Do a binary search of the tokens in the half-open range [lo, hi).
---
--- Return the index i in range such that tokens[j].line < line for all j < i, and
--- tokens[j].line >= line for all j >= i, or return hi if no such index is found.
---
---@private
local function lower_bound(tokens, line, lo, hi)
while lo < hi do
local mid = bit.rshift(lo + hi, 1) -- Equivalent to floor((lo + hi) / 2).
if tokens[mid].line < line then
lo = mid + 1
else
hi = mid
end
end
return lo
end
---@param bufnr integer
---@param lnum integer
---@return quicker.LSPHighlight[]
function M.buf_get_lsp_highlights(bufnr, lnum)
local highlighter = STHighlighter.active[bufnr]
if not highlighter then
return {}
end
local ft = vim.bo[bufnr].filetype
local lsp_highlights = {}
for _, client in pairs(highlighter.client_state) do
local highlights = client.current_result.highlights
if highlights then
local idx = lower_bound(highlights, lnum - 1, 1, #highlights + 1)
for i = idx, #highlights do
local token = highlights[i]
if token.line >= lnum then
break
end
table.insert(
lsp_highlights,
{ token.start_col, token.end_col, string.format("@lsp.type.%s.%s", token.type, ft), 0 }
)
for modifier, _ in pairs(token.modifiers) do
table.insert(
lsp_highlights,
{ token.start_col, token.end_col, string.format("@lsp.mod.%s.%s", modifier, ft), 1 }
)
table.insert(lsp_highlights, {
token.start_col,
token.end_col,
string.format("@lsp.typemod.%s.%s.%s", token.type, modifier, ft),
2,
})
end
end
end
end
return lsp_highlights
end
---@param item QuickFixItem
---@param line string
---@return quicker.TSHighlight[]
M.get_heuristic_ts_highlights = function(item, line)
local filetype = vim.filetype.match({ buf = item.bufnr })
if not filetype then
return {}
end
local lang = vim.treesitter.language.get_lang(filetype)
if not lang then
return {}
end
local has_parser, parser = pcall(vim.treesitter.get_string_parser, line, lang)
if not has_parser then
return {}
end
local root = parser:parse(true)[1]:root()
local query = vim.treesitter.query.get(lang, "highlights")
if not query then
return {}
end
local highlights = {}
for capture, node, metadata in query:iter_captures(root, line) do
if capture == nil then
break
end
local range = vim.treesitter.get_range(node, line, metadata[capture])
local start_row, start_col, _, end_row, end_col, _ = unpack(range)
local capture_name = query.captures[capture]
local hl = string.format("@%s.%s", capture_name, lang)
if end_row > start_row then
end_col = -1
end
table.insert(highlights, { start_col, end_col, hl })
end
return highlights
end
function M.set_highlight_groups()
if vim.tbl_isempty(vim.api.nvim_get_hl(0, { name = "QuickFixHeaderHard" })) then
vim.api.nvim_set_hl(0, "QuickFixHeaderHard", { link = "Delimiter", default = true })
end
if vim.tbl_isempty(vim.api.nvim_get_hl(0, { name = "QuickFixHeaderSoft" })) then
vim.api.nvim_set_hl(0, "QuickFixHeaderSoft", { link = "Comment", default = true })
end
if vim.tbl_isempty(vim.api.nvim_get_hl(0, { name = "QuickFixFilename" })) then
vim.api.nvim_set_hl(0, "QuickFixFilename", { link = "Directory", default = true })
end
if vim.tbl_isempty(vim.api.nvim_get_hl(0, { name = "QuickFixFilenameInvalid" })) then
vim.api.nvim_set_hl(0, "QuickFixFilenameInvalid", { link = "Comment", default = true })
end
if vim.tbl_isempty(vim.api.nvim_get_hl(0, { name = "QuickFixLineNr" })) then
vim.api.nvim_set_hl(0, "QuickFixLineNr", { link = "LineNr", default = true })
end
end
return M
|