Skip to content

Commit 2b637a8

Browse files
committed
Merge branch 'fix-11'
* fix-11: Update binstubs. Fix the new tests. Failing test. Fix "Single-character CamelCase segments seem not to work" (#11)
2 parents 51c323d + f4f43e2 commit 2b637a8

File tree

6 files changed

+114
-34
lines changed

6 files changed

+114
-34
lines changed

autoload/textobj/variable_segment.vim

+79-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,85 @@
1+
" Given three positions, return the position closest to the first one.
2+
function! s:closest_position(main, a, b)
3+
let a_distance = mapnew(a:main, {k,v->abs(v-a:a[k])})
4+
let b_distance = mapnew(a:main, {k,v->abs(v-a:b[k])})
5+
if a_distance[1] < b_distance[1]
6+
return a:a
7+
endif
8+
if a_distance[1] > b_distance[1]
9+
return a:b
10+
endif
11+
if a_distance[2] < b_distance[2]
12+
return a:a
13+
endif
14+
if a_distance[2] > b_distance[2]
15+
return a:b
16+
endif
17+
" Distances are the same, return either one
18+
return a:a
19+
endfunction!
20+
121
function! s:select(object_type, right_boundary)
222
let left_boundaries = ['_\+\k', '\<', '\l\u', '\u\u\ze\l', '\a\d', '\d\a']
3-
call search(join(left_boundaries, '\|'), 'bce')
4-
let start_position = getpos('.')
523

24+
" Gather all possible matches
25+
let cursor_position = getpos('.')
26+
let all_positions = []
27+
for boundary in left_boundaries
28+
" search() moves the cursor, so we need to reset the cursor's position
29+
" before each search
30+
call setpos('.', cursor_position)
31+
if search(boundary, 'bce') > 0
32+
call add(all_positions, getpos('.'))
33+
endif
34+
endfor
35+
36+
" Try to find a good match on the same line and on the left of the cursor
37+
let start_position = v:null
38+
let potential_matches = filter(copy(all_positions),
39+
\ {v -> v[1] == cursor_position[1] && v[2] <= cursor_position[2]})
40+
if len(potential_matches) > 0
41+
let start_position = reduce(potential_matches,
42+
\ {a, b -> s:closest_position(cursor_position, a, b)})
43+
endif
44+
45+
if type(start_position) == type(v:null)
46+
" No match found yet, try on the same line but on the right of the
47+
" cursor
48+
let potential_matches = filter(copy(all_positions),
49+
\ {v -> v[1] == cursor_position[1] && v[2] > cursor_position[2]})
50+
if len(potential_matches) > 0
51+
let start_position = reduce(potential_matches,
52+
\ {a, b -> s:closest_position(cursor_position, a, b)})
53+
endif
54+
endif
55+
56+
if type(start_position) == type(v:null)
57+
" No match found yet, try to find one on lines above the cursor
58+
let potential_matches = filter(copy(all_positions),
59+
\ {v -> v[1] < cursor_position[1]})
60+
if len(potential_matches) > 0
61+
let start_position = reduce(potential_matches,
62+
\ {a, b -> s:closest_position(cursor_position, a, b)})
63+
endif
64+
endif
65+
66+
if type(start_position) == type(v:null)
67+
" No match found yet, try to find one on below after the cursor
68+
let potential_matches = filter(copy(all_positions),
69+
\ {v -> v[1] > cursor_position[1]})
70+
if len(potential_matches) > 0
71+
let start_position = reduce(potential_matches,
72+
\ {a, b -> s:closest_position(cursor_position, a, b)})
73+
endif
74+
endif
75+
76+
if type(start_position) == type(v:null)
77+
" The buffer must not contain any words - fall back to the cursor's
78+
" position
79+
let start_position = cursor_position
80+
endif
81+
82+
call setpos('.', start_position)
683
call search('\>', 'c')
784
let word_end = getpos('.')
885
call setpos('.', start_position)

bin/bundle

+12-17
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ m = Module.new do
2727
bundler_version = nil
2828
update_index = nil
2929
ARGV.each_with_index do |a, i|
30-
if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
30+
if update_index && update_index.succ == i && a.match?(Gem::Version::ANCHORED_VERSION_PATTERN)
3131
bundler_version = a
3232
end
3333
next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
@@ -41,13 +41,13 @@ m = Module.new do
4141
gemfile = ENV["BUNDLE_GEMFILE"]
4242
return gemfile if gemfile && !gemfile.empty?
4343

44-
File.expand_path("../../Gemfile", __FILE__)
44+
File.expand_path("../Gemfile", __dir__)
4545
end
4646

4747
def lockfile
4848
lockfile =
4949
case File.basename(gemfile)
50-
when "gems.rb" then gemfile.sub(/\.rb$/, gemfile)
50+
when "gems.rb" then gemfile.sub(/\.rb$/, ".locked")
5151
else "#{gemfile}.lock"
5252
end
5353
File.expand_path(lockfile)
@@ -60,24 +60,19 @@ m = Module.new do
6060
Regexp.last_match(1)
6161
end
6262

63-
def bundler_version
64-
@bundler_version ||=
65-
env_var_version || cli_arg_version ||
66-
lockfile_version
67-
end
68-
6963
def bundler_requirement
70-
return "#{Gem::Requirement.default}.a" unless bundler_version
71-
72-
bundler_gem_version = Gem::Version.new(bundler_version)
73-
74-
requirement = bundler_gem_version.approximate_recommendation
64+
@bundler_requirement ||=
65+
env_var_version ||
66+
cli_arg_version ||
67+
bundler_requirement_for(lockfile_version)
68+
end
7569

76-
return requirement unless Gem::Version.new(Gem::VERSION) < Gem::Version.new("2.7.0")
70+
def bundler_requirement_for(version)
71+
return "#{Gem::Requirement.default}.a" unless version
7772

78-
requirement += ".a" if bundler_gem_version.prerelease?
73+
bundler_gem_version = Gem::Version.new(version)
7974

80-
requirement
75+
bundler_gem_version.approximate_recommendation
8176
end
8277

8378
def load_bundler!

bin/rake

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
# this file is here to facilitate running it.
99
#
1010

11-
require "pathname"
12-
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13-
Pathname.new(__FILE__).realpath)
11+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
1412

