use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.
the class IssuePublisher method createReportExternalIssue.
private static ScannerReport.ExternalIssue createReportExternalIssue(ExternalIssue issue, int componentRef) {
// primary location of an external issue must have a message
String primaryMessage = issue.primaryLocation().message();
Severity severity = Severity.valueOf(issue.severity().name());
IssueType issueType = IssueType.valueOf(issue.type().name());
ScannerReport.ExternalIssue.Builder builder = ScannerReport.ExternalIssue.newBuilder();
ScannerReport.IssueLocation.Builder locationBuilder = IssueLocation.newBuilder();
ScannerReport.TextRange.Builder textRangeBuilder = ScannerReport.TextRange.newBuilder();
// non-null fields
builder.setSeverity(severity);
builder.setType(issueType);
builder.setEngineId(issue.engineId());
builder.setRuleId(issue.ruleId());
builder.setMsg(primaryMessage);
locationBuilder.setMsg(primaryMessage);
locationBuilder.setComponentRef(componentRef);
TextRange primaryTextRange = issue.primaryLocation().textRange();
if (primaryTextRange != null) {
builder.setTextRange(toProtobufTextRange(textRangeBuilder, primaryTextRange));
}
Long effort = issue.remediationEffort();
if (effort != null) {
builder.setEffort(effort);
}
applyFlows(builder::addFlow, locationBuilder, textRangeBuilder, issue.flows());
return builder.build();
}
use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.
the class DefaultSensorStorage method store.
@Override
public void store(NewSymbolTable newSymbolTable) {
DefaultSymbolTable symbolTable = (DefaultSymbolTable) newSymbolTable;
ScannerReportWriter writer = reportPublisher.getWriter();
DefaultInputFile inputFile = (DefaultInputFile) symbolTable.inputFile();
if (shouldSkipStorage(inputFile)) {
return;
}
inputFile.setPublished(true);
int componentRef = inputFile.scannerId();
if (writer.hasComponentData(FileStructure.Domain.SYMBOLS, componentRef)) {
throw new UnsupportedOperationException("Trying to save symbol table twice for the same file is not supported: " + symbolTable.inputFile());
}
final ScannerReport.Symbol.Builder builder = ScannerReport.Symbol.newBuilder();
final ScannerReport.TextRange.Builder rangeBuilder = ScannerReport.TextRange.newBuilder();
writer.writeComponentSymbols(componentRef, symbolTable.getReferencesBySymbol().entrySet().stream().map(input -> {
builder.clear();
rangeBuilder.clear();
TextRange declaration = input.getKey();
builder.setDeclaration(rangeBuilder.setStartLine(declaration.start().line()).setStartOffset(declaration.start().lineOffset()).setEndLine(declaration.end().line()).setEndOffset(declaration.end().lineOffset()).build());
for (TextRange reference : input.getValue()) {
builder.addReference(rangeBuilder.setStartLine(reference.start().line()).setStartOffset(reference.start().lineOffset()).setEndLine(reference.end().line()).setEndOffset(reference.end().lineOffset()).build());
}
return builder.build();
}).collect(Collectors.toList()));
}
use of org.sonar.api.batch.fs.TextRange in project sonarlint-core by SonarSource.
the class DefaultFilterableIssueTests method delegate_textRange_to_rawIssue.
@Test
void delegate_textRange_to_rawIssue() {
TextRange textRange = new DefaultTextRange(new DefaultTextPointer(0, 1), new DefaultTextPointer(2, 3));
var activeRule = mock(ActiveRuleAdapter.class);
when(activeRule.ruleKey()).thenReturn(RuleKey.of("foo", "S123"));
var rawIssue = new Issue(activeRule, null, textRange, null, null, null);
FilterableIssue underTest = new DefaultFilterableIssue(rawIssue, mock(InputComponent.class));
assertThat(underTest.textRange()).usingRecursiveComparison().isEqualTo(textRange);
}
use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.
the class DefaultInputFileTest method checkValidRangeUsingGlobalOffset.
@Test
public void checkValidRangeUsingGlobalOffset() {
Metadata metadata = new Metadata(2, 2, "", new int[] { 0, 10 }, 15);
DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php"), f -> f.setMetadata(metadata));
TextRange newRange = file.newRange(10, 13);
assertThat(newRange.start().line()).isEqualTo(2);
assertThat(newRange.start().lineOffset()).isEqualTo(0);
assertThat(newRange.end().line()).isEqualTo(2);
assertThat(newRange.end().lineOffset()).isEqualTo(3);
}
use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.
the class TaskResult method highlightingTypeFor.
/**
* Get highlighting types at a given position in an inputfile
* @param lineOffset 0-based offset in file
*/
public List<TypeOfText> highlightingTypeFor(InputFile file, int line, int lineOffset) {
int ref = reportComponents.get(((DefaultInputFile) file).key()).getRef();
if (!reader.hasSyntaxHighlighting(ref)) {
return Collections.emptyList();
}
TextPointer pointer = file.newPointer(line, lineOffset);
List<TypeOfText> result = new ArrayList<>();
try (CloseableIterator<ScannerReport.SyntaxHighlightingRule> it = reader.readComponentSyntaxHighlighting(ref)) {
while (it.hasNext()) {
ScannerReport.SyntaxHighlightingRule rule = it.next();
TextRange ruleRange = toRange(file, rule.getRange());
if (ruleRange.start().compareTo(pointer) <= 0 && ruleRange.end().compareTo(pointer) > 0) {
result.add(ScannerReportUtils.toBatchType(rule.getType()));
}
}
} catch (Exception e) {
throw new IllegalStateException("Can't read syntax highlighting for " + file.absolutePath(), e);
}
return result;
}
Aggregations