Skip to content

Commit 753c056

Browse files
committed
Move doc autocmds to enable_autocmds()
1 parent f8a51ca commit 753c056

File tree

5 files changed

+51
-65
lines changed

5 files changed

+51
-65
lines changed

lua/neocodeium/_meta.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
---Decoded json table
1414
---Encoded json string
1515
---@alias json_str string
16-
---Represenation of the url string, must start with http:// or https://.
16+
---Represenation of the url string, must start with http:// https:// or file://
1717
---@alias url string
1818

1919
---@alias extmark_id integer

lua/neocodeium/chat.lua

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ local options = require("neocodeium.options").options
44
local server = require("neocodeium.server")
55
local doc = require("neocodeium.doc")
66
local log = require("neocodeium.log")
7-
local stdio = require("neocodeium.utils.stdio")
87
local utils = require("neocodeium.utils")
98
local state = require("neocodeium.state")
109
local STATUS = require("neocodeium.enums").STATUS
@@ -72,6 +71,7 @@ function chat.launch(response)
7271
end)
7372
end
7473

74+
--- XXX: should it change workspace?
7575
---Sends a request to the server to refresh context.
7676
function chat.refresh_context()
7777
if state:get_status() == STATUS.enabled then
@@ -84,12 +84,7 @@ end
8484

8585
---Sends a request to the server to add a tracked workspace.
8686
function chat.add_tracked_workspace()
87-
local root = stdio.get_project_root()
88-
if root then
89-
server:request("AddTrackedWorkspace", { workspace = root })
90-
else
91-
log.error("Project root not found. Unable to add tracked workspace.", { type = log.BOTH })
92-
end
87+
server:request("AddTrackedWorkspace", { workspace = state.project_root })
9388
end
9489

9590
return chat

lua/neocodeium/doc.lua

