Class: EditorCore::Buffer
- Inherits:
-
Object
- Object
- EditorCore::Buffer
- Includes:
- DRb::DRbObservable
- Defined in:
- lib/editor_core/buffer.rb
Instance Attribute Summary collapse
-
#buffer_id ⇒ Object
Returns the value of attribute buffer_id.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json(options = { }) ⇒ Object
- #break_line(cursor) ⇒ Object
- #can_redo? ⇒ Boolean
- #can_undo? ⇒ Boolean
- #delete(cursor, from, to = nil) ⇒ Object
- #indent(cursor, row, pos) ⇒ Object
-
#initialize(id, name, lines, created_at = 0) ⇒ Buffer
constructor
A new instance of Buffer.
- #insert(cursor, char) ⇒ Object
- #join_lines(cursor, offset = 0) ⇒ Object
- #line_length(row) ⇒ Object
- #lines(r) ⇒ Object
- #lines_count ⇒ Object
- #redo ⇒ Object
- #replace_contents(cursor, new_contents) ⇒ Object
- #to_json(*a) ⇒ Object
- #undo(old_cursor) ⇒ Object
Constructor Details
#initialize(id, name, lines, created_at = 0) ⇒ Buffer
Returns a new instance of Buffer.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/editor_core/buffer.rb', line 15 def initialize(id,name, lines, created_at = 0) @buffer_id = id # An id for this buffer unique for this session @name = name @history = History.new @lines = lines @created_at = case created_at when Numeric Time.at(created_at) when Time created_at when String DateTime.parse(created_at) else raise "Unknown time format: #{created_at}" end end |
Instance Attribute Details
#buffer_id ⇒ Object
Returns the value of attribute buffer_id.
12 13 14 |
# File 'lib/editor_core/buffer.rb', line 12 def buffer_id @buffer_id end |
#created_at ⇒ Object
Returns the value of attribute created_at.
11 12 13 |
# File 'lib/editor_core/buffer.rb', line 11 def created_at @created_at end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
11 12 13 |
# File 'lib/editor_core/buffer.rb', line 11 def history @history end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/editor_core/buffer.rb', line 11 def name @name end |
Class Method Details
.json_create(o) ⇒ Object
48 49 50 51 |
# File 'lib/editor_core/buffer.rb', line 48 def self.json_create(o) d = o["data"] new(d["id"], d["name"], d["lines"], (d["created_at"] || d["modified_at"] || 0)) end |
Instance Method Details
#as_json(options = { }) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/editor_core/buffer.rb', line 32 def as_json( = { }) { "json_class" => self.class.name, "data" => { "id" => @buffer_id, "name" => @name, "lines" => @lines, "created_at" => Time.at(created_at.to_i) } } end |
#break_line(cursor) ⇒ Object
74 75 76 77 78 |
# File 'lib/editor_core/buffer.rb', line 74 def break_line(cursor) modify(cursor, cursor.row..cursor.row) do |l| [l[0][0...cursor.col], l[0][cursor.col..-1]] end end |
#can_redo? ⇒ Boolean
99 100 101 |
# File 'lib/editor_core/buffer.rb', line 99 def can_redo? @history.can_redo? end |
#can_undo? ⇒ Boolean
95 96 97 |
# File 'lib/editor_core/buffer.rb', line 95 def can_undo? @history.can_undo? end |
#delete(cursor, from, to = nil) ⇒ Object
69 70 71 72 |
# File 'lib/editor_core/buffer.rb', line 69 def delete(cursor, from, to =nil) to ||= from modify(cursor, cursor.row) {|l| l[from..to] = ''; l } end |
#indent(cursor, row, pos) ⇒ Object
85 86 87 88 89 |
# File 'lib/editor_core/buffer.rb', line 85 def indent(cursor, row, pos) modify(cursor,row) do |line| (" "*pos)+line.lstrip end end |
#insert(cursor, char) ⇒ Object
65 66 67 |
# File 'lib/editor_core/buffer.rb', line 65 def insert(cursor, char) modify(cursor, cursor.row) {|l| l.insert(cursor.col,char) } end |
#join_lines(cursor, offset = 0) ⇒ Object
80 81 82 83 |
# File 'lib/editor_core/buffer.rb', line 80 def join_lines(cursor,offset=0) row=cursor.row+offset modify(cursor, row..row+1) {|l| l.join } end |
#line_length(row) ⇒ Object
57 58 59 |
# File 'lib/editor_core/buffer.rb', line 57 def line_length(row) @lines[row]&.size || 0 end |
#lines(r) ⇒ Object
91 92 93 |
# File 'lib/editor_core/buffer.rb', line 91 def lines(r) @lines[r] end |
#lines_count ⇒ Object
53 54 55 |
# File 'lib/editor_core/buffer.rb', line 53 def lines_count @lines.size end |
#redo ⇒ Object
111 112 113 114 115 |
# File 'lib/editor_core/buffer.rb', line 111 def redo cursor, rowrange, rows = @history.redo @lines[rowrange] = rows cursor end |
#replace_contents(cursor, new_contents) ⇒ Object
61 62 63 |
# File 'lib/editor_core/buffer.rb', line 61 def replace_contents(cursor,new_contents) modify(cursor, 0..-1) {|l| new_contents } end |
#to_json(*a) ⇒ Object
44 45 46 |
# File 'lib/editor_core/buffer.rb', line 44 def to_json(*a) as_json.to_json(*a) end |
#undo(old_cursor) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/editor_core/buffer.rb', line 103 def undo(old_cursor) cursor, rowrange, rows = @history.undo_snapshot store_snapshot(old_cursor,rowrange, false) cursor, rowrange, rows = @history.undo @lines[rowrange] = rows cursor end |