Skip to content

autoloadize to save 50ms Vim startup-time #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions autoload/textobj/variable_segment.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
function! s:select(object_type, right_boundary)
let left_boundaries = ['_\+\i', '\<', '\l\u', '\u\u\ze\l', '\a\d', '\d\a']
call search(join(left_boundaries, '\|'), 'bce')
let start_position = getpos('.')

call search('\>', 'c')
let word_end = getpos('.')
call setpos('.', start_position)

call search(a:right_boundary, 'c')
for _ in range(v:count1 - 1)
if getpos('.') != word_end
call search(a:right_boundary)
endif
endfor
let end_position = getpos('.')

return ['v', start_position, end_position]
endfunction

function! s:select_a()
let right_boundaries = ['_', '\l\u', '\u\u\l', '\a\d', '\d\a', '\i\>']
let right_boundary = join(right_boundaries, '\|')
let [type, start_position, end_position] = s:select('a', right_boundary)
let [_, start_line, start_column, _] = start_position

call search('\i\>', 'c')
if end_position == getpos('.') &&
\ getline(start_line)[start_column - 2] =~# '_'
let start_position[2] -= 1
endif

let was_small_camel = match(expand('<cword>'), '^_*\l.*\u') != -1
if was_small_camel
call search('\<', 'bc')
let [_, _, word_start, _] = getpos('.')

if start_column - 2 <= word_start ||
\ getline(start_line)[:start_column - 2] =~# '^_*$'
call setpos('.', end_position)
normal! l~
endif
endif

return [type, start_position, end_position]
endfunction

function! s:select_i()
let right_boundaries = ['\i_', '\l\u', '\u\u\l', '\a\d', '\d\a', '\i\>']
return s:select('i', join(right_boundaries, '\|'))
endfunction

function! textobj#variable_segment#select_i() abort
return s:select_i()
endfunction

function! textobj#variable_segment#select_a() abort
return s:select_a()
endfunction

61 changes: 2 additions & 59 deletions plugin/textobj/variable-segment.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,11 @@ if exists('g:loaded_textobj_variable_segment')
finish
endif


call textobj#user#plugin('variable', {
\ '-': {
\ 'sfile': expand('<sfile>:p'),
\ 'select-a': 'av', 'select-a-function': 's:select_a',
\ 'select-i': 'iv', 'select-i-function': 's:select_i',
\ 'select-a': 'av', 'select-a-function': 'textobj#variable_segment#select_a',
\ 'select-i': 'iv', 'select-i-function': 'textobj#variable_segment#select_i',
\ }})


function! s:select(object_type, right_boundary)
let left_boundaries = ['_\+\i', '\<', '\l\u', '\u\u\ze\l', '\a\d', '\d\a']
call search(join(left_boundaries, '\|'), 'bce')
let start_position = getpos('.')

call search('\>', 'c')
let word_end = getpos('.')
call setpos('.', start_position)

call search(a:right_boundary, 'c')
for _ in range(v:count1 - 1)
if getpos('.') != word_end
call search(a:right_boundary)
endif
endfor
let end_position = getpos('.')

return ['v', start_position, end_position]
endfunction


function! s:select_a()
let right_boundaries = ['_', '\l\u', '\u\u\l', '\a\d', '\d\a', '\i\>']
let right_boundary = join(right_boundaries, '\|')
let [type, start_position, end_position] = s:select('a', right_boundary)
let [_, start_line, start_column, _] = start_position

call search('\i\>', 'c')
if end_position == getpos('.') &&
\ getline(start_line)[start_column - 2] =~# '_'
let start_position[2] -= 1
endif

let was_small_camel = match(expand('<cword>'), '^_*\l.*\u') != -1
if was_small_camel
call search('\<', 'bc')
let [_, _, word_start, _] = getpos('.')

if start_column - 2 <= word_start ||
\ getline(start_line)[:start_column - 2] =~# '^_*$'
call setpos('.', end_position)
normal! l~
endif
endif

return [type, start_position, end_position]
endfunction


function! s:select_i()
let right_boundaries = ['\i_', '\l\u', '\u\u\l', '\a\d', '\d\a', '\i\>']
return s:select('i', join(right_boundaries, '\|'))
endfunction


let g:loaded_textobj_variable_segment = 1