Class: Neovim::LineRange

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/neovim/line_range.rb

Overview

Provide an enumerable interface for dealing with ranges of lines.

Instance Method Summary collapse

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.

Returns:

  • Boolean



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.

Examples:

Get the first line using an index

line_range[0] # => "first"

Get the first two lines using a Range

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

Get the first two lines using an index and length

line_range[0, 2] # => ["first", "second"]

Overloads:

  • #[](index) ⇒ Object

    Parameters:

    • index (Integer)
  • #[](range) ⇒ Object

    Parameters:

    • range (Range)
  • #[](index, length) ⇒ Object

    Parameters:

    • index (Integer)
    • length (Integer)


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.

Examples:

Replace the first line using an index

line_range[0] = "first"

Replace the first two lines using a Range

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

Replace the first two lines using an index and length

line_range[0, 2] = ["first", "second"]

Overloads:

  • #[]=(index, string) ⇒ Object

    Parameters:

    • index (Integer)
    • string (String)
  • #[]=(index, length, strings) ⇒ Object

    Parameters:

    • index (Integer)
    • length (Integer)
    • strings (Array<String>)
  • #[]=(range, strings) ⇒ Object

    Parameters:

    • range (Range)
    • strings (Array<String>)


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.

Parameters:

  • index (Integer)


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.

Yield Parameters:

  • line (String)


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.

Parameters:

  • other (Array)

    The replacement 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_aArray<String>

Resolve to an array of lines as strings.

Returns:

  • (Array<String>)


23
24
25
# File 'lib/neovim/line_range.rb', line 23

def to_a
  map { |line| line }
end