Skip to content

Commit 089e0ce

Browse files
committed
Improve support for gems in “Open Require” (⇧⌘D)
The previously reworked command would only handle the root gem file, e.g. ‘sinatra’ but not ‘sinatra/base’. It would also fail for gems that have a slash/dash in their name, for example ‘rack/protection’ is one gem named ‘rack-protection’ (and not a file named ‘protection’ in the ‘rack’ gem).
1 parent 5711fb8 commit 089e0ce

File tree

1 file changed

+16
-34
lines changed

1 file changed

+16
-34
lines changed

Commands/Open Require.tmCommand

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,31 @@
88
<string>#!/usr/bin/env ruby
99
require 'shellwords'
1010
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
3015
end
16+
rescue Gem::MissingSpecError =&gt; e
3117
end
3218
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
3822
end
3923
4024
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"
4326
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 =&gt; 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
5133
end
5234
53-
if path
35+
if path = search_paths.map { |path| File.join(path, file) }.find { |path| File.exist?(path) }
5436
%x{ "$TM_MATE" #{path.shellescape} }
5537
else
5638
puts "Unable to locate ‘#{file}’. Locations searched:"

0 commit comments

Comments
 (0)