Class: Redcar::Document
- Inherits:
-
Object
- Object
- Redcar::Document
- Extended by:
- Forwardable
- Includes:
- Model, Observable
- Defined in:
- plugins/edit_view/lib/edit_view/document.rb,
plugins/edit_view/lib/edit_view/document/mirror.rb,
plugins/edit_view/lib/edit_view/document/history.rb,
plugins/edit_view/lib/edit_view/document/controller.rb,
plugins/edit_view/lib/edit_view/document/indentation.rb
Overview
This class controls access to the document text in an edit tab. There are methods to read, modify and register listeners for the document.
Defined Under Namespace
Modules: Controller, Mirror Classes: History, Indentation
Constant Summary
Constant Summary
Constants included from Observable
Class Attribute Summary (collapse)
-
+ (Object) default_mirror
Returns the value of attribute default_mirror.
Instance Attribute Summary (collapse)
-
- (Object) edit_view
readonly
Returns the value of attribute edit_view.
-
- (Object) mirror
Returns the value of attribute mirror.
Attributes included from Model
Class Method Summary (collapse)
Instance Method Summary (collapse)
- - (Object) about_to_be_changed(start_offset, length, text)
-
- (Object) between_save_hooks
Call the before_save and after_save callbacks on any plugins that need it.
-
- (Integer) biggest_visible_line
The line_ix of the line at the bottom of the viewport.
-
- (Object) block_selection_mode=(boolean)
Turn the block selection mode on or off.
-
- (Boolean) block_selection_mode?
Is the document in block selection mode.
- - (Object) changed(start_offset, length, text)
-
- (Object) compound
Everything within the block will be treated as a single action for the purposes of Undo.
- - (Object) controller_text
- - (Object) controllers(klass = nil)
- - (Object) create_mark(offset, gravity = :right)
-
- (String) current_word
The word found at the current cursor offset.
-
- (Range<Integer>) current_word_range
The range of the word at the current cursor position.
-
- (Integer) cursor_line
The line index the cursor is on (zero-based).
- - (Object) cursor_line_end_offset
- - (Object) cursor_line_offset
- - (Object) cursor_line_start_offset
- - (Object) cursor_moved(new_offset)
-
- (Integer) cursor_offset
Get the position of the cursor.
-
- (Object) cursor_offset=(offset)
Set the position of the cursor.
-
- (Object) cursor_scope
The scope hierarchy at this point.
-
- (Object) delete(offset, length)
Delete text.
- - (Object) delete_mark(mark)
-
- (Object) ensure_cursor_visible
Ensures the cursor is visible as near the center of the viewport as possible.
- - (Object) ensure_visible(offset)
- - (Boolean) exists?
-
- (Object) expand_selection_to_full_lines
Ensures the selection runs from the start of a line to the end of that or another line, by widening it.
-
- (Object) get_all_text
Get all text.
- - (Object) get_controllers
-
- (String) get_line(line_ix)
Get the text of a line by index.
-
- (String) get_line_without_end_of_line(line_ix)
Get the text of a line by index.
-
- (String) get_range(start, length)
Get a range of text from the document.
-
- (String) get_slice(start_offset, end_offset)
Get a slice of text from the document.
- - (Object) indentation
-
- (Document) initialize(edit_view)
constructor
A new instance of Document.
-
- (Object) insert(offset, text)
Insert text.
-
- (Object) insert_at_cursor(text)
Insert text at the cursor offset.
- - (Object) inspect
- - (Object) largest_visible_horizontal_index
-
- (Integer) length
Length of the document in characters.
-
- (Integer) line_at_offset(offset)
Get the line index of the given offset.
-
- (Integer) line_count
Number of lines.
-
- (String) line_delimiter
(also: #delim)
Returns the line delimiter for this document.
-
- (Range<Integer>) match_word_around(offset)
Returns the range of the word located around an offset.
-
- (Range<Integer>) match_word_left_of(offset)
Returns the range of the word located left of an offset.
-
- (Range<Integer>) match_word_right_of(offset)
Returns the range of the word located right of an offset.
- - (Boolean) mirror_changed?
- - (Boolean) modified?
- - (Object) modify_text
- - (Object) num_lines_visible
-
- (Object) offset_at_inner_end_of_line(line_ix)
Get the offset at the end of a given line, before the line delimiter.
-
- (Integer) offset_at_line(line)
Get the character offset at the start of the given line.
- - (Object) offset_at_line_end(line_ix)
-
- (Object) path
helper method to get the mirror's path if it has one.
-
- (Object) replace(offset, length, text)
Replace text.
-
- (Object) replace_line(line_ix, text = nil)
Replace a line in the document.
-
- (Object) replace_selection(new_text = nil)
Replace the currently selected text.
-
- (Object) replace_word_at_offset(offset, new_text = nil)
Replace the current word.
- - (Object) save!
-
- (Object) scroll_to_horizontal_offset(offset)
Does the minimum amount of scrolling that brings the given horizontal offset into the viewport.
-
- (Object) scroll_to_line(line_ix)
Does the minimum amount of scrolling that brings the given line into the viewport.
-
- (Object) scroll_to_line_at_middle(line_ix)
Tries to scroll so the given line is in the middle of the viewport.
-
- (Object) scroll_to_line_at_top(line_ix)
Tries to scroll so the given line is at the top of the viewport.
-
- (Object) select_all
Select all text in the document.
-
- (String) selected_text
Get the text selected by the user.
-
- (Boolean) selection?
Is there any text selected? (Or equivalently, is the length of the selection equal to 0).
- - (Object) selection_line
- - (Object) selection_line_offset
- - (Object) selection_offset
- - (Object) selection_offset=(value)
-
- (Range<Integer>) selection_range
The range of text selected by the user.
- - (Object) selection_range_changed(start_offset, end_offset)
-
- (Range<Integer>) selection_ranges
The ranges of text selected by the user.
- - (Object) set_modified(boolean)
-
- (Object) set_selection_range(cursor_offset, selection_offset)
Set the range of text selected by the user.
- - (Boolean) single_line?
- - (Object) smallest_visible_horizontal_index
-
- (Integer) smallest_visible_line
The line_ix of the line at the top of the viewport.
-
- (Object) text=(text)
Set the contents of the document.
- - (Object) title
-
- (String) to_s
The entire contents of the document.
- - (Object) update_from_mirror
- - (Object) verify_text(start_offset, end_offset, text)
- - (Object) visible_horizontal_index_range
- - (Object) word
-
- (String) word_at_offset(offset)
The word at an offset.
-
- (Range<Integer>) word_range_at_offset(offset)
The range of the word at an offset.
Methods included from Observable
#add_listener, #notify_listeners, #remove_listener
Methods included from ReentryHelpers
Constructor Details
- (Document) initialize(edit_view)
A new instance of Document
24 25 26 27 28 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 24 def initialize(edit_view) @edit_view = edit_view @grammar = Redcar::Grammar.new(self) get_controllers end |
Class Attribute Details
+ (Object) default_mirror
Returns the value of attribute default_mirror
19 20 21 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 19 def default_mirror @default_mirror end |
Instance Attribute Details
- (Object) edit_view (readonly)
Returns the value of attribute edit_view
22 23 24 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 22 def edit_view @edit_view end |
- (Object) mirror
Returns the value of attribute mirror
22 23 24 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 22 def mirror @mirror end |
Class Method Details
+ (Object) all_document_controller_types
10 11 12 13 14 15 16 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 10 def self.all_document_controller_types result = [] Redcar.plugin_manager.objects_implementing(:document_controller_types).each do |object| result += object.document_controller_types end result end |
Instance Method Details
- (Object) about_to_be_changed(start_offset, length, text)
160 161 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 160 def about_to_be_changed(start_offset, length, text) end |
- (Object) between_save_hooks
Call the before_save and after_save callbacks on any plugins that need it
Pass self as an argument since plugins that use before_save will most likely need access to the document being saved.
69 70 71 72 73 74 75 76 77 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 69 def between_save_hooks Redcar.plugin_manager.objects_implementing(:before_save).each do |object| object.before_save(self) end yield Redcar.plugin_manager.objects_implementing(:after_save).each do |object| object.after_save(self) end end |
- (Integer) biggest_visible_line
The line_ix of the line at the bottom of the viewport.
713 714 715 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 713 def biggest_visible_line @edit_view.controller.biggest_visible_line end |
- (Object) block_selection_mode=(boolean)
Turn the block selection mode on or off.
516 517 518 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 516 def block_selection_mode=(boolean) controller.block_selection_mode = !!boolean end |
- (Boolean) block_selection_mode?
Is the document in block selection mode.
511 512 513 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 511 def block_selection_mode? controller.block_selection_mode? end |
- (Object) changed(start_offset, length, text)
163 164 165 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 163 def changed(start_offset, length, text) notify_listeners(:changed) end |
- (Object) compound
Everything within the block will be treated as a single action for the purposes of Undo.
doc.compound { first_thing; second_thing }
764 765 766 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 764 def compound @edit_view.controller.compound { yield } end |
- (Object) controller_text
257 258 259 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 257 def controller_text controller.text end |
- (Object) controllers(klass = nil)
57 58 59 60 61 62 63 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 57 def controllers(klass=nil) if klass @controllers.values.flatten.uniq.select {|c| c.is_a?(klass) } else @controllers.values.flatten.uniq end end |
- (Object) create_mark(offset, gravity = :right)
752 753 754 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 752 def create_mark(offset, gravity=:right) controller.create_mark(offset, gravity) end |
- (String) current_word
The word found at the current cursor offset.
334 335 336 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 334 def current_word word_at_offset(cursor_offset) end |
- (Range<Integer>) current_word_range
The range of the word at the current cursor position.
432 433 434 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 432 def current_word_range word_range_at_offset(cursor_offset) end |
- (Integer) cursor_line
The line index the cursor is on (zero-based)
298 299 300 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 298 def cursor_line line_at_offset(cursor_offset) end |
- (Object) cursor_line_end_offset
306 307 308 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 306 def cursor_line_end_offset offset_at_line_end(cursor_line) end |
- (Object) cursor_line_offset
284 285 286 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 284 def cursor_line_offset cursor_offset - offset_at_line(cursor_line) end |
- (Object) cursor_line_start_offset
302 303 304 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 302 def cursor_line_start_offset offset_at_line(cursor_line) end |
- (Object) cursor_moved(new_offset)
152 153 154 155 156 157 158 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 152 def cursor_moved(new_offset) @controllers[Controller::CursorCallbacks].each do |controller| rescue_document_controller_error(controller) do controller.cursor_moved(new_offset) end end end |
- (Integer) cursor_offset
Get the position of the cursor.
280 281 282 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 280 def cursor_offset controller.cursor_offset end |
- (Object) cursor_offset=(offset)
Set the position of the cursor.
291 292 293 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 291 def cursor_offset=(offset) controller.cursor_offset = offset end |
- (Object) cursor_scope
The scope hierarchy at this point
748 749 750 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 748 def cursor_scope controller.scope_at(cursor_line, cursor_line_offset) end |
- (Object) delete(offset, length)
Delete text
215 216 217 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 215 def delete(offset, length) replace(offset, length, "") end |
- (Object) delete_mark(mark)
756 757 758 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 756 def delete_mark(mark) controller.delete_mark(mark) end |
- (Object) ensure_cursor_visible
Ensures the cursor is visible as near the center of the viewport as possible. Scrolls vertically until the cursor is near the center. Scrolls horizontally only enough to make the cursor visible.
732 733 734 735 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 732 def ensure_cursor_visible scroll_to_line_at_middle(cursor_line) ensure_visible(cursor_offset) unless visible_horizontal_index_range.include?(cursor_line_offset) end |
- (Object) ensure_visible(offset)
725 726 727 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 725 def ensure_visible(offset) @edit_view.controller.ensure_visible(offset) end |
- (Boolean) exists?
106 107 108 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 106 def exists? edit_view.exists? end |
- (Object) expand_selection_to_full_lines
Ensures the selection runs from the start of a line to the end of that or another line, by widening it.
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 481 def #are the selections in the right order? start_offset, end_offset = cursor_offset, selection_offset if start_offset > end_offset end_offset, start_offset = start_offset, end_offset end start_index = line_at_offset(start_offset) end_index = line_at_offset(end_offset) # is the selection of the last line empty? if end_offset == offset_at_line(end_index) end_index -= 1 end start_offset = offset_at_line(start_index) end_offset = offset_at_inner_end_of_line(end_index) set_selection_range(start_offset, end_offset) end |
- (Object) get_all_text
Get all text
562 563 564 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 562 def get_all_text get_range(0, length) end |
- (Object) get_controllers
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 30 def get_controllers @controllers = { Controller::ModificationCallbacks => [], Controller::NewlineCallback => [], Controller::CursorCallbacks => [], Controller => [] } Document.all_document_controller_types.each do |type| controller = type.new controller.document = self @controllers.each do |key, value| if controller.is_a?(key) value << controller end end end Redcar.plugin_manager.objects_implementing(:document_cursor_listener).each do |object| controller = object.document_cursor_listener controller.document = self @controllers.each do |key, value| if controller.is_a?(key) value << controller end end end end |
- (String) get_line(line_ix)
Get the text of a line by index. (Includes a trailing “n”, unless it is the last line in the document.)
543 544 545 546 547 548 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 543 def get_line(line_ix) controller.get_range( offset_at_line(line_ix), offset_at_line_end(line_ix) - offset_at_line(line_ix) ) end |
- (String) get_line_without_end_of_line(line_ix)
Get the text of a line by index.
554 555 556 557 558 559 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 554 def get_line_without_end_of_line(line_ix) controller.get_range( offset_at_line(line_ix), offset_at_inner_end_of_line(line_ix) - offset_at_line(line_ix) ) end |
- (String) get_range(start, length)
Get a range of text from the document.
525 526 527 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 525 def get_range(start, length) controller.get_range(start, length) end |
- (String) get_slice(start_offset, end_offset)
Get a slice of text from the document.
534 535 536 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 534 def get_slice(start_offset, end_offset) get_range(start_offset, end_offset - start_offset) end |
- (Object) indentation
787 788 789 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 787 def indentation Document::Indentation.new(self, @edit_view.tab_width, @edit_view.soft_tabs?) end |
- (Object) insert(offset, text)
Insert text
198 199 200 201 202 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 198 def insert(offset, text) return unless text and offset text = text.gsub(delim, "") if single_line? replace(offset, 0, text) end |
- (Object) insert_at_cursor(text)
Insert text at the cursor offset
207 208 209 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 207 def insert_at_cursor(text) insert(cursor_offset, text) end |
- (Object) inspect
791 792 793 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 791 def inspect "#<Redcar::Document:#{object_id} #{title.inspect}>" end |
- (Object) largest_visible_horizontal_index
717 718 719 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 717 def largest_visible_horizontal_index @edit_view.controller.largest_visible_horizontal_index end |
- (Integer) length
Length of the document in characters
232 233 234 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 232 def length controller.length end |
- (Integer) line_at_offset(offset)
Get the line index of the given offset
265 266 267 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 265 def line_at_offset(offset) controller.line_at_offset(offset) end |
- (Integer) line_count
Number of lines.
239 240 241 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 239 def line_count controller.line_count end |
- (String) line_delimiter Also known as: delim
Returns the line delimiter for this document. Either n or rn. It will attempt to detect the delimiter from the document or it will default to the platform delimiter.
180 181 182 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 180 def line_delimiter controller.get_line_delimiter end |
- (Range<Integer>) match_word_around(offset)
Returns the range of the word located around an offset. Before using this method, it's best to make sure there actually might be a word around the offset. This means we are not at the beginning or end of the file and there are no spaces left and right from the offset.
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 364 def match_word_around(offset) line_index = line_at_offset(offset) line_end_offset = offset_at_line_end(line_index) right = 0 matched_offsets = offset..offset until false new_match = match_word_left_of(offset + right) if new_match.last - new_match.first > matched_offsets.last - matched_offsets.first && new_match.first <= offset matched_offsets = new_match end right += 1 if offset + right == length + 1 || /\s/.match(get_slice(offset, offset + right)) break end end matched_offsets end |
- (Range<Integer>) match_word_left_of(offset)
Returns the range of the word located left of an offset. Before using this method, it's best to make sure there actually might be a word left of the offset. This means we are not at the beginning of the file and there are no spaces left of the offset.
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 389 def match_word_left_of(offset) line_index = line_at_offset(offset) line_start_offset = offset_at_line(line_index) left = -1 matched_left = false matched_offsets = offset..offset until offset + left == line_start_offset - 1 || /\s/.match(get_slice(offset + left, offset)) current_offsets = offset + left..offset if word.match(get_slice(current_offsets.first, current_offsets.last)) matched_offsets = current_offsets matched_left = true elsif matched_left break end left -= 1 end matched_offsets end |
- (Range<Integer>) match_word_right_of(offset)
Returns the range of the word located right of an offset. Before using this method, it's best to make sure there actually might be a word right of the offset. This means we are not at the end of the file and there are no spaces right of the offset.
415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 415 def match_word_right_of(offset) line_index = line_at_offset(offset) line_end_offset = offset_at_line_end(line_index) right = 0 matched_offsets = offset..offset until offset + right == length + 1 || /\s/.match(get_slice(offset, offset + right)) if word.match(get_slice(offset, offset + right)) matched_offsets = offset..offset + right end right += 1 end matched_offsets end |
- (Boolean) mirror_changed?
120 121 122 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 120 def mirror_changed? mirror and mirror.changed? end |
- (Boolean) modified?
89 90 91 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 89 def modified? @modified end |
- (Object) modify_text
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 133 def modify_text start_offset, end_offset, text = *@change set_modified(true) @controllers[Controller::ModificationCallbacks].each do |controller| rescue_document_controller_error(controller) do controller.after_modify end end @controllers[Controller::NewlineCallback].each do |controller| if text == line_delimiter rescue_document_controller_error(controller) do controller.after_newline(line_at_offset(start_offset) + 1) end end end @change = nil notify_listeners(:changed) end |
- (Object) num_lines_visible
737 738 739 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 737 def num_lines_visible biggest_visible_line - smallest_visible_line end |
- (Object) offset_at_inner_end_of_line(line_ix)
Get the offset at the end of a given line, before the line delimiter.
637 638 639 640 641 642 643 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 637 def offset_at_inner_end_of_line(line_ix) if line_ix == line_count - 1 length else offset_at_line(line_ix + 1) - delim.length end end |
- (Integer) offset_at_line(line)
Get the character offset at the start of the given line
273 274 275 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 273 def offset_at_line(line) controller.offset_at_line(line) end |
- (Object) offset_at_line_end(line_ix)
310 311 312 313 314 315 316 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 310 def offset_at_line_end(line_ix) if line_ix == line_count - 1 end_offset = length else end_offset = offset_at_line(line_ix + 1) end end |
- (Object) path
helper method to get the mirror's path if it has one
98 99 100 101 102 103 104 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 98 def path if @mirror and @mirror.respond_to?(:path) and @mirror.path @mirror.path else nil end end |
- (Object) replace(offset, length, text)
Replace text
224 225 226 227 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 224 def replace(offset, length, text) text = text.gsub(delim, "") if single_line? controller.replace(offset, length, text) end |
- (Object) replace_line(line_ix, text = nil)
Replace a line in the document. This has two modes. In the first, you supply the replacement text as an argument:
replace_line(10, "new line text")
In the second, you supply a block. The block argument is the current text of the line, and the return value of the block is the replacement text:
replace_line(10) {|current_text| current_text.upcase }
576 577 578 579 580 581 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 576 def replace_line(line_ix, text=nil) text ||= yield(get_line_without_end_of_line(line_ix)) start_offset = offset_at_line(line_ix) end_offset = offset_at_inner_end_of_line(line_ix) replace(start_offset, end_offset - start_offset, text) end |
- (Object) replace_selection(new_text = nil)
Replace the currently selected text. This has two modes. In the first, you supply the replacement text as an argument:
replace_selection("new text")
In the second, you supply a block. The block argument is the current selected text, and the return value of the block is the replacement text:
replace_selection {|current_text| current_text.upcase }
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 593 def replace_selection(new_text=nil) previous_cursor_offset = cursor_offset sr = selection_range start_offset = sr.first end_offset = sr.last new_text = new_text || yield(selected_text) replace(start_offset, end_offset - start_offset, new_text) new_end_offset = start_offset + new_text.length if previous_cursor_offset == end_offset self.set_selection_range(new_end_offset, start_offset) else self.set_selection_range(start_offset, new_end_offset) end end |
- (Object) replace_word_at_offset(offset, new_text = nil)
Replace the current word. This has two modes. In the first, you supply the replacement text as an argument:
replace_word_at_offset(offset, "new text")
In the second, you supply a block. The block argument is the current word, and the return value of the block is the replacement text:
replace_word_at_offset(offset) {|current_text| current_text.upcase }
622 623 624 625 626 627 628 629 630 631 632 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 622 def replace_word_at_offset(offset, new_text=nil) previous_offset = cursor_offset wr = word_range_at_offset(offset) start_offset = wr.first end_offset = wr.last new_text = new_text || yield(word_at_offset(offset)) replace(start_offset, end_offset - start_offset, new_text) self.cursor_offset = [previous_offset, start_offset + new_text.length].min end |
- (Object) save!
79 80 81 82 83 84 85 86 87 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 79 def save! between_save_hooks do @mirror.commit(to_s) @edit_view.reset_last_checked set_modified(false) notify_listeners(:mirror_committed,@mirror) Redcar.app.repeat_event(:mirror_committed) if Redcar.app end end |
- (Object) scroll_to_horizontal_offset(offset)
Does the minimum amount of scrolling that brings the given horizontal offset into the viewport. Which may be none at all.
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 665 def scroll_to_horizontal_offset(offset) line = cursor_line start = offset_at_line(line) lend = offset_at_inner_end_of_line(line) dist = largest_visible_horizontal_index - smallest_visible_horizontal_index if offset > 0 and offset <= lend - start # if offset exists at line if offset < dist @edit_view.controller.scroll_to_horizontal_offset(0) else if offset > largest_visible_horizontal_index @edit_view.controller.scroll_to_horizontal_offset(offset-dist) elsif offset < smallest_visible_horizontal_index @edit_view.controller.scroll_to_horizontal_offset(offset) end end end end |
- (Object) scroll_to_line(line_ix)
Does the minimum amount of scrolling that brings the given line into the viewport. Which may be none at all.
649 650 651 652 653 654 655 656 657 658 659 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 649 def scroll_to_line(line_ix) if line_ix > biggest_visible_line top_line_ix = smallest_visible_line + (line_ix - biggest_visible_line) + 2 top_line_ix = [top_line_ix, line_count - 1].min scroll_to_line_at_top(top_line_ix) elsif line_ix < smallest_visible_line bottom_line_ix = line_ix - 2 bottom_line_ix = [bottom_line_ix, 0].max scroll_to_line_at_top(bottom_line_ix) end end |
- (Object) scroll_to_line_at_middle(line_ix)
Tries to scroll so the given line is in the middle of the viewport. If the line is above (smaller index) than the midline, defers to scroll_to_line_at_top(0).
694 695 696 697 698 699 700 701 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 694 def scroll_to_line_at_middle(line_ix) midline = num_lines_visible / 2 if line_ix > midline scroll_to_line_at_top(line_ix - midline) else scroll_to_line_at_top(0) end end |
- (Object) scroll_to_line_at_top(line_ix)
Tries to scroll so the given line is at the top of the viewport.
686 687 688 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 686 def scroll_to_line_at_top(line_ix) @edit_view.controller.scroll_to_line(line_ix) end |
- (Object) select_all
Select all text in the document.
475 476 477 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 475 def select_all set_selection_range(length, 0) end |
- (String) selected_text
Get the text selected by the user. If no text is selected returns “”.
506 507 508 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 506 def selected_text get_range(selection_range.begin, selection_range.count) end |
- (Boolean) selection?
Is there any text selected? (Or equivalently, is the length of the selection equal to 0)
190 191 192 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 190 def selection? selection_range.count > 0 end |
- (Object) selection_line
458 459 460 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 458 def selection_line line_at_offset(selection_offset) end |
- (Object) selection_line_offset
462 463 464 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 462 def selection_line_offset selection_offset - offset_at_line(selection_line) end |
- (Object) selection_offset
450 451 452 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 450 def selection_offset controller.selection_offset end |
- (Object) selection_offset=(value)
454 455 456 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 454 def selection_offset=(value) set_selection_range(cursor_offset, value) end |
- (Range<Integer>) selection_range
The range of text selected by the user.
439 440 441 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 439 def selection_range controller.selection_range end |
- (Object) selection_range_changed(start_offset, end_offset)
167 168 169 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 167 def selection_range_changed(start_offset, end_offset) notify_listeners(:selection_range_changed, start_offset..end_offset) end |
- (Range<Integer>) selection_ranges
The ranges of text selected by the user.
446 447 448 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 446 def selection_ranges controller.selection_ranges end |
- (Object) set_modified(boolean)
782 783 784 785 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 782 def set_modified(boolean) @modified = boolean @edit_view.title = title_with_star end |
- (Object) set_selection_range(cursor_offset, selection_offset)
Set the range of text selected by the user.
470 471 472 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 470 def set_selection_range(cursor_offset, selection_offset) controller.set_selection_range(cursor_offset, selection_offset) end |
- (Boolean) single_line?
171 172 173 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 171 def single_line? controller.single_line? end |
- (Object) smallest_visible_horizontal_index
721 722 723 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 721 def smallest_visible_horizontal_index @edit_view.controller.smallest_visible_horizontal_index end |
- (Integer) smallest_visible_line
The line_ix of the line at the top of the viewport.
706 707 708 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 706 def smallest_visible_line @edit_view.controller.smallest_visible_line end |
- (Object) text=(text)
Set the contents of the document
253 254 255 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 253 def text=(text) controller.text = text end |
- (Object) title
93 94 95 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 93 def title @mirror ? @mirror.title : nil end |
- (String) to_s
The entire contents of the document
246 247 248 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 246 def to_s controller.to_s end |
- (Object) update_from_mirror
768 769 770 771 772 773 774 775 776 777 778 779 780 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 768 def update_from_mirror previous_line = cursor_line top_line = smallest_visible_line self.text = mirror.read @modified = false @edit_view.title = title_with_star if line_count > previous_line self.cursor_offset = offset_at_line(previous_line) scroll_to_line_at_top(top_line) end end |
- (Object) verify_text(start_offset, end_offset, text)
124 125 126 127 128 129 130 131 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 124 def verify_text(start_offset, end_offset, text) @change = [start_offset, end_offset, text] @controllers[Controller::ModificationCallbacks].each do |controller| rescue_document_controller_error(controller) do controller.before_modify(start_offset, end_offset, text) end end end |
- (Object) visible_horizontal_index_range
741 742 743 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 741 def visible_horizontal_index_range smallest_visible_horizontal_index .. largest_visible_horizontal_index end |
- (Object) word
318 319 320 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 318 def word @grammar.word end |
- (String) word_at_offset(offset)
The word at an offset.
326 327 328 329 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 326 def word_at_offset(offset) range = word_range_at_offset(offset) get_range(range.first, range.last - range.first) end |
- (Range<Integer>) word_range_at_offset(offset)
The range of the word at an offset.
342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'plugins/edit_view/lib/edit_view/document.rb', line 342 def word_range_at_offset(offset) line_ix = line_at_offset(offset) match_left = offset == 0 ? false : !/\s/.match(get_slice(offset - 1, offset)) match_right = offset == length ? false : !/\s/.match(get_slice(offset, offset + 1)) if match_left && match_right match_word_around(offset) elsif match_left match_word_left_of(offset) elsif match_right match_word_right_of(offset) else offset..offset end end |