Skip to content

Commit

Permalink
Return nil if is not an actual file path
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Jul 27, 2023
1 parent 43ea1d4 commit 659b09e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/irb/cmd/edit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def execute(*args)
# in this case, we should just ignore the error
end

if source && File.exist?(source.file)
if source
path = source.file
else
puts "Can not find file: #{path}"
Expand Down
2 changes: 1 addition & 1 deletion lib/irb/cmd/show_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def execute(str = nil)

source = SourceFinder.new(@irb_context).find_source(str)

if source && File.exist?(source.file)
if source
show_source(source)
else
puts "Error: Couldn't locate a definition for #{str}"
Expand Down
9 changes: 4 additions & 5 deletions lib/irb/source_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ def find_source(signature)
method = Regexp.last_match[:method]
file, line = receiver.method(method).source_location if receiver.respond_to?(method, true)
end
if file && line
Source.new(file: file, first_line: line, last_line: find_end(file, line, @irb_context))
if file && line && File.exist?(file)
Source.new(file: file, first_line: line, last_line: find_end(file, line))
end
end

private

def find_end(file, first_line, irb_context)
return first_line unless File.exist?(file)
lex = RubyLex.new(irb_context)
def find_end(file, first_line)
lex = RubyLex.new(@irb_context)
lines = File.read(file).lines[(first_line - 1)..-1]
tokens = RubyLex.ripper_lex_without_warning(lines.join)
prev_tokens = []
Expand Down

0 comments on commit 659b09e

Please sign in to comment.