Class: Canopus::Pane

Inherits:
Object
  • Object
show all
Defined in:
lib/canopus/pane.rb,
sig/canopus.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Returns:

  • (Integer)


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.

Returns:



5
6
7
# File 'lib/canopus/pane.rb', line 5

def editors
  @editors
end

#pinned ⇒ Array[Editor] (readonly)

Returns the value of attribute pinned.

Returns:



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.

Parameters:

  • index (Integer)

Raises:

  • (IndexError)


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

#active ⇒ Editor?

Returns:



10
# File 'lib/canopus/pane.rb', line 10

def active = @editors[@active_index]

#back ⇒ Object

Returns:

  • (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.

Parameters:

  • editor (Editor) (defaults to: active)
  • discard: (Boolean) (defaults to: false)
  • activate: (Symbol) (defaults to: :history)

Raises:



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.

Parameters:

Raises:



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

Returns:

  • (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

Parameters:

Returns:



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

Parameters:

  • editor (Editor) (defaults to: active)

Returns:

  • (Object)


27
28
29
# File 'lib/canopus/pane.rb', line 27

def pin(editor = active)
  @pinned << editor unless @pinned.include?(editor)
end