Class: ImportJS::VIMEditor
- Inherits:
-
Object
- Object
- ImportJS::VIMEditor
- Defined in:
- lib/import_js/vim_editor.rb
Overview
This is the implementation of the VIM integration in Import-JS. It can be used as a template for other editor integrations.
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.
-
#current_word ⇒ String
Get the word currently under the cursor.
-
#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.
-
#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.
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).
83 84 85 |
# File 'lib/import_js/vim_editor.rb', line 83 def append_line(line_number, str) VIM::Buffer.current.append(line_number, str) end |
#ask_for_selection(word, alternatives) ⇒ Number?
Ask the user to select something from a list of alternatives.
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/import_js/vim_editor.rb', line 100 def ask_for_selection(word, alternatives) escaped_list = ["\"ImportJS: Pick JS module to import for '#{word}':\""] 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.
90 91 92 |
# File 'lib/import_js/vim_editor.rb', line 90 def count_lines VIM::Buffer.current.count end |
#current_file_content ⇒ String
Read the entire file into a string.
43 44 45 |
# File 'lib/import_js/vim_editor.rb', line 43 def current_file_content VIM.evaluate('join(getline(1, "$"), "\n")') end |
#current_word ⇒ String
Get the word currently under the cursor.
9 10 11 |
# File 'lib/import_js/vim_editor.rb', line 9 def current_word VIM.evaluate("expand('<cword>')") end |
#cursor ⇒ Array(Number, Number)
Get the cursor position.
58 59 60 |
# File 'lib/import_js/vim_editor.rb', line 58 def cursor VIM::Window.current.cursor end |
#cursor=(position_tuple) ⇒ Object
Place the cursor at a specified position.
66 67 68 |
# File 'lib/import_js/vim_editor.rb', line 66 def cursor=(position_tuple) VIM::Window.current.cursor = position_tuple end |
#delete_line(line_number) ⇒ Object
Delete a line.
74 75 76 |
# File 'lib/import_js/vim_editor.rb', line 74 def delete_line(line_number) VIM::Buffer.current.delete(line_number) end |
#message(str) ⇒ Object
Display a message to the user.
31 32 33 34 35 36 37 38 |
# File 'lib/import_js/vim_editor.rb', line 31 def (str) # To prevent having to press enter to dismiss, we ellipsize the message if str.length > available_columns - 1 str = str[0...(available_columns - 2)] + '…' end VIM.command(":call importjs#WideMsg('#{str}')") end |
#open_file(file_path) ⇒ Object
Open a file specified by a path.
24 25 26 |
# File 'lib/import_js/vim_editor.rb', line 24 def open_file(file_path) VIM.command("e #{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.
17 18 19 |
# File 'lib/import_js/vim_editor.rb', line 17 def path_to_current_file VIM.evaluate("expand('%')") 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.
51 52 53 |
# File 'lib/import_js/vim_editor.rb', line 51 def read_line(line_number) VIM::Buffer.current[line_number] end |