Skip to content

Commit

Permalink
SONAR-6258 Persist symbols into file sources
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Apr 14, 2015
1 parent 9dda9e9 commit dcc2e1d
Show file tree
Hide file tree
Showing 10 changed files with 662 additions and 374 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@

public class HighlightingLineReader implements LineReader {

private static final String OFFSET_SEPARATOR = ",";
private static final String ITEM_SEPARATOR = ";";

private static final Map<Constants.HighlightingType, String> cssClassByType = ImmutableMap.<Constants.HighlightingType, String>builder()
.put(Constants.HighlightingType.ANNOTATION, "a")
.put(Constants.HighlightingType.CONSTANT, "c")
Expand Down Expand Up @@ -70,10 +67,8 @@ public void read(FileSourceDb.Line.Builder lineBuilder) {
BatchReport.SyntaxHighlighting syntaxHighlighting = syntaxHighlightingIterator.next();
BatchReport.Range range = syntaxHighlighting.getRange();
if (range.getStartLine() <= line) {
if (highlighting.length() > 0) {
highlighting.append(ITEM_SEPARATOR);
}
highlighting.append(convertHighlightingToString(syntaxHighlighting, line, lineBuilder.getSource()));
RangeHelper.appendRange(highlighting, syntaxHighlighting.getRange(), line, lineBuilder.getSource().length());
highlighting.append(getCssClass(syntaxHighlighting.getType()));
if (range.getEndLine() == line) {
syntaxHighlightingIterator.remove();
}
Expand All @@ -84,29 +79,6 @@ public void read(FileSourceDb.Line.Builder lineBuilder) {
}
}

private String convertHighlightingToString(BatchReport.SyntaxHighlighting syntaxHighlighting, int line, String sourceLine){
BatchReport.Range range = syntaxHighlighting.getRange();
validateStartAndEndOffset(range, line);

StringBuilder symbolLine = new StringBuilder();
if (range.getStartLine() == line) {
validateStartOffsetNotGreaterThanLineLength(range, sourceLine, line);
symbolLine.append(range.getStartOffset()).append(OFFSET_SEPARATOR);
} else if (range.getStartLine() < line) {
symbolLine.append(0).append(OFFSET_SEPARATOR);
}

if (range.getEndLine() == line) {
validateEndOffsetNotGreaterThanLineLength(range, sourceLine, line);
symbolLine.append(range.getEndOffset()).append(OFFSET_SEPARATOR);
} else if (range.getEndLine() > line) {
symbolLine.append(sourceLine.length() - 1).append(OFFSET_SEPARATOR);
}

symbolLine.append(getCssClass(syntaxHighlighting.getType()));
return symbolLine.toString();
}

private static String getCssClass(Constants.HighlightingType type) {
String cssClass = cssClassByType.get(type);
if (cssClass != null) {
Expand Down Expand Up @@ -138,22 +110,4 @@ private BatchReport.SyntaxHighlighting getNextHighlightingMatchingLine(int line)
return null;
}

private static void validateStartAndEndOffset(BatchReport.Range range, int line){
if (range.getStartLine() == range.getEndLine() && range.getStartOffset() > range.getEndOffset()) {
throw new IllegalArgumentException(String.format("End offset %s cannot be defined before start offset %s on line %s", range.getEndOffset(), range.getStartOffset(), line));
}
}

private static void validateStartOffsetNotGreaterThanLineLength(BatchReport.Range range, String sourceLine, int line){
if (range.getStartOffset() > sourceLine.length()) {
throw new IllegalArgumentException(String.format("Start offset %s is defined outside the length (%s) of the line %s", range.getStartOffset(), sourceLine.length(), line));
}
}

private static void validateEndOffsetNotGreaterThanLineLength(BatchReport.Range range, String sourceLine, int line){
if (range.getEndOffset() > sourceLine.length()) {
throw new IllegalArgumentException(String.format("End offset %s is defined outside the length (%s) of the line %s", range.getEndOffset(), sourceLine.length(), line));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.computation.source;

import org.sonar.batch.protocol.output.BatchReport;

public class RangeHelper {

private static final String OFFSET_SEPARATOR = ",";
private static final String SYMBOLS_SEPARATOR = ";";

private RangeHelper() {
// Only static methods
}

public static void appendRange(StringBuilder element, BatchReport.Range range, int lineIndex, int lineLength) {
validateOffsetOrder(range, lineIndex);

if (element.length() > 0) {
element.append(SYMBOLS_SEPARATOR);
}

if (range.getStartLine() == lineIndex) {
validateStartOffsetNotGreaterThanLineLength(range, lineLength, lineIndex);
element.append(range.getStartOffset()).append(OFFSET_SEPARATOR);
} else if (range.getStartLine() < lineIndex) {
element.append(0).append(OFFSET_SEPARATOR);
}

if (range.getEndLine() == lineIndex) {
validateEndOffsetNotGreaterThanLineLength(range, lineLength, lineIndex);
element.append(range.getEndOffset()).append(OFFSET_SEPARATOR);
} else if (range.getEndLine() > lineIndex) {
element.append(lineLength - 1).append(OFFSET_SEPARATOR);
}
}

private static void validateOffsetOrder(BatchReport.Range range, int line) {
if (range.getStartLine() == range.getEndLine() && range.getStartOffset() > range.getEndOffset()) {
throw new IllegalArgumentException(String.format("End offset %s cannot be defined before start offset %s on line %s", range.getEndOffset(), range.getStartOffset(), line));
}
}

private static void validateStartOffsetNotGreaterThanLineLength(BatchReport.Range range, int lineLength, int line) {
if (range.getStartOffset() > lineLength) {
throw new IllegalArgumentException(String.format("Start offset %s is defined outside the length (%s) of the line %s", range.getStartOffset(), lineLength, line));
}
}

private static void validateEndOffsetNotGreaterThanLineLength(BatchReport.Range range, int lineLength, int line) {
if (range.getEndOffset() > lineLength) {
throw new IllegalArgumentException(String.format("End offset %s is defined outside the length (%s) of the line %s", range.getEndOffset(), lineLength, line));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.computation.source;

import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.server.source.db.FileSourceDb;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;

public class SymbolsLineReader implements LineReader {

private final List<BatchReport.Symbols.Symbol> symbols;
private final Map<BatchReport.Symbols.Symbol, Integer> idsBySymbol;

public SymbolsLineReader(List<BatchReport.Symbols.Symbol> symbols) {
this.symbols = symbols;
// Sort symbols to have deterministic results and avoid false variation that would lead to an unnecessary update of the source files
// data
Collections.sort(this.symbols, new SymbolsDuplication());

this.idsBySymbol = createIdsBySymbolMap(symbols);
}

@Override
public void read(FileSourceDb.Line.Builder lineBuilder) {
int line = lineBuilder.getLine();
List<BatchReport.Symbols.Symbol> lineSymbols = findSymbolsMatchingLine(line);
for (BatchReport.Symbols.Symbol lineSymbol : lineSymbols) {
int symbolId = idsBySymbol.get(lineSymbol);
StringBuilder symbolString = new StringBuilder(lineBuilder.getSymbols());

appendSymbol(symbolString, lineSymbol.getDeclaration(), line, symbolId, lineBuilder.getSource());
for (BatchReport.Range range : lineSymbol.getReferenceList()) {
appendSymbol(symbolString, range, line, symbolId, lineBuilder.getSource());
}

if (symbolString.length() > 0) {
lineBuilder.setSymbols(symbolString.toString());
}
}
}

private void appendSymbol(StringBuilder lineSymbol, BatchReport.Range range, int line, int symbolId, String sourceLine) {
if (matchLine(range, line)) {
RangeHelper.appendRange(lineSymbol, range, line, sourceLine.length());
lineSymbol.append(symbolId);
}
}

private List<BatchReport.Symbols.Symbol> findSymbolsMatchingLine(int line) {
List<BatchReport.Symbols.Symbol> lineSymbols = newArrayList();
for (BatchReport.Symbols.Symbol symbol : symbols) {
if (matchLine(symbol.getDeclaration(), line)) {
lineSymbols.add(symbol);
} else {
for (BatchReport.Range range : symbol.getReferenceList()) {
if (matchLine(range, line)) {
lineSymbols.add(symbol);
}
}
}
}
return lineSymbols;
}

private static boolean matchLine(BatchReport.Range range, int line) {
return range.getStartLine() <= line && range.getEndLine() >= line;
}

private Map<BatchReport.Symbols.Symbol, Integer> createIdsBySymbolMap(List<BatchReport.Symbols.Symbol> symbols) {
Map<BatchReport.Symbols.Symbol, Integer> map = newHashMap();
int symbolId = 1;
for (BatchReport.Symbols.Symbol symbol : symbols) {
map.put(symbol, symbolId);
symbolId++;
}
return map;
}

private static class SymbolsDuplication implements Comparator<BatchReport.Symbols.Symbol> {
@Override
public int compare(BatchReport.Symbols.Symbol o1, BatchReport.Symbols.Symbol o2) {
if (o1.getDeclaration().getStartLine() == o2.getDeclaration().getStartLine()) {
return Integer.compare(o1.getDeclaration().getStartOffset(), o2.getDeclaration().getStartOffset());
} else {
return Integer.compare(o1.getDeclaration().getStartLine(), o2.getDeclaration().getStartLine());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ private static class LineReaders {
File coverageFile = reportReader.readComponentCoverage(componentRef);
BatchReport.Scm scmReport = reportReader.readComponentScm(componentRef);
File highlightingFile = reportReader.readComponentSyntaxHighlighting(componentRef);
List<BatchReport.Symbols.Symbol> symbols = reportReader.readComponentSymbols(componentRef);

if (coverageFile != null) {
ReportIterator<BatchReport.Coverage> coverageReportIterator = new ReportIterator<>(coverageFile, BatchReport.Coverage.PARSER);
Expand All @@ -190,13 +191,16 @@ private static class LineReaders {
reportIterators.add(syntaxHighlightingReportIterator);
lineReaders.add(new HighlightingLineReader(syntaxHighlightingReportIterator));
}
if (!symbols.isEmpty()) {
lineReaders.add(new SymbolsLineReader(newArrayList(symbols)));
}
}

List<LineReader> readers(){
List<LineReader> readers() {
return lineReaders;
}

void close(){
void close() {
for (ReportIterator reportIterator : reportIterators) {
reportIterator.close();
}
Expand Down
Loading

0 comments on commit dcc2e1d

Please sign in to comment.