]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7390 support highlighting over multiline including empty one 975/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 13 May 2016 13:38:00 +0000 (15:38 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 17 May 2016 08:57:57 +0000 (10:57 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/source/HighlightingLineReader.java
server/sonar-server/src/main/java/org/sonar/server/computation/source/RangeOffsetConverter.java
server/sonar-server/src/test/java/org/sonar/server/computation/source/HighlightingLineReaderTest.java

index 5e465939a2cb26cd6a5784f7111c565b8c348ff6..6d96228e8368a0cc8340ab82f6681bb5e9a3b50c 100644 (file)
@@ -103,7 +103,9 @@ public class HighlightingLineReader implements LineReader {
     if (range.getStartLine() <= line) {
       String offsets = rangeOffsetConverter.offsetToString(syntaxHighlighting.getRange(), line, lineBuilder.getSource().length());
       if (offsets.isEmpty()) {
-        syntaxHighlightingIterator.remove();
+        if (range.getEndLine() == line) {
+          syntaxHighlightingIterator.remove();
+        }
       } else {
         if (highlighting.length() > 0) {
           highlighting.append(SYMBOLS_SEPARATOR);
index b029fe8254f3ec41a857836a8d109da93739b08e..9cb8b131452bee4e1bea56abe5f15dcf21a0114c 100644 (file)
@@ -29,8 +29,6 @@ public class RangeOffsetConverter {
   static final String SYMBOLS_SEPARATOR = ";";
 
   public String offsetToString(ScannerReport.TextRange range, int lineIndex, int lineLength) {
-    StringBuilder element = new StringBuilder();
-
     validateOffsetOrder(range, lineIndex);
     validateStartOffsetNotGreaterThanLineLength(range, lineLength, lineIndex);
     validateEndOffsetNotGreaterThanLineLength(range, lineLength, lineIndex);
@@ -38,6 +36,7 @@ public class RangeOffsetConverter {
     int startOffset = range.getStartLine() == lineIndex ? range.getStartOffset() : 0;
     int endOffset = range.getEndLine() == lineIndex ? range.getEndOffset() : lineLength;
 
+    StringBuilder element = new StringBuilder();
     if (startOffset < endOffset) {
       element.append(startOffset).append(OFFSET_SEPARATOR);
       element.append(endOffset);
index fe2493acb7817069756cd3942ab424cb4ac86a40..124653a77c17e294c23137425235aba219f47fb3 100644 (file)
@@ -44,7 +44,9 @@ import static org.sonar.db.protobuf.DbFileSources.Data.newBuilder;
 import static org.sonar.scanner.protocol.output.ScannerReport.SyntaxHighlightingRule.HighlightingType.ANNOTATION;
 import static org.sonar.scanner.protocol.output.ScannerReport.SyntaxHighlightingRule.HighlightingType.COMMENT;
 import static org.sonar.scanner.protocol.output.ScannerReport.SyntaxHighlightingRule.HighlightingType.CONSTANT;
+import static org.sonar.scanner.protocol.output.ScannerReport.SyntaxHighlightingRule.HighlightingType.CPP_DOC;
 import static org.sonar.scanner.protocol.output.ScannerReport.SyntaxHighlightingRule.HighlightingType.HIGHLIGHTING_STRING;
+import static org.sonar.scanner.protocol.output.ScannerReport.SyntaxHighlightingRule.HighlightingType.KEYWORD;
 import static org.sonar.server.computation.component.ReportComponent.builder;
 
 public class HighlightingLineReaderTest {
@@ -112,6 +114,56 @@ public class HighlightingLineReaderTest {
     assertThat(line4.getHighlighting()).isEqualTo(RANGE_LABEL_3 + ",c");
   }
 
+  @Test
+  public void supports_highlighting_over_multiple_lines_including_an_empty_one() {
+    List<ScannerReport.SyntaxHighlightingRule> syntaxHighlightingList = new ArrayList<>();
+    addHighlighting(syntaxHighlightingList, 1, 0, 1, 7, KEYWORD); // package
+    addHighlighting(syntaxHighlightingList, 2, 0, 4, 6, CPP_DOC); // comment over 3 lines
+    addHighlighting(syntaxHighlightingList, 5, 0, 5, 6, KEYWORD); // public
+    addHighlighting(syntaxHighlightingList, 5, 7, 5, 12, KEYWORD); // class
+    HighlightingLineReader highlightingLineReader = new HighlightingLineReader(FILE, syntaxHighlightingList.iterator(), new RangeOffsetConverter());
+
+    DbFileSources.Line.Builder[] builders = new DbFileSources.Line.Builder[] {
+      addSourceLine(highlightingLineReader, 1, "package example;"),
+      addSourceLine(highlightingLineReader, 2, "/*"),
+      addSourceLine(highlightingLineReader, 3, ""),
+      addSourceLine(highlightingLineReader, 4, " foo*/"),
+      addSourceLine(highlightingLineReader, 5, "public class One {"),
+      addSourceLine(highlightingLineReader, 6, "}")
+    };
+
+    assertThat(builders)
+      .extracting("highlighting")
+      .containsExactly(
+        "0,7,k",
+        "0,2,cppd",
+        "",
+        "0,6,cppd",
+        "0,6,k;7,12,k",
+        "");
+  }
+
+  private DbFileSources.Line.Builder addSourceLine(HighlightingLineReader highlightingLineReader, int line, String source) {
+    DbFileSources.Line.Builder lineBuilder = sourceData.addLinesBuilder().setSource(source).setLine(line);
+    highlightingLineReader.read(lineBuilder);
+    return lineBuilder;
+  }
+
+  private void addHighlighting(List<ScannerReport.SyntaxHighlightingRule> syntaxHighlightingList,
+    int startLine, int startOffset,
+    int endLine, int endOffset,
+    HighlightingType type) {
+    TextRange.Builder textRangeBuilder = TextRange.newBuilder();
+    ScannerReport.SyntaxHighlightingRule.Builder ruleBuilder = ScannerReport.SyntaxHighlightingRule.newBuilder();
+    syntaxHighlightingList.add(ruleBuilder
+      .setRange(textRangeBuilder
+        .setStartLine(startLine).setEndLine(endLine)
+        .setStartOffset(startOffset).setEndOffset(endOffset)
+        .build())
+      .setType(type)
+      .build());
+  }
+
   @Test
   public void read_many_syntax_highlighting_on_same_line() {
     HighlightingLineReader highlightingLineReader = newReader(of(