@Override
public String getDescription(@NotNull final String sonarIssueKey, @NotNull final Editor editor) {
// retrieve sonar issue data
final Project project = editor.getProject();
if (project == null || project.isDisposed()) return null;
final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
if (psiFile == null) return null;
final Optional<IssuesByFileIndexProjectComponent> indexComponent = IssuesByFileIndexProjectComponent.getInstance(project);
if (!indexComponent.isPresent()) return null;
final IssuesByFileIndexProjectComponent state = indexComponent.get().getState();
if (state == null) return null;
final Map<String, Set<SonarIssue>> index = state.getIndex();
final String path = psiFile.getVirtualFile().getPath();
final Set<SonarIssue> issues = index.get(path);
final Optional<SonarIssue> issue = FluentIterable.from(issues).firstMatch(new Predicate<SonarIssue>() {
@Override
public boolean apply(SonarIssue sonarIssue) {
return sonarIssueKey.equals(sonarIssue.getKey());
}
});
if (!issue.isPresent()) return null;
// try to get desc from already fetched rules
final Optional<SonarRules> sonarRules = SonarRules.getInstance(project);
if (sonarRules.isPresent()) {
final SonarRules sonarRulesState = sonarRules.get().getState();
if (sonarRulesState != null) {
final Rule rule = sonarRulesState.getSonarRulesByRuleKey().get(issue.get().getRuleKey());
if (rule != null && !rule.isEmpty()) return rule.getDesc();
}
}
// fetch and persist rule
Settings settings;
final Module moduleForFile = ModuleUtil.findModuleForFile(psiFile.getVirtualFile(), project);
if (moduleForFile != null) {
settings = ModuleSettings.getInstance(moduleForFile).getState();
settings = SettingsUtil.process(project, settings);
} else {
settings = ProjectSettings.getInstance(project).getState();