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
44 lines (39 loc) · 1.4 KB
/
TextLoader.swift
File metadata and controls
44 lines (39 loc) · 1.4 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
import AppKit
import Foundation
/// Load a text document from local file.
public struct TextLoader: DocumentLoader {
enum MetadataKeys {
static let filename = "filename"
static let `extension` = "extension"
static let contentModificationDate = "contentModificationDate"
}
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
),
])]
}
}