Class: Canopus::Pane
- Inherits:
-
Object
- Object
- Canopus::Pane
- Defined in:
- lib/canopus/pane.rb,
sig/canopus.rbs
Instance Attribute Summary collapse
-
#active_index ⇒ Integer
Returns the value of attribute active_index.
-
#editors ⇒ Array[Editor]
readonly
Returns the value of attribute editors.
-
#pinned ⇒ Array[Editor]
readonly
Returns the value of attribute pinned.
Instance Method Summary collapse
- #activate(index) ⇒ void
- #active ⇒ Editor?
- #back ⇒ Object
- #close(editor = active, discard: false, activate: :history) ⇒ void
- #detach(editor) ⇒ void
- #forward ⇒ Object
-
#initialize ⇒ Pane
constructor
A new instance of Pane.
- #open(buffer) ⇒ Editor
- #pin(editor = active) ⇒ Object
Constructor Details
#initialize ⇒ Pane
Returns a new instance of Pane.
7 8 9 |
# File 'lib/canopus/pane.rb', line 7 def initialize @editors, @pinned, @active_index, @past, @future = [], [], 0, [], [] end |
Instance Attribute Details
#active_index ⇒ Integer
Returns the value of attribute active_index.
6 7 8 |
# File 'lib/canopus/pane.rb', line 6 def active_index @active_index end |
#editors ⇒ Array[Editor] (readonly)
Returns the value of attribute editors.
5 6 7 |
# File 'lib/canopus/pane.rb', line 5 def editors @editors end |
#pinned ⇒ Array[Editor] (readonly)
Returns the value of attribute pinned.
5 6 7 |
# File 'lib/canopus/pane.rb', line 5 def pinned @pinned end |
Instance Method Details
#activate(index) ⇒ void
This method returns an undefined value.
21 22 23 24 25 26 |
# File 'lib/canopus/pane.rb', line 21 def activate(index) raise IndexError, "tab outside pane" unless index.is_a?(Integer) && index.between?(0, @editors.length - 1) @past << active if active && @active_index != index @active_index = index @future.clear end |
#back ⇒ Object
64 65 66 67 68 69 |
# File 'lib/canopus/pane.rb', line 64 def back previous = @past.pop return unless previous && @editors.include?(previous) @future << active @active_index = @editors.index(previous) end |
#close(editor = active, discard: false, activate: :history) ⇒ void
This method returns an undefined value.
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 |
# File 'lib/canopus/pane.rb', line 30 def close(editor = active, discard: false, activate: :history) raise Error, "buffer has unsaved changes" if editor.buffer.dirty? && !discard index = @editors.index(editor) raise Error, "tab is not in pane" unless index current = active historical = @past.reverse.find { |item| !item.equal?(editor) && @editors.include?(item) } @pinned.delete(editor) @editors.delete(editor) @past.delete(editor) @future.delete(editor) @future.clear if current.equal?(editor) @active_index = if @editors.empty? 0 elsif !current.equal?(editor) @editors.index(current) elsif activate == :history && historical @past.delete(historical) @editors.index(historical) elsif activate == :left [index - 1, 0].max else [index, @editors.length - 1].min end editor.dispose end |
#detach(editor) ⇒ void
This method returns an undefined value.
55 56 57 58 59 60 61 62 63 |
# File 'lib/canopus/pane.rb', line 55 def detach(editor) raise Error, "tab is not in pane" unless @editors.include?(editor) active = self.active @pinned.delete(editor) @editors.delete(editor) @past.delete(editor) @future.delete(editor) @active_index = @editors.index(active) || [@active_index, @editors.length - 1].min.clamp(0, @editors.length) end |
#forward ⇒ Object
70 71 72 73 74 75 |
# File 'lib/canopus/pane.rb', line 70 def forward following = @future.pop return unless following && @editors.include?(following) @past << active @active_index = @editors.index(following) end |
#open(buffer) ⇒ Editor
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/canopus/pane.rb', line 11 def open(buffer) existing = @editors.index { |editor| editor.buffer.equal?(buffer) } if existing activate(existing) else @editors << Editor.new(buffer) activate(@editors.length - 1) end active end |
#pin(editor = active) ⇒ Object
27 28 29 |
# File 'lib/canopus/pane.rb', line 27 def pin(editor = active) @pinned << editor unless @pinned.include?(editor) end |