Search in sources :

Example 1 with TextPointer

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);
    }
}
Also used : Matcher(java.util.regex.Matcher) TextPointer(org.sonar.api.batch.fs.TextPointer) IOException(java.io.IOException)

Example 2 with TextPointer

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();
    }
}
Also used : NewIssueLocation(org.sonar.api.batch.sensor.issue.NewIssueLocation) RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) TextPointer(org.sonar.api.batch.fs.TextPointer) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with TextPointer

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;
}
Also used : TextPointer(org.sonar.api.batch.fs.TextPointer) InputFile(org.sonar.api.batch.fs.InputFile) CheckForNull(javax.annotation.CheckForNull)

Example 4 with TextPointer

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);
    }
}
Also used : Matcher(java.util.regex.Matcher) TextPointer(org.sonar.api.batch.fs.TextPointer) IOException(java.io.IOException)

Example 5 with TextPointer

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;
}
Also used : TypeOfText(org.sonar.api.batch.sensor.highlighting.TypeOfText) TextPointer(org.sonar.api.batch.fs.TextPointer) ArrayList(java.util.ArrayList) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) TextRange(org.sonar.api.batch.fs.TextRange) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Aggregations

TextPointer (org.sonar.api.batch.fs.TextPointer)10 IOException (java.io.IOException)3 Matcher (java.util.regex.Matcher)3 TextRange (org.sonar.api.batch.fs.TextRange)3 ArrayList (java.util.ArrayList)2 TypeOfText (org.sonar.api.batch.sensor.highlighting.TypeOfText)2 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CheckForNull (javax.annotation.CheckForNull)1 InputFile (org.sonar.api.batch.fs.InputFile)1 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)1 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)1 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)1 NewIssueLocation (org.sonar.api.batch.sensor.issue.NewIssueLocation)1 RuleKey (org.sonar.api.rule.RuleKey)1