Skip to content

Commit 8183f46

Browse files
committed
autoloadize to save 50ms Vim startup-time
1 parent 263dff7 commit 8183f46

File tree

2 files changed

+62
-59
lines changed

2 files changed

+62
-59
lines changed

Diff for: autoload/textobj/variable_segment.vim

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function! s:select(object_type, right_boundary)
2+
let left_boundaries = ['_\+\i', '\<', '\l\u', '\u\u\ze\l', '\a\d', '\d\a']
3+
call search(join(left_boundaries, '\|'), 'bce')
4+
let start_position = getpos('.')
5+
6+
call search('\>', 'c')
7+
let word_end = getpos('.')
8+
call setpos('.', start_position)
9+
10+
call search(a:right_boundary, 'c')
11+
for _ in range(v:count1 - 1)
12+
if getpos('.') != word_end
13+
call search(a:right_boundary)
14+
endif
15+
endfor
16+
let end_position = getpos('.')
17+
18+
return ['v', start_position, end_position]
19+
endfunction
20+
21+
function! s:select_a()
22+
let right_boundaries = ['_', '\l\u', '\u\u\l', '\a\d', '\d\a', '\i\>']
23+
let right_boundary = join(right_boundaries, '\|')
24+
let [type, start_position, end_position] = s:select('a', right_boundary)
25+
let [_, start_line, start_column, _] = start_position
26+
27+
call search('\i\>', 'c')
28+
if end_position == getpos('.') &&
29+
\ getline(start_line)[start_column - 2] =~# '_'
30+
let start_position[2] -= 1
31+
endif
32+
33+
let was_small_camel = match(expand('<cword>'), '^_*\l.*\u') != -1
34+
if was_small_camel
35+
call search('\<', 'bc')
36+
let [_, _, word_start, _] = getpos('.')
37+
38+
if start_column - 2 <= word_start ||
39+
\ getline(start_line)[:start_column - 2] =~# '^_*$'
40+
call setpos('.', end_position)
41+
normal! l~
42+
endif
43+
endif
44+
45+
return [type, start_position, end_position]
46+
endfunction
47+
48+
function! s:select_i()
49+
let right_boundaries = ['\i_', '\l\u', '\u\u\l', '\a\d', '\d\a', '\i\>']
50+
return s:select('i', join(right_boundaries, '\|'))
51+
endfunction
52+
53+
function! textobj#variable_segment#select_i() abort
54+
return s:select_i()
55+
endfunction
56+
57+
function! textobj#variable_segment#select_a() abort
58+
return s:select_a()
59+
endfunction
60+

Diff for: plugin/textobj/variable-segment.vim

+2-59
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,11 @@ if exists('g:loaded_textobj_variable_segment')
66
finish
77
endif
88

9-
109
call textobj#user#plugin('variable', {
1110
\ '-': {
1211
\ 'sfile': expand('<sfile>:p'),
13-
\ 'select-a': 'av', 'select-a-function': 's:select_a',
14-
\ 'select-i': 'iv', 'select-i-function': 's:select_i',
12+
\ 'select-a': 'av', 'select-a-function': 'textobj#variable_segment#select_a',
13+
\ 'select-i': 'iv', 'select-i-function': 'textobj#variable_segment#select_i',
1514
\ }})
1615

17-
18-
function! s:select(object_type, right_boundary)
19-
let left_boundaries = ['_\+\i', '\<', '\l\u', '\u\u\ze\l', '\a\d', '\d\a']
20-
call search(join(left_boundaries, '\|'), 'bce')
21-
let start_position = getpos('.')
22-
23-
call search('\>', 'c')
24-
let word_end = getpos('.')
25-
call setpos('.', start_position)
26-
27-
call search(a:right_boundary, 'c')
28-
for _ in range(v:count1 - 1)
29-
if getpos('.') != word_end
30-
call search(a:right_boundary)
31-
endif
32-
endfor
33-
let end_position = getpos('.')
34-
35-
return ['v', start_position, end_position]
36-
endfunction
37-
38-
39-
function! s:select_a()
40-
let right_boundaries = ['_', '\l\u', '\u\u\l', '\a\d', '\d\a', '\i\>']
41-
let right_boundary = join(right_boundaries, '\|')
42-
let [type, start_position, end_position] = s:select('a', right_boundary)
43-
let [_, start_line, start_column, _] = start_position
44-
45-
call search('\i\>', 'c')
46-
if end_position == getpos('.') &&
47-
\ getline(start_line)[start_column - 2] =~# '_'
48-
let start_position[2] -= 1
49-
endif
50-
51-
let was_small_camel = match(expand('<cword>'), '^_*\l.*\u') != -1
52-
if was_small_camel
53-
call search('\<', 'bc')
54-
let [_, _, word_start, _] = getpos('.')
55-
56-
if start_column - 2 <= word_start ||
57-
\ getline(start_line)[:start_column - 2] =~# '^_*$'
58-
call setpos('.', end_position)
59-
normal! l~
60-
endif
61-
endif
62-
63-
return [type, start_position, end_position]
64-
endfunction
65-
66-
67-
function! s:select_i()
68-
let right_boundaries = ['\i_', '\l\u', '\u\u\l', '\a\d', '\d\a', '\i\>']
69-
return s:select('i', join(right_boundaries, '\|'))
70-
endfunction
71-
72-
7316
let g:loaded_textobj_variable_segment = 1

0 commit comments

Comments
 (0)