Class: Fir::ReplState

Inherits:
Object
  • Object
show all
Defined in:
lib/fir/repl_state.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines, cursor, repl_binding = TOPLEVEL_BINDING, history = Fir::History.new) ⇒ ReplState

Returns a new instance of ReplState.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fir/repl_state.rb', line 19

def initialize(
  lines,
  cursor,
  repl_binding = TOPLEVEL_BINDING,
  history = Fir::History.new
)
  @lines = lines
  @cursor = cursor
  @repl_binding = repl_binding
  @history = history
  set_indent
end

Instance Attribute Details

#cursorObject

Returns the value of attribute cursor.



12
13
14
# File 'lib/fir/repl_state.rb', line 12

def cursor
  @cursor
end

#historyObject (readonly)

Returns the value of attribute history.



13
14
15
# File 'lib/fir/repl_state.rb', line 13

def history
  @history
end

#indentObject (readonly)

Returns the value of attribute indent.



13
14
15
# File 'lib/fir/repl_state.rb', line 13

def indent
  @indent
end

#linesObject

Returns the value of attribute lines.



12
13
14
# File 'lib/fir/repl_state.rb', line 12

def lines
  @lines
end

#repl_bindingObject (readonly)

Returns the value of attribute repl_binding.



13
14
15
# File 'lib/fir/repl_state.rb', line 13

def repl_binding
  @repl_binding
end

Class Method Details

.blankObject



15
16
17
# File 'lib/fir/repl_state.rb', line 15

def self.blank
  new(Lines.blank, Cursor.blank)
end

Instance Method Details

#==(other) ⇒ Object



62
63
64
# File 'lib/fir/repl_state.rb', line 62

def ==(other)
  lines == other.lines && cursor == other.cursor
end

#blankObject



49
50
51
52
53
54
55
56
# File 'lib/fir/repl_state.rb', line 49

def blank
  self.class.new(
    Lines.blank,
    Cursor.blank,
    repl_binding,
    history
  )
end

#blank?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/fir/repl_state.rb', line 58

def blank?
  lines.blank? && cursor.blank?
end

#cloneObject



40
41
42
43
44
45
46
47
# File 'lib/fir/repl_state.rb', line 40

def clone
  self.class.new(
    lines.clone,
    cursor.clone,
    repl_binding,
    history
  )
end

#commit_current_line_to_historyObject



86
87
88
89
90
# File 'lib/fir/repl_state.rb', line 86

def commit_current_line_to_history
  Fir::History.add_line_to_history_file(
    current_line.join
  )
end

#current_lineObject



70
71
72
# File 'lib/fir/repl_state.rb', line 70

def current_line
  lines[cursor.y]
end

#current_line=(new_line) ⇒ Object



66
67
68
# File 'lib/fir/repl_state.rb', line 66

def current_line=(new_line)
  lines[cursor.y] = new_line
end

#executable?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/fir/repl_state.rb', line 78

def executable?
  indent.executable?
end

#indentsObject



74
75
76
# File 'lib/fir/repl_state.rb', line 74

def indents
  indent.indents
end

#suggestionObject



82
83
84
# File 'lib/fir/repl_state.rb', line 82

def suggestion
  history.suggestion(current_line.join)
end

#transition(command) {|new_state| ... } ⇒ Object

Yields:

  • (new_state)


32
33
34
35
36
37
38
# File 'lib/fir/repl_state.rb', line 32

def transition(command)
  new_state = command.execute
  new_state.set_indent
  yield new_state if block_given?
  return blank if new_state.executable?
  new_state
end