Module: RuVim::Lang::Json

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

Constant Summary collapse

INDENT_OPEN_RE =
/[\[{]\s*$/
INDENT_CLOSE_RE =
/\A\s*[\]}]/
DEDENT_TRIGGERS =
{
  "}" => /\A(\s*)\}/,
  "]" => /\A(\s*)\]/
}.freeze

Class Method Summary collapse

Class Method Details

.calculate_indent(lines, target_row, shiftwidth) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruvim/lang/json.rb', line 16

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



42
43
44
45
46
47
48
49
# File 'lib/ruvim/lang/json.rb', line 42

def color_columns(text)
  cols = {}
  Highlighter.apply_regex(cols, text, /"(?:\\.|[^"\\])*"\s*(?=:)/, "\e[36m")
  Highlighter.apply_regex(cols, text, /"(?:\\.|[^"\\])*"/, "\e[32m")
  Highlighter.apply_regex(cols, text, /\b(?:true|false|null)\b/, "\e[35m")
  Highlighter.apply_regex(cols, text, /-?\b\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b/, "\e[33m")
  cols
end

.dedent_trigger(char) ⇒ Object



38
39
40
# File 'lib/ruvim/lang/json.rb', line 38

def dedent_trigger(char)
  DEDENT_TRIGGERS[char]
end

.indent_trigger?(line) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/ruvim/lang/json.rb', line 34

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