Last active
February 10, 2023 08:27
-
-
Save qRoC/288727caed4aa361ef5f15fa7852ca3e to your computer and use it in GitHub Desktop.
semantic tokens nvim 0.9
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
-- https://github.com/neovim/neovim/pull/21100/files#diff-f11809d8946cebd21b6bec568546afe4589930ac9c6e89f49e7f398adc574c90R39 | |
local function binary_search(tokens, line) | |
local lo = 1 | |
local hi = #tokens | |
while lo < hi do | |
local mid = math.floor((lo + hi) / 2) | |
if tokens[mid].line < line then | |
lo = mid + 1 | |
else | |
hi = mid | |
end | |
end | |
return lo | |
end | |
M.init = function() | |
vim.api.nvim_set_decoration_provider(vim.api.nvim_get_namespaces()["vim_lsp_semantic_tokens"], { | |
-- https://github.com/neovim/neovim/pull/21100/files#diff-f11809d8946cebd21b6bec568546afe4589930ac9c6e89f49e7f398adc574c90R359 | |
on_win = function(_, _, bufnr, topline, botline) | |
local highlighter = vim.lsp.semantic_tokens.__STHighlighter.active[bufnr] | |
if highlighter then | |
local ft = vim.api.nvim_buf_get_option(bufnr, "filetype") | |
for _, state in pairs(highlighter.client_state) do | |
local current_result = state.current_result | |
if current_result.version and current_result.version == vim.lsp.util.buf_versions[bufnr] then | |
if not current_result.namespace_cleared then | |
vim.api.nvim_buf_clear_namespace(bufnr, state.namespace, 0, -1) | |
current_result.namespace_cleared = true | |
end | |
local highlights = current_result.highlights | |
local idx = binary_search(highlights, topline) | |
for i = idx, #highlights do | |
local token = highlights[i] | |
if token.line > botline then | |
break | |
end | |
if not token.__hl_groups then | |
-- `hl_groups` already used in `vim.inspect_pos`. | |
token.__hl_groups = { "@" .. token.type .. "." .. ft } | |
for _, modifier in pairs(token.modifiers) do | |
table.insert(token.__hl_groups, "@_" .. modifier .. "." .. token.type .. "." .. ft) | |
end | |
for _, hl_group in ipairs(token.__hl_groups) do | |
vim.api.nvim_buf_set_extmark(bufnr, state.namespace, token.line, token.start_col, { | |
hl_group = hl_group, | |
end_col = token.end_col, | |
priority = vim.highlight.priorities.semantic_tokens, | |
strict = false, | |
}) | |
end | |
token.extmark_added = true | |
end | |
end | |
end | |
end | |
end | |
end, | |
}) | |
end |
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
-- vs code mapping | |
return { | |
-- default, not exists in TS | |
["@regexp"] = "@string.regex", | |
["@class"] = "@type", | |
["@interface"] = "@type", | |
["@enum"] = "@type", | |
["@struct"] = "@structure", | |
["@typeParameter"] = "@type", | |
["@member"] = "@method", | |
["@enumMember"] = "@constant", | |
["@event"] = "@variable", | |
["@decorator"] = "@attribute", | |
["@modifier"] = "@keyword", | |
-- Rust: Style for builtin attributes | |
["@builtinAttribute.rust"] = "@attribute", | |
-- Rust: '#' symbol in attributes | |
["@attributeBracket.rust"] = "@attribute", | |
-- Rust: Style for builtin types | |
["@builtinType.rust"] = "@type.builtin", | |
-- Rust: Style for char escapes in strings | |
["@escapeSequence.rust"] = "@string.escape", | |
-- Rust: Style for {} placeholders in format strings | |
["@formatSpecifier.rust"] = "@operator", | |
-- Rust: Style for the self keyword | |
["@selfKeyword.rust"] = "@keyword", | |
-- Rust: Style for the self type keyword | |
["@selfTypeKeyword.rust"] = "@keyword", | |
-- modifiers | |
["@_readonly.variable"] = "@constant", | |
["@_readonly.property"] = "@constant", | |
["@_constant.variable"] = "@constant", | |
["@_constant.property"] = "@constant", | |
["@_defaultLibrary.typeAlias"] = "@type.builtin", | |
["@_defaultLibrary.type"] = "@type.builtin", | |
["@_defaultLibrary.class"] = "@type.builtin", | |
["@_defaultLibrary.interface"] = "@type.builtin", | |
["@_defaultLibrary.variable"] = "@type.builtin", | |
["@_defaultLibrary.property"] = "@type.builtin", | |
["@_defaultLibrary.enum"] = "@type.builtin", | |
["@_defaultLibrary.function"] = "@type.builtin", | |
["@_defaultLibrary.method"] = "@type.builtin", | |
["@_defaultLibrary.member"] = "@type.builtin", | |
-- Rust: Style for mutable locals and statics as well as functions taking `&mut self` | |
["@_mutable.parameter.rust"] = "@text.underline", | |
["@_mutable.selfKeyword.rust"] = "@text.underline", | |
["@_mutable.method.rust"] = "@text.underline", | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment