Sources/OrgApp/FileLint.swift
43 lines · 2287 bytes
1import Foundation
2import OrgCore
3import OrgIndex
4
5/// Lint File in the apps: the checkers run over an open file's text off the main actor, with
6/// the file system and the workspace index for IDs in other files.
7public enum FileLint {
8 /// The checkers' environment: the file system, IDs through `index` (unsaved buffers'
9 /// from `overlay`), and the attachment and crypt settings.
10 public static func environment(index: IndexStore?, overlay: [String: FileRecord] = [:], defaults: UserDefaults = .standard) -> LintEnvironment {
11 var environment = LintEnvironment.fileSystem
12 environment.attachIDDir = OrgPreferences.attachIDDir(defaults)
13 environment.attachInherit = OrgPreferences.attachUseInheritance(defaults)
14 environment.cryptTag = OrgPreferences.cryptTag(defaults)
15 if let index {
16 environment.idFiles = { id in ((try? index.headings(withID: id, overlay: overlay)) ?? []).map(\.path) }
17 }
18 return environment
19 }
20
21 /// Whether Lint File applies to the file at `path`: Org files, and encrypted ones once
22 /// decrypted in memory.
23 public static func applies(to path: String?) -> Bool {
24 guard let path else { return false }
25 return path.hasSuffix(".org") || path.hasSuffix(".org_archive") || path.hasSuffix(".org.gpg")
26 }
27
28 /// The findings in `text`, off the main actor.
29 @concurrent
30 public static func lint(_ text: String, tree: OrgTree, path: String?, environment: LintEnvironment) async -> [LintFinding] {
31 OrgLint.lint(text, tree: tree, path: path, environment: environment)
32 }
33
34 /// A finding's trust as the panels show it: org-lint's high trust as an error, low as a warning.
35 public static func severity(_ finding: LintFinding) -> String {
36 finding.trust == .high ? String(localized: "Error", comment: "Lint File: a problem org-lint trusts highly") : String(localized: "Warning", comment: "Lint File: a problem that may be intended")
37 }
38
39 /// The panels' summary line.
40 public static func summary(_ findings: [LintFinding]) -> String {
41 findings.isEmpty ? String(localized: "No problems found", comment: "Lint File") : String(localized: "\(findings.count) problems", comment: "Lint File: the number of problems found")
42 }
43}