From 45f2da118a4fff160a9dbf57a1b5c2a67ac0849a Mon Sep 17 00:00:00 2001 From: Lukasz Jarocki Date: Fri, 25 Nov 2022 10:15:15 +0100 Subject: SONAR-17592 adding code formatting in one of the sensor in xoo plugin --- .../org/sonar/xoo/rule/MultilineIssuesSensor.java | 34 +++++++++++++++------- .../sonar/xoo/rule/MultilineIssuesSensorTest.java | 13 ++++++--- 2 files changed, 33 insertions(+), 14 deletions(-) (limited to 'plugins/sonar-xoo-plugin') 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 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 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 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 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 parseIssues(InputFile file) { Map 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 flows = sensorContextTester.allIssues().iterator().next().flows(); + Issue issue = sensorContextTester.allIssues().iterator().next(); + assertThat(issue.primaryLocation().messageFormattings()).isNotEmpty(); + + List flows = issue.flows(); assertThat(flows).hasSize(2); List 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) { -- cgit v1.2.3