-
-
Notifications
You must be signed in to change notification settings - Fork 296
fix(editor): sync external text binding changes into the source editor #1592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+198
−18
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...s/CodeEditSourceEditor/Tests/CodeEditSourceEditorTests/SourceEditorBindingSyncTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import AppKit | ||
| import SwiftUI | ||
| import XCTest | ||
|
|
||
| @testable import CodeEditSourceEditor | ||
| import CodeEditTextView | ||
|
|
||
| final class SourceEditorBindingSyncTests: XCTestCase { | ||
| var controller: TextViewController! | ||
|
|
||
| override func setUpWithError() throws { | ||
| controller = Mock.textViewController(theme: Mock.theme()) | ||
| controller.loadView() | ||
| controller.view.frame = NSRect(x: 0, y: 0, width: 1_000, height: 1_000) | ||
| controller.view.layoutSubtreeIfNeeded() | ||
| } | ||
|
|
||
| @MainActor | ||
| private func makeCoordinator( | ||
| get: @escaping () -> String, | ||
| set: @escaping (String) -> Void | ||
| ) -> SourceEditor.Coordinator { | ||
| SourceEditor.Coordinator( | ||
| text: .binding(Binding(get: get, set: { set($0) })), | ||
| editorState: .constant(SourceEditorState()), | ||
| highlightProviders: nil | ||
| ) | ||
| } | ||
|
|
||
| @MainActor | ||
| func test_externalBindingChangeUpdatesTextView() { | ||
| var bound = "initial" | ||
| let coordinator = makeCoordinator(get: { bound }, set: { bound = $0 }) | ||
| controller.textView.setText("initial") | ||
|
|
||
| bound = "updated" | ||
| coordinator.syncBindingText(bound, controller: controller) | ||
|
|
||
| XCTAssertEqual(controller.textView.string, "updated") | ||
| XCTAssertEqual(coordinator.lastSyncedText, "updated") | ||
| } | ||
|
|
||
| @MainActor | ||
| func test_syncSkipsWhenBindingMatchesLastSyncedText() { | ||
| var bound = "before edit" | ||
| let coordinator = makeCoordinator(get: { bound }, set: { bound = $0 }) | ||
| controller.textView.setText("in-flight user edit") | ||
|
|
||
| coordinator.syncBindingText(bound, controller: controller) | ||
|
|
||
| XCTAssertEqual(controller.textView.string, "in-flight user edit") | ||
| XCTAssertEqual(coordinator.lastSyncedText, "before edit") | ||
| } | ||
|
|
||
| @MainActor | ||
| func test_textViewEditWritesBackAndRecordsLastSyncedText() { | ||
| var bound = "" | ||
| let coordinator = makeCoordinator(get: { bound }, set: { bound = $0 }) | ||
| controller.textView.setText("typed text") | ||
|
|
||
| coordinator.textViewDidChangeText( | ||
| Notification(name: TextView.textDidChangeNotification, object: controller.textView) | ||
| ) | ||
|
|
||
| XCTAssertEqual(bound, "typed text") | ||
| XCTAssertEqual(coordinator.lastSyncedText, "typed text") | ||
| XCTAssertTrue(coordinator.isUpdateFromTextView) | ||
| } | ||
|
|
||
| @MainActor | ||
| func test_editNotificationDuringProgrammaticSyncDoesNotWriteBack() { | ||
| var bound = "external" | ||
| var setterCallCount = 0 | ||
| let coordinator = makeCoordinator(get: { bound }, set: { bound = $0; setterCallCount += 1 }) | ||
| controller.textView.setText("stale") | ||
| coordinator.isUpdatingFromRepresentable = true | ||
|
|
||
| coordinator.textViewDidChangeText( | ||
| Notification(name: TextView.textDidChangeNotification, object: controller.textView) | ||
| ) | ||
|
|
||
| XCTAssertEqual(setterCallCount, 0) | ||
| XCTAssertFalse(coordinator.isUpdateFromTextView) | ||
| XCTAssertEqual(bound, "external") | ||
| } | ||
|
|
||
| @MainActor | ||
| func test_largeDocumentEditDebouncesWritebackWithoutClobbering() async throws { | ||
| var bound = "" | ||
| let coordinator = makeCoordinator(get: { bound }, set: { bound = $0 }) | ||
| let largeText = String(repeating: "a", count: 500_001) | ||
| controller.textView.setText(largeText) | ||
|
|
||
| coordinator.textViewDidChangeText( | ||
| Notification(name: TextView.textDidChangeNotification, object: controller.textView) | ||
| ) | ||
|
|
||
| XCTAssertEqual(bound, "") | ||
| XCTAssertTrue(coordinator.isUpdateFromTextView) | ||
|
|
||
| coordinator.syncBindingText(bound, controller: controller) | ||
| XCTAssertEqual(controller.textView.string, largeText) | ||
|
|
||
| try await Task.sleep(for: .milliseconds(300)) | ||
| XCTAssertEqual(bound, largeText) | ||
| XCTAssertEqual(coordinator.lastSyncedText, largeText) | ||
| } | ||
|
|
||
| @MainActor | ||
| func test_syncShorterTextDropsOutOfBoundsSelection() { | ||
| var bound = "select * from table" | ||
| let coordinator = makeCoordinator(get: { bound }, set: { bound = $0 }) | ||
| controller.textView.setText("select * from table") | ||
| controller.textView.selectionManager.setSelectedRange(NSRange(location: 19, length: 0)) | ||
|
|
||
| bound = "ab" | ||
| coordinator.syncBindingText(bound, controller: controller) | ||
|
|
||
| XCTAssertEqual(controller.textView.string, "ab") | ||
| for selection in controller.textView.selectionManager.textSelections { | ||
| XCTAssertLessThanOrEqual(selection.range.max, 2) | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
| func test_repeatedSyncWithSameValueLeavesTextViewUntouched() { | ||
| var bound = "stable" | ||
| let coordinator = makeCoordinator(get: { bound }, set: { bound = $0 }) | ||
| controller.textView.setText("stable") | ||
| let storageBefore = controller.textView.textStorage | ||
|
|
||
| coordinator.syncBindingText(bound, controller: controller) | ||
|
|
||
| XCTAssertIdentical(controller.textView.textStorage, storageBefore) | ||
| XCTAssertEqual(controller.textView.string, "stable") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a loaded editor receives an external binding change, this swaps the
TextViewstorage directly, but it bypassesTextViewController.setText(_:), which is the path that callssetUpHighlighter()after replacing the text.TextView.setTextStoragereuses the existing storage delegate without emitting a full-character edit, so the current highlighter/style container remains sized and initialized for the old document; externally loaded text can render with stale or missing syntax highlighting until some later highlighter reset/edit happens.Useful? React with 👍 / 👎.