Skip to content

Commit

Permalink
fix: refresh context on diagnostics and semantic tokens
Browse files Browse the repository at this point in the history
Fixes: #542, #509
  • Loading branch information
lewis6991 committed Dec 20, 2024
1 parent f6c99b6 commit 2bcf700
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions lua/treesitter-context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ local function throttle_by_id(f, ms)
local state = {} --- @type table<any,string>
local waiting = {} --- @type table<any,boolean>

local schedule = function(id, wrapper)
local function schedule(id, wrapper)
state[id] = 'scheduled'
vim.schedule_wrap(wrapper)(id, wrapper)
end

local on_throttle_finish = function(id, wrapper)
local function on_throttle_finish(id, wrapper)
assert(state[id] == 'throttled')
if waiting[id] == nil then
timers[id] = nil
Expand All @@ -37,7 +37,7 @@ local function throttle_by_id(f, ms)
schedule(id, wrapper)
end

local wrapper = function(id, wrapper)
local function wrapper(id, self)
assert(state[id] == 'scheduled')
state[id] = 'running'
f(id)
Expand All @@ -48,7 +48,7 @@ local function throttle_by_id(f, ms)
timers[id] = assert(vim.loop.new_timer())
end
timers[id]:start(ms, 0, function()
on_throttle_finish(id, wrapper)
on_throttle_finish(id, self)
end)
end

Expand Down Expand Up @@ -158,7 +158,7 @@ local M = {
local group = augroup('treesitter_context_update', {})

--- @param event string|string[]
--- @param callback fun(args: table)
--- @param callback fun(args: vim.api.keyset.create_autocmd.callback_args):boolean?
--- @param opts? vim.api.keyset.create_autocmd
local function autocmd(event, callback, opts)
opts = opts or {}
Expand All @@ -176,6 +176,18 @@ local function should_attach(bufnr)
return nil
end

--- @param req { type:string, method: string }
--- @return boolean
local function is_semantic_tokens_request(req)
local ms = require('vim.lsp.protocol').Methods
return req.type == 'complete'
and (
req.method == ms.textDocument_semanticTokens_full
or req.method == ms.textDocument_semanticTokens_full_delta
or req.method == ms.textDocument_semanticTokens_range
)
end

function M.enable()
if enabled then
-- Some options may have changed.
Expand All @@ -195,7 +207,6 @@ function M.enable()
'BufEnter',
'WinEnter',
'VimResized',
'DiagnosticChanged',
'CursorMoved',
'OptionSet',
}
Expand All @@ -206,6 +217,8 @@ function M.enable()

autocmd(update_events, update)

autocmd('DiagnosticChanged', vim.schedule_wrap(update))

autocmd('BufReadPost', function(args)
attached[args.buf] = should_attach(args.buf)
end)
Expand All @@ -223,6 +236,14 @@ function M.enable()
autocmd('User', close, { pattern = 'SessionSavePre' })
autocmd('User', update, { pattern = 'SessionSavePost' })

autocmd('LspRequest', function(args)
if is_semantic_tokens_request(args.data.request) then
vim.schedule(function()
update(args)
end)
end
end)

if config.multiwindow then
for _, winid in pairs(api.nvim_list_wins()) do
update_single_context(winid)
Expand Down

0 comments on commit 2bcf700

Please sign in to comment.