Skip to content

Commit 29caa7f

Browse files
committed
Merge pull request #201 from JelteF/develop
Fix gq to not split strings halfway through
2 parents f01cc17 + ca20190 commit 29caa7f

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

indent/javascript.vim

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ setlocal nosmartindent
1515

1616
" Now, set up our indentation expression and keys that trigger it.
1717
setlocal indentexpr=GetJavascriptIndent()
18+
setlocal formatexpr=Fixedgq(v:lnum,v:count)
1819
setlocal indentkeys=0{,0},0),0],0\,,!^F,o,O,e
1920

2021
" Only define the function once.
@@ -437,3 +438,52 @@ endfunction
437438

438439
let &cpo = s:cpo_save
439440
unlet 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

Comments
 (0)