Class: Goodcheck::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/goodcheck/buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, content:) ⇒ Buffer

Returns a new instance of Buffer.



6
7
8
9
# File 'lib/goodcheck/buffer.rb', line 6

def initialize(path:, content:)
  @path = path
  @content = content
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



4
5
6
# File 'lib/goodcheck/buffer.rb', line 4

def content
  @content
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/goodcheck/buffer.rb', line 3

def path
  @path
end

Instance Method Details

#line(line_number) ⇒ Object



37
38
39
# File 'lib/goodcheck/buffer.rb', line 37

def line(line_number)
  content.lines[line_number-1]
end

#line_startsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/goodcheck/buffer.rb', line 11

def line_starts
  unless @line_starts
    @line_starts = []

    start_position = 0

    content.lines.each do |line|
      range = start_position..(start_position + line.bytesize)
      @line_starts << range
      start_position = range.end
    end
  end

  @line_starts
end

#location_for_position(position) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/goodcheck/buffer.rb', line 27

def location_for_position(position)
  line_index = line_starts.bsearch_index do |range|
    position < range.end
  end

  if line_index
    [line_index + 1, position - line_starts[line_index].begin]
  end
end

#position_for_location(line, column) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/goodcheck/buffer.rb', line 41

def position_for_location(line, column)
  if (range = line_starts[line-1])
    pos = range.begin + column
    if pos < range.end
      pos
    end
  end
end