Module: TypeProf::LSP

Defined in:
lib/typeprof/lsp/text.rb,
lib/typeprof/lsp/util.rb,
lib/typeprof/lsp/server.rb,
lib/typeprof/lsp/messages.rb

Defined Under Namespace

Modules: ErrorCodes Classes: Message, Reader, Server, Text, Writer

Class Method Summary collapse

Class Method Details

.load_json_with_comments(path, **opts) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/typeprof/lsp/util.rb', line 2

def self.load_json_with_comments(path, **opts)
  json = File.read(path)

  state = :normal
  last_comma_index = nil
  trailing_commas = []
  json = json.gsub(%r(\\.|//|/\*|\*/|[",\n/}\]*]|(\s+)|[^\s\\"*/,]+)) do
    case $&
    when "//"
      state = :single_line_comment if state == :normal
    when "\n"
      state = :normal if state == :single_line_comment
      next "\n"
    when "/*"
      state = :multi_line_comment if state == :normal
    when "*/"
      state = :normal if state == :multi_line_comment
      next "  " if state == :normal
    when "\""
      case state
      when :normal
        last_comma_index = nil
        state = :string_literal
      when :string_literal
        state = :normal
      end
    when ","
      last_comma_index = $~.begin(0) if state == :normal
    when "}", "]"
      if state == :normal && last_comma_index
        trailing_commas << last_comma_index
        last_comma_index = nil
      end
    when $1
    else
      last_comma_index = nil if state == :normal
    end
    if state == :normal || state == :string_literal
      $&
    else
      " " * $&.size
    end
  end
  trailing_commas.each do |i|
    json[i] = " "
  end

  JSON.parse(json, **opts)
end