15-
bundle_binstub = File.expand_path("../bundle", __FILE__)
13+
bundle_binstub = File.expand_path("bundle", __dir__)
1614

1715
if File.file?(bundle_binstub)
18-
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
16+
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
1917
load(bundle_binstub)
2018
else
2119
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.

bin/thor

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
# this file is here to facilitate running it.
99
#
1010

11-
require "pathname"
12-
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13-
Pathname.new(__FILE__).realpath)
11+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
1412

15-
bundle_binstub = File.expand_path("../bundle", __FILE__)
13+
bundle_binstub = File.expand_path("bundle", __dir__)
1614

1715
if File.file?(bundle_binstub)
18-
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
16+
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
1917
load(bundle_binstub)
2018
else
2119
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.

bin/vim-flavor

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
# this file is here to facilitate running it.
99
#
1010

11-
require "pathname"
12-
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13-
Pathname.new(__FILE__).realpath)
11+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
1412

15-
bundle_binstub = File.expand_path("../bundle", __FILE__)
13+
bundle_binstub = File.expand_path("bundle", __dir__)
1614

1715
if File.file?(bundle_binstub)
18-
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
16+
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
1917
load(bundle_binstub)
2018
else
2119
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.

t/variable-segment.vim

+14
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,20 @@ describe 'iv'
164164
Expect getline(1) == 'theThingILike'
165165
end
166166

167+
it 'selects segments after single letter snake case sections'
168+
put! = 'a_thing_I_like'
169+
normal! 3|
170+
normal civtest
171+
Expect getline(1) == 'a_test_I_like'
172+
end
173+
174+
it 'selects segments after single letter camel sections'
175+
put! = 'aThingILike'
176+
normal! 2|
177+
normal civTest
178+
Expect getline(1) == 'aTestILike'
179+
end
180+
167181
it 'does not cross left snake boundaries'
168182
put! = 'foo_bar baz_quux'
169183
normal! 10|

0 commit comments

Comments
 (0)