Class: ImportJS::VIMEditor

Inherits:
Object
  • Object
show all
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

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)


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.

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.



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_linesNumber

Count the number of lines in the file.

Returns:

  • (Number)

    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_contentString

Read the entire file into a string.

Returns:

  • (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_wordString

Get the word currently under the cursor.

Returns:

  • (String)


9
10
11
# File 'lib/import_js/vim_editor.rb', line 9

def current_word
  VIM.evaluate("expand('<cword>')")
end

#cursorArray(Number, Number)

Get the cursor position.

Returns:

  • (Array(Number, Number))


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.

Parameters:

  • position_tuple (Array(Number, Number))

    the row and column to place the cursor at.



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.

Parameters:

  • line_number (Number)

    One-indexed line number. 1 is the first line in the file.



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.

Parameters:

  • str (String)


31
32
33
34
35
36
37
38
# File 'lib/import_js/vim_editor.rb', line 31

def message(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.

Parameters:

  • file_path (String)


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

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

Returns:

  • (String?)


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.

Returns:

  • (String)


51
52
53
# File 'lib/import_js/vim_editor.rb', line 51

def read_line(line_number)
  VIM::Buffer.current[line_number]
end