@@ -18,6 +18,11 @@ module Color
1818 CYAN = 36
1919 WHITE = 37
2020
21+ PRIORITY_ERROR = 0
22+ PRIORITY_HELPER_METHOD = 1
23+ PRIORITY_PRIOR_TOKEN = 2
24+ PRIORITY_NORMAL_TOKEN = 3
25+
2126 # Following pry's colors where possible
2227 TOKEN_SEQS = {
2328 KEYWORD_NIL : [ CYAN , BOLD ] ,
@@ -106,6 +111,8 @@ module Color
106111 method_name : [ CYAN , BOLD ] ,
107112 message_name : [ CYAN ] ,
108113 symbol : [ YELLOW ] ,
114+ # helper method
115+ helper_method : [ BOLD ] ,
109116 # special colorization
110117 error : [ RED , REVERSE ] ,
111118 } . transform_values do |styles |
@@ -162,7 +169,7 @@ def colorize(text, seq, colorable: colorable?)
162169 # If `complete` is false (code is incomplete), this does not warn compile_error.
163170 # This option is needed to avoid warning a user when the compile_error is happening
164171 # because the input is not wrong but just incomplete.
165- def colorize_code ( code , complete : true , ignore_error : false , colorable : colorable? , local_variables : [ ] )
172+ def colorize_code ( code , complete : true , ignore_error : false , colorable : colorable? , local_variables : [ ] , helper_methods : false )
166173 return code unless colorable
167174
168175 result = Prism . parse_lex ( code , scopes : [ local_variables ] )
@@ -180,9 +187,13 @@ def colorize_code(code, complete: true, ignore_error: false, colorable: colorabl
180187 visitor = ColorizeVisitor . new
181188 prism_node . accept ( visitor )
182189
183- error_tokens = errors . map { |e | [ e . location . start_line , e . location . start_column , 0 , e . location . end_line , e . location . end_column , :error , e . location . slice ] }
190+ helper_method_locations = helper_methods ? IRB ::HelperMethod . extract_helper_method_locations ( prism_node ) : [ ]
191+ helper_method_tokens = helper_method_locations . map { |loc | [ loc . start_line , loc . start_column , PRIORITY_HELPER_METHOD , loc . end_line , loc . end_column , :helper_method , loc . slice ] }
192+
193+ error_tokens = errors . map { |e | [ e . location . start_line , e . location . start_column , PRIORITY_ERROR , e . location . end_line , e . location . end_column , :error , e . location . slice ] }
184194 error_tokens . reject! { |t | t . last . match? ( /\A \s *\z / ) }
185- tokens = prism_tokens . map { |t , | [ t . location . start_line , t . location . start_column , 2 , t . location . end_line , t . location . end_column , t . type , t . value ] }
195+
196+ tokens = prism_tokens . map { |t , | [ t . location . start_line , t . location . start_column , PRIORITY_NORMAL_TOKEN , t . location . end_line , t . location . end_column , t . type , t . value ] }
186197 tokens . pop if tokens . last &.[]( 5 ) == :EOF
187198
188199 colored = +''
@@ -201,7 +212,7 @@ def colorize_code(code, complete: true, ignore_error: false, colorable: colorabl
201212 end
202213 }
203214
204- ( visitor . tokens + tokens + error_tokens ) . sort . each do |start_line , start_column , _priority , end_line , end_column , type , value |
215+ ( visitor . tokens + tokens + error_tokens + helper_method_tokens ) . sort . each do |start_line , start_column , _priority , end_line , end_column , type , value |
205216 next if start_line - 1 < line_index || ( start_line - 1 == line_index && start_column < col )
206217
207218 flush . call ( start_line - 1 , start_column )
@@ -235,7 +246,7 @@ def initialize
235246
236247 def dispatch ( location , type )
237248 if location
238- @tokens << [ location . start_line , location . start_column , 1 , location . end_line , location . end_column , type , location . slice ]
249+ @tokens << [ location . start_line , location . start_column , PRIORITY_PRIOR_TOKEN , location . end_line , location . end_column , type , location . slice ]
239250 end
240251 end
241252
0 commit comments