Skip to content

Commit ea489ba

Browse files
committed
Initial commit: div. It works.
0 parents  commit ea489ba

File tree

8 files changed

+285
-0
lines changed

8 files changed

+285
-0
lines changed

Diff for: .travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: ruby
2+
rvm:
3+
- 1.9.3
4+
script: rake ci

Diff for: COPYING

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2013 Julian Berman
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

Diff for: Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'vim-flavor', '~> 1.1'

Diff for: README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# vim-textobj-variable-segment
2+
3+
A vim plugin providing a single text object (on `iv`) for
4+
variable segments. A variable segment is defined to be a substring in
5+
an identifier followed by an underscore for snake cased variables or a
6+
substring in an identifier followed by an uppercase character for camel
7+
cased variables.
8+
9+
E.g.:
10+
11+
foo_ba|r_baz -> div -> foo_baz
12+
QU|UX_SPAM -> div -> SPAM
13+
eggsAn|dCheese -> div -> eggsCheese
14+
_privat|e_quux -> div -> _quux
15+
16+
It will also preserve case for small camels when initial segments are deleted:
17+
18+
_g|etJiggyYo -> div -> _jiggyYo
19+
20+
21+
`av` right now does the same thing as `iv`, but if you have ideas for how it
22+
should differ feel free to open a ticket (with a most welcome Pull Request if
23+
possible :D ).
24+
25+
26+
Requires [vim-textobj-user](https://github.com/kana/vim-textobj-user).

Diff for: Rakefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env rake
2+
3+
task :ci => [:dump, :test]
4+
5+
task :dump do
6+
sh 'vim --version'
7+
end
8+
9+
task :test do
10+
sh 'bundle exec vim-flavor test'
11+
end

Diff for: VimFlavor

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flavor 'kana/vim-textobj-user', '~> 0.3'

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

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
" textobj-variable-segment: a text object for segments of variable names
2+
" Author: Julian Berman
3+
" Version: 0.1.0
4+
5+
if exists('g:loaded_textobj_variable_segment')
6+
finish
7+
endif
8+
9+
call textobj#user#plugin('variable', {
10+
\ '-': {
11+
\ '*sfile*': expand('<sfile>:p'),
12+
\ 'select-a': 'av', '*select-a-function*': 's:select_a',
13+
\ 'select-i': 'iv', '*select-i-function*': 's:select_i',
14+
\ }})
15+
16+
function! s:select(object_type)
17+
let variable = expand('<cword>')
18+
19+
if variable =~# '\i_\i'
20+
let [left_boundary, right_boundary] = ['_\i', '_']
21+
let was_small_camel = 0
22+
else
23+
let [left_boundary, right_boundary] = ['_\i\|\u', '\i\u']
24+
let was_small_camel = match(variable, '^_*\l') != -1
25+
endif
26+
27+
call search(left_boundary . '\|\<', 'bce')
28+
let start_position = getpos('.')
29+
30+
call search(right_boundary . '\|\i\>', 'c')
31+
let end_position = getpos('.')
32+
33+
call search('\i\>', 'c')
34+
if end_position == getpos('.')
35+
if getline(start_position[1])[start_position[2] - 2] =~# '_'
36+
let start_position[2] -= 1
37+
endif
38+
endif
39+
40+
if was_small_camel
41+
let [_, line_number, column, _] = start_position
42+
43+
call search('\<', 'bc')
44+
let [_, _, word_start, _] = getpos('.')
45+
46+
if column - 2 <= word_start || getline(line_number)[:column - 2] =~# '^_*$'
47+
call setpos('.', end_position)
48+
normal! l~
49+
endif
50+
endif
51+
52+
return ['v', start_position, end_position]
53+
endfunction
54+
55+
function! s:select_a()
56+
return s:select('a')
57+
endfunction
58+
59+
function! s:select_i()
60+
return s:select('i')
61+
endfunction
62+
63+
let g:loaded_textobj_variable_segment = 1

