115 lines
4.8 KiB
Swift
115 lines
4.8 KiB
Swift
import Foundation
|
|
|
|
/// One `"type":"match"` event from `rg --json`.
|
|
public struct RipgrepMatch: Sendable, Equatable {
|
|
/// The path as ripgrep printed it (absolute when the search root was absolute).
|
|
public var path: String
|
|
public var line: Int
|
|
/// The line text without its trailing newline.
|
|
public var text: String
|
|
/// Byte offset of the first submatch, as ripgrep reports it.
|
|
public var byteOffset: Int
|
|
|
|
public init(path: String, line: Int, text: String, byteOffset: Int) {
|
|
self.path = path
|
|
self.line = line
|
|
self.text = text
|
|
self.byteOffset = byteOffset
|
|
}
|
|
}
|
|
|
|
public enum RipgrepJSON {
|
|
/// Decodes one line of `rg --json` output; nil for every event that is not a match.
|
|
///
|
|
/// An event carrying `path.bytes` or `lines.bytes` (base64, non-UTF-8 data) is skipped.
|
|
/// Offsets stay in bytes here; `ContentGrouper` turns them into UTF-16.
|
|
public static func parseLine(_ line: String) -> RipgrepMatch? {
|
|
guard line.contains("\"match\""),
|
|
let event = try? JSONDecoder().decode(Event.self, from: Data(line.utf8)),
|
|
event.type == "match",
|
|
let data = event.data,
|
|
let path = data.path?.text,
|
|
var text = data.lines?.text
|
|
else { return nil }
|
|
if text.unicodeScalars.last == "\n" { text.unicodeScalars.removeLast() }
|
|
if text.unicodeScalars.last == "\r" { text.unicodeScalars.removeLast() }
|
|
return RipgrepMatch(path: path, line: data.line_number ?? 0, text: text, byteOffset: data.submatches?.first?.start ?? 0)
|
|
}
|
|
|
|
/// The slice of an `rg --json` event this reader needs; a `bytes` field decodes as a nil `text`.
|
|
private struct Event: Decodable {
|
|
struct TextField: Decodable {
|
|
var text: String?
|
|
}
|
|
struct Submatch: Decodable {
|
|
var start: Int
|
|
}
|
|
struct Payload: Decodable {
|
|
var path: TextField?
|
|
var lines: TextField?
|
|
var line_number: Int?
|
|
var submatches: [Submatch]?
|
|
}
|
|
var type: String
|
|
var data: Payload?
|
|
}
|
|
|
|
/// Makes a ripgrep path relative to the project root, "/"-separated.
|
|
public static func relativePath(_ path: String, root: String) -> String {
|
|
let base = root.hasSuffix("/") ? root : root + "/"
|
|
return path.hasPrefix(base) ? String(path.dropFirst(base.count)) : path
|
|
}
|
|
}
|
|
|
|
/// Collects matches into per-file groups in first-seen order, honouring the search limits.
|
|
public struct ContentGrouper: Sendable {
|
|
public let root: String
|
|
public private(set) var groups: [ContentGroup] = []
|
|
|
|
public init(root: String) {
|
|
self.root = root
|
|
}
|
|
|
|
/// Adds a match: new files beyond `SearchLimits.maxFiles` are dropped, the line text is
|
|
/// capped at `maxLineLength`, and the byte offset becomes a clamped UTF-16 `matchOffset`.
|
|
public mutating func add(_ match: RipgrepMatch) {
|
|
let path = RipgrepJSON.relativePath(match.path, root: root)
|
|
let index: Int
|
|
if let existing = indexByPath[path] {
|
|
index = existing
|
|
} else {
|
|
guard groups.count < SearchLimits.maxFiles else { return }
|
|
index = groups.count
|
|
indexByPath[path] = index
|
|
groups.append(ContentGroup(path: path, hits: []))
|
|
}
|
|
guard groups[index].hits.count < SearchLimits.maxHitsPerFile else { return }
|
|
let text = ContentGrouper.capped(match.text)
|
|
let offset = ContentGrouper.utf16Offset(ofByte: match.byteOffset, in: match.text)
|
|
groups[index].hits.append(ContentHit(line: match.line, text: text, matchOffset: min(offset, text.utf16.count)))
|
|
}
|
|
|
|
private var indexByPath: [String: Int] = [:]
|
|
|
|
/// The line cut to `SearchLimits.maxLineLength` UTF-16 units, never inside a surrogate pair.
|
|
static func capped(_ text: String) -> String {
|
|
let utf16 = text.utf16
|
|
guard utf16.count > SearchLimits.maxLineLength else { return text }
|
|
var end = utf16.index(utf16.startIndex, offsetBy: SearchLimits.maxLineLength)
|
|
while end > utf16.startIndex && UTF16.isTrailSurrogate(utf16[end]) { end = utf16.index(before: end) }
|
|
return String(utf16[..<end]) ?? String(text.prefix(SearchLimits.maxLineLength))
|
|
}
|
|
|
|
/// ripgrep counts UTF-8 bytes; the views count UTF-16 units.
|
|
static func utf16Offset(ofByte byteOffset: Int, in text: String) -> Int {
|
|
let utf8 = text.utf8
|
|
guard byteOffset > 0 else { return 0 }
|
|
guard byteOffset < utf8.count else { return text.utf16.count }
|
|
var index = utf8.index(utf8.startIndex, offsetBy: byteOffset)
|
|
while index > utf8.startIndex && UTF8.isContinuation(utf8[index]) {
|
|
index = utf8.index(before: index)
|
|
}
|
|
return text.utf16.distance(from: text.utf16.startIndex, to: index)
|
|
}
|
|
}
|