Class: ImportJS::EmacsEditor

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

Overview

This is the implementation of the emacs integration in Import-JS.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEmacsEditor

Returns a new instance of EmacsEditor.



7
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
# File 'lib/import_js/emacs_editor.rb', line 7

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'
        Importer.new(self).import
        write_file
        puts 'import:success'
      when 'goto'
        Importer.new(self).goto
      when 'fix'
        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.



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

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)


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

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.



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

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



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

def count_lines
  @file.size
end

#current_file_contentString

Read the entire file into a string.

Returns:

  • (String)


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

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

#cursorArray(Number, Number)

Get the cursor position.

Returns:

  • (Array(Number, Number))


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

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



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

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.



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

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

#message(str) ⇒ Object

Display a message to the user.

Parameters:

  • str (String)


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

def message(str)
  puts str
end

#open_file(file_path) ⇒ Object

Open a file specified by a path.

Parameters:

  • file_path (String)


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

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?)


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

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)


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

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

#write_fileObject



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

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