Skip to content

Commit 2b691a6

Browse files
Danilo Verde RibeiroDanilo Verde Ribeiro
authored andcommitted
fix: add caching to context.load(), debounce WinLeave autocmd, fix types for LSP
1 parent 9e8ebb5 commit 2b691a6

3 files changed

Lines changed: 40 additions & 11 deletions

File tree

lua/opencode/context.lua

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ local state = require('opencode.state')
66

77
local M = {}
88

9+
local cache = { timestamp = 0, last_changedtick = 0, data = nil }
10+
911
local cwd = vim.fn.getcwd()
1012

1113
local function is_in_cwd(path)
@@ -81,6 +83,15 @@ function M.unload_attachments()
8183
end
8284

8385
function M.load()
86+
local now = vim.uv.now()
87+
local current_buf = vim.api.nvim_get_current_buf()
88+
local current_changedtick = vim.b[current_buf].changedtick or 0
89+
if cache.timestamp > 0 and now - cache.timestamp < 500 and current_changedtick == cache.last_changedtick then
90+
if cache.data then
91+
M.context = vim.deepcopy(cache.data)
92+
end
93+
return
94+
end
8495
if util.is_current_buf_a_file() then
8596
local current_file = M.get_current_file()
8697
local cursor_data = M.get_current_cursor_data()
@@ -117,6 +128,9 @@ function M.load()
117128
M.context.macros = M.get_macros()
118129
M.context.terminal_buffers = M.get_terminal_buffers()
119130
M.context.session_duration = M.get_session_duration()
131+
cache.timestamp = now
132+
cache.data = vim.deepcopy(M.context)
133+
cache.last_changedtick = current_changedtick
120134
end
121135

122136
function M.check_linter_errors()
@@ -740,7 +754,7 @@ function M.get_lsp_context()
740754
local result = {}
741755

742756
-- Get diagnostics with more details
743-
local diagnostics = vim.diagnostic.get(bufnr)
757+
local diagnostics = vim.diagnostic.get(bufnr) or {}
744758
local limit = config.context.lsp_context.diagnostics_limit or 10
745759

746760
result.diagnostics = {}

lua/opencode/types.lua

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,7 @@
7979
---@field next_prompt_history string
8080
---@field switch_mode string
8181
---@field focus_input string
82-
---@field select_child_session string
83-
---@field debug_message string
84-
---@field debug_output string
85-
---@field debug_session string
86-
---@field debug_message string
87-
---@field debug_output string
88-
---@field debug_session string
82+
---@field select_child_session string\n---@field debug_message string\n---@field debug_output string\n---@field debug_session string
8983
---@class OpencodeKeymap
9084
---@field global OpencodeKeymapGlobal
9185
---@field window OpencodeKeymapWindow
@@ -118,14 +112,13 @@
118112
---@class OpencodeContextConfig
119113
---@field enabled boolean
120114
---@field plugin_versions { enabled: boolean, limit: number }
121-
---@field plugin_versions { enabled: boolean, limit: number }
122115
---@field cursor_data { enabled: boolean }
123116
---@field diagnostics { info: boolean, warning: boolean, error: boolean }
124-
---@field current_file { enabled: boolean }
117+
---@field current_file { enabled: boolean, show_full_path: boolean }
125118
---@field selection { enabled: boolean }
126119
---@field marks { enabled: boolean, limit: number }
127120
---@field jumplist { enabled: boolean, limit: number }
128-
---@field recent_buffers { enabled: boolean, limit: number }
121+
---@field recent_buffers { enabled: boolean, limit: number, symbols_only: boolean }
129122
---@field undo_history { enabled: boolean, limit: number }
130123
---@field windows_tabs { enabled: boolean }
131124
---@field highlights { enabled: boolean }
@@ -149,6 +142,21 @@
149142
--- @class OpencodeProviders
150143
--- @field [string] string[]
151144

145+
---@class OpencodeConfigModule
146+
---@field defaults OpencodeConfig
147+
---@field values OpencodeConfig
148+
---@field setup fun(opts?: OpencodeConfig): nil
149+
---@overload fun(key: nil): OpencodeConfig
150+
---@overload fun(key: "preferred_picker"): 'mini.pick' | 'telescope' | 'fzf' | 'snacks' | nil
151+
---@overload fun(key: "preferred_completion"): 'blink' | 'nvim-cmp' | 'vim_complete' | nil
152+
---@overload fun(key: "default_mode"): 'build' | 'plan'
153+
---@overload fun(key: "default_global_keymaps"): boolean
154+
---@overload fun(key: "keymap"): OpencodeKeymap
155+
---@overload fun(key: "ui"): OpencodeUIConfig
156+
---@overload fun(key: "providers"): OpencodeProviders
157+
---@overload fun(key: "context"): OpencodeContextConfig
158+
---@overload fun(key: "debug"): OpencodeDebugConfig
159+
152160
---@class OpencodeConfig
153161
---@field preferred_picker 'telescope' | 'fzf' | 'mini.pick' | 'snacks' | nil
154162
---@field preferred_completion 'blink' | 'nvim-cmp' | 'vim_complete' | nil -- Preferred completion strategy for mentons and commands

lua/opencode/ui/autocmds.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ local input_window = require('opencode.ui.input_window')
22
local output_window = require('opencode.ui.output_window')
33
local M = {}
44

5+
local last_update = 0
6+
57
function M.setup_autocmds(windows)
68
local group = vim.api.nvim_create_augroup('OpencodeWindows', { clear = true })
79
input_window.setup_autocmds(windows, group)
@@ -26,6 +28,11 @@ function M.setup_autocmds(windows)
2628
group = group,
2729
pattern = '*',
2830
callback = function()
31+
local now = vim.uv.now()
32+
if now - last_update < 1000 then
33+
return
34+
end
35+
last_update = now
2936
if not require('opencode.ui.ui').is_opencode_focused() then
3037
require('opencode.context').load()
3138
require('opencode.state').last_code_win_before_opencode = vim.api.nvim_get_current_win()

0 commit comments

Comments
 (0)