Skip to content

Commit 1103551

Browse files
authored
fix(chat): #915 parsing show_settings when InsertLeave is executed (#916)
Co-authored-by: Oli Morris <[email protected]>
1 parent 0a8950b commit 1103551

File tree

1 file changed

+20
-12
lines changed
  • lua/codecompanion/strategies/chat

1 file changed

+20
-12
lines changed

lua/codecompanion/strategies/chat/init.lua

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The Chat Buffer - This is where all of the logic for conversing with an LLM sits
1616
---@field id integer The unique identifier for the chat
1717
---@field messages? table The messages in the chat buffer
1818
---@field opts CodeCompanion.ChatArgs Store all arguments in this table
19-
---@field parser vim.treesitter.LanguageTree The Tree-sitter parser for the chat buffer
19+
---@field parser vim.treesitter.LanguageTree The Markdown Tree-sitter parser for the chat buffer
2020
---@field references CodeCompanion.Chat.References
2121
---@field refs? table<CodeCompanion.Chat.Ref> References which are sent to the LLM e.g. buffers, slash command output
2222
---@field settings? table The settings that are used in the adapter of the chat buffer
@@ -28,6 +28,7 @@ The Chat Buffer - This is where all of the logic for conversing with an LLM sits
2828
---@field ui CodeCompanion.Chat.UI The UI of the chat buffer
2929
---@field variables? CodeCompanion.Variables The variables available to the user
3030
---@field watchers CodeCompanion.Watchers The buffer watcher instance
31+
---@field yaml_parser vim.treesitter.LanguageTree The Yaml Tree-sitter parser for the chat buffer
3132

3233
---@class CodeCompanion.ChatArgs Arguments that can be injected into the chat
3334
---@field adapter? CodeCompanion.Adapter The adapter used in this chat buffer
@@ -79,13 +80,13 @@ local function make_id(val)
7980
end
8081

8182
local _cached_settings = {}
82-
local _yaml_parser
8383

8484
---Parse the chat buffer for settings
8585
---@param bufnr integer
86+
---@param parser vim.treesitter.LanguageTree
8687
---@param adapter? CodeCompanion.Adapter
8788
---@return table
88-
local function ts_parse_settings(bufnr, adapter)
89+
local function ts_parse_settings(bufnr, parser, adapter)
8990
if _cached_settings[bufnr] then
9091
return _cached_settings[bufnr]
9192
end
@@ -99,12 +100,9 @@ local function ts_parse_settings(bufnr, adapter)
99100
end
100101

101102
local settings = {}
102-
if not _yaml_parser then
103-
_yaml_parser = vim.treesitter.get_parser(bufnr, "yaml", { ignore_injections = false })
104-
end
105103

106104
local query = get_query("yaml", "chat")
107-
local root = _yaml_parser:parse()[1]:root()
105+
local root = parser:parse()[1]:root()
108106

109107
local end_line = -1
110108
if adapter then
@@ -236,12 +234,22 @@ function Chat.new(args)
236234
clear = false,
237235
})
238236

239-
local ok, parser = pcall(vim.treesitter.get_parser, self.bufnr, "markdown")
237+
-- Assign the parsers to the chat object for performance
238+
local ok, parser, yaml_parser
239+
ok, parser = pcall(vim.treesitter.get_parser, self.bufnr, "markdown")
240240
if not ok then
241-
return log:error("Could not find the markdown Tree-sitter parser")
241+
return log:error("Could not find the Markdown Tree-sitter parser")
242242
end
243243
self.parser = parser
244244

245+
if config.display.chat.show_settings then
246+
ok, yaml_parser = pcall(vim.treesitter.get_parser, self.bufnr, "yaml", { ignore_injections = false })
247+
if not ok then
248+
return log:error("Could not find the Yaml Tree-sitter parser")
249+
end
250+
self.yaml_parser = yaml_parser
251+
end
252+
245253
self.references = require("codecompanion.strategies.chat.references").new({ chat = self })
246254
self.subscribers = require("codecompanion.strategies.chat.subscribers").new()
247255
self.tools = require("codecompanion.strategies.chat.tools").new({ bufnr = self.bufnr, messages = self.messages })
@@ -373,7 +381,7 @@ function Chat:set_autocmds()
373381
buffer = bufnr,
374382
desc = "Parse the settings in the CodeCompanion chat buffer for any errors",
375383
callback = function()
376-
local settings = ts_parse_settings(bufnr, self.adapter)
384+
local settings = ts_parse_settings(bufnr, self.yaml_parser, self.adapter)
377385

378386
local errors = schema.validate(self.adapter.schema, settings, self.adapter)
379387
local node = settings.__ts_node
@@ -728,7 +736,7 @@ function Chat:submit(opts)
728736
self.adapter = adapters.resolve(config.adapters[vim.g.codecompanion_adapter])
729737
end
730738

731-
local settings = ts_parse_settings(bufnr, self.adapter)
739+
local settings = ts_parse_settings(bufnr, self.yaml_parser, self.adapter)
732740
settings = self.adapter:map_schema_to_params(settings)
733741

734742
log:trace("Settings:\n%s", settings)
@@ -1110,7 +1118,7 @@ function Chat:debug()
11101118
return
11111119
end
11121120

1113-
return ts_parse_settings(self.bufnr, self.adapter), self.messages
1121+
return ts_parse_settings(self.bufnr, self.yaml_parser, self.adapter), self.messages
11141122
end
11151123

11161124
---Returns the chat object(s) based on the buffer number

0 commit comments

Comments
 (0)