@@ -15,6 +15,7 @@ setlocal nosmartindent
1515
1616" Now, set up our indentation expression and keys that trigger it.
1717setlocal indentexpr = GetJavascriptIndent ()
18+ setlocal formatexpr = Fixedgq (v: lnum ,v: count )
1819setlocal indentkeys = 0 {,0 },0 ),0 ],0 \, ,! ^F,o ,O,e
1920
2021" Only define the function once.
@@ -437,3 +438,52 @@ endfunction
437438
438439let &cpo = s: cpo_save
439440unlet s: cpo_save
441+
442+ function ! Fixedgq (lnum, count )
443+ let l: count = a: count
444+
445+ if len (getline (a: lnum )) < 80 && l: count == 1 " No need for gq
446+ return 1
447+ endif
448+
449+ " Put all the lines on one line and do normal spliting after that
450+ if l: count > 1
451+ while l: count > 1
452+ let l: count -= 1
453+ normal J
454+ endwhile
455+ endif
456+
457+ let l: winview = winsaveview ()
458+
459+ call cursor (a: lnum , 81 )
460+ let orig_breakpoint = searchpairpos (' ' , ' ' , ' \.' , ' bcW' , ' ' , a: lnum )
461+ call cursor (a: lnum , 81 )
462+ let breakpoint = searchpairpos (' ' , ' ' , ' \.' , ' bcW' , s: skip_expr , a: lnum )
463+
464+ " No need for special treatment, normal gq handles edgecases better
465+ if breakpoint[1 ] == orig_breakpoint[1 ]
466+ call winrestview (l: winview )
467+ return 1
468+ endif
469+
470+ " Try breaking after string
471+ if breakpoint[1 ] == indent (a: lnum )
472+ call cursor (a: lnum , 81 )
473+ let breakpoint = searchpairpos (' \.' , ' ' , ' ' , ' cW' , s: skip_expr , a: lnum )
474+ endif
475+
476+
477+ if breakpoint[1 ] != 0
478+ call feedkeys (" r\<CR> " )
479+ else
480+ let l: count = l: count - 1
481+ endif
482+
483+ " run gq on new lines
484+ if l: count == 1
485+ call feedkeys (" gqq" )
486+ endif
487+
488+ return 0
489+ endfunction
0 commit comments