Class: SeeingIsBelieving::Binary::AnnotateMarkedLines

Inherits:
Object
  • Object
show all
Defined in:
lib/seeing_is_believing/binary/annotate_marked_lines.rb

Overview

Based on the behaviour of xmpfilger (a binary in the rcodetools gem) See github.com/JoshCheek/seeing_is_believing/issues/44 for more details

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, results, options = {}) ⇒ AnnotateMarkedLines

Returns a new instance of AnnotateMarkedLines.



64
65
66
67
68
# File 'lib/seeing_is_believing/binary/annotate_marked_lines.rb', line 64

def initialize(body, results, options={})
  @options = options
  @body    = body
  @results = results
end

Class Method Details

.call(body, results, options) ⇒ Object



60
61
62
# File 'lib/seeing_is_believing/binary/annotate_marked_lines.rb', line 60

def self.call(body, results, options)
  new(body, results, options).call
end

.code_rewriter(markers) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/seeing_is_believing/binary/annotate_marked_lines.rb', line 14

def self.code_rewriter(markers)
  lambda do |program|
    inspect_linenos = []
    pp_linenos      = []
    value_regex     = markers[:value][:regex]
    Code.new(program).inline_comments.each do |c|
      next unless c.text[value_regex]
      c.whitespace_col == 0 ? pp_linenos      << c.line_number - 1
                            : inspect_linenos << c.line_number
    end

    should_inspect = false
    should_pp      = false
    WrapExpressions.call \
      program,
      before_each: -> line_number {
        # 74 b/c pretty print_defaults to 79 (guessing 80 chars with 1 reserved for newline), and
        # 79 - "# => ".length # => 4
        inspect        = "$SiB.record_result(:inspect, #{line_number}, ("
        pp             = "$SiB.record_result(:pp, #{line_number}, ("

        should_inspect = inspect_linenos.include? line_number
        should_pp      = pp_linenos.include?      line_number

        if    should_inspect && should_pp then "#{pp}#{inspect}"
        elsif should_inspect              then inspect
        elsif should_pp                   then pp
        else                                   ""
        end
      },
      after_each:  -> line_number {
        inspect = "))"
        pp      = ")) { |v| PP.pp v, '', 74 }"

        should_inspect = inspect_linenos.include? line_number
        should_pp      = pp_linenos.include?      line_number

        if    should_inspect && should_pp then "#{inspect}#{pp}"
        elsif should_inspect              then inspect
        elsif should_pp                   then pp
        else                                   ""
        end
      }
  end
end

Instance Method Details

#callObject

seems like maybe this should respect the alignment strategy (not what xmpfilter does, but there are other ways I’d like to deviate anyway) and we should just add a new alignment strategy for default xmpfilter style



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/seeing_is_believing/binary/annotate_marked_lines.rb', line 72

def call
  @new_body ||= begin
    require 'seeing_is_believing/binary/rewrite_comments'
    require 'seeing_is_believing/binary/format_comment'
    include_lines = []

    if @results.has_exception?
      exception_result  = sprintf "%s: %s", @results.exception.class_name, @results.exception.message.gsub("\n", '\n')
      exception_lineno  = @results.exception.line_number
      include_lines << exception_lineno
    end

    new_body = RewriteComments.call @body, include_lines: include_lines do |comment|
      exception_on_line  = exception_lineno == comment.line_number
      annotate_this_line = comment.text[value_regex]
      pp_annotation      = annotate_this_line && comment.whitespace_col.zero?
      normal_annotation  = annotate_this_line && !pp_annotation
      if exception_on_line && annotate_this_line
        [comment.whitespace, FormatComment.call(comment.text_col, value_prefix, exception_result, @options)]
      elsif exception_on_line
        whitespace = comment.whitespace
        whitespace = " " if whitespace.empty?
        [whitespace, FormatComment.call(0, exception_prefix, exception_result, @options)]
      elsif normal_annotation
        result = @results[comment.line_number].map { |result| result.gsub "\n", '\n' }.join(', ')
        [comment.whitespace, FormatComment.call(comment.text_col, value_prefix, result, @options)]
      elsif pp_annotation
        result = @results[comment.line_number-1, :pp].map { |result| result.chomp }.join("\n,") # ["1\n2", "1\n2", ...
        swap_leading_whitespace_in_multiline_comment(result)
        comment_lines = result.each_line.map.with_index do |comment_line, result_offest|
          if result_offest == 0
            FormatComment.call(comment.whitespace_col, value_prefix, comment_line.chomp, @options)
          else
            leading_whitespace = " " * comment.text_col
            leading_whitespace << FormatComment.call(comment.whitespace_col, nextline_prefix, comment_line.chomp, @options)
          end
        end
        comment_lines = [value_prefix.rstrip] if comment_lines.empty?
        [comment.whitespace, comment_lines.join("\n")]
      else
        [comment.whitespace, comment.text]
      end
    end

    require 'seeing_is_believing/binary/annotate_end_of_file'
    AnnotateEndOfFile.add_stdout_stderr_and_exceptions_to new_body, @results, @options

    new_body
  end
end

#exception_prefixObject



131
132
133
# File 'lib/seeing_is_believing/binary/annotate_marked_lines.rb', line 131

def exception_prefix
  @exception_prefix ||= @options[:markers][:exception][:prefix]
end

#nextline_prefixObject



127
128
129
# File 'lib/seeing_is_believing/binary/annotate_marked_lines.rb', line 127

def nextline_prefix
  @nextline_prefix ||= ('#' + ' '*value_prefix.length.pred)
end

#swap_leading_whitespace_in_multiline_comment(comment) ⇒ Object



139
140
141
142
143
144
# File 'lib/seeing_is_believing/binary/annotate_marked_lines.rb', line 139

def swap_leading_whitespace_in_multiline_comment(comment)
  return if comment.scan("\n").size < 2
  return if comment[0] =~ /\S/
  nonbreaking_space = " "
  comment[0] = nonbreaking_space
end

#value_prefixObject



123
124
125
# File 'lib/seeing_is_believing/binary/annotate_marked_lines.rb', line 123

def value_prefix
  @value_prefix ||= @options[:markers][:value][:prefix]
end

#value_regexObject



135
136
137
# File 'lib/seeing_is_believing/binary/annotate_marked_lines.rb', line 135

def value_regex
  @value_regex ||= @options[:markers][:value][:regex]
end