Class: Neovim::LineRange

Inherits:
Object
  • 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, _begin, _end) ⇒ LineRange

Returns a new instance of LineRange.



6
7
8
9
10
# File 'lib/neovim/line_range.rb', line 6

def initialize(buffer, _begin, _end)
  @buffer = buffer
  @begin = _begin
  @end = _end
end

Instance Method Details

#[](index) ⇒ Object #[](range) ⇒ Object #[](index, length) ⇒ Object Also known as: slice

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 (Fixnum)
  • #[](range) ⇒ Object

    Parameters:

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

    Parameters:

    • index (Fixnum)
    • length (Fixnum)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/neovim/line_range.rb', line 39

def [](pos, len=nil)
  case pos
  when Range
    LineRange.new(
      @buffer,
      abs_line(pos.begin),
      abs_line(pos.exclude_end? ? pos.end - 1 : pos.end)
    )
  else
    if len
      LineRange.new(
        @buffer,
        abs_line(pos),
        abs_line(pos + len -1)
      )
    else
      @buffer.get_line(abs_line(pos))
    end
  end
end

#[]=(index, string) ⇒ Object #[]=(index, length, strings) ⇒ Object #[]=(range, strings) ⇒ Object

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 (Fixnum)
    • string (String)
  • #[]=(index, length, strings) ⇒ Object

    Parameters:

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

    Parameters:

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


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/neovim/line_range.rb', line 80

def []=(*args)
  *target, val = args
  pos, len = target

  case pos
  when Range
    @buffer.set_line_slice(
      abs_line(pos.begin),
      abs_line(pos.end),
      true,
      !pos.exclude_end?,
      val
    )
  else
    if len
      @buffer.set_line_slice(
        abs_line(pos),
        abs_line(pos + len),
        true,
        false,
        val
      )
    else
      @buffer.set_line(abs_line(pos), val)
    end
  end
end

#delete(index) ⇒ Object

Parameters:

  • index (Fixnum)


115
116
117
# File 'lib/neovim/line_range.rb', line 115

def delete(index)
  @buffer.del_line(abs_line(index))
end

#each {|String| ... } ⇒ Array<String>

Yields:

  • (String)

    The current line

Returns:

  • (Array<String>)


19
20
21
# File 'lib/neovim/line_range.rb', line 19

def each(&block)
  to_a.each(&block)
end

#replace(other) ⇒ Object

Parameters:

  • other (Array)

    The replacement lines



109
110
111
112
# File 'lib/neovim/line_range.rb', line 109

def replace(other)
  self[0..-1] = other
  self
end

#to_aArray<String>

Returns:

  • (Array<String>)


13
14
15
# File 'lib/neovim/line_range.rb', line 13

def to_a
  @buffer.get_line_slice(@begin, @end, true, true)
end