Class: Mui::Lsp::Handlers::Diagnostics

Inherits:
Object
  • Object
show all
Defined in:
lib/mui/lsp/handlers/diagnostics.rb

Overview

Handler for textDocument/publishDiagnostics notifications

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(editor:) ⇒ Diagnostics

Returns a new instance of Diagnostics.



10
11
12
13
14
# File 'lib/mui/lsp/handlers/diagnostics.rb', line 10

def initialize(editor:)
  @editor = editor
  @diagnostics_by_uri = {}
  @mutex = Mutex.new
end

Instance Attribute Details

#diagnostics_by_uriObject (readonly)

Returns the value of attribute diagnostics_by_uri.



8
9
10
# File 'lib/mui/lsp/handlers/diagnostics.rb', line 8

def diagnostics_by_uri
  @diagnostics_by_uri
end

#editorObject (readonly)

Returns the value of attribute editor.



8
9
10
# File 'lib/mui/lsp/handlers/diagnostics.rb', line 8

def editor
  @editor
end

Instance Method Details

#all_diagnosticsObject



39
40
41
# File 'lib/mui/lsp/handlers/diagnostics.rb', line 39

def all_diagnostics
  @mutex.synchronize { @diagnostics_by_uri.dup }
end

#clear(uri) ⇒ Object



53
54
55
# File 'lib/mui/lsp/handlers/diagnostics.rb', line 53

def clear(uri)
  @mutex.synchronize { @diagnostics_by_uri.delete(uri) }
end

#clear_allObject



49
50
51
# File 'lib/mui/lsp/handlers/diagnostics.rb', line 49

def clear_all
  @mutex.synchronize { @diagnostics_by_uri.clear }
end

#counts(uri = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/mui/lsp/handlers/diagnostics.rb', line 57

def counts(uri = nil)
  diagnostics = uri ? diagnostics_for(uri) : @mutex.synchronize { @diagnostics_by_uri.values.flatten }

  {
    error: diagnostics.count(&:error?),
    warning: diagnostics.count(&:warning?),
    information: diagnostics.count(&:information?),
    hint: diagnostics.count(&:hint?)
  }
end

#diagnostics_at_line(uri, line) ⇒ Object



43
44
45
46
47
# File 'lib/mui/lsp/handlers/diagnostics.rb', line 43

def diagnostics_at_line(uri, line)
  diagnostics_for(uri).select do |d|
    line.between?(d.range.start.line, d.range.end.line)
  end
end

#diagnostics_for(uri) ⇒ Object



35
36
37
# File 'lib/mui/lsp/handlers/diagnostics.rb', line 35

def diagnostics_for(uri)
  @mutex.synchronize { @diagnostics_by_uri[uri] || [] }
end

#handle(params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mui/lsp/handlers/diagnostics.rb', line 16

def handle(params)
  uri = params["uri"]
  raw_diagnostics = params["diagnostics"] || []

  diagnostics = raw_diagnostics.map do |d|
    Protocol::Diagnostic.from_hash(d)
  end

  @mutex.synchronize do
    if diagnostics.empty?
      @diagnostics_by_uri.delete(uri)
    else
      @diagnostics_by_uri[uri] = diagnostics
    end
  end

  update_display(uri, diagnostics)
end

#summary(uri = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/mui/lsp/handlers/diagnostics.rb', line 68

def summary(uri = nil)
  c = counts(uri)
  parts = []
  parts << "E:#{c[:error]}" if c[:error].positive?
  parts << "W:#{c[:warning]}" if c[:warning].positive?
  parts << "I:#{c[:information]}" if c[:information].positive?
  parts << "H:#{c[:hint]}" if c[:hint].positive?
  parts.empty? ? "" : parts.join(" ")
end