Module: RuVim::Lang::Javascript

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

Constant Summary collapse

KEYWORDS =
%w[
  async await break case catch class const continue debugger default
  delete do else export extends finally for from function if import
  in instanceof let new of return static super switch this throw
  try typeof var void while with yield
  true false null undefined NaN Infinity
].freeze
KEYWORD_RE =
/\b(?:#{KEYWORDS.join("|")})\b/
STRING_DOUBLE_RE =
/"(?:\\.|[^"\\])*"/
STRING_SINGLE_RE =
/'(?:\\.|[^'\\])*'/
TEMPLATE_RE =
/`(?:\\.|[^`\\])*`/
NUMBER_RE =
/\b(?:0[xXoObB][\da-fA-F_]+|\d[\d_]*(?:\.[\d_]+)?(?:[eE][+-]?\d+)?n?)\b/
LINE_COMMENT_RE =
%r{//.*}
BLOCK_COMMENT_RE =
%r{/\*.*?\*/}
CONSTANT_RE =
/\b[A-Z][A-Z0-9_]{1,}\b/
REGEX_RE =
%r{/(?:\\.|[^/\\])+/[gimsuvy]*}
INDENT_OPEN_RE =
/[{(\[]\s*(?:\/\/.*)?$/
INDENT_CLOSE_RE =
/\A\s*[}\])]/
DEDENT_TRIGGERS =
{
  "}" => /\A(\s*)\}/,
  "]" => /\A(\s*)\]/,
  ")" => /\A(\s*)\)/
}.freeze

Class Method Summary collapse

Class Method Details

.calculate_indent(lines, target_row, shiftwidth) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruvim/lang/javascript.rb', line 35

def calculate_indent(lines, target_row, shiftwidth)
  depth = 0
  (0...target_row).each do |row|
    line = lines[row].to_s
    line.each_char do |ch|
      case ch
      when "{", "[", "(" then depth += 1
      when "}", "]", ")" then depth -= 1
      end
    end
  end

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

.color_columns(text) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ruvim/lang/javascript.rb', line 61

def color_columns(text)
  cols = {}
  Highlighter.apply_regex(cols, text, STRING_SINGLE_RE, Highlighter::STRING_COLOR)
  Highlighter.apply_regex(cols, text, STRING_DOUBLE_RE, Highlighter::STRING_COLOR)
  Highlighter.apply_regex(cols, text, TEMPLATE_RE, Highlighter::STRING_COLOR)
  Highlighter.apply_regex(cols, text, KEYWORD_RE, Highlighter::KEYWORD_COLOR)
  Highlighter.apply_regex(cols, text, NUMBER_RE, Highlighter::NUMBER_COLOR)
  Highlighter.apply_regex(cols, text, CONSTANT_RE, Highlighter::CONSTANT_COLOR)
  Highlighter.apply_regex(cols, text, BLOCK_COMMENT_RE, Highlighter::COMMENT_COLOR, override: true)
  Highlighter.apply_regex(cols, text, LINE_COMMENT_RE, Highlighter::COMMENT_COLOR, override: true)
  cols
end

.dedent_trigger(char) ⇒ Object



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

def dedent_trigger(char)
  DEDENT_TRIGGERS[char]
end

.indent_trigger?(line) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/ruvim/lang/javascript.rb', line 53

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