Class: ImportJS::CommandLineEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/import_js/command_line_editor.rb

Overview

This is the implementation of command line integration in Import-JS.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines, opts) ⇒ CommandLineEditor

Returns a new instance of CommandLineEditor.



4
5
6
7
8
9
10
11
# File 'lib/import_js/command_line_editor.rb', line 4

def initialize(lines, opts)
  @lines = lines
  @messages = []
  @ask_for_selections = []
  @selections = opts[:selections] unless opts[:selections].empty?
  @word = opts[:word]
  @path_to_file = opts[:path_to_file]
end

Instance Attribute Details

#ask_for_selectionsObject (readonly)

Returns the value of attribute ask_for_selections.



35
36
37
# File 'lib/import_js/command_line_editor.rb', line 35

def ask_for_selections
  @ask_for_selections
end

#gotoObject (readonly)

Returns the value of attribute goto.



28
29
30
# File 'lib/import_js/command_line_editor.rb', line 28

def goto
  @goto
end

Instance Method Details

#append_line(line_number, str) ⇒ Object

Append a line right after the specified line.

Lines are one-indexed, but you need to support appending to line 0 (add content at top of file).

Parameters:

  • line_number (Number)


84
85
86
# File 'lib/import_js/command_line_editor.rb', line 84

def append_line(line_number, str)
  @lines.insert(line_number, str)
end

#ask_for_selection(word, alternatives) ⇒ Number?

Ask the user to select something from a list of alternatives.

Parameters:

  • word (String)

    The word/variable to import

  • alternatives (Array<String>)

    A list of alternatives

Returns:

  • (Number, nil)

    the index of the selected alternative, or nil if nothing was selected.



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/import_js/command_line_editor.rb', line 101

def ask_for_selection(word, alternatives)
  if @selections
    # this is a re-run, where selections have already been made
    @selections[word]
  else
    @ask_for_selections << {
      word: word,
      alternatives: alternatives,
    }
    nil
  end
end

#count_linesNumber

Count the number of lines in the file.

Returns:

  • (Number)

    the number of lines in the file



91
92
93
# File 'lib/import_js/command_line_editor.rb', line 91

def count_lines
  @lines.length
end

#current_file_contentString

Returns:

  • (String)


38
39
40
# File 'lib/import_js/command_line_editor.rb', line 38

def current_file_content
  @lines.join("\n")
end

#current_wordString

Returns:

  • (String)


14
15
16
# File 'lib/import_js/command_line_editor.rb', line 14

def current_word
  @word
end

#cursorArray(Number, Number)

Get the cursor position.

Returns:

  • (Array(Number, Number))


58
59
60
61
# File 'lib/import_js/command_line_editor.rb', line 58

def cursor
  # not relevant for this editor
  [0, 0]
end

#cursor=(_position_tuple) ⇒ Object

Place the cursor at a specified position.

Parameters:

  • _position_tuple (Array(Number, Number))

    the row and column to place the cursor at.



67
68
69
# File 'lib/import_js/command_line_editor.rb', line 67

def cursor=(_position_tuple)
  # no-op
end

#delete_line(line_number) ⇒ Object

Delete a line.

Parameters:

  • line_number (Number)

    One-indexed line number. 1 is the first line in the file.



75
76
77
# File 'lib/import_js/command_line_editor.rb', line 75

def delete_line(line_number)
  @lines.delete_at(line_number - 1)
end

#message(str) ⇒ Object

Parameters:

  • str (String)


31
32
33
# File 'lib/import_js/command_line_editor.rb', line 31

def message(str)
  @messages << str
end

#messagesString

Returns:

  • (String)


43
44
45
# File 'lib/import_js/command_line_editor.rb', line 43

def messages
  @messages.join("\n")
end

#open_file(file_path) ⇒ Object

Parameters:

  • file_path (String)


24
25
26
# File 'lib/import_js/command_line_editor.rb', line 24

def open_file(file_path)
  @goto = file_path
end

#path_to_current_fileString?

Returns:

  • (String?)


19
20
21
# File 'lib/import_js/command_line_editor.rb', line 19

def path_to_current_file
  @path_to_file
end

#read_line(line_number) ⇒ String

Reads a line from the file.

Lines are one-indexed, so 1 means the first line in the file.

Returns:

  • (String)


51
52
53
# File 'lib/import_js/command_line_editor.rb', line 51

def read_line(line_number)
  @lines[line_number - 1]
end