Skip to content

Instantly share code, notes, and snippets.

@chrisbra
Created February 13, 2015 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbra/0ba9df9011ddc38e7181 to your computer and use it in GitHub Desktop.
Save chrisbra/0ba9df9011ddc38e7181 to your computer and use it in GitHub Desktop.
commentary-issue40
diff --git a/plugin/commentary.vim b/plugin/commentary.vim
index a91cd71..073e614 100644
--- a/plugin/commentary.vim
+++ b/plugin/commentary.vim
@@ -12,6 +12,16 @@ function! s:surroundings() abort
return split(get(b:, 'commentary_format', substitute(substitute(
\ &commentstring, '\S\zs%s',' %s','') ,'%s\ze\S', '%s ', '')), '%s', 1)
endfunction
+function! s:StripWhiteSpace(l,r,line)
+ " Strip whitespace from the comment,
+ " if the line does not have any
+ let [l, r] = [a:l, a:r]
+ if stridx(a:line,l) == -1 && stridx(a:line,l[0:-2])==0 && a:line[strlen(a:line)-strlen(r[1:]):-1]==r[1:]
+ return [l[0:-2], r[1:]]
+ endif
+ return [l,r]
+endfunction
+
function! s:go(type,...) abort
if a:0
@@ -24,6 +34,7 @@ function! s:go(type,...) abort
let uncomment = 2
for lnum in range(lnum1,lnum2)
let line = matchstr(getline(lnum),'\S.*\s\@<!')
+ let [l, r] = s:StripWhiteSpace(l,r,line)
if line != '' && (stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
let uncomment = 0
endif
@@ -53,12 +64,14 @@ function! s:go(type,...) abort
endfunction
function! s:textobject(inner) abort
- let [l, r] = s:surroundings()
+ let [l_, r_] = s:surroundings()
+ let [l,r] = [l_,r_]
let lnums = [line('.')+1, line('.')-2]
for [index, dir, bound, line] in [[0, -1, 1, ''], [1, 1, line('$'), '']]
while lnums[index] != bound && line ==# '' || !(stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
let lnums[index] += dir
let line = matchstr(getline(lnums[index]+dir),'\S.*\s\@<!')
+ let [l, r] = s:StripWhiteSpace(l_,r_,line)
endwhile
endfor
while (a:inner || lnums[1] != line('$')) && empty(getline(lnums[0]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment