Class: EditorCore::Cursor
- Inherits:
-
Object
- Object
- EditorCore::Cursor
- Defined in:
- lib/editor_core/cursor.rb
Instance Attribute Summary collapse
-
#col ⇒ Object
readonly
Returns the value of attribute col.
-
#row ⇒ Object
readonly
Returns the value of attribute row.
Instance Method Summary collapse
- #beginning_of_file? ⇒ Boolean
- #clamp(buffer) ⇒ Object
- #down(buffer, offset = 1) ⇒ Object
- #end_of_file?(buffer) ⇒ Boolean
- #end_of_line?(buffer) ⇒ Boolean
- #enter(buffer) ⇒ Object
- #final_line?(buffer) ⇒ Boolean
-
#initialize(row = 0, col = 0) ⇒ Cursor
constructor
A new instance of Cursor.
- #left(buffer, offset = 1) ⇒ Object
- #line_end(buffer) ⇒ Object
- #line_home ⇒ Object
- #move(buffer, row, col) ⇒ Object
- #right(buffer, offset = 1) ⇒ Object
- #up(buffer, offset = 1) ⇒ Object
Constructor Details
#initialize(row = 0, col = 0) ⇒ Cursor
Returns a new instance of Cursor.
6 7 8 9 10 |
# File 'lib/editor_core/cursor.rb', line 6 def initialize(row = 0, col = 0) @row = row @col = col freeze end |
Instance Attribute Details
#col ⇒ Object (readonly)
Returns the value of attribute col.
4 5 6 |
# File 'lib/editor_core/cursor.rb', line 4 def col @col end |
#row ⇒ Object (readonly)
Returns the value of attribute row.
4 5 6 |
# File 'lib/editor_core/cursor.rb', line 4 def row @row end |
Instance Method Details
#beginning_of_file? ⇒ Boolean
68 69 70 |
# File 'lib/editor_core/cursor.rb', line 68 def beginning_of_file? row == 0 && col == 0 end |
#clamp(buffer) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/editor_core/cursor.rb', line 29 def clamp(buffer) row = @row.clamp(0, buffer.lines_count - 1) # FIXME: Is this `buffer.lines` check needed? It ought not to be. if !buffer.lines(row) col = 0 else col = @col.clamp(0, buffer.line_length(row)) end Cursor.new(row,col) end |
#down(buffer, offset = 1) ⇒ Object
14 |
# File 'lib/editor_core/cursor.rb', line 14 def down(buffer, offset = 1); move(buffer, row+offset,col); end |
#end_of_file?(buffer) ⇒ Boolean
64 65 66 |
# File 'lib/editor_core/cursor.rb', line 64 def end_of_file?(buffer) final_line?(buffer) && end_of_line?(buffer) end |
#end_of_line?(buffer) ⇒ Boolean
52 53 54 55 56 57 58 |
# File 'lib/editor_core/cursor.rb', line 52 def end_of_line?(buffer) if buffer.lines(row) col == buffer.line_length(row) else true end end |
#enter(buffer) ⇒ Object
40 41 42 |
# File 'lib/editor_core/cursor.rb', line 40 def enter(buffer) down(buffer).line_home end |
#final_line?(buffer) ⇒ Boolean
60 61 62 |
# File 'lib/editor_core/cursor.rb', line 60 def final_line?(buffer) row == buffer.lines_count - 1 end |
#left(buffer, offset = 1) ⇒ Object
16 17 18 19 20 |
# File 'lib/editor_core/cursor.rb', line 16 def left(buffer, offset = 1) return Cursor.new(row, col - offset) if offset <= col return self if beginning_of_file? return move(buffer,row-1,buffer.line_length(row-1)) end |
#line_end(buffer) ⇒ Object
48 49 50 |
# File 'lib/editor_core/cursor.rb', line 48 def line_end(buffer) Cursor.new(row, buffer.line_length(row)) end |
#line_home ⇒ Object
44 45 46 |
# File 'lib/editor_core/cursor.rb', line 44 def line_home Cursor.new(row, 0) end |
#move(buffer, row, col) ⇒ Object
12 |
# File 'lib/editor_core/cursor.rb', line 12 def move(buffer,row,col); self.class.new(row,col).clamp(buffer); end |
#right(buffer, offset = 1) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/editor_core/cursor.rb', line 22 def right(buffer, offset = 1) #buffer.right(self,offset) return Cursor.new(row, col + offset).clamp(buffer) unless end_of_line?(buffer) return self if final_line?(buffer) Cursor.new(row + 1, 0) end |
#up(buffer, offset = 1) ⇒ Object
13 |
# File 'lib/editor_core/cursor.rb', line 13 def up(buffer, offset = 1); move(buffer, row-offset,col); end |