@@ -250,22 +250,6 @@ M.setup = function(opts)
250250 end
251251 end
252252
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-
269253 vim .api .nvim_create_autocmd (" BufEnter" , {
270254 pattern = " *.md" ,
271255 callback = function (ev )
@@ -279,7 +263,7 @@ M.setup = function(opts)
279263 vim .bo [buf ].filetype = " markdown.gpmd"
280264 end
281265 vim .cmd (" doautocmd User GpRefresh" )
282- end , 3 )
266+ end , 1 )
283267 end ,
284268 })
285269
@@ -731,8 +715,11 @@ M.new_chat = function(params, toggle, system_prompt, agent)
731715 -- strip leading and trailing newlines
732716 template = template :gsub (" ^%s*(.-)%s*$" , " %1" ) .. " \n "
733717
718+ local lines = vim .split (template , " \n " )
719+ lines = M .chat_header_lines (lines )
720+
734721 -- create chat file
735- vim .fn .writefile (vim . split ( template , " \n " ) , filename )
722+ vim .fn .writefile (lines , filename )
736723 local target = M .resolve_buf_target (params )
737724 local buf = M .open_buf (filename , target , M ._toggle_kind .chat , toggle )
738725
@@ -1063,23 +1050,23 @@ M.chat_respond = function(params)
10631050 )
10641051end
10651052
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 )
1053+ --- @param lines table # array of lines to process
1054+ --- @return table # updated array of lines
1055+ --- @return number # original header end
1056+ --- @return number # new header end
1057+ M .chat_header_lines = function (lines )
10771058 local _ , _ , header_end , comments = M .helpers .parse_headers (lines )
10781059 if header_end == nil then
10791060 M .logger .error (" Error while parsing headers: --- not found. Check your chat template." )
1080- return
1061+ return lines , 0 , 0
1062+ end
1063+
1064+ if header_end + 1 >= # lines then
1065+ return lines , 0 , 0
10811066 end
10821067
1068+ local header_lines = table.concat (vim .list_slice (lines , 0 , header_end + 1 ), " \n " )
1069+
10831070 local help_template = M .render .template (M .defaults .chat_help , {
10841071 [" {{user_prefix}}" ] = M .config .chat_user_prefix ,
10851072 [" {{respond_shortcut}}" ] = M .config .chat_shortcut_respond .shortcut ,
@@ -1105,16 +1092,48 @@ M.chat_help = function(buf)
11051092 end
11061093 end
11071094
1095+ local new_header_end = header_end
1096+
11081097 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 )
1098+ for i = # help_lines , 1 , - 1 do
1099+ table.insert (lines , new_header_end + 1 , help_lines [i ])
1100+ end
1101+ new_header_end = new_header_end + # help_lines
11101102 elseif not M ._state .show_chat_help and not insert_help then
11111103 table.sort (drop_lines , function (a , b )
11121104 return a > b
11131105 end )
11141106 for _ , index in ipairs (drop_lines ) do
1115- vim .api .nvim_buf_set_lines (buf , index , index + 1 , false , {})
1107+ table.remove (lines , index + 1 )
1108+ end
1109+ new_header_end = new_header_end - # drop_lines
1110+ end
1111+
1112+ local j = 1
1113+ while j <= new_header_end do
1114+ if lines [j ]:match (" ^%s*$" ) then
1115+ table.remove (lines , j )
1116+ new_header_end = new_header_end - 1
1117+ else
1118+ j = j + 1
11161119 end
11171120 end
1121+
1122+ return lines , header_end , new_header_end
1123+ end
1124+
1125+ --- @param buf number
1126+ M .chat_header = function (buf )
1127+ local file_name = vim .api .nvim_buf_get_name (buf )
1128+ M .logger .debug (" ChatHelp: buffer: " .. buf .. " file: " .. file_name )
1129+ local reason = M .not_chat (buf , file_name )
1130+ if reason then
1131+ M .logger .debug (" File " .. vim .inspect (file_name ) .. " does not look like a chat file: " .. vim .inspect (reason ))
1132+ return
1133+ end
1134+
1135+ local lines , old_header_end , header_end = M .chat_header_lines (vim .api .nvim_buf_get_lines (buf , 0 , - 1 , false ))
1136+ vim .api .nvim_buf_set_lines (buf , 0 , old_header_end + 1 , false , vim .list_slice (lines , 0 , header_end + 1 ))
11181137end
11191138
11201139M .cmd .ChatHelp = function ()
0 commit comments