-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONAR-6258 Persist symbols into file sources
- Loading branch information
1 parent
9dda9e9
commit dcc2e1d
Showing
10 changed files
with
662 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
server/sonar-server/src/main/java/org/sonar/server/computation/source/RangeHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
|
||
} |
114 changes: 114 additions & 0 deletions
114
server/sonar-server/src/main/java/org/sonar/server/computation/source/SymbolsLineReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.