Skip to content

Commit adf0302

Browse files
committed
Added awareness of comments, so word-based columns will not extend into them.
Example Code: # Git aliases alias gs="git status" alias ts="tig status" alias glr="git pull --rebase" Before this fix, if you ran vic on one of "alias" words, then "# Git" would also have been selected. After this fix it won't.
1 parent 7c34eed commit adf0302

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

plugin/textobj/word-column.vim

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ function! TextObjWordBasedColumn()
77
let stopcol = col("'>")
88
let linenum = line(".")
99
let indentlevel = s:indent_level(".")
10-
let startline = s:find_boundary_row(linenum, indentlevel, -1)
11-
let stopline = s:find_boundary_row(linenum, indentlevel, 1)
10+
let startline = s:find_boundary_row(linenum, startcol, indentlevel, -1)
11+
let stopline = s:find_boundary_row(linenum, startcol, indentlevel, 1)
1212
exec "silent normal!" startline . "gg" . startcol . "|" stopline . "gg" . stopcol . "|"
1313
endfunction
1414

15-
function! s:find_boundary_row(linenum, indentlevel, step)
16-
let sameindent = s:indent_level(a:linenum + a:step) == a:indentlevel
17-
let nonblank = getline(a:linenum + a:step) =~ "[^ \t]"
18-
if sameindent && nonblank
19-
return s:find_boundary_row(a:linenum + a:step, a:indentlevel, a:step)
15+
function! s:find_boundary_row(linenum, startcol, indentlevel, step)
16+
let nonblank = getline(a:linenum + a:step) =~ "[^ \t]"
17+
let sameindent = s:indent_level(a:linenum + a:step) == a:indentlevel
18+
let is_not_comment = ! s:is_comment(a:linenum + a:step, a:startcol)
19+
if sameindent && nonblank && is_not_comment
20+
return s:find_boundary_row(a:linenum + a:step, a:startcol, a:indentlevel, a:step)
2021
else
2122
return a:linenum
2223
endif
@@ -27,6 +28,10 @@ function! s:indent_level(linenum)
2728
return match(line, "[^ \t]")
2829
endfunction
2930

31+
function! s:is_comment(linenum, column)
32+
return synIDattr(synIDtrans(synID(a:linenum, a:column, 1)),"name") == "Comment"
33+
endfunction
34+
3035
if (!exists("g:skip_default_textobj_word_column_mappings"))
3136
vnoremap <silent> ac <Esc>vaw:call TextObjWordBasedColumn()<cr>
3237
vnoremap <silent> aC <Esc>vaW:call TextObjWordBasedColumn()<cr>

0 commit comments

Comments
 (0)