Class: Goodcheck::Buffer
- Inherits:
-
Object
- Object
- Goodcheck::Buffer
- Defined in:
- lib/goodcheck/buffer.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path:, content:) ⇒ Buffer
constructor
A new instance of Buffer.
- #line(line_number) ⇒ Object
- #line_ranges ⇒ Object
- #location_for_position(position) ⇒ Object
- #position_for_location(line, column) ⇒ Object
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
#content ⇒ Object (readonly)
Returns the value of attribute content.
4 5 6 |
# File 'lib/goodcheck/buffer.rb', line 4 def content @content end |
#path ⇒ Object (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_ranges ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/goodcheck/buffer.rb', line 11 def line_ranges unless @line_ranges @line_ranges = [] start_position = 0 content.split(/\n/, -1).each do |line| range = start_position...(start_position + line.bytesize) @line_ranges << range start_position = range.end + 1 end end @line_ranges 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_ranges.bsearch_index do |range| position <= range.end end if line_index [line_index + 1, position - line_ranges[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_ranges[line-1]) pos = range.begin + column if pos <= range.end pos end end end |