Search in sources :

Example 6 with CodeMatchResult

use of com.searchcode.app.dto.CodeMatchResult in project searchcode-server by boyter.

the class CodeMatcher method matchResults.

/**
     * Actually does the matching for a single code result given the match terms
     */
public List<CodeMatchResult> matchResults(List<String> code, List<String> matchTerms, boolean highlightLine) {
    List<CodeMatchResult> resultLines = findMatchingLines(code, matchTerms, highlightLine);
    List<CodeMatchResult> newResultLines = new ArrayList<>();
    // get the top matching lines for this result
    resultLines.sort((p1, p2) -> Integer.valueOf(p2.getLineMatches()).compareTo(p1.getLineMatches()));
    // gets the best snippets based on number of matches
    for (int i = 0; i < resultLines.size(); i++) {
        CodeMatchResult match = resultLines.get(i);
        match.setLineNumber(match.getLineNumber() + 1);
        if (!resultExists(newResultLines, match.getLineNumber())) {
            newResultLines.add(match);
        }
        CodeMatchResult resultBefore = getResultByLineNumber(resultLines, match.getLineNumber() - 1);
        CodeMatchResult resultAfter = getResultByLineNumber(resultLines, match.getLineNumber() + 1);
        if (resultBefore != null && !resultExists(newResultLines, match.getLineNumber() - 1)) {
            newResultLines.add(resultBefore);
        }
        if (resultAfter != null && !resultExists(newResultLines, match.getLineNumber() + 1)) {
            newResultLines.add(resultAfter);
        }
        if (newResultLines.size() >= MATCHLINES) {
            break;
        }
    }
    newResultLines.sort((p1, p2) -> Integer.valueOf(p1.getLineNumber()).compareTo(p2.getLineNumber()));
    if (!newResultLines.isEmpty()) {
        newResultLines.get(0).addBreak = false;
        return newResultLines;
    }
    return null;
}
Also used : CodeMatchResult(com.searchcode.app.dto.CodeMatchResult)

Aggregations

CodeMatchResult (com.searchcode.app.dto.CodeMatchResult)6 CodeResult (com.searchcode.app.dto.CodeResult)2 ArrayList (java.util.ArrayList)2 CodeRouteService (com.searchcode.app.service.route.CodeRouteService)1 Instant (java.time.Instant)1 Request (spark.Request)1 Response (spark.Response)1