类 SyntaxSuggest::DisplayCodeWithLineNumbers
输出带有高亮行的代码
传递给此类的任何内容都将被渲染,即使它被“标记为不可见”,任何输出过滤都应在调用此类之前完成。
DisplayCodeWithLineNumbers.new( lines: lines, highlight_lines: [lines[2], lines[3]] ).call # => 1 2 def cat > 3 Dir.chdir > 4 end 5 end 6
常量
- TERMINAL_END
- TERMINAL_HIGHLIGHT
公共类方法
new(lines:, highlight_lines: [], terminal: false) 点击切换源代码
# File lib/syntax_suggest/display_code_with_line_numbers.rb, line 25 def initialize(lines:, highlight_lines: [], terminal: false) @lines = Array(lines).sort @terminal = terminal @highlight_line_hash = Array(highlight_lines).each_with_object({}) { |line, h| h[line] = true } @digit_count = @lines.last&.line_number.to_s.length end
公共实例方法
call() 点击切换源代码
# File lib/syntax_suggest/display_code_with_line_numbers.rb, line 32 def call @lines.map do |line| format_line(line) end.join end
私有实例方法
format(contents:, number:, empty:, highlight: false) 点击切换源代码
# File lib/syntax_suggest/display_code_with_line_numbers.rb, line 50 def format(contents:, number:, empty:, highlight: false) string = +"" string << if highlight "> " else " " end string << number.rjust(@digit_count).to_s if empty string << contents else string << " " string << TERMINAL_HIGHLIGHT if @terminal && highlight string << contents string << TERMINAL_END if @terminal end string end
format_line(code_line) 点击切换源代码
# File lib/syntax_suggest/display_code_with_line_numbers.rb, line 38 def format_line(code_line) # Handle trailing slash lines code_line.original.lines.map.with_index do |contents, i| format( empty: code_line.empty?, number: (code_line.number + i).to_s, contents: contents, highlight: @highlight_line_hash[code_line] ) end.join end