class Neovim::LineRange

Provide an enumerable interface for dealing with ranges of lines.

Public Class Methods

new(buffer) click to toggle source
# File lib/neovim/line_range.rb, line 6
def initialize(buffer)
  @buffer = buffer
end

Public Instance Methods

==(other) click to toggle source

Override +#==+ to compare contents of lines.

@return Boolean

# File lib/neovim/line_range.rb, line 30
def ==(other)
  to_a == other.to_a
end
[](pos, len=nil) click to toggle source

Access a line or line range.

@overload [](index)

@param index [Integer]

@overload [](range)

@param range [Range]

@overload [](index, length)

@param index [Integer]
@param length [Integer]

@example Get the first line using an index

line_range[0] # => "first"

@example Get the first two lines using a Range

line_range[0..1] # => ["first", "second"]

@example Get the first two lines using an index and length

line_range[0, 2] # => ["first", "second"]
# 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
Also aliased as: slice
[]=(*args) click to toggle source

Set a line or line range.

@overload []=(index, string)

@param index [Integer]
@param string [String]

@overload []=(index, length, strings)

@param index [Integer]
@param length [Integer]
@param strings [Array<String>]

@overload []=(range, strings)

@param range [Range]
@param strings [Array<String>]

@example Replace the first line using an index

line_range[0] = "first"

@example Replace the first two lines using a Range

line_range[0..1] = ["first", "second"]

@example Replace the first two lines using an index and length

line_range[0, 2] = ["first", "second"]
# 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) click to toggle source

Delete the line at the given index within the range.

@param index [Integer]

# File lib/neovim/line_range.rb, line 107
def delete(index)
  i = Integer(index)
  self[i].tap { self[i, 1] = [] }
rescue TypeError
end
each(&block) click to toggle source

Satisfy the Enumerable interface by yielding each line.

@yieldparam line [String]

# 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) click to toggle source

Replace the range of lines.

@param other [Array] The replacement lines

# File lib/neovim/line_range.rb, line 99
def replace(other)
  self[0..-1] = other.to_ary
  self
end
slice(pos, len=nil)
Alias for: []
to_a() click to toggle source

Resolve to an array of lines as strings.

@return [Array<String>]

# File lib/neovim/line_range.rb, line 23
def to_a
  map { |line| line }
end

Private Instance Methods

adjust_index(i) click to toggle source
# File lib/neovim/line_range.rb, line 130
def adjust_index(i)
  i < 0 ? i - 1 : i
end
length_indices(index, len) click to toggle source
# File lib/neovim/line_range.rb, line 123
def length_indices(index, len)
  start = adjust_index(index)
  stop = start < 0 ? [start + len, -1].min : start + len

  [start, stop]
end
range_indices(range) click to toggle source
# File lib/neovim/line_range.rb, line 115
def range_indices(range)
  start = adjust_index(range.begin)
  stop = adjust_index(range.end)
  stop += 1 unless range.exclude_end?

  [start, stop]
end