Class: ImportJS::EmacsEditor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEmacsEditor

Returns a new instance of EmacsEditor.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/import_js/emacs_editor.rb', line 8

def initialize
  loop do
    input = gets.chomp
    command, value, path = input.split(':')

    begin
      @path = path
      @file = File.readlines(path).map(&:chomp)
      @current_word = value

      case command
      when 'import'
        ImportJS::Importer.new(self).import
        write_file
        puts 'import:success'
      when 'goto'
        ImportJS::Importer.new(self).goto
      when 'fix'
        ImportJS::Importer.new(self).fix_imports
        write_file
        puts 'import:success'
      else
        puts "unknown command: #{command}"
      end
    rescue Exception => e
      puts e.inspect
    end
  end
end

Instance Attribute Details

#current_wordObject

Returns the value of attribute current_word.



6
7
8
# File 'lib/import_js/emacs_editor.rb', line 6

def current_word
  @current_word
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)


108
109
110
# File 'lib/import_js/emacs_editor.rb', line 108

def append_line(line_number, str)
  @file.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.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/import_js/emacs_editor.rb', line 125

def ask_for_selection(word, alternatives)
  puts "asking for selection"
  puts "ImportJS: Pick JS module to import for '#{word}':"
  puts JSON.pretty_generate(alternatives)
  return

  # need to implement this
  escaped_list = [heading]
  escaped_list << alternatives.each_with_index.map do |alternative, i|
    "\"#{i + 1}: #{alternative}\""
  end
  escaped_list_string = '[' + escaped_list.join(',') + ']'

  selected_index = VIM.evaluate("inputlist(#{escaped_list_string})")
  return if selected_index < 1
  selected_index - 1
end

#count_linesNumber

Count the number of lines in the file.

Returns:

  • (Number)

    the number of lines in the file



115
116
117
# File 'lib/import_js/emacs_editor.rb', line 115

def count_lines
  @file.size
end

#current_file_contentString

Read the entire file into a string.

Returns:

  • (String)


69
70
71
# File 'lib/import_js/emacs_editor.rb', line 69

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

#cursorArray(Number, Number)

Get the cursor position.

Returns:

  • (Array(Number, Number))


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

def cursor
  return 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.



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

def cursor=(position_tuple)
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.



99
100
101
# File 'lib/import_js/emacs_editor.rb', line 99

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

#max_line_lengthNumber?

Get the preferred max length of a line

Returns:

  • (Number?)


145
146
147
# File 'lib/import_js/emacs_editor.rb', line 145

def max_line_length
  80
end

#message(str) ⇒ Object

Display a message to the user.

Parameters:

  • str (String)


62
63
64
# File 'lib/import_js/emacs_editor.rb', line 62

def message(str)
  puts str
end

#open_file(file_path) ⇒ Object

Open a file specified by a path.

Parameters:

  • file_path (String)


47
48
49
# File 'lib/import_js/emacs_editor.rb', line 47

def open_file(file_path)
  puts "goto:success:#{File.expand_path(file_path)}"
end

#path_to_current_fileString?

Get the path to the file currently being edited. May return ‘nil` if an anonymous file is being edited.

Returns:

  • (String?)


55
56
57
# File 'lib/import_js/emacs_editor.rb', line 55

def path_to_current_file
  @path
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)


77
78
79
# File 'lib/import_js/emacs_editor.rb', line 77

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

#shift_widthNumber?

Returns:

  • (Number?)


156
157
158
# File 'lib/import_js/emacs_editor.rb', line 156

def shift_width
  2
end

#tabString

Returns shiftwidth number of spaces if expandtab is not set, otherwise ‘t`.

Returns:

  • (String)

    shiftwidth number of spaces if expandtab is not set, otherwise ‘t`



151
152
153
# File 'lib/import_js/emacs_editor.rb', line 151

def tab
  return ' ' * shift_width || 2
end

#write_fileObject



38
39
40
41
42
# File 'lib/import_js/emacs_editor.rb', line 38

def write_file
  new_file = File.open(@path, 'w')
  @file.each {|line| new_file.puts(line) }
  new_file.close
end