use of org.sonar.api.batch.fs.TextPointer in project sonarqube by SonarSource.
the class MultilineIssuesSensor method parseFlows.
private static void parseFlows(InputFile file, Map<Integer, Table<Integer, Integer, TextPointer>> startFlowsPositions, Map<Integer, Table<Integer, Integer, TextPointer>> endFlowsPositions) {
int currentLine = 0;
try {
for (String lineStr : Files.readAllLines(file.path(), file.charset())) {
currentLine++;
Matcher m = START_FLOW_PATTERN.matcher(lineStr);
while (m.find()) {
Integer issueId = Integer.parseInt(m.group(1));
Integer issueFlowId = Integer.parseInt(m.group(2));
Integer issueFlowNum = Integer.parseInt(m.group(3));
TextPointer newPointer = file.newPointer(currentLine, m.end());
if (!startFlowsPositions.containsKey(issueId)) {
startFlowsPositions.put(issueId, HashBasedTable.create());
}
startFlowsPositions.get(issueId).row(issueFlowId).put(issueFlowNum, newPointer);
}
m = END_FLOW_PATTERN.matcher(lineStr);
while (m.find()) {
Integer issueId = Integer.parseInt(m.group(1));
Integer issueFlowId = Integer.parseInt(m.group(2));
Integer issueFlowNum = Integer.parseInt(m.group(3));
TextPointer newPointer = file.newPointer(currentLine, m.start());
if (!endFlowsPositions.containsKey(issueId)) {
endFlowsPositions.put(issueId, HashBasedTable.create());
}
endFlowsPositions.get(issueId).row(issueFlowId).put(issueFlowNum, newPointer);
}
}
} catch (IOException e) {
throw new IllegalStateException("Unable to read file", e);
}
}
use of org.sonar.api.batch.fs.TextPointer in project sonarqube by SonarSource.
the class MultilineIssuesSensor method createIssues.
private static void createIssues(InputFile file, SensorContext context, Map<Integer, TextPointer> startPositions, Map<Integer, TextPointer> endPositions, Map<Integer, Table<Integer, Integer, TextPointer>> startFlowsPositions, Map<Integer, Table<Integer, Integer, TextPointer>> endFlowsPositions) {
RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY);
for (Map.Entry<Integer, TextPointer> entry : startPositions.entrySet()) {
NewIssue newIssue = context.newIssue().forRule(ruleKey);
Integer issueId = entry.getKey();
NewIssueLocation primaryLocation = newIssue.newLocation().on(file).at(file.newRange(entry.getValue(), endPositions.get(issueId)));
newIssue.at(primaryLocation.message("Primary location"));
if (startFlowsPositions.containsKey(issueId)) {
Table<Integer, Integer, TextPointer> flows = startFlowsPositions.get(issueId);
for (Map.Entry<Integer, Map<Integer, TextPointer>> flowEntry : flows.rowMap().entrySet()) {
Integer flowId = flowEntry.getKey();
List<NewIssueLocation> flowLocations = Lists.newArrayList();
List<Integer> flowNums = Lists.newArrayList(flowEntry.getValue().keySet());
Collections.sort(flowNums);
for (Integer flowNum : flowNums) {
TextPointer start = flowEntry.getValue().get(flowNum);
TextPointer end = endFlowsPositions.get(issueId).row(flowId).get(flowNum);
NewIssueLocation newLocation = newIssue.newLocation().on(file).at(file.newRange(start, end)).message("Flow step #" + flowNum);
flowLocations.add(newLocation);
}
if (flowLocations.size() == 1) {
newIssue.addLocation(flowLocations.get(0));
} else {
newIssue.addFlow(flowLocations);
}
}
}
newIssue.save();
}
}
use of org.sonar.api.batch.fs.TextPointer in project sonarqube by SonarSource.
the class ExternalIssueImporter method fillLocation.
@CheckForNull
private static NewIssueLocation fillLocation(SensorContext context, NewIssueLocation newLocation, Location location) {
InputFile file = findFile(context, location.filePath);
if (file == null) {
return null;
}
newLocation.on(file);
if (location.message != null) {
newLocation.message(location.message);
}
if (location.textRange != null) {
if (location.textRange.startColumn != null) {
TextPointer start = file.newPointer(location.textRange.startLine, location.textRange.startColumn);
int endLine = (location.textRange.endLine != null) ? location.textRange.endLine : location.textRange.startLine;
int endColumn;
if (location.textRange.endColumn == null) {
// assume it's until the last character of the end line
endColumn = file.selectLine(endLine).end().lineOffset();
} else {
endColumn = location.textRange.endColumn;
}
TextPointer end = file.newPointer(endLine, endColumn);
newLocation.at(file.newRange(start, end));
} else {
newLocation.at(file.selectLine(location.textRange.startLine));
}
}
return newLocation;
}
use of org.sonar.api.batch.fs.TextPointer in project sonarqube by SonarSource.
the class MultilineIssuesSensor method parseFlows.
private static void parseFlows(InputFile file, SensorContext context, Map<Integer, Table<Integer, Integer, TextPointer>> startFlowsPositions, Map<Integer, Table<Integer, Integer, TextPointer>> endFlowsPositions) {
int currentLine = 0;
try {
for (String lineStr : Files.readAllLines(file.path(), file.charset())) {
currentLine++;
Matcher m = START_FLOW_PATTERN.matcher(lineStr);
while (m.find()) {
Integer issueId = Integer.parseInt(m.group(1));
Integer issueFlowId = Integer.parseInt(m.group(2));
Integer issueFlowNum = Integer.parseInt(m.group(3));
TextPointer newPointer = file.newPointer(currentLine, m.end());
if (!startFlowsPositions.containsKey(issueId)) {
startFlowsPositions.put(issueId, HashBasedTable.<Integer, Integer, TextPointer>create());
}
startFlowsPositions.get(issueId).row(issueFlowId).put(issueFlowNum, newPointer);
}
m = END_FLOW_PATTERN.matcher(lineStr);
while (m.find()) {
Integer issueId = Integer.parseInt(m.group(1));
Integer issueFlowId = Integer.parseInt(m.group(2));
Integer issueFlowNum = Integer.parseInt(m.group(3));
TextPointer newPointer = file.newPointer(currentLine, m.start());
if (!endFlowsPositions.containsKey(issueId)) {
endFlowsPositions.put(issueId, HashBasedTable.<Integer, Integer, TextPointer>create());
}
endFlowsPositions.get(issueId).row(issueFlowId).put(issueFlowNum, newPointer);
}
}
} catch (IOException e) {
throw new IllegalStateException("Unable to read file", e);
}
}
use of org.sonar.api.batch.fs.TextPointer 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