Skip to content

Commit

Permalink
fix: support find require xxx.rb in local workspace.
Browse files Browse the repository at this point in the history
perf: lazy cache filenames
  • Loading branch information
SolaWing committed Jul 3, 2024
1 parent 524c94e commit bcfac73
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
17 changes: 12 additions & 5 deletions lib/solargraph/library.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,15 @@ def locate_ref location
return if map.nil?
pin = map.requires.select { |p| p.location.range.contain?(location.range.start) }.first
return nil if pin.nil?
return_if_match = proc do |full|
if source_map_hash.key?(full)
return Location.new(full, Solargraph::Range.from_to(0, 0, 0, 0))
end
end
workspace.require_paths.each do |path|
full = Pathname.new(path).join("#{pin.name}.rb").to_s
next unless source_map_hash.key?(full)
return Location.new(full, Solargraph::Range.from_to(0, 0, 0, 0))
full = File.join path, pin.name
return_if_match.(full)
return_if_match.(full << ".rb")
end
nil
rescue FileNotFoundError
Expand Down Expand Up @@ -481,10 +486,12 @@ def source_map_external_require_hash
def find_external_requires source_map
new_set = source_map.requires.map(&:name).to_set
# return if new_set == source_map_external_require_hash[source_map.filename]
_filenames = nil
filenames = ->{ _filenames ||= workspace.filenames.to_set }
source_map_external_require_hash[source_map.filename] = new_set.reject do |path|
workspace.require_paths.any? do |base|
full = Pathname.new(base).join("#{path}.rb").to_s
workspace.filenames.include?(full)
full = File.join(base, path)
filenames[].include?(full) or filenames[].include?(full << ".rb")
end
end
@external_requires = nil
Expand Down
3 changes: 2 additions & 1 deletion lib/solargraph/workspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ def source filename
# @return [Boolean]
def would_require? path
require_paths.each do |rp|
return true if File.exist?(File.join(rp, "#{path}.rb"))
full = File.join rp, path
return true if File.exist?(full) or File.exist?(full << ".rb")
end
false
end
Expand Down
5 changes: 3 additions & 2 deletions lib/solargraph/yard_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ def require_reference path
# @type [Gem::Specification]
spec = spec_for_require(path)
spec.full_require_paths.each do |rp|
file = File.join(rp, "#{path}.rb")
next unless File.file?(file)
file = File.join(rp, path)
file = [file, file + ".rb"].find { |file| File.file?(file) }
next unless file
return Solargraph::Location.new(file, Solargraph::Range.from_to(0, 0, 0, 0))
end
nil
Expand Down

0 comments on commit bcfac73

Please sign in to comment.