museion/external-watch-compares-unnormalized-text: guest watch reads through the ONE shared external normalizer — false #67 conflict gone #203
No files matched your search
@@ -157,16 +157,27 @@ final class EditorSession {
|
||||
private static func readDocument(_ target: SessionTarget) -> String? {
|
||||
switch target {
|
||||
case let .post(id, home):
|
||||
return PostStore.readBody(id: id, home: home)
|
||||
PostStore.readBody(id: id, home: home)
|
||||
case let .externalFile(url):
|
||||
guard var text = try? String(contentsOf: url, encoding: .utf8) else { return nil }
|
||||
if text.hasPrefix("\u{FEFF}") {
|
||||
text.removeFirst()
|
||||
}
|
||||
return text.replacingOccurrences(of: "\r\n", with: "\n")
|
||||
readExternalDocument(at: url)
|
||||
}
|
||||
}
|
||||
|
||||
/// #67/brief-32: the ONE external-target disk read — raw UTF-8, BOM
|
||||
/// stripped, CRLF normalized to LF. Every reader of an external file's
|
||||
/// bytes goes through here (the open read above, the guest watch in
|
||||
/// PostsModelImports, Yes-reload, keep-local), so the #67 compare is
|
||||
/// always normalized-to-normalized: the watch used to read raw and
|
||||
/// raised a false conflict on every directory event over an unchanged
|
||||
/// BOM/CRLF file.
|
||||
static func readExternalDocument(at url: URL) -> String? {
|
||||
guard var text = try? String(contentsOf: url, encoding: .utf8) else { return nil }
|
||||
if text.hasPrefix("\u{FEFF}") {
|
||||
text.removeFirst()
|
||||
}
|
||||
return text.replacingOccurrences(of: "\r\n", with: "\n")
|
||||
}
|
||||
|
||||
/// Target-aware write: posts → `PostStore.overwrite` (+ sidecar);
|
||||
/// external → the whole text, in place, no sidecar (#93).
|
||||
private func writeDocument() throws {
|
||||
|
||||
@@ -62,11 +62,14 @@ extension PostsModel {
|
||||
}
|
||||
|
||||
/// #93: watch the open external file — its rescan is the #67 disk-change
|
||||
/// check, reused verbatim for foreign files. PostsWatcher fires on the
|
||||
/// main actor already; no re-hop needed.
|
||||
/// check, reused verbatim for foreign files. The disk read goes through
|
||||
/// the shared external normalizer (brief-32): the session's text is
|
||||
/// normalized on open, so a raw read raised a false conflict on every
|
||||
/// directory event over an unchanged BOM/CRLF file. PostsWatcher fires
|
||||
/// on the main actor already; no re-hop needed.
|
||||
private func startExternalWatch(url: URL) {
|
||||
externalWatcher.onEvent = { [weak self] in
|
||||
guard let self, let session = externalSession, let disk = try? String(contentsOf: url, encoding: .utf8) else { return }
|
||||
guard let self, let session = externalSession, let disk = EditorSession.readExternalDocument(at: url) else { return }
|
||||
session.diskDidChange(diskText: disk)
|
||||
}
|
||||
externalWatcher.start(urls: [url.deletingLastPathComponent()])
|
||||
|
||||
Reference in new issue
Block a user