forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextLoader.swift
More file actions
46 lines (41 loc) · 1.53 KB
/
TextLoader.swift
File metadata and controls
46 lines (41 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import AppKit
import Foundation
/// Load a text document from local file.
public struct TextLoader: DocumentLoader {
public enum MetadataKeys {
public static let filename = "filename"
public static let `extension` = "extension"
public static let contentModificationDate = "contentModificationDate"
public static let filePath = "filePath"
}
let url: URL
let encoding: String.Encoding
let options: [NSAttributedString.DocumentReadingOptionKey: Any]
public init(
url: URL,
encoding: String.Encoding = .utf8,
options: [NSAttributedString.DocumentReadingOptionKey: Any] = [:]
) {
self.url = url
self.encoding = encoding
self.options = options
}
public func load() async throws -> [Document] {
let data = try Data(contentsOf: url)
let attributedString = try NSAttributedString(
data: data,
options: options,
documentAttributes: nil
)
let modificationDate = try? url.resourceValues(forKeys: [.contentModificationDateKey])
.contentModificationDate
return [Document(pageContent: attributedString.string, metadata: [
MetadataKeys.filename: .string(url.lastPathComponent),
MetadataKeys.extension: .string(url.pathExtension),
MetadataKeys.contentModificationDate: .number(
(modificationDate ?? Date()).timeIntervalSince1970
),
MetadataKeys.filePath: .string(url.path),
])]
}
}