Lines changed: 10 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,14 @@ local nvim_buf_get_lines = vim.api.nvim_buf_get_lines
1111
local nvim_buf_get_name = vim.api.nvim_buf_get_name
1212
local nvim_get_option_value = vim.api.nvim_get_option_value
1313
local nvim_buf_get_var = vim.api.nvim_buf_get_var
14-
local nvim_create_augroup = vim.api.nvim_create_augroup
15-
local nvim_create_autocmd = vim.api.nvim_create_autocmd
16-
17-
local uv = vim.uv
18-
19-
-- Cache --------------------------------------------------- {{{1
20-
21-
---Persistent cache of the buffers data.
22-
---@type table<bufnr, { data: document, tick: integer }>
23-
local cached_data = {}
24-
25-
local prev_cwd = uv.cwd()
26-
local project_root, project_root_uri
27-
28-
local function update_project_root()
29-
project_root = stdio.get_project_root()
30-
project_root_uri = stdio.to_uri(project_root)
31-
end
32-
33-
update_project_root()
34-
35-
local augroup = nvim_create_augroup("neocodeium_docs", {})
36-
37-
---Autocmd to clear docs cache.
38-
nvim_create_autocmd("BufUnload", {
39-
group = augroup,
40-
desc = "Clear documents cache on buffer unload",
41-
callback = function(args)
42-
cached_data[args.buf] = nil
43-
end,
44-
})
45-
46-
---Autocmd to update project's root directory.
47-
nvim_create_autocmd("DirChanged", {
48-
group = augroup,
49-
desc = "Update project's root directory",
50-
callback = function()
51-
update_project_root()
52-
end,
53-
})
54-
55-
---Autocmd to update project's root directory.
56-
nvim_create_autocmd("WinEnter", {
57-
group = augroup,
58-
desc = "Update project's root directory",
59-
callback = function()
60-
if uv.cwd() ~= prev_cwd then
61-
update_project_root()
62-
end
63-
end,
64-
})
6514

6615
-- Public API ---------------------------------------------- {{{1
6716

68-
local M = {}
17+
---@alias docdata table<bufnr, { data: document, tick: integer }>
18+
19+
---@class Doc
20+
---@field cached_data docdata # Persistent cache of the buffers data.
21+
local M = { cached_data = {} }
6922

7023
---Returns document data.
7124
---@param buf bufnr
@@ -90,7 +43,7 @@ function M.get(buf, ft, max_lines, pos)
9043
language = filetype.language[lang] or filetype.language.unspecified,
9144
cursor_position = { row = pos[1], col = pos[2] },
9245
absolute_uri = stdio.to_uri(name),
93-
workspace_uri = project_root_uri,
46+
workspace_uri = state.project_root_uri,
9447
line_ending = "\n",
9548
}
9649
end
@@ -113,20 +66,20 @@ function M.get_all_loaded(cur_bufnr)
11366
and buf_ft ~= ""
11467
and utils.is_normal_buf(b)
11568
and state:get_status(b) == STATUS.enabled
116-
and nvim_buf_get_name(b):find(project_root, 1, true)
69+
and nvim_buf_get_name(b):find(state.project_root, 1, true)
11770
then
118-
local buf_cache = cached_data[b]
71+
local buf_cache = M.cached_data[b]
11972
local buf_tick = nvim_buf_get_var(b, "changedtick") ---@type integer
12073
-- use new data only when buffer's content has changed, otherwise use cached data
12174
if not buf_cache or buf_tick ~= buf_cache.tick then
12275
doc_data = M.get(b, buf_ft, options.max_lines, pos)
12376
table.insert(docs, doc_data)
124-
cached_data[b] = {
77+
M.cached_data[b] = {
12578
data = doc_data,
12679
tick = buf_tick,
12780
}
12881
else
129-
table.insert(docs, cached_data[b].data)
82+
table.insert(docs, M.cached_data[b].data)
13083
end
13184
end
13285
end

lua/neocodeium/init.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,27 @@ local function enable_autocmds()
242242
Server:stop()
243243
end,
244244
})
245+
246+
create_autocmd("BufUnload", {
247+
desc = "Clear documents cache on buffer unload",
248+
callback = function(args)
249+
doc.cached_data[args.buf] = nil
250+
end,
251+
})
252+
253+
create_autocmd("DirChanged", {
254+
desc = "Update project's root directory",
255+
callback = function()
256+
state:update_project_root()
257+
end,
258+
})
259+
260+
create_autocmd("WinEnter", {
261+
desc = "Update project's root directory",
262+
callback = function()
263+
state:update_project_root()
264+
end,
265+
})
245266
end
246267

247268
-- API ----------------------------------------------------- {{{1

lua/neocodeium/state.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local options = require("neocodeium.options").options
22
local utils = require("neocodeium.utils")
3+
local stdio = require("neocodeium.utils.stdio")
34
local STATUS = require("neocodeium.enums").STATUS
45

56
local uv = vim.uv
@@ -31,6 +32,9 @@ local nvim_get_option_value = vim.api.nvim_get_option_value
3132
---@field block block
3233
---@field debounce_timer uv.uv_timer_t
3334
---@field curline_text string
35+
---@field project_root filepath
36+
---@field project_root_uri url
37+
---@field cwd filepath
3438
local State = {
3539
active = false,
3640
chat_enabled = false,
@@ -51,6 +55,9 @@ local State = {
5155
block = {},
5256
debounce_timer = assert(uv.new_timer()),
5357
curline_text = "",
58+
project_root = stdio.get_project_root(),
59+
project_root_uri = stdio.to_uri(stdio.get_project_root()),
60+
cwd = fn.getcwd(),
5461
}
5562

5663
---Returns `true` if completion data is present and valid.
@@ -96,4 +103,14 @@ function State:update_editor_options()
96103
self.completion_request_data.editor_options.insert_spaces = vim.bo.expandtab
97104
end
98105

106+
function State:update_project_root()
107+
local cwd = fn.getcwd()
108+
if self.cwd ~= cwd then
109+
self.cwd = cwd
110+
local root = stdio.get_project_root()
111+
self.project_root = root
112+
self.project_root_uri = stdio.to_uri(root)
113+
end
114+
end
115+
99116
return State

0 commit comments

Comments
 (0)