Class: ImportJS::EmacsEditor
- Inherits:
-
Object
- Object
- ImportJS::EmacsEditor
- Defined in:
- lib/import_js/emacs_editor.rb
Overview
This is the implementation of the emacs integration in Import-JS.
Instance Attribute Summary collapse
-
#current_word ⇒ Object
Returns the value of attribute current_word.
Instance Method Summary collapse
-
#append_line(line_number, str) ⇒ Object
Append a line right after the specified line.
-
#ask_for_selection(word, alternatives) ⇒ Number?
Ask the user to select something from a list of alternatives.
-
#count_lines ⇒ Number
Count the number of lines in the file.
-
#current_file_content ⇒ String
Read the entire file into a string.
-
#cursor ⇒ Array(Number, Number)
Get the cursor position.
-
#cursor=(position_tuple) ⇒ Object
Place the cursor at a specified position.
-
#delete_line(line_number) ⇒ Object
Delete a line.
-
#initialize ⇒ EmacsEditor
constructor
A new instance of EmacsEditor.
-
#message(str) ⇒ Object
Display a message to the user.
-
#open_file(file_path) ⇒ Object
Open a file specified by a path.
-
#path_to_current_file ⇒ String?
Get the path to the file currently being edited.
-
#read_line(line_number) ⇒ String
Reads a line from the file.
- #write_file ⇒ Object
Constructor Details
#initialize ⇒ EmacsEditor
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_word ⇒ Object
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).
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.
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_lines ⇒ Number
Count 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_content ⇒ String
Read the entire file into a string.
68 69 70 |
# File 'lib/import_js/emacs_editor.rb', line 68 def current_file_content @file.join("\n") end |
#cursor ⇒ Array(Number, Number)
Get the cursor position.
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.
91 92 |
# File 'lib/import_js/emacs_editor.rb', line 91 def cursor=(position_tuple) end |
#delete_line(line_number) ⇒ Object
Delete a line.
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.
61 62 63 |
# File 'lib/import_js/emacs_editor.rb', line 61 def (str) puts str end |
#open_file(file_path) ⇒ Object
Open a file specified by a path.
46 47 48 |
# File 'lib/import_js/emacs_editor.rb', line 46 def open_file(file_path) puts "goto:success:#{File.(file_path)}" end |
#path_to_current_file ⇒ String?
Get the path to the file currently being edited. May return ‘nil` if an anonymous file is being edited.
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.
76 77 78 |
# File 'lib/import_js/emacs_editor.rb', line 76 def read_line(line_number) @file[line_number - 1] end |
#write_file ⇒ Object
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 |