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
37 lines (33 loc) · 1.14 KB
/
TextLoader.swift
File metadata and controls
37 lines (33 loc) · 1.14 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
import AppKit
import Foundation
public struct TextLoader: DocumentLoader {
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: [
"filename": .string(url.lastPathComponent),
"extension": .string(url.pathExtension),
"contentModificationDate": .number(
(modificationDate ?? Date()).timeIntervalSince1970
),
])]
}
}