Skip to content

Commit 5f48d29

Browse files
committed
Refactored the regex for the highlight patterns.
1 parent a3ba000 commit 5f48d29

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

autoload/indent_guides.vim

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,11 @@ function! indent_guides#enable()
4141
" will automagically figure out whether to use tabs or spaces
4242
for l:level in range(s:start_level, s:indent_levels)
4343
let l:group = 'IndentGuides' . ((l:level % 2 == 0) ? 'Even' : 'Odd')
44+
let l:column_start = (l:level - 1) * s:indent_size + 1
45+
let l:soft_pattern = indent_guides#indent_highlight_pattern('\s', l:column_start, s:guide_size)
46+
let l:hard_pattern = indent_guides#indent_highlight_pattern('\t', l:column_start, s:indent_size)
4447

45-
" soft-tab pattern
46-
let l:soft_pattern = '^\s*\%' . ((l:level - 1) * s:indent_size + 1) . 'v\zs'
47-
let l:soft_pattern .= '\s*\%' . (((l:level - 1) * s:indent_size + 1) + s:guide_size) . 'v'
48-
let l:soft_pattern .= '\ze'
49-
50-
" hard-tab pattern
51-
let l:hard_pattern = '^\t*\%' . ((l:level - 1) * s:indent_size + 1) . 'v\zs'
52-
let l:hard_pattern .= '\t*\%' . (((l:level - 1) * s:indent_size + 1) + s:indent_size) . 'v'
53-
let l:hard_pattern .= '\ze'
54-
55-
" define the higlight pattern and add to list
48+
" define the higlight patterns and add to matches list
5649
call add(w:indent_guides_matches, matchadd(l:group, l:soft_pattern))
5750
call add(w:indent_guides_matches, matchadd(l:group, l:hard_pattern))
5851
endfor
@@ -246,3 +239,21 @@ function! indent_guides#capture_highlight(group_name)
246239
return l:output
247240
endfunction
248241

242+
"
243+
" Returns a regex pattern for highlighting an indent level.
244+
"
245+
" Example: indent_guides#indent_highlight_pattern(' ', 1, 4)
246+
" Returns: /^ *\%1v\zs *\%5v\ze/
247+
"
248+
" Example: indent_guides#indent_highlight_pattern('\s', 5, 2)
249+
" Returns: /^\s*\%5v\zs\s*\%7v\ze/
250+
"
251+
" Example: indent_guides#indent_highlight_pattern('\t', 9, 2)
252+
" Returns: /^\t*\%9v\zs\t*\%11v\ze/
253+
"
254+
function! indent_guides#indent_highlight_pattern(indent_pattern, column_start, indent_size)
255+
let l:pattern = '^' . a:indent_pattern . '*\%' . a:column_start . 'v\zs'
256+
let l:pattern .= a:indent_pattern . '*\%' . (a:column_start + a:indent_size) . 'v'
257+
let l:pattern .= '\ze'
258+
return l:pattern
259+
endfunction

0 commit comments

Comments
 (0)