Class: Line

Inherits:
Object
  • Object
show all
Defined in:
lib/troshka/editor/line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, cursor = 0) ⇒ Line

Returns a new instance of Line.



5
6
7
8
# File 'lib/troshka/editor/line.rb', line 5

def initialize(text, cursor=0)
  @text = text
  @cursor = cursor
end

Instance Attribute Details

#cursorObject

Returns the value of attribute cursor.



2
3
4
# File 'lib/troshka/editor/line.rb', line 2

def cursor
  @cursor
end

#selectionObject

Returns the value of attribute selection.



3
4
5
# File 'lib/troshka/editor/line.rb', line 3

def selection
  @selection
end

Instance Method Details

#replace_selection_text(text) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/troshka/editor/line.rb', line 31

def replace_selection_text(text)
  pre = self.text [0, selection.first]
  post = self.text [selection[1], size]
  
  @text = "#{pre}#{text}#{post}"
  @cursor = selection[1] + text.size
  @selection = nil
end

#sizeObject



40
41
42
# File 'lib/troshka/editor/line.rb', line 40

def size
  @text.size
end

#text(range = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/troshka/editor/line.rb', line 19

def text(range=nil)
  if range.nil?
    @text
  else
    if range[0]==range[1]
      ""
    else
    @text[range[0]..range[1]-1]
    end
  end
end

#word(separator = /[ ,();]/) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/troshka/editor/line.rb', line 10

def word(separator=/[ ,();]/)
  word_begin = @text.rindex(separator, cursor-1)+1 rescue 0
  word_begin = 0 if word_begin.nil?
  word_end = @text.index(separator, cursor)
  word_end = size if word_end.nil?
  
  [word_begin, word_end]
end