fix(editor): normalize the guest watch's disk read
The external guest's watch read disk raw while the session's text and lastFlushedText came through readDocument's normalizing external branch, so diskTextChanged saw a difference on every BOM/CRLF file and raised conflictAlert on unchanged content — re-arming after each sibling directory event and after either alert answer, blocking departures and quit until the user's own flush rewrote the file normalized (finding museion/external-watch-compares-unnormalized-text, argus cycle-6; unticketed, brief-32). Extract the external branch (BOM strip + CRLF->LF) into EditorSession.readExternalDocument(at:) — one shared normalizer, two callers: the open read (readDocument) and the watch's onEvent. The #67 compare is now normalized-to-normalized; what readDocument loads is unchanged.
This commit is contained in:
1 parent
b69ddd3963
commit
70dd47ed8c
2 files changed
+19
-5
No files matched your search
@@ -157,15 +157,26 @@ final class EditorSession {
|
|||||||
private static func readDocument(_ target: SessionTarget) -> String? {
|
private static func readDocument(_ target: SessionTarget) -> String? {
|
||||||
switch target {
|
switch target {
|
||||||
case let .post(id, home):
|
case let .post(id, home):
|
||||||
return PostStore.readBody(id: id, home: home)
|
PostStore.readBody(id: id, home: home)
|
||||||
case let .externalFile(url):
|
case let .externalFile(url):
|
||||||
|
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 }
|
guard var text = try? String(contentsOf: url, encoding: .utf8) else { return nil }
|
||||||
if text.hasPrefix("\u{FEFF}") {
|
if text.hasPrefix("\u{FEFF}") {
|
||||||
text.removeFirst()
|
text.removeFirst()
|
||||||
}
|
}
|
||||||
return text.replacingOccurrences(of: "\r\n", with: "\n")
|
return text.replacingOccurrences(of: "\r\n", with: "\n")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Target-aware write: posts → `PostStore.overwrite` (+ sidecar);
|
/// Target-aware write: posts → `PostStore.overwrite` (+ sidecar);
|
||||||
/// external → the whole text, in place, no sidecar (#93).
|
/// external → the whole text, in place, no sidecar (#93).
|
||||||
|
|||||||
@@ -62,11 +62,14 @@ extension PostsModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// #93: watch the open external file — its rescan is the #67 disk-change
|
/// #93: watch the open external file — its rescan is the #67 disk-change
|
||||||
/// check, reused verbatim for foreign files. PostsWatcher fires on the
|
/// check, reused verbatim for foreign files. The disk read goes through
|
||||||
/// main actor already; no re-hop needed.
|
/// 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) {
|
private func startExternalWatch(url: URL) {
|
||||||
externalWatcher.onEvent = { [weak self] in
|
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)
|
session.diskDidChange(diskText: disk)
|
||||||
}
|
}
|
||||||
externalWatcher.start(urls: [url.deletingLastPathComponent()])
|
externalWatcher.start(urls: [url.deletingLastPathComponent()])
|
||||||
|
|||||||
Reference in new issue
Block a user