Class: Halunke::SourceCodePosition

Inherits:
Object
  • Object
show all
Defined in:
lib/halunke/source_code_position.rb

Instance Method Summary collapse

Constructor Details

#initialize(ts, te) ⇒ SourceCodePosition

Returns a new instance of SourceCodePosition.



3
4
5
6
# File 'lib/halunke/source_code_position.rb', line 3

def initialize(ts, te)
  @ts = ts
  @te = te
end

Instance Method Details

#reveal(source, error_mode) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/halunke/source_code_position.rb', line 8

def reveal(source, error_mode)
  line, line_number = source.lines.each_with_index do |candidate, candidate_line_number|
    break candidate, candidate_line_number if @ts < candidate.length

    @ts -= candidate.length
    @te -= candidate.length
  end

  if @te > line.length
    @te = line.length - 2
    ellipsis = '...'
  end

  prefix = error_mode == :repl ? ">> " : "#{line_number + 1} | "

  output = []
  output << [prefix, line.rstrip, ellipsis].join("") if error_mode == :file
  output << " " * (@ts + prefix.length) + "^" * (@te - @ts + 1)
  output << "\n"

  output
end