Class: Holistic::Document::Unsaved::Record
- Inherits:
-
Object
- Object
- Holistic::Document::Unsaved::Record
- Defined in:
- lib/holistic/document/unsaved/record.rb
Constant Summary collapse
- LINE_BREAK =
"\n"
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #apply_change(change) ⇒ Object
- #expand_code(cursor) ⇒ Object
- #has_unsaved_changes? ⇒ Boolean
-
#initialize(path:, content:) ⇒ Record
constructor
A new instance of Record.
- #mark_as_saved! ⇒ Object
- #restore_original_content! ⇒ Object
Constructor Details
#initialize(path:, content:) ⇒ Record
Returns a new instance of Record.
7 8 9 10 11 |
# File 'lib/holistic/document/unsaved/record.rb', line 7 def initialize(path:, content:) @path = path @content = content @original_content = content.dup end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
5 6 7 |
# File 'lib/holistic/document/unsaved/record.rb', line 5 def content @content end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/holistic/document/unsaved/record.rb', line 5 def path @path end |
Instance Method Details
#apply_change(change) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/holistic/document/unsaved/record.rb', line 53 def apply_change(change) line = 0 column = 0 # first edition to the document is special because we can't iterate over the content to find the insert position. # there is nothing to iterate over. if @content.empty? && change.insertion? @content = change.text return end @content.each_char.with_index do |char, index| if change.insertion? && change.starts_on?(line, column) content.insert(index, change.text) return end if change.deletion? && change.starts_on?(line, column) content[index..index + change.range_length - 1] = "" return end if char == LINE_BREAK line += 1 column = 0 else column += 1 end end # off-by-one error to insert at the of the document if change.insertion? && change.starts_on?(line, column) content.insert(@content.length, change.text) end end |
#expand_code(cursor) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/holistic/document/unsaved/record.rb', line 13 def (cursor) line = 0 column = 0 @content.each_char.with_index do |char, index| if cursor.line == line && cursor.column == column + 1 token_index = index while @content[token_index].match?(/[a-zA-Z0-9_\.:]/) token_index -= 1 end return @content[token_index+1..index] end if char == LINE_BREAK line += 1 column = 0 else column += 1 end end nil end |
#has_unsaved_changes? ⇒ Boolean
47 48 49 |
# File 'lib/holistic/document/unsaved/record.rb', line 47 def has_unsaved_changes? @original_content != @content end |
#mark_as_saved! ⇒ Object
39 40 41 |
# File 'lib/holistic/document/unsaved/record.rb', line 39 def mark_as_saved! @original_content = @content.dup end |
#restore_original_content! ⇒ Object
43 44 45 |
# File 'lib/holistic/document/unsaved/record.rb', line 43 def restore_original_content! @content = @original_content.dup end |