Class: ThemeCheck::LanguageServer::DiagnosticsManager

Inherits:
Object
  • Object
show all
Defined in:
lib/theme_check/language_server/diagnostics_manager.rb

Instance Method Summary collapse

Constructor Details

#initializeDiagnosticsManager

This class exists to facilitate LanguageServer diagnostics tracking.

Motivations:

1. The first time we lint, we want all the errors from all the files.
2. If we fix all the errors in a file, we have to send an empty array for that file.
3. If we do a partial check, we should consider the whole theme diagnostics as valid, and return cached results
4. We should be able to create WorkspaceEdits from diagnostics, so that the ExecuteCommandEngine can do its job
5. We should clean up diagnostics that were applied by the client


15
16
17
18
19
# File 'lib/theme_check/language_server/diagnostics_manager.rb', line 15

def initialize
  @latest_diagnostics = {} # { [Pathname(relative_path)] => Diagnostic[] }
  @mutex = Mutex.new
  @first_run = true
end

Instance Method Details

#build_diagnostics(offenses, analyzed_files: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/theme_check/language_server/diagnostics_manager.rb', line 30

def build_diagnostics(offenses, analyzed_files: nil)
  @mutex.synchronize do
    full_check = analyzed_files.nil?
    analyzed_paths = analyzed_files.map { |path| Pathname.new(path) } unless full_check

    # When analyzed_files is nil, contains all offenses.
    # When analyzed_files is !nil, contains all whole theme offenses and single file offenses of the analyzed_files.
    current_diagnostics = offenses
      .select(&:theme_file)
      .group_by(&:theme_file)
      .transform_keys { |theme_file| Pathname.new(theme_file.relative_path) }
      .transform_values do |theme_file_offenses|
        theme_file_offenses.map { |o| Diagnostic.new(o) }
      end

    previous_paths = paths(@latest_diagnostics)
    current_paths = paths(current_diagnostics)

    diagnostics_update = (current_paths + previous_paths).map do |path|
      # When doing a full_check, we either send the current
      # diagnostics or an empty array to clear the diagnostics
      # for that file.
      if full_check
        [path, current_diagnostics[path] || []]

      # When doing a partial check, the single file diagnostics
      # from the previous runs should be sent. Otherwise the
      # latest results are the good ones.
      else
        new_diagnostics = current_diagnostics[path] || []
        should_use_cached_results = !analyzed_paths.include?(path)
        old_diagnostics = should_use_cached_results ? single_file_diagnostics(path) : []
        [path, new_diagnostics + old_diagnostics]
      end
    end.to_h

    @latest_diagnostics = diagnostics_update.reject { |_, v| v.empty? }
    @first_run = false
    diagnostics_update
  end
end

#delete_applied(diagnostics) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/theme_check/language_server/diagnostics_manager.rb', line 81

def delete_applied(diagnostics)
  diagnostics = sanitize(diagnostics)
    .select(&:correctable?)

  previous_paths = paths(@latest_diagnostics)

  diagnostics.each do |diagnostic|
    delete(diagnostic.relative_path, diagnostic)
  end

  current_paths = paths(@latest_diagnostics)

  (current_paths + previous_paths).map do |path|
    [path, @latest_diagnostics[path] || []]
  end.to_h
end

#diagnostics(relative_path) ⇒ Object



25
26
27
28
# File 'lib/theme_check/language_server/diagnostics_manager.rb', line 25

def diagnostics(relative_path)
  relative_path = Pathname.new(relative_path) if relative_path.is_a?(String)
  @mutex.synchronize { @latest_diagnostics[relative_path] || [] }
end

#first_run?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/theme_check/language_server/diagnostics_manager.rb', line 21

def first_run?
  @first_run
end

#workspace_edit(diagnostics) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/theme_check/language_server/diagnostics_manager.rb', line 72

def workspace_edit(diagnostics)
  diagnostics = sanitize(diagnostics)
    .select(&:correctable?)

  {
    documentChanges: document_changes(diagnostics),
  }
end