Module: RuVim::Lang::Elixir

Defined in:
lib/ruvim/lang/elixir.rb

Constant Summary collapse

KEYWORDS =
%w[
  def defp defmodule defmacro defmacrop defstruct defprotocol defimpl
  defguard defguardp defdelegate defoverridable defexception
  do end fn if else unless cond case when with for receive after
  raise rescue try catch throw
  import use alias require
  and or not in
  true false nil
  quote unquote
].freeze
KEYWORD_RE =
/\b(?:#{KEYWORDS.join("|")})\b/
STRING_DOUBLE_RE =
/"(?:\\.|[^"\\])*"/
STRING_SINGLE_RE =
/'(?:\\.|[^'\\])*'/
HEREDOC_RE =
/"""/
SIGIL_RE =
/~[a-zA-Z](?:\(.*?\)|\[.*?\]|\{.*?\}|<.*?>|\/.*?\/|".*?"|\|.*?\|)/
ATOM_RE =
/:\w+|:"(?:\\.|[^"\\])*"/
MODULE_RE =
/\b[A-Z]\w*(?:\.[A-Z]\w*)*/
NUMBER_RE =
/\b(?:0[xXoObB][\da-fA-F_]+|\d[\d_]*(?:\.[\d_]+)?(?:[eE][+-]?\d+)?)\b/
COMMENT_RE =
/#.*/
VARIABLE_RE =
/@\w+/
INDENT_OPEN_RE =
/\b(?:do|fn)\s*(?:->)?\s*$/
INDENT_CLOSE_RE =
/\A\s*end\b/
INDENT_MID_RE =
/\A\s*(?:else|rescue|catch|after)\b/
DEDENT_TRIGGERS =
{
  "d" => /\A(\s*)end\z/,
  "e" => /\A(\s*)(?:else|rescue)\z/,
  "h" => /\A(\s*)catch\z/,
  "r" => /\A(\s*)after\z/
}.freeze

Class Method Summary collapse

Class Method Details

.calculate_indent(lines, target_row, shiftwidth) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruvim/lang/elixir.rb', line 41

def calculate_indent(lines, target_row, shiftwidth)
  depth = 0
  (0...target_row).each do |row|
    line = lines[row].to_s.strip
    next if line.empty? || line.start_with?("#")

    depth += 1 if line.match?(/\b(?:do|fn)\b/) || line.match?(/->$/)
    depth -= 1 if line.match?(/\Aend\b/)
  end

  target_line = lines[target_row].to_s.strip
  depth -= 1 if target_line.match?(INDENT_CLOSE_RE) || target_line.match?(INDENT_MID_RE)
  depth = 0 if depth < 0
  depth * shiftwidth
end

.color_columns(text) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ruvim/lang/elixir.rb', line 65

def color_columns(text)
  cols = {}
  Highlighter.apply_regex(cols, text, STRING_DOUBLE_RE, Highlighter::STRING_COLOR)
  Highlighter.apply_regex(cols, text, STRING_SINGLE_RE, Highlighter::STRING_COLOR)
  Highlighter.apply_regex(cols, text, ATOM_RE, Highlighter::CONSTANT_COLOR)
  Highlighter.apply_regex(cols, text, KEYWORD_RE, Highlighter::KEYWORD_COLOR)
  Highlighter.apply_regex(cols, text, MODULE_RE, Highlighter::CONSTANT_COLOR)
  Highlighter.apply_regex(cols, text, VARIABLE_RE, Highlighter::VARIABLE_COLOR)
  Highlighter.apply_regex(cols, text, NUMBER_RE, Highlighter::NUMBER_COLOR)
  Highlighter.apply_regex(cols, text, SIGIL_RE, Highlighter::STRING_COLOR)
  Highlighter.apply_regex(cols, text, COMMENT_RE, Highlighter::COMMENT_COLOR, override: true)
  cols
end

.dedent_trigger(char) ⇒ Object



61
62
63
# File 'lib/ruvim/lang/elixir.rb', line 61

def dedent_trigger(char)
  DEDENT_TRIGGERS[char]
end

.indent_trigger?(line) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/ruvim/lang/elixir.rb', line 57

def indent_trigger?(line)
  line.to_s.rstrip.match?(INDENT_OPEN_RE)
end