@@ -34,7 +34,9 @@ function Question:set_lines(code)
3434 if not vim .api .nvim_buf_is_valid (self .bufnr ) then return end
3535
3636 pcall (vim .cmd .undojoin )
37- local s_i , e_i = self :range ()
37+ local s_i , e_i , lines = self :range ()
38+ s_i = s_i or 1
39+ e_i = e_i or # lines
3840 code = code and code or (self :snippet (true ) or " " )
3941 vim .api .nvim_buf_set_lines (self .bufnr , s_i - 1 , e_i , false , vim .split (code , " \n " ))
4042end
@@ -51,7 +53,7 @@ function Question:reset_lines()
5153 self :set_lines (new_lines )
5254end
5355
54- --- @return string path , boolean ? existed
56+ --- @return string path , boolean existed
5557function Question :path ()
5658 local lang = utils .get_lang (self .lang )
5759 local alt = lang .alt and (" ." .. lang .alt ) or " "
@@ -85,6 +87,12 @@ function Question:create_buffer()
8587 self .bufnr = vim .api .nvim_get_current_buf ()
8688 self .winid = vim .api .nvim_get_current_win ()
8789
90+ self :open_buffer (existed , false )
91+ end
92+
93+ --- @param existed boolean
94+ --- @param loaded boolean
95+ function Question :open_buffer (existed , loaded )
8896 vim .api .nvim_set_option_value (" buflisted" , true , { buf = self .bufnr })
8997
9098 local i = self :fold_range ()
@@ -95,6 +103,11 @@ function Question:create_buffer()
95103 if existed then --
96104 self :reset_lines ()
97105 end
106+
107+ if not loaded then
108+ utils .exec_hook (" question_enter" , self )
109+ self :autocmds ()
110+ end
98111end
99112
100113--- @param before boolean
@@ -180,14 +193,10 @@ function Question:handle_mount()
180193
181194 table.insert (_Lc_questions , self )
182195
183- self :autocmds ()
184-
185196 self .description = Description (self ):mount ()
186197 self .console = Console (self )
187198 self .info = Info (self )
188199
189- utils .exec_hook (" question_enter" , self )
190-
191200 return self
192201end
193202
@@ -275,22 +284,8 @@ Question.change_lang = vim.schedule_wrap(function(self, lang)
275284 vim .api .nvim_win_set_buf (self .winid , self .bufnr )
276285
277286 vim .api .nvim_set_option_value (" buflisted" , false , { buf = old_bufnr })
278- vim .api .nvim_set_option_value (" buflisted" , true , { buf = self .bufnr })
279287
280- local i = self :fold_range ()
281- if i then --
282- pcall (vim .cmd , (" %d,%dfold" ):format (1 , i ))
283- end
284-
285- if existed then --
286- self :reset_lines ()
287- end
288-
289- if not loaded then --
290- utils .exec_hook (" question_enter" , self )
291- end
292-
293- self :autocmds ()
288+ self :open_buffer (existed , loaded )
294289 end )
295290
296291 if not ok then
0 commit comments