diff options
author | Christian Clason <c.clason@uni-graz.at> | 2022-12-17 13:43:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-17 13:43:46 +0100 |
commit | 1c4794944deb734b24b4a424c50b766a96e050dd (patch) | |
tree | e1ccf4ccce199b19e1b98d2c6b2cc179a1fa25c1 /runtime/lua/vim/lsp/semantic_tokens.lua | |
parent | d65684f0c755a9341412423543b65ec872196440 (diff) | |
parent | ef91146efcece1b6d97152251e7137d301146189 (diff) | |
download | rneovim-1c4794944deb734b24b4a424c50b766a96e050dd.tar.gz rneovim-1c4794944deb734b24b4a424c50b766a96e050dd.tar.bz2 rneovim-1c4794944deb734b24b4a424c50b766a96e050dd.zip |
Merge pull request #21393 from folke/highlight_show
feat(lsp): add function to get semantic tokens at cursor
feat: `vim.inspect_pos()`, `vim.show_pos()` and `:Inspect[!]`
Diffstat (limited to 'runtime/lua/vim/lsp/semantic_tokens.lua')
-rw-r--r-- | runtime/lua/vim/lsp/semantic_tokens.lua | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/runtime/lua/vim/lsp/semantic_tokens.lua b/runtime/lua/vim/lsp/semantic_tokens.lua index b7ffedab2b..e14d3e51cd 100644 --- a/runtime/lua/vim/lsp/semantic_tokens.lua +++ b/runtime/lua/vim/lsp/semantic_tokens.lua @@ -585,6 +585,51 @@ function M.stop(bufnr, client_id) end end +--- Return the semantic token(s) at the given position. +--- If called without arguments, returns the token under the cursor. +--- +---@param bufnr number|nil Buffer number (0 for current buffer, default) +---@param row number|nil Position row (default cursor position) +---@param col number|nil Position column (default cursor position) +--- +---@return table|nil (table|nil) List of tokens at position +function M.get_at_pos(bufnr, row, col) + if bufnr == nil or bufnr == 0 then + bufnr = api.nvim_get_current_buf() + end + + local highlighter = STHighlighter.active[bufnr] + if not highlighter then + return + end + + if row == nil or col == nil then + local cursor = api.nvim_win_get_cursor(0) + row, col = cursor[1] - 1, cursor[2] + end + + local tokens = {} + for client_id, client in pairs(highlighter.client_state) do + local highlights = client.current_result.highlights + if highlights then + local idx = binary_search(highlights, row) + for i = idx, #highlights do + local token = highlights[i] + + if token.line > row then + break + end + + if token.start_col <= col and token.end_col > col then + token.client_id = client_id + tokens[#tokens + 1] = token + end + end + end + end + return tokens +end + --- Force a refresh of all semantic tokens --- --- Only has an effect if the buffer is currently active for semantic token |