@@ -242,17 +242,34 @@ M.setup = function(opts)
242
242
if M .hooks [cmd ] == nil then
243
243
M .helpers .create_user_command (M .config .cmd_prefix .. cmd , function (params )
244
244
M .logger .debug (" running command: " .. cmd )
245
- M .refresh_state ()
245
+ if cmd ~= " ChatHelp" then
246
+ M .refresh_state ()
247
+ end
246
248
M .cmd [cmd ](params )
247
249
end , completions [cmd ])
248
250
end
249
251
end
250
252
253
+ vim .filetype .add ({
254
+ extension = {
255
+ md = function (path , buf )
256
+ M .logger .debug (" filetype markdown: " .. path .. " buf: " .. buf )
257
+ if not M .not_chat (buf , path ) then
258
+ return " markdown.gpchat"
259
+ end
260
+
261
+ if M .helpers .ends_with (path , " .gp.md" ) then
262
+ return " markdown.gpmd"
263
+ end
264
+ return " markdown"
265
+ end ,
266
+ },
267
+ })
268
+
251
269
vim .api .nvim_create_autocmd (" BufEnter" , {
252
270
pattern = " *.md" ,
253
271
callback = function (ev )
254
- vim .defer_fn (function ()
255
- M .logger .debug (" Markdown BufEnter: " .. ev .file )
272
+ M .helpers .schedule (function ()
256
273
local path = ev .file
257
274
local buf = ev .buf
258
275
local current_ft = vim .bo [buf ].filetype
@@ -262,7 +279,7 @@ M.setup = function(opts)
262
279
vim .bo [buf ].filetype = " markdown.gpmd"
263
280
end
264
281
vim .cmd (" doautocmd User GpRefresh" )
265
- end , 100 )
282
+ end , 3 )
266
283
end ,
267
284
})
268
285
@@ -313,6 +330,10 @@ M.refresh_state = function(update)
313
330
M ._state .last_chat = nil
314
331
end
315
332
333
+ if M ._state .show_chat_help == nil then
334
+ M ._state .show_chat_help = true
335
+ end
336
+
316
337
for k , _ in pairs (M ._state ) do
317
338
if M ._state [k ] ~= old_state [k ] or M ._state [k ] ~= disk_state [k ] then
318
339
M .logger .debug (
@@ -699,6 +720,7 @@ M.new_chat = function(params, toggle, system_prompt, agent)
699
720
[" {{stop_shortcut}}" ] = M .config .chat_shortcut_stop .shortcut ,
700
721
[" {{delete_shortcut}}" ] = M .config .chat_shortcut_delete .shortcut ,
701
722
[" {{new_shortcut}}" ] = M .config .chat_shortcut_new .shortcut ,
723
+ [" {{help_shortcut}}" ] = M .config .chat_shortcut_help .shortcut ,
702
724
})
703
725
704
726
-- escape underscores (for markdown)
@@ -1041,6 +1063,64 @@ M.chat_respond = function(params)
1041
1063
)
1042
1064
end
1043
1065
1066
+ --- @param buf number
1067
+ M .chat_help = function (buf )
1068
+ local file_name = vim .api .nvim_buf_get_name (buf )
1069
+ M .logger .debug (" ChatHelp: buffer: " .. buf .. " file: " .. file_name )
1070
+ local reason = M .not_chat (buf , file_name )
1071
+ if reason then
1072
+ M .logger .debug (" File " .. vim .inspect (file_name ) .. " does not look like a chat file: " .. vim .inspect (reason ))
1073
+ return
1074
+ end
1075
+
1076
+ local lines = vim .api .nvim_buf_get_lines (buf , 0 , - 1 , false )
1077
+ local _ , _ , header_end , comments = M .helpers .parse_headers (lines )
1078
+ if header_end == nil then
1079
+ M .logger .error (" Error while parsing headers: --- not found. Check your chat template." )
1080
+ return
1081
+ end
1082
+
1083
+ local help_template = M .render .template (M .defaults .chat_help , {
1084
+ [" {{user_prefix}}" ] = M .config .chat_user_prefix ,
1085
+ [" {{respond_shortcut}}" ] = M .config .chat_shortcut_respond .shortcut ,
1086
+ [" {{cmd_prefix}}" ] = M .config .cmd_prefix ,
1087
+ [" {{stop_shortcut}}" ] = M .config .chat_shortcut_stop .shortcut ,
1088
+ [" {{delete_shortcut}}" ] = M .config .chat_shortcut_delete .shortcut ,
1089
+ [" {{new_shortcut}}" ] = M .config .chat_shortcut_new .shortcut ,
1090
+ [" {{help_shortcut}}" ] = M .config .chat_shortcut_help .shortcut ,
1091
+ })
1092
+
1093
+ local help_lines = vim .split (help_template , " \n " )
1094
+ local help_map = {}
1095
+ for _ , line in ipairs (help_lines ) do
1096
+ help_map [line ] = true
1097
+ end
1098
+
1099
+ local insert_help = true
1100
+ local drop_lines = {}
1101
+ for comment , index in pairs (comments ) do
1102
+ if help_map [comment ] then
1103
+ insert_help = false
1104
+ table.insert (drop_lines , index )
1105
+ end
1106
+ end
1107
+
1108
+ if M ._state .show_chat_help and insert_help then
1109
+ vim .api .nvim_buf_set_lines (buf , header_end , header_end , false , help_lines )
1110
+ elseif not M ._state .show_chat_help and not insert_help then
1111
+ table.sort (drop_lines , function (a , b )
1112
+ return a > b
1113
+ end )
1114
+ for _ , index in ipairs (drop_lines ) do
1115
+ vim .api .nvim_buf_set_lines (buf , index , index + 1 , false , {})
1116
+ end
1117
+ end
1118
+ end
1119
+
1120
+ M .cmd .ChatHelp = function ()
1121
+ M .refresh_state ({ show_chat_help = not M ._state .show_chat_help })
1122
+ end
1123
+
1044
1124
M .cmd .ChatRespond = function (params )
1045
1125
if params .args == " " and vim .v .count == 0 then
1046
1126
M .chat_respond (params )
0 commit comments