@@ -250,22 +250,6 @@ M.setup = function(opts)
250
250
end
251
251
end
252
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
-
269
253
vim .api .nvim_create_autocmd (" BufEnter" , {
270
254
pattern = " *.md" ,
271
255
callback = function (ev )
@@ -279,7 +263,7 @@ M.setup = function(opts)
279
263
vim .bo [buf ].filetype = " markdown.gpmd"
280
264
end
281
265
vim .cmd (" doautocmd User GpRefresh" )
282
- end , 3 )
266
+ end , 1 )
283
267
end ,
284
268
})
285
269
@@ -731,8 +715,11 @@ M.new_chat = function(params, toggle, system_prompt, agent)
731
715
-- strip leading and trailing newlines
732
716
template = template :gsub (" ^%s*(.-)%s*$" , " %1" ) .. " \n "
733
717
718
+ local lines = vim .split (template , " \n " )
719
+ lines = M .chat_header_lines (lines )
720
+
734
721
-- create chat file
735
- vim .fn .writefile (vim . split ( template , " \n " ) , filename )
722
+ vim .fn .writefile (lines , filename )
736
723
local target = M .resolve_buf_target (params )
737
724
local buf = M .open_buf (filename , target , M ._toggle_kind .chat , toggle )
738
725
@@ -1063,23 +1050,23 @@ M.chat_respond = function(params)
1063
1050
)
1064
1051
end
1065
1052
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 )
1077
1058
local _ , _ , header_end , comments = M .helpers .parse_headers (lines )
1078
1059
if header_end == nil then
1079
1060
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
1081
1066
end
1082
1067
1068
+ local header_lines = table.concat (vim .list_slice (lines , 0 , header_end + 1 ), " \n " )
1069
+
1083
1070
local help_template = M .render .template (M .defaults .chat_help , {
1084
1071
[" {{user_prefix}}" ] = M .config .chat_user_prefix ,
1085
1072
[" {{respond_shortcut}}" ] = M .config .chat_shortcut_respond .shortcut ,
@@ -1105,16 +1092,48 @@ M.chat_help = function(buf)
1105
1092
end
1106
1093
end
1107
1094
1095
+ local new_header_end = header_end
1096
+
1108
1097
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
1110
1102
elseif not M ._state .show_chat_help and not insert_help then
1111
1103
table.sort (drop_lines , function (a , b )
1112
1104
return a > b
1113
1105
end )
1114
1106
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
1116
1119
end
1117
1120
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 ))
1118
1137
end
1119
1138
1120
1139
M .cmd .ChatHelp = function ()
0 commit comments