diff options
author | Lukasz Jarocki <lukasz.jarocki@sonarsource.com> | 2022-11-25 10:15:15 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-12-01 20:03:12 +0000 |
commit | 45f2da118a4fff160a9dbf57a1b5c2a67ac0849a (patch) | |
tree | 774828c56c84be4f9f1e264ca958fb84a5ce4e56 /plugins/sonar-xoo-plugin | |
parent | 66b8bff0dcc778eb93da92cd408214bbfca82c2a (diff) | |
download | sonarqube-45f2da118a4fff160a9dbf57a1b5c2a67ac0849a.tar.gz sonarqube-45f2da118a4fff160a9dbf57a1b5c2a67ac0849a.zip |
SONAR-17592 adding code formatting in one of the sensor in xoo plugin
Diffstat (limited to 'plugins/sonar-xoo-plugin')
-rw-r--r-- | plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/MultilineIssuesSensor.java | 34 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/MultilineIssuesSensorTest.java | 13 |
2 files changed, 33 insertions, 14 deletions
diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/MultilineIssuesSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/MultilineIssuesSensor.java index db54208c928..1db7c8639a3 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/MultilineIssuesSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/MultilineIssuesSensor.java @@ -40,9 +40,11 @@ import org.sonar.api.batch.fs.TextPointer; import org.sonar.api.batch.sensor.Sensor; import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.SensorDescriptor; +import org.sonar.api.batch.sensor.issue.MessageFormatting; import org.sonar.api.batch.sensor.issue.NewIssue; import org.sonar.api.batch.sensor.issue.NewIssue.FlowType; import org.sonar.api.batch.sensor.issue.NewIssueLocation; +import org.sonar.api.batch.sensor.issue.NewMessageFormatting; import org.sonar.api.rule.RuleKey; import org.sonar.xoo.Xoo; @@ -68,10 +70,7 @@ public class MultilineIssuesSensor implements Sensor { @Override public void describe(SensorDescriptor descriptor) { - descriptor - .name("Multiline Issues") - .onlyOnLanguages(Xoo.KEY) - .createIssuesForRuleRepositories(XooRulesDefinition.XOO_REPOSITORY); + descriptor.name("Multiline Issues").onlyOnLanguages(Xoo.KEY).createIssuesForRuleRepositories(XooRulesDefinition.XOO_REPOSITORY); } @Override @@ -97,19 +96,25 @@ public class MultilineIssuesSensor implements Sensor { for (ParsedIssue parsedIssue : parsedIssues) { NewIssue newIssue = context.newIssue().forRule(ruleKey); - NewIssueLocation primaryLocation = newIssue.newLocation() - .on(file) - .at(file.newRange(parsedIssue.start, parsedIssue.end)); - newIssue.at(primaryLocation.message("Primary location")); + NewIssueLocation primaryLocation = newIssue.newLocation(); + String message = "Primary location of the issue in xoo code"; + List<NewMessageFormatting> newMessageFormattings = formatIssueMessage(message, primaryLocation.newMessageFormatting()); + newIssue.at(primaryLocation.on(file) + .at(file.newRange(parsedIssue.start, parsedIssue.end)) + .message(message, newMessageFormattings)); for (ParsedFlow flow : flowIndex.getFlows(parsedIssue.issueId)) { List<NewIssueLocation> flowLocations = new LinkedList<>(); for (ParsedFlowLocation flowLocation : flow.getLocations()) { - flowLocations.add(newIssue.newLocation() + String locationMessage = "Xoo code, flow step #" + flowLocation.flowLocationId; + NewIssueLocation newIssueLocation = newIssue.newLocation(); + List<NewMessageFormatting> locationMessageFormattings = formatIssueMessage(locationMessage, newIssueLocation.newMessageFormatting()); + newIssueLocation .on(file) .at(file.newRange(flowLocation.start, flowLocation.end)) - .message("Flow step #" + flowLocation.flowLocationId)); + .message(locationMessage, locationMessageFormattings); + flowLocations.add(newIssueLocation); } if (flow.getType() != null) { @@ -122,6 +127,15 @@ public class MultilineIssuesSensor implements Sensor { } } + private static List<NewMessageFormatting> formatIssueMessage(String message, NewMessageFormatting newMessageFormatting) { + int startIndex = message.toLowerCase().indexOf("xoo"); + if(startIndex == -1) { + return List.of(); + } + int endIndex = startIndex + "xoo".length(); + return List.of(newMessageFormatting.start(startIndex).end(endIndex).type(MessageFormatting.Type.CODE)); + } + private static Collection<ParsedIssue> parseIssues(InputFile file) { Map<Integer, ParsedIssue> issuesById = new HashMap<>(); diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/MultilineIssuesSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/MultilineIssuesSensorTest.java index 916ff200329..a0d405743aa 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/MultilineIssuesSensorTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/MultilineIssuesSensorTest.java @@ -35,7 +35,7 @@ import org.sonar.api.batch.rule.ActiveRule; import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.batch.sensor.internal.SensorContextTester; import org.sonar.api.batch.sensor.issue.Issue; -import org.sonar.api.batch.sensor.issue.NewIssue; +import org.sonar.api.batch.sensor.issue.IssueLocation; import org.sonar.api.batch.sensor.issue.NewIssue.FlowType; import org.sonar.api.batch.sensor.issue.internal.DefaultIssueFlow; import org.sonar.api.internal.apachecommons.io.IOUtils; @@ -61,7 +61,7 @@ public class MultilineIssuesSensorTest { } @Test - public void execute_dataAndExecutionFlowsAreDetected() throws IOException { + public void execute_dataAndExecutionFlowsAreDetectedAndMessageIsFormatted() throws IOException { DefaultInputFile inputFile = newTestFile(IOUtils.toString(getClass().getResource("dataflow.xoo"), StandardCharsets.UTF_8)); DefaultFileSystem fs = new DefaultFileSystem(temp.newFolder()); @@ -73,12 +73,17 @@ public class MultilineIssuesSensorTest { assertThat(sensorContextTester.allIssues()).hasSize(1); - List<Issue.Flow> flows = sensorContextTester.allIssues().iterator().next().flows(); + Issue issue = sensorContextTester.allIssues().iterator().next(); + assertThat(issue.primaryLocation().messageFormattings()).isNotEmpty(); + + List<Issue.Flow> flows = issue.flows(); assertThat(flows).hasSize(2); List<DefaultIssueFlow> defaultIssueFlows = flows.stream().map(DefaultIssueFlow.class::cast).collect(Collectors.toList()); - assertThat(defaultIssueFlows).extracting(DefaultIssueFlow::type).containsExactlyInAnyOrder(FlowType.DATA, FlowType.EXECUTION); + + assertThat(flows.get(0).locations()).extracting(IssueLocation::messageFormattings).isNotEmpty(); + assertThat(flows.get(1).locations()).extracting(IssueLocation::messageFormattings).isNotEmpty(); } private DefaultInputFile newTestFile(String content) { |