@@ -107,14 +107,13 @@ function Question:editor_fold_imports(strict)
107107 return
108108 end
109109
110- local range = self :editor_section_range (" imports" , { inclusive = true , strict = strict } )
111- if range and range .end_i then
110+ local range = self :editor_section_range (" imports" , true )
111+ if range . complete or range .end_i then
112112 vim .api .nvim_buf_call (self .bufnr , function ()
113- --- @diagnostic disable-next-line : param-type-mismatch
114- pcall (vim .cmd , (" %d,%dfold" ):format (range .start_i or 1 , range .end_i ))
113+ pcall (vim .cmd , (" %d,%dfold" ):format (range .start_i or 1 , range .end_i )) --- @diagnostic disable-line : param-type-mismatch
115114 end )
116- else
117- log . warn ( " `@leet imports` section not found in editor. " )
115+ elseif strict then
116+ range . log_not_found ( )
118117 end
119118end
120119
@@ -123,8 +122,8 @@ function Question:editor_yank_code()
123122 return
124123 end
125124
126- local range = self :editor_section_range (" code" , { strict = true } )
127- if range then
125+ local range = self :editor_section_range (" code" )
126+ if range : is_valid_or_log () then
128127 vim .api .nvim_buf_call (self .bufnr , function ()
129128 vim .cmd ((" %d,%dyank" ):format (range .start_i , range .end_i ))
130129 end )
@@ -218,11 +217,6 @@ function Question:inject(before)
218217 end
219218end
220219
221- function Question :editor_section_is_present (name )
222- local range = self :editor_section_range (name )
223- return range and true or false
224- end
225-
226220--- @param lines string | string[]
227221--- @param name lc.editor.section
228222--- @return string
241235function Question :editor_section_replace (lines , name )
242236 local range = self :editor_section_range (name )
243237
244- if range then
238+ if range : is_valid_or_log () then
245239 self :editor_set_lines (range .start_i - 1 , range .end_i , lines )
246240 end
247241end
361355--- @field complete boolean
362356
363357--- @param name string
364- --- @param opts ? { inclusive ?: boolean , strict ?: boolean }
365- --- @return lc.Question.Editor.Range ?
366- function Question :editor_section_range (name , opts )
367- opts = vim .tbl_extend (" keep" , opts or {}, { strict = true , inclusive = false })
358+ --- @param inclusive ? boolean
359+ function Question :editor_section_range (name , inclusive )
368360 local lines = vim .api .nvim_buf_get_lines (self .bufnr , 0 , - 1 , false )
369361 local start_i , end_i
370362
@@ -373,31 +365,59 @@ function Question:editor_section_range(name, opts)
373365
374366 for i , line in ipairs (lines ) do
375367 if line :match (start_tag ) then
376- start_i = i + (opts . inclusive and 0 or 1 )
368+ start_i = i + (inclusive and 0 or 1 )
377369 elseif line :match (end_tag ) then
378- end_i = i - (opts . inclusive and 0 or 1 )
370+ end_i = i - (inclusive and 0 or 1 )
379371 end
380372 end
381373
382- if opts .strict and not (start_i and end_i ) then
383- log .error ((" Section `@leet %s` not found in editor." ):format (name ))
384- return nil
374+ local res = {
375+ start_i = start_i ,
376+ end_i = end_i ,
377+ lines = lines ,
378+ complete = start_i and end_i ,
379+ }
380+
381+ res .log_not_found = function ()
382+ if res .complete then
383+ return
384+ end
385+
386+ local missing = {}
387+ if not res .start_i then
388+ table.insert (missing , (" `%s`" ):format (start_tag ))
389+ end
390+ if not res .end_i then
391+ table.insert (missing , (" `%s`" ):format (end_tag ))
392+ end
393+
394+ log .warn (table.concat (missing , " and " ) .. " not found." )
385395 end
386396
387- return { start_i = start_i , end_i = end_i , lines = lines , complete = start_i and end_i }
397+ res .is_partial = function ()
398+ return res .complete or (res .start_i or res .end_i )
399+ end
400+
401+ res .is_valid_or_log = function ()
402+ if res .complete then
403+ return true
404+ else
405+ res .log_not_found ()
406+ return false
407+ end
408+ end
409+
410+ return res
388411end
389412
390413--- @param submit boolean
391414--- @return string
392415function Question :editor_submit_lines (submit )
393416 local range = self :editor_section_range (" code" )
394- assert (range , " Code section not found in editor" )
395-
396- local start_i = range .start_i or 1
397- local end_i = range .end_i or # range .lines
417+ assert (range .complete , " Code section not found" )
398418
399- local prefix = not submit and (" \n " ):rep (start_i - 1 ) or " "
400- return prefix .. table.concat (range .lines , " \n " , start_i , end_i )
419+ local prefix = not submit and (" \n " ):rep (range . start_i - 1 ) or " "
420+ return prefix .. table.concat (range .lines , " \n " , range . start_i , range . end_i )
401421end
402422
403423--- @param self lc.ui.Question
0 commit comments