Skip to content

Commit 06d2b34

Browse files
author
Nate Kane
committed
Refactored some core logic and added some simple debugging
1 parent d53103f commit 06d2b34

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

autoload/indent_guides.vim

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ endfunction
3333
function! indent_guides#enable()
3434
let g:indent_guides_autocmds_enabled = 1
3535

36-
call indent_guides#init_buffer_vars()
36+
call indent_guides#init_script_vars()
3737
call indent_guides#highlight_colors()
3838
call indent_guides#clear_matches()
3939

4040
" loop through each indent level and define a highlight pattern
4141
" will automagically figure out whether to use tabs or spaces
42-
for l:level in range(1, g:indent_guides_indent_levels)
42+
for l:level in range(1, s:indent_levels)
4343
let l:group = 'IndentGuides' . ((l:level % 2 == 0) ? 'Even' : 'Odd')
44-
let l:pattern = '^\s\{' . (l:level * b:indent_size - b:indent_size) . '\}\zs'
45-
let l:pattern .= '\s\{' . b:guide_size . '\}'
44+
let l:pattern = '^\s\{' . (l:level * s:indent_size - s:indent_size) . '\}\zs'
45+
let l:pattern .= '\s\{' . s:guide_size . '\}'
4646
let l:pattern .= '\ze'
4747

4848
" define the higlight pattern and add to list
@@ -78,7 +78,7 @@ endfunction
7878
" Automagically calculates and defines the indent highlight colors.
7979
"
8080
function! indent_guides#highlight_colors()
81-
if g:indent_guides_auto_colors
81+
if s:auto_colors
8282
if has('gui_running')
8383
call indent_guides#gui_highlight_colors()
8484
else
@@ -108,17 +108,17 @@ function! indent_guides#gui_highlight_colors()
108108
let l:hi_normal_guibg = ''
109109

110110
" capture the backgroud color from the normal highlight
111-
if b:hi_normal =~ g:indent_guides_color_hex_guibg_pattern
111+
if s:hi_normal =~ s:color_hex_bg_pat
112112
" hex color code is being used, eg. '#FFFFFF'
113-
let l:hi_normal_guibg = matchstr(b:hi_normal, g:indent_guides_color_hex_guibg_pattern)
113+
let l:hi_normal_guibg = matchstr(s:hi_normal, s:color_hex_bg_pat)
114114

115-
elseif b:hi_normal =~ g:indent_guides_color_name_guibg_pattern
115+
elseif s:hi_normal =~ s:color_name_bg_pat
116116
" color name is being used, eg. 'white'
117-
let l:color_name = matchstr(b:hi_normal, g:indent_guides_color_name_guibg_pattern)
117+
let l:color_name = matchstr(s:hi_normal, s:color_name_bg_pat)
118118
let l:hi_normal_guibg = color_helper#color_name_to_hex(l:color_name)
119119
endif
120120

121-
if l:hi_normal_guibg =~ g:indent_guides_color_hex_pattern
121+
if l:hi_normal_guibg =~ s:color_hex_pat
122122
" calculate the highlight background colors
123123
let l:hi_odd_bg = indent_guides#lighten_or_darken_color(l:hi_normal_guibg)
124124
let l:hi_even_bg = indent_guides#lighten_or_darken_color(l:hi_odd_bg)
@@ -135,12 +135,11 @@ endfunction
135135
"
136136
function! indent_guides#lighten_or_darken_color(color)
137137
let l:new_color = ''
138-
let l:percent = g:indent_guides_color_change_percent / 100.0
139138

140139
if (&g:background == 'dark')
141-
let l:new_color = color_helper#hex_color_lighten(a:color, l:percent)
140+
let l:new_color = color_helper#hex_color_lighten(a:color, s:change_percent)
142141
else
143-
let l:new_color = color_helper#hex_color_darken (a:color, l:percent)
142+
let l:new_color = color_helper#hex_color_darken (a:color, s:change_percent)
144143
endif
145144

146145
return l:new_color
@@ -165,10 +164,31 @@ endfunction
165164
" We need to initialize these vars every time a buffer is entered while the
166165
" plugin is enabled.
167166
"
168-
function! indent_guides#init_buffer_vars()
169-
let b:indent_size = indent_guides#get_indent_size()
170-
let b:guide_size = indent_guides#calculate_guide_size()
171-
let b:hi_normal = indent_guides#capture_highlight('Normal')
167+
function! indent_guides#init_script_vars()
168+
let s:indent_size = indent_guides#get_indent_size()
169+
let s:guide_size = indent_guides#calculate_guide_size()
170+
let s:hi_normal = indent_guides#capture_highlight('Normal')
171+
172+
" shortcuts to the global variables - this makes the code easier to read
173+
let s:debug = g:indent_guides_debug
174+
let s:indent_levels = g:indent_guides_indent_levels
175+
let s:auto_colors = g:indent_guides_auto_colors
176+
let s:change_percent = g:indent_guides_color_change_percent / 100.0
177+
let s:color_hex_pat = g:indent_guides_color_hex_pattern
178+
let s:color_hex_bg_pat = g:indent_guides_color_hex_guibg_pattern
179+
let s:color_name_bg_pat = g:indent_guides_color_name_guibg_pattern
180+
181+
if s:debug
182+
echo 's:indent_size = ' . s:indent_size
183+
echo 's:guide_size = ' . s:guide_size
184+
echo 's:hi_normal = ' . s:hi_normal
185+
echo 's:indent_levels = ' . s:indent_levels
186+
echo 's:auto_colors = ' . s:auto_colors
187+
echo 's:change_percent = ' . string(s:change_percent)
188+
echo 's:color_hex_pat = ' . s:color_hex_pat
189+
echo 's:color_hex_bg_pat = ' . s:color_hex_bg_pat
190+
echo 's:color_name_bg_pat = ' . s:color_name_bg_pat
191+
endif
172192
endfunction
173193

174194
"
@@ -182,9 +202,9 @@ function! indent_guides#calculate_guide_size()
182202
let l:indent_size = indent_guides#get_indent_size()
183203

184204
if l:indent_size > 1 && l:guide_size >= 1
185-
let l:guide_size = (l:guide_size > b:indent_size) ? b:indent_size : l:guide_size
205+
let l:guide_size = (l:guide_size > s:indent_size) ? s:indent_size : l:guide_size
186206
else
187-
let l:guide_size = b:indent_size
207+
let l:guide_size = s:indent_size
188208
endif
189209

190210
return l:guide_size
@@ -209,6 +229,7 @@ function! indent_guides#capture_highlight(group_name)
209229
exe "silent hi " . a:group_name
210230
redir END
211231

232+
let l:output = substitute(l:output, "\n", "", "")
212233
return l:output
213234
endfunction
214235

0 commit comments

Comments
 (0)