Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9790517

Browse files
authoredJul 31, 2023
Decouple edit and show_source commands (#658)
* Decouple `edit` command from `show_source` 2 commands should not depend on each other. If `edit` command also needs to find a source, the source finding logic should be extracted into a separate class. * Return nil if is not an actual file path * Refactor SourceFinder
1 parent 82d1687 commit 9790517

File tree

3 files changed

+71
-65
lines changed

3 files changed

+71
-65
lines changed
 

‎lib/irb/cmd/edit.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'shellwords'
22
require_relative "nop"
3+
require_relative "../source_finder"
34

45
module IRB
56
# :stopdoc:
@@ -28,17 +29,15 @@ def execute(*args)
2829
end
2930

3031
if !File.exist?(path)
31-
require_relative "show_source"
32-
3332
source =
3433
begin
35-
ShowSource.find_source(path, @irb_context)
34+
SourceFinder.new(@irb_context).find_source(path)
3635
rescue NameError
3736
# if user enters a path that doesn't exist, it'll cause NameError when passed here because find_source would try to evaluate it as well
3837
# in this case, we should just ignore the error
3938
end
4039

41-
if source && File.exist?(source.file)
40+
if source
4241
path = source.file
4342
else
4443
puts "Can not find file: #{path}"

‎lib/irb/cmd/show_source.rb

Lines changed: 4 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# frozen_string_literal: true
22

33
require_relative "nop"
4+
require_relative "../source_finder"
45
require_relative "../color"
5-
require_relative "../ruby-lex"
66

77
module IRB
8-
# :stopdoc:
9-
108
module ExtendCommand
119
class ShowSource < Nop
1210
category "Context"
@@ -21,51 +19,6 @@ def transform_args(args)
2119
args.strip.dump
2220
end
2321
end
24-
25-
def find_source(str, irb_context)
26-
case str
27-
when /\A[A-Z]\w*(::[A-Z]\w*)*\z/ # Const::Name
28-
eval(str, irb_context.workspace.binding) # trigger autoload
29-
base = irb_context.workspace.binding.receiver.yield_self { |r| r.is_a?(Module) ? r : Object }
30-
file, line = base.const_source_location(str)
31-
when /\A(?<owner>[A-Z]\w*(::[A-Z]\w*)*)#(?<method>[^ :.]+)\z/ # Class#method
32-
owner = eval(Regexp.last_match[:owner], irb_context.workspace.binding)
33-
method = Regexp.last_match[:method]
34-
if owner.respond_to?(:instance_method)
35-
methods = owner.instance_methods + owner.private_instance_methods
36-
file, line = owner.instance_method(method).source_location if methods.include?(method.to_sym)
37-
end
38-
when /\A((?<receiver>.+)(\.|::))?(?<method>[^ :.]+)\z/ # method, receiver.method, receiver::method
39-
receiver = eval(Regexp.last_match[:receiver] || 'self', irb_context.workspace.binding)
40-
method = Regexp.last_match[:method]
41-
file, line = receiver.method(method).source_location if receiver.respond_to?(method, true)
42-
end
43-
if file && line
44-
Source.new(file: file, first_line: line, last_line: find_end(file, line, irb_context))
45-
end
46-
end
47-
48-
private
49-
50-
def find_end(file, first_line, irb_context)
51-
return first_line unless File.exist?(file)
52-
lex = RubyLex.new(irb_context)
53-
lines = File.read(file).lines[(first_line - 1)..-1]
54-
tokens = RubyLex.ripper_lex_without_warning(lines.join)
55-
prev_tokens = []
56-
57-
# chunk with line number
58-
tokens.chunk { |tok| tok.pos[0] }.each do |lnum, chunk|
59-
code = lines[0..lnum].join
60-
prev_tokens.concat chunk
61-
continue = lex.should_continue?(prev_tokens)
62-
syntax = lex.check_code_syntax(code)
63-
if !continue && syntax == :valid
64-
return first_line + lnum
65-
end
66-
end
67-
first_line
68-
end
6922
end
7023

7124
def execute(str = nil)
@@ -74,8 +27,9 @@ def execute(str = nil)
7427
return
7528
end
7629

77-
source = self.class.find_source(str, @irb_context)
78-
if source && File.exist?(source.file)
30+
source = SourceFinder.new(@irb_context).find_source(str)
31+
32+
if source
7933
show_source(source)
8034
else
8135
puts "Error: Couldn't locate a definition for #{str}"
@@ -85,7 +39,6 @@ def execute(str = nil)
8539

8640
private
8741

88-
# @param [IRB::ExtendCommand::ShowSource::Source] source
8942
def show_source(source)
9043
puts
9144
puts "#{bold("From")}: #{source.file}:#{source.first_line}"
@@ -98,16 +51,6 @@ def show_source(source)
9851
def bold(str)
9952
Color.colorize(str, [:BOLD])
10053
end
101-
102-
Source = Struct.new(
103-
:file, # @param [String] - file name
104-
:first_line, # @param [String] - first line
105-
:last_line, # @param [String] - last line
106-
keyword_init: true,
107-
)
108-
private_constant :Source
10954
end
11055
end
111-
112-
# :startdoc:
11356
end

‎lib/irb/source_finder.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "ruby-lex"
4+
5+
module IRB
6+
class SourceFinder
7+
Source = Struct.new(
8+
:file, # @param [String] - file name
9+
:first_line, # @param [String] - first line
10+
:last_line, # @param [String] - last line
11+
keyword_init: true,
12+
)
13+
private_constant :Source
14+
15+
def initialize(irb_context)
16+
@irb_context = irb_context
17+
end
18+
19+
def find_source(signature)
20+
context_binding = @irb_context.workspace.binding
21+
case signature
22+
when /\A[A-Z]\w*(::[A-Z]\w*)*\z/ # Const::Name
23+
eval(signature, context_binding) # trigger autoload
24+
base = context_binding.receiver.yield_self { |r| r.is_a?(Module) ? r : Object }
25+
file, line = base.const_source_location(signature)
26+
when /\A(?<owner>[A-Z]\w*(::[A-Z]\w*)*)#(?<method>[^ :.]+)\z/ # Class#method
27+
owner = eval(Regexp.last_match[:owner], context_binding)
28+
method = Regexp.last_match[:method]
29+
if owner.respond_to?(:instance_method)
30+
methods = owner.instance_methods + owner.private_instance_methods
31+
file, line = owner.instance_method(method).source_location if methods.include?(method.to_sym)
32+
end
33+
when /\A((?<receiver>.+)(\.|::))?(?<method>[^ :.]+)\z/ # method, receiver.method, receiver::method
34+
receiver = eval(Regexp.last_match[:receiver] || 'self', context_binding)
35+
method = Regexp.last_match[:method]
36+
file, line = receiver.method(method).source_location if receiver.respond_to?(method, true)
37+
end
38+
if file && line && File.exist?(file)
39+
Source.new(file: file, first_line: line, last_line: find_end(file, line))
40+
end
41+
end
42+
43+
private
44+
45+
def find_end(file, first_line)
46+
lex = RubyLex.new(@irb_context)
47+
lines = File.read(file).lines[(first_line - 1)..-1]
48+
tokens = RubyLex.ripper_lex_without_warning(lines.join)
49+
prev_tokens = []
50+
51+
# chunk with line number
52+
tokens.chunk { |tok| tok.pos[0] }.each do |lnum, chunk|
53+
code = lines[0..lnum].join
54+
prev_tokens.concat chunk
55+
continue = lex.should_continue?(prev_tokens)
56+
syntax = lex.check_code_syntax(code)
57+
if !continue && syntax == :valid
58+
return first_line + lnum
59+
end
60+
end
61+
first_line
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)
Please sign in to comment.