Skip to content
Kward Search API index

Class: Kward::PromptInterface::EditorSelections

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/prompt_interface/editor/selections.rb

Overview

Primary and secondary selection/cursor storage for editor buffers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cursor:, buffer_length:, anchor: nil, secondary: []) ⇒ EditorSelections

Returns a new instance of EditorSelections.



9
10
11
12
13
14
15
# File 'lib/kward/prompt_interface/editor/selections.rb', line 9

def initialize(cursor:, buffer_length:, anchor: nil, secondary: [])
  @cursor = cursor
  @buffer_length = buffer_length
  @anchor = anchor.nil? ? nil : clamp_offset(anchor)
  @secondary = secondary.map { |selection| normalized_selection(selection) }
  normalize
end

Instance Attribute Details

#anchorObject

Returns the value of attribute anchor.



7
8
9
# File 'lib/kward/prompt_interface/editor/selections.rb', line 7

def anchor
  @anchor
end

#secondaryObject (readonly)

Returns the value of attribute secondary.



7
8
9
# File 'lib/kward/prompt_interface/editor/selections.rb', line 7

def secondary
  @secondary
end

Instance Method Details

#add(anchor, cursor = anchor) ⇒ Object



57
58
59
60
# File 'lib/kward/prompt_interface/editor/selections.rb', line 57

def add(anchor, cursor = anchor)
  @secondary << normalized_selection(anchor: anchor, cursor: cursor)
  normalize
end

#allObject



34
35
36
37
# File 'lib/kward/prompt_interface/editor/selections.rb', line 34

def all
  normalize
  [primary] + @secondary.map(&:dup)
end

#buffer_length=(value) ⇒ Object



22
23
24
25
26
27
# File 'lib/kward/prompt_interface/editor/selections.rb', line 22

def buffer_length=(value)
  @buffer_length = value.to_i
  @anchor = clamp_offset(@anchor) unless @anchor.nil?
  @secondary = @secondary.map { |selection| normalized_selection(selection) }
  normalize
end

#clearObject



62
63
64
65
# File 'lib/kward/prompt_interface/editor/selections.rb', line 62

def clear
  @anchor = nil
  @secondary = []
end

#collapse_to_primaryObject



67
68
69
70
# File 'lib/kward/prompt_interface/editor/selections.rb', line 67

def collapse_to_primary
  @secondary = []
  @anchor = nil
end

#cursor=(value) ⇒ Object



17
18
19
20
# File 'lib/kward/prompt_interface/editor/selections.rb', line 17

def cursor=(value)
  @cursor = value
  normalize
end

#multi_cursor?Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/kward/prompt_interface/editor/selections.rb', line 39

def multi_cursor?
  normalize
  @secondary.any?
end

#primaryObject



79
80
81
# File 'lib/kward/prompt_interface/editor/selections.rb', line 79

def primary
  { anchor: @anchor || @cursor, cursor: @cursor }
end

#primary_active?(vibe_visual: false) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/kward/prompt_interface/editor/selections.rb', line 83

def primary_active?(vibe_visual: false)
  return false if @anchor.nil?
  return true if vibe_visual

  @anchor != @cursor
end

#range_for(selection) ⇒ Object



90
91
92
# File 'lib/kward/prompt_interface/editor/selections.rb', line 90

def range_for(selection)
  [selection[:anchor], selection[:cursor]].minmax
end

#secondary_cursor_offsetsObject



72
73
74
75
76
77
# File 'lib/kward/prompt_interface/editor/selections.rb', line 72

def secondary_cursor_offsets
  normalize
  @secondary.filter_map do |selection|
    selection[:cursor] if selection[:anchor] == selection[:cursor]
  end
end

#set(values) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kward/prompt_interface/editor/selections.rb', line 44

def set(values)
  first, *rest = values.to_a
  if first
    @anchor = first[:anchor]
    @cursor = first[:cursor]
  else
    @anchor = nil
    @cursor = 0
  end
  @secondary = rest.map { |selection| normalized_selection(selection) }
  normalize
end