Trying to add quickfixes to a custom plugin

Please provide:

  • Operating system: Windows 10
  • IDE name and flavor/env: IntelliJ with SonarQube for IDE version 10.23.0.81344

And a thorough description of the problem / question:

I’m currently working on a custom plugin with a few custom Java rules. For some those rules I would like to add quickfixes, however, currently they are not showing up when I open an issue in the IDE. Unfortunately, I couldn’t find any documentation on how to create your own quickfixes so if anyone has something I could look into, I’d be very grateful.

This is the code snippet where I attempt to add the quickfixes:

for (String value : values) {
    NewQuickFix quickFix = issue.newQuickFix().message("Change value to " + value);
    NewInputFileEdit inputFileEdit = quickFix.newInputFileEdit().on(inputFile);
    NewTextEdit textEdit = inputFileEdit.newTextEdit().at(location);
    if (value.matches("[0-9]+")) {
        textEdit = textEdit.withNewText(value);
    } else {
        textEdit = textEdit.withNewText("\"" + value + "\"");
    }
    inputFileEdit = inputFileEdit.addTextEdit(textEdit);
    quickFix = quickFix.addInputFileEdit(inputFileEdit);
    issue.addQuickFix(quickFix);
}
issue.setQuickFixAvailable(true);

I have confirmed that the values array is not empty so that can’t be the problem and there aren’t any errors being thrown.

Thanks in advance!

Hello @brec , thanks for sharing with the community.

It would help me a lot if you could share the whole rule code.

Also which is the process you follow with your rule that includes the quickfix and SonarQube / SonarQube for IDE in terms of compilation, etc ? I mean in order to see it in SonarQube for IDE.

Meanwhile here you can see a built in rule with a quickfix : sonar-java/java-checks/src/main/java/org/sonar/java/checks/MathClampRangeCheck.java at c44fcee0e9c22a1b0662b8c443771617db430d82 · SonarSource/sonar-java · GitHub

Thank you for your answer.

About compilation and viewing issues in IDE:
The plugin is a shaded maven jar with SonarLint support set to true. However, we use a custom sensor for the analysis instead of tying into the SonarJava analyzer and I’m aware that (from what I understand) SonarQube for IDE doesn’t execute custom sensors. Therefore, the analysis is currently only run from the command line, but through Connected Mode the issues can still be opened in the IDE. I was hoping that this would be enough to make quickfixes available, since creating issues from a SensorContext (NewIssue) appears to have methods for this, as I used above.

As for rule code:
The plugin is for integrating a SAST tool (as a maven dependency, in case that’s important) that analyses a jar. This means that inside our sensor the process is to generate the jar → run the tool which creates a json report → create a SonarQube issue for every issue in the json report. Thus, the only actual “rule code” we have is the RulesDefinition where the repository is created and the html + json files that contain the descriptions, tags, etc. In the json files the rules have a quickfix field set to partial, covered, infeasible…

So the exact process is (after the SAST tool analysis):
The issues from the json report are mapped to the rules and (before the code snippet I already posted) a NewIssue is created with the rule, location and error message → if the error message from the json report contains quickfix options then those options are added (the code snippet) → the issue is saved and then shows up in SonarQube. If quickfixesAvailable was set to true, then the issue in SonarQube has a SonarQube for IDE icon with the text “Quick fix”, but when opening the issue in the IDE I can’t seem to find the quickfix.

Do you think the problem could be solely from the fact that a custom sensor is used? Because I was hoping that I won’t have to try to change that part, but unfortunately the example in the link only works with a JavaFileScannerContext, at least from what I can see.

Also, is there some kind of API endpoint that I could use to check if the quickfixes actually “arrived” in the SonarQube server? I looked through the Web API but didn’t see anything for the actual quickfixes, only the parameter quickfixAvaliable.

Thanks again in advance for your help!