Class: ImportJS::EmacsEditor
- Inherits:
-
Object
- Object
- ImportJS::EmacsEditor
- Defined in:
- lib/import_js/emacs_editor.rb
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.
-
#max_line_length ⇒ Number?
Get the preferred max length of a line.
-
#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.
- #shift_width ⇒ Number?
-
#tab ⇒ String
Shiftwidth number of spaces if expandtab is not set, otherwise ‘t`.
- #write_file ⇒ Object
Constructor Details
#initialize ⇒ EmacsEditor
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_word ⇒ Object
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).
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.
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_lines ⇒ Number
Count 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_content ⇒ String
Read the entire file into a string.
69 70 71 |
# File 'lib/import_js/emacs_editor.rb', line 69 def current_file_content @file.join("\n") end |
#cursor ⇒ Array(Number, Number)
Get the cursor position.
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.
92 93 |
# File 'lib/import_js/emacs_editor.rb', line 92 def cursor=(position_tuple) end |
#delete_line(line_number) ⇒ Object
Delete a line.
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_length ⇒ Number?
Get the preferred max length of a line
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.
62 63 64 |
# File 'lib/import_js/emacs_editor.rb', line 62 def (str) puts str end |
#open_file(file_path) ⇒ Object
Open a file specified by a path.
47 48 49 |
# File 'lib/import_js/emacs_editor.rb', line 47 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.
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.
77 78 79 |
# File 'lib/import_js/emacs_editor.rb', line 77 def read_line(line_number) @file[line_number - 1] end |
#shift_width ⇒ Number?
156 157 158 |
# File 'lib/import_js/emacs_editor.rb', line 156 def shift_width 2 end |
#tab ⇒ String
Returns 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_file ⇒ Object
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 |