Diff for: t/variable-segment.vim

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
filetype plugin on
2+
runtime! plugin/textobj/variable-segment.vim
3+
4+
5+
describe 'snake case'
6+
after
7+
bwipeout!
8+
end
9+
10+
it 'selects between underscores'
11+
put! = 'foo_bar_baz'
12+
normal! 6|
13+
normal div
14+
Expect getline(1) == 'foo_baz'
15+
end
16+
17+
it 'selects leading sections'
18+
put! = 'BAG_OF_SPAM'
19+
normal! 2|
20+
normal div
21+
Expect getline(1) == 'OF_SPAM'
22+
end
23+
24+
it 'selects ending sections'
25+
put! = 'BAG_OF_SPAM'
26+
normal! 9|
27+
normal div
28+
Expect getline(1) == 'BAG_OF'
29+
end
30+
31+
it 'ignores leading underscores'
32+
put! = '_SPAM_EGGS'
33+
normal! 3|
34+
normal div
35+
Expect getline(1) == '_EGGS'
36+
end
37+
38+
it 'can match on the left boundary'
39+
put! = 'hello_world'
40+
normal! 0
41+
normal div
42+
Expect getline(1) == 'world'
43+
end
44+
45+
it 'can match on the right boundary'
46+
put! = 'hello_world'
47+
normal! $
48+
normal div
49+
Expect getline(1) == 'hello'
50+
end
51+
52+
it 'does not cross left word boundaries'
53+
put! = 'foo_bar baz_quux'
54+
normal! 10|
55+
normal div
56+
Expect getline(1) == 'foo_bar quux'
57+
end
58+
59+
it 'does not cross right word boundaries'
60+
put! = 'foo_bar baz_quux'
61+
normal! 6|
62+
normal div
63+
Expect getline(1) == 'foo baz_quux'
64+
end
65+
66+
it 'can match on the underscore'
67+
end
68+
end
69+
70+
71+
describe 'camel case'
72+
after
73+
bwipeout!
74+
end
75+
76+
it 'selects between small camels'
77+
put! = 'eggsAndCheese'
78+
normal! 6|
79+
normal div
80+
Expect getline(1) == 'eggsCheese'
81+
end
82+
83+
it 'selects leading small camels and swaps case'
84+
put! = 'greatQuuxThings'
85+
normal! 3|
86+
normal div
87+
Expect getline(1) == 'quuxThings'
88+
end
89+
90+
it 'selects leading small camels and swaps case even with underscores'
91+
put! = '_greatQuuxThings'
92+
normal! 3|
93+
normal div
94+
Expect getline(1) == '_quuxThings'
95+
end
96+
97+
it 'selects leading large camels and preserves case'
98+
put! = '_ThingDoer'
99+
normal! 3|
100+
normal div
101+
Expect getline(1) == '_Doer'
102+
end
103+
104+
it 'selects ending sections'
105+
put! = 'ohNoWhatsThis'
106+
normal 12|
107+
normal div
108+
Expect getline(1) == 'ohNoWhats'
109+
end
110+
111+
it 'ignores leading underscores'
112+
put! = '_doCoolThings'
113+
normal! 5|
114+
normal div
115+
Expect getline(1) == '_doThings'
116+
end
117+
118+
it 'can match on the left boundary'
119+
put! = 'helloWorld'
120+
normal! 0
121+
normal div
122+
Expect getline(1) == 'world'
123+
end
124+
125+
it 'can match on the right boundary'
126+
put! = 'helloWorld'
127+
normal! $
128+
normal div
129+
Expect getline(1) == 'hello'
130+
end
131+
132+
it 'does not cross left word boundaries'
133+
put! = 'fooBar bazQuux'
134+
normal! 10|
135+
normal div
136+
Expect getline(1) == 'fooBar quux'
137+
end
138+
139+
it 'does not cross right word boundaries'
140+
put! = 'fooBar bazQuux'
141+
normal! 6|
142+
normal div
143+
Expect getline(1) == 'foo bazQuux'
144+
end
145+
end
146+
147+
148+
describe 'degenerate case'
149+
after
150+
bwipeout!
151+
end
152+
153+
it 'works'
154+
put! = 'thing'
155+
normal div
156+
Expect getline(1) == ''
157+
end
158+
end

0 commit comments

Comments
 (0)