summaryrefslogtreecommitdiff
path: root/plugins/structuralsearch/source/com/intellij/structuralsearch/inspection/highlightTemplate/SSBasedInspectionCompiledPatternsCache.java
blob: 2d6335fe10ce5afe1bcd01e26213c790072124d9 (plain)
1
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.intellij.structuralsearch.inspection.highlightTemplate;

import com.intellij.codeInspection.InspectionProfile;
import com.intellij.codeInspection.ex.InspectionToolWrapper;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;
import com.intellij.openapi.util.Key;
import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
import com.intellij.structuralsearch.Matcher;
import com.intellij.structuralsearch.impl.matcher.MatcherImpl;
import com.intellij.structuralsearch.plugin.ui.Configuration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;

import java.util.Collections;
import java.util.List;

/**
 * @author Eugene.Kudelevsky
 */
public class SSBasedInspectionCompiledPatternsCache implements StartupActivity {
  private static final Key<MatcherImpl.CompiledOptions> COMPILED_OPTIONS_KEY = Key.create("SSR_INSPECTION_COMPILED_OPTIONS_KEY");

  @Override
  public void runActivity(@NotNull final Project project) {
     precompileConfigurations(project, null);
  }

  static void precompileConfigurations(final Project project, @Nullable final SSBasedInspection ssBasedInspection) {
    if (project.isDisposed()) {
      return;
    }
    final MatcherImpl.CompiledOptions currentCompiledOptions = getCompiledOptions(project);

    final SSBasedInspection inspection = ssBasedInspection != null ? ssBasedInspection : getInspection(project);
    if (inspection == null) {
      return;
    }

    List<Configuration> configurations = inspection.getConfigurations();
    if (configurations == null) {
      configurations = Collections.emptyList();
    }

    if ((currentCompiledOptions == null || currentCompiledOptions.getMatchContexts().isEmpty()) &&
        configurations.isEmpty()) {
      return;
    }

    final Matcher matcher = new Matcher(project);
    final MatcherImpl.CompiledOptions compiledOptions = matcher.precompileOptions(configurations);

    if (compiledOptions != null) {
      project.putUserData(COMPILED_OPTIONS_KEY, compiledOptions);
    }
  }

  @Nullable
  private static SSBasedInspection getInspection(@NotNull Project project) {
    final InspectionProfile profile = InspectionProjectProfileManager.getInstance(project).getInspectionProfile();
    final InspectionToolWrapper entry = profile.getInspectionTool(SSBasedInspection.SHORT_NAME, project);

    return entry == null ? null : (SSBasedInspection)entry.getTool();
  }

  @Nullable
  static MatcherImpl.CompiledOptions getCompiledOptions(@NotNull Project project) {
    return project.getUserData(COMPILED_OPTIONS_KEY);
  }

  @TestOnly
  static void setCompiledOptions(@NotNull Project project, @NotNull List<Configuration> configurations) {
    final Matcher matcher = new Matcher(project);
    project.putUserData(COMPILED_OPTIONS_KEY,
                        matcher.precompileOptions(configurations));
  }
}