Skip to content

Fix gq to not split strings halfway through #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions indent/javascript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ setlocal nosmartindent

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

" Only define the function once.
Expand Down Expand Up @@ -437,3 +438,52 @@ endfunction

let &cpo = s:cpo_save
unlet s:cpo_save

function! Fixedgq(lnum, count)
let l:count = a:count

if len(getline(a:lnum)) < 80 && l:count == 1 " No need for gq
return 1
endif

" Put all the lines on one line and do normal spliting after that
if l:count > 1
while l:count > 1
let l:count -= 1
normal J
endwhile
endif

let l:winview = winsaveview()

call cursor(a:lnum, 81)
let orig_breakpoint = searchpairpos(' ', '', '\.', 'bcW', '', a:lnum)
call cursor(a:lnum, 81)
let breakpoint = searchpairpos(' ', '', '\.', 'bcW', s:skip_expr, a:lnum)

" No need for special treatment, normal gq handles edgecases better
if breakpoint[1] == orig_breakpoint[1]
call winrestview(l:winview)
return 1
endif

" Try breaking after string
if breakpoint[1] == indent(a:lnum)
call cursor(a:lnum, 81)
let breakpoint = searchpairpos('\.', '', ' ', 'cW', s:skip_expr, a:lnum)
endif


if breakpoint[1] != 0
call feedkeys("r\<CR>")
else
let l:count = l:count - 1
endif

" run gq on new lines
if l:count == 1
call feedkeys("gqq")
endif

return 0
endfunction