restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
|
||||
public protocol SearchServicing: Sendable {
|
||||
/// False when no ripgrep binary was found; callers then fall back to a walk.
|
||||
func isAvailable() async -> Bool
|
||||
/// Fixed-string, smart-case content search; [] below `SearchLimits.minContentQuery` characters.
|
||||
func searchContent(_ query: String) async -> [ContentGroup]
|
||||
/// Relative paths of every project file, dotfiles included, honouring gitignore and excludes.
|
||||
/// `directory` limits the list to one subtree; paths stay relative to the root.
|
||||
func listFiles(under directory: String?) async -> [String]
|
||||
}
|
||||
|
||||
/// Answers through ripgrep when it can be found, else through a walk with the same filters.
|
||||
public struct RipgrepSearchService: SearchServicing {
|
||||
public let root: URL
|
||||
private let config: ConfigServicing
|
||||
private let runner: ProcessRunning
|
||||
private let locator: ExecutableLocator
|
||||
private let environment: [String: String]
|
||||
|
||||
/// `environment` is read for `RG_BINARY`, which points at a ripgrep outside the search path.
|
||||
public init(root: URL, config: ConfigServicing, runner: ProcessRunning = ProcessRunner(), locator: ExecutableLocator = .standard,
|
||||
environment: [String: String] = ProcessInfo.processInfo.environment) {
|
||||
self.root = root
|
||||
self.config = config
|
||||
self.runner = runner
|
||||
self.locator = locator
|
||||
self.environment = environment
|
||||
}
|
||||
|
||||
public func isAvailable() async -> Bool {
|
||||
ProjectScanner.ripgrep(locator: locator, environment: environment) != nil
|
||||
}
|
||||
|
||||
public func searchContent(_ query: String) async -> [ContentGroup] {
|
||||
guard query.trimmingCharacters(in: .whitespacesAndNewlines).count >= SearchLimits.minContentQuery else { return [] }
|
||||
let search = LineQuery(pattern: .smartCaseLiteral(query), maxPerFile: SearchLimits.maxHitsPerFile,
|
||||
maxColumns: 2000, maxFiles: SearchLimits.maxFiles)
|
||||
var grouper = ContentGrouper(root: root.path)
|
||||
for match in await scanner.matches(search, filter: await filter()) {
|
||||
grouper.add(match)
|
||||
}
|
||||
return grouper.groups
|
||||
}
|
||||
|
||||
public func listFiles(under directory: String?) async -> [String] {
|
||||
if let directory, !directory.isEmpty, !isInsideRoot(directory) { return [] }
|
||||
return await scanner.files(under: directory, filter: await filter())
|
||||
}
|
||||
|
||||
private var scanner: ProjectScanning {
|
||||
ProjectScanner.make(root: root, runner: runner, locator: locator, environment: environment)
|
||||
}
|
||||
|
||||
private func filter() async -> ScanFilter {
|
||||
ScanFilter(base: IgnoreRules.ripgrepBaseIgnore, files: await config.current().files)
|
||||
}
|
||||
|
||||
private func isInsideRoot(_ path: String) -> Bool {
|
||||
let base = root.standardizedFileURL.path
|
||||
let target = root.appendingPathComponent(path).standardizedFileURL.path
|
||||
return target.hasPrefix(base + "/")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user