@@ -16,7 +16,7 @@ The Chat Buffer - This is where all of the logic for conversing with an LLM sits
16
16
--- @field id integer The unique identifier for the chat
17
17
--- @field messages ? table The messages in the chat buffer
18
18
--- @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
20
20
--- @field references CodeCompanion.Chat.References
21
21
--- @field refs ? table<CodeCompanion.Chat.Ref> References which are sent to the LLM e.g. buffers , slash command output
22
22
--- @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
28
28
--- @field ui CodeCompanion.Chat.UI The UI of the chat buffer
29
29
--- @field variables ? CodeCompanion.Variables The variables available to the user
30
30
--- @field watchers CodeCompanion.Watchers The buffer watcher instance
31
+ --- @field yaml_parser vim.treesitter.LanguageTree The Yaml Tree-sitter parser for the chat buffer
31
32
32
33
--- @class CodeCompanion.ChatArgs Arguments that can be injected into the chat
33
34
--- @field adapter ? CodeCompanion.Adapter The adapter used in this chat buffer
@@ -79,13 +80,13 @@ local function make_id(val)
79
80
end
80
81
81
82
local _cached_settings = {}
82
- local _yaml_parser
83
83
84
84
--- Parse the chat buffer for settings
85
85
--- @param bufnr integer
86
+ --- @param parser vim.treesitter.LanguageTree
86
87
--- @param adapter ? CodeCompanion.Adapter
87
88
--- @return table
88
- local function ts_parse_settings (bufnr , adapter )
89
+ local function ts_parse_settings (bufnr , parser , adapter )
89
90
if _cached_settings [bufnr ] then
90
91
return _cached_settings [bufnr ]
91
92
end
@@ -99,12 +100,9 @@ local function ts_parse_settings(bufnr, adapter)
99
100
end
100
101
101
102
local settings = {}
102
- if not _yaml_parser then
103
- _yaml_parser = vim .treesitter .get_parser (bufnr , " yaml" , { ignore_injections = false })
104
- end
105
103
106
104
local query = get_query (" yaml" , " chat" )
107
- local root = _yaml_parser :parse ()[1 ]:root ()
105
+ local root = parser :parse ()[1 ]:root ()
108
106
109
107
local end_line = - 1
110
108
if adapter then
@@ -236,12 +234,22 @@ function Chat.new(args)
236
234
clear = false ,
237
235
})
238
236
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" )
240
240
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" )
242
242
end
243
243
self .parser = parser
244
244
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
+
245
253
self .references = require (" codecompanion.strategies.chat.references" ).new ({ chat = self })
246
254
self .subscribers = require (" codecompanion.strategies.chat.subscribers" ).new ()
247
255
self .tools = require (" codecompanion.strategies.chat.tools" ).new ({ bufnr = self .bufnr , messages = self .messages })
@@ -373,7 +381,7 @@ function Chat:set_autocmds()
373
381
buffer = bufnr ,
374
382
desc = " Parse the settings in the CodeCompanion chat buffer for any errors" ,
375
383
callback = function ()
376
- local settings = ts_parse_settings (bufnr , self .adapter )
384
+ local settings = ts_parse_settings (bufnr , self .yaml_parser , self . adapter )
377
385
378
386
local errors = schema .validate (self .adapter .schema , settings , self .adapter )
379
387
local node = settings .__ts_node
@@ -728,7 +736,7 @@ function Chat:submit(opts)
728
736
self .adapter = adapters .resolve (config .adapters [vim .g .codecompanion_adapter ])
729
737
end
730
738
731
- local settings = ts_parse_settings (bufnr , self .adapter )
739
+ local settings = ts_parse_settings (bufnr , self .yaml_parser , self . adapter )
732
740
settings = self .adapter :map_schema_to_params (settings )
733
741
734
742
log :trace (" Settings:\n %s" , settings )
@@ -1110,7 +1118,7 @@ function Chat:debug()
1110
1118
return
1111
1119
end
1112
1120
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
1114
1122
end
1115
1123
1116
1124
--- Returns the chat object(s) based on the buffer number
0 commit comments