Class: Neovim::LineRange
- Includes:
- Enumerable
- Defined in:
- lib/neovim/line_range.rb
Overview
Provide an enumerable interface for dealing with ranges of lines.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Override #== to compare contents of lines.
-
#[](pos, len = nil) ⇒ Object
(also: #slice)
Access a line or line range.
-
#[]=(*args) ⇒ Object
Set a line or line range.
-
#delete(index) ⇒ Object
Delete the line at the given index within the range.
-
#each {|line| ... } ⇒ Object
Satisfy the
Enumerable
interface by yielding each line. -
#initialize(buffer) ⇒ LineRange
constructor
A new instance of LineRange.
-
#replace(other) ⇒ Object
Replace the range of lines.
-
#to_a ⇒ Array<String>
Resolve to an array of lines as strings.
Constructor Details
#initialize(buffer) ⇒ LineRange
Returns a new instance of LineRange.
6 7 8 |
# File 'lib/neovim/line_range.rb', line 6 def initialize(buffer) @buffer = buffer end |
Instance Method Details
#==(other) ⇒ Object
Override #== to compare contents of lines.
30 31 32 |
# File 'lib/neovim/line_range.rb', line 30 def ==(other) to_a == other.to_a end |
#[](index) ⇒ Object #[](range) ⇒ Object #[](index, length) ⇒ Object Also known as: slice
Access a line or line range.
52 53 54 55 56 57 58 59 60 |
# File 'lib/neovim/line_range.rb', line 52 def [](pos, len=nil) if pos.is_a?(Range) @buffer.get_lines(*range_indices(pos), true) else start, stop = length_indices(pos, len || 1) lines = @buffer.get_lines(start, stop, true) len ? lines : lines.first end end |
#[]=(index, string) ⇒ Object #[]=(index, length, strings) ⇒ Object #[]=(range, strings) ⇒ Object
Set a line or line range.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/neovim/line_range.rb', line 84 def []=(*args) *target, val = args pos, len = target if pos.is_a?(Range) @buffer.set_lines(*range_indices(pos), true, Array(val)) else start, stop = length_indices(pos, len || 1) @buffer.set_lines(start, stop, true, Array(val)) end end |
#delete(index) ⇒ Object
Delete the line at the given index within the range.
107 108 109 110 111 |
# File 'lib/neovim/line_range.rb', line 107 def delete(index) i = Integer(index) self[i].tap { self[i, 1] = [] } rescue TypeError end |
#each {|line| ... } ⇒ Object
Satisfy the Enumerable
interface by yielding each line.
13 14 15 16 17 18 |
# File 'lib/neovim/line_range.rb', line 13 def each(&block) (0...@buffer.count).each_slice(5000) do |linenos| start, stop = linenos[0], linenos[-1] + 1 @buffer.get_lines(start, stop, true).each(&block) end end |
#replace(other) ⇒ Object
Replace the range of lines.
99 100 101 102 |
# File 'lib/neovim/line_range.rb', line 99 def replace(other) self[0..-1] = other.to_ary self end |
#to_a ⇒ Array<String>
Resolve to an array of lines as strings.
23 24 25 |
# File 'lib/neovim/line_range.rb', line 23 def to_a map { |line| line } end |