Class: Spacy::TextBuffer

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/spacy/textbuffer.rb

Instance Method Summary collapse

Constructor Details

#initialize(str = nil) ⇒ TextBuffer

Returns a new instance of TextBuffer.



11
12
13
14
# File 'lib/spacy/textbuffer.rb', line 11

def initialize (str = nil)
  @lines = [TextLine.new]
  insert 0, str.to_s if str
end

Instance Method Details

#[](*args) ⇒ Object



78
79
80
# File 'lib/spacy/textbuffer.rb', line 78

def [] (*args)
  @lines[*args]
end

#[]=(row, line) ⇒ Object



82
83
84
# File 'lib/spacy/textbuffer.rb', line 82

def []= (row, line)
  @lines[row] = line.to_textline
end

#cursor2pos(row, col) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/spacy/textbuffer.rb', line 100

def cursor2pos (row, col)
  nrow_ = nrow
  if row < 0
    row = 0
  elsif row >= nrow_
    row = nrow_ - 1
  end
  ncol_ = ncolumn(row)
  if col < 0
    col = 0
  elsif col >= ncol_
    col = ncol_ - 1
  end
  c2p row, col, nrow_, ncol_
end

#each(*args, &block) ⇒ Object



86
87
88
# File 'lib/spacy/textbuffer.rb', line 86

def each (*args, &block)
  @lines.each *args, &block
end

#empty?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/spacy/textbuffer.rb', line 66

def empty? ()
  nrow == 1 && ncolumn(0) == 1
end

#erase(first, last = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/spacy/textbuffer.rb', line 35

def erase (first, last = nil)
  last = first + 1 unless last
  first, last = last, first unless first < last
  last_ = self.last
  row1, col1 = p2c first, last_
  row2, col2 = p2c last, last_
  return self if row1 == row2 && col1 == col2

  if row1 == row2
    @lines[row1].erase col1, col2
  else
    @lines[row2][0...col2] = @lines[row1][0...col1]
    @lines[row1...row2] = []
  end
  self
end

#insert(pos, str) ⇒ Object

Raises:

  • (RangeError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/spacy/textbuffer.rb', line 16

def insert (pos, str)
  last_ = last
  raise RangeError unless 0 <= pos && pos <= last_
  str.to_s.split(/(\r\n|\r|\n)/).each do |s|
    row, col = p2c pos, last_
    added = 0
    if s =~ /\r|\n/
      split_line row, col
      added = 1
    else
      self[row].insert s, col
      added = s.size
    end
    pos   += added
    last_ += added
  end
  pos
end

#inspectObject



116
117
118
119
# File 'lib/spacy/textbuffer.rb', line 116

def inspect ()
  text = @lines.map{|x| x.to_s}.join('\n')[0, 64]
  "#<TextBuffer #{nrow} lines, '#{text}'>"
end

#lastObject



61
62
63
64
# File 'lib/spacy/textbuffer.rb', line 61

def last ()
  raise 'Invalid text buffer.' if @lines.empty?
  inject(0) {|n, line| n + line.ncolumn} - 1
end

#ncolumn(row) ⇒ Object

Raises:

  • (RangeError)


56
57
58
59
# File 'lib/spacy/textbuffer.rb', line 56

def ncolumn (row)
  raise RangeError unless 0 <= row && row < nrow
  self[row].ncolumn
end

#nrowObject



52
53
54
# File 'lib/spacy/textbuffer.rb', line 52

def nrow ()
  @lines.size
end

#pos2cursor(pos) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/spacy/textbuffer.rb', line 90

def pos2cursor (pos)
  last_ = last
  if pos < 0
    pos = 0
  elsif pos > last_
    pos = last_
  end
  p2c pos, last_
end

#to_aObject



70
71
72
# File 'lib/spacy/textbuffer.rb', line 70

def to_a ()
  @lines
end

#to_sObject



74
75
76
# File 'lib/spacy/textbuffer.rb', line 74

def to_s ()
  to_a.join("\n")
end