|
8 | 8 | <string>#!/usr/bin/env ruby
|
9 | 9 | require 'shellwords'
|
10 | 10 |
|
11 |
| -# Return ‘mail-2.7.1’ given: ‘/path/to/mail-2.7.1/lib/mail.rb’, ‘/path/to’ |
12 |
| -def get_child_of_directory(descendent, directory) |
13 |
| - parent, child = File.split(descendent) |
14 |
| - unless child.start_with?('/') |
15 |
| - parent == directory ? child : get_child_of_directory(parent, directory) |
16 |
| - end |
17 |
| -end |
18 |
| -
|
19 |
| -def global_search_paths |
20 |
| - res = $LOAD_PATH |
21 |
| -
|
22 |
| - if current_file = ENV['TM_FILEPATH'] |
23 |
| - gem_dirs = Gem::Specification.dirs.map { |dir| File.expand_path('../gems', dir) } |
24 |
| - if gem_dir = gem_dirs.find { |dir| current_file.start_with?(dir) } |
25 |
| - if full_name = get_child_of_directory(current_file, gem_dir) |
26 |
| - Gem::Specification.find_all_by_full_name(full_name).each do |spec| |
27 |
| - res = spec.full_require_paths + res |
28 |
| - end |
29 |
| - end |
| 11 | +def require_paths_for_gem(path) |
| 12 | + begin |
| 13 | + if gem_spec = Gem::Specification.find_by_name(path.tr('/', '-')) |
| 14 | + return gem_spec.full_require_paths |
30 | 15 | end
|
| 16 | + rescue Gem::MissingSpecError => e |
31 | 17 | end
|
32 | 18 |
|
33 |
| - res |
34 |
| -end |
35 |
| -
|
36 |
| -def find_in_search_path(file, search_paths) |
37 |
| - search_paths.map { |path| File.join(path, file) }.find { |path| File.exist?(path) } |
| 19 | + if path.include?('/') |
| 20 | + require_paths_for_gem(File.dirname(path)) |
| 21 | + end |
38 | 22 | end
|
39 | 23 |
|
40 | 24 | if ENV['TM_CURRENT_LINE'] =~ /^\s*(?:require(_relative)?|load)\s*(['"])(.+?)(?:\.rb)?\2(?:\s+|$)?/
|
41 |
| - search_paths = $1 ? [ ENV['TM_DIRECTORY'] ] : global_search_paths |
42 |
| - name, file = $3, "#$3.rb" |
| 25 | + name, file = $3, "#$3.rb" |
43 | 26 |
|
44 |
| - unless path = find_in_search_path(file, search_paths) |
45 |
| - begin |
46 |
| - if gem_spec = Gem::Specification.find_by_name(name) |
47 |
| - path = find_in_search_path(file, gem_spec.full_require_paths) |
48 |
| - end |
49 |
| - rescue Gem::MissingSpecError => e |
50 |
| - end |
| 27 | + if $1 == '_relative' |
| 28 | + search_paths = [ ENV['TM_DIRECTORY'] ] |
| 29 | + elsif paths = require_paths_for_gem(name) |
| 30 | + search_paths = $LOAD_PATH + paths |
| 31 | + else |
| 32 | + search_paths = $LOAD_PATH |
51 | 33 | end
|
52 | 34 |
|
53 |
| - if path |
| 35 | + if path = search_paths.map { |path| File.join(path, file) }.find { |path| File.exist?(path) } |
54 | 36 | %x{ "$TM_MATE" #{path.shellescape} }
|
55 | 37 | else
|
56 | 38 | puts "Unable to locate ‘#{file}’. Locations searched:"
|
|
0 commit comments