From c0c77159e1a512a1724b73550f4ede36619d1560 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Fri, 13 May 2016 15:38:00 +0200 Subject: [PATCH] SONAR-7390 support highlighting over multiline including empty one --- .../source/HighlightingLineReader.java | 4 +- .../source/RangeOffsetConverter.java | 3 +- .../source/HighlightingLineReaderTest.java | 52 +++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/source/HighlightingLineReader.java b/server/sonar-server/src/main/java/org/sonar/server/computation/source/HighlightingLineReader.java index 5e465939a2c..6d96228e836 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/source/HighlightingLineReader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/source/HighlightingLineReader.java @@ -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); diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/source/RangeOffsetConverter.java b/server/sonar-server/src/main/java/org/sonar/server/computation/source/RangeOffsetConverter.java index b029fe8254f..9cb8b131452 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/source/RangeOffsetConverter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/source/RangeOffsetConverter.java @@ -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); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/source/HighlightingLineReaderTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/source/HighlightingLineReaderTest.java index fe2493acb78..124653a77c1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/source/HighlightingLineReaderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/source/HighlightingLineReaderTest.java @@ -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 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 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( -- 2.39.5