Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,12 @@ extension TextView: NSTextInputClient {
return resolved
}

/// Returns an attributed string representing the receiver's text storage.
/// - Returns: The attributed string of the receiver’s text storage.
/// Returns the receiver's text storage itself, as `NSTextView` does.
///
/// A substring of the whole document copies every character and attribute run, about 41 ms and 5.7 MB at 1.9M
/// characters, for a caller that reads only the part it needs.
@objc public func attributedString() -> NSAttributedString {
textStorage.attributedSubstring(from: documentRange)
textStorage
}

// MARK: - Positions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import AppKit
import Darwin
@testable import TableProTextEngine
import Testing

/// The editor reads its whole text on every keystroke and caret move: the run controls, the statement band, the binding
/// writeback and the fold pass. Those reads are cheap only because bridging the storage to a `String` shares the
/// storage's buffer and copies a block only when the next edit touches it. The fold pass and the binding also rely on
/// the bridged value staying as it was while the editor goes on editing.
@MainActor
struct TextStorageReadTests {
private static let documentUnits = 4_000_000

@Test()
func readingTheStringDoesNotCopyTheDocument() {
let textView = TextView(string: Self.largeDocument())
textView.replaceCharacters(in: NSRange(location: 0, length: 0), with: "-- edited\n")

let before = Self.bytesInUse()
let snapshot = textView.string
textView.replaceCharacters(in: NSRange(location: textView.textStorage.length, length: 0), with: ";")
let grown = Self.bytesInUse() - before

// A copy of the document is 8 MB of UTF-16; what sharing costs is one block of it.
#expect(grown < 2_000_000)
#expect((snapshot as NSString).length == textView.textStorage.length - 1)
}

@Test()
func theStringIsASnapshotThatLaterEditsLeaveAlone() {
let textView = TextView(string: "SELECT 1;")
let snapshot = textView.string

textView.replaceCharacters(in: NSRange(location: 0, length: 6), with: "DELETE")

#expect(snapshot == "SELECT 1;")
#expect(textView.string == "DELETE 1;")
}

@Test()
func theInputSystemReadsTheStorageItself() {
let textView = TextView(string: Self.largeDocument())

#expect(textView.attributedString() === textView.textStorage)
}

private static func largeDocument() -> String {
let line = "INSERT INTO t (a, b) VALUES (1, 'héllo'), (2, 'x');\n"
return String(repeating: line, count: documentUnits / (line as NSString).length)
}

private static func bytesInUse() -> Int {
var statistics = malloc_statistics_t()
malloc_zone_statistics(nil, &statistics)
return statistics.size_in_use
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ final class InlineSuggestionManager {
guard let cursor = controller.cursorPositions.first,
cursor.range.length == 0 else { return false }

let text = textView.string
guard (text as NSString).length > 0 else { return false }

return true
return textView.textStorage.length > 0
}

// MARK: - Request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ final class QueryDiagnosticsController {
private func apply(_ produced: [QueryDiagnostic], in controller: TextViewController) {
guard let manager = controller.textView.emphasisManager else { return }

let length = (controller.textView.string as NSString).length
let length = controller.textView.textStorage.length
let emphases = produced.compactMap { diagnostic -> Emphasis? in
guard diagnostic.range.location >= 0,
NSMaxRange(diagnostic.range) <= length else { return nil }
Expand Down
2 changes: 1 addition & 1 deletion apple/TablePro/Views/Editor/SQLEditorController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ final class SQLEditorController {
// The caret arrives before its text, so it waits; one from a document the binding does not hold is dropped.
private func passPendingCaretOn() {
guard let positions = pendingCaret else { return }
let editorLength = (textViewController.textView.string as NSString).length
let editorLength = textViewController.textView.textStorage.length
guard editorLength == (input.text.wrappedValue as NSString).length else { return }
pendingCaret = nil
input.cursorPositions.wrappedValue = positions
Expand Down
7 changes: 3 additions & 4 deletions apple/TablePro/Views/Editor/StatementRunController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ final class StatementRunController {
pendingRefresh?.cancel()
guard let controller else { return }

if (controller.textView?.string as NSString?)?.length ?? 0 <= Self.synchronousRefreshLimit {
if controller.textView?.textStorage.length ?? 0 <= Self.synchronousRefreshLimit {
refreshControls(in: controller)
return
}
Expand Down Expand Up @@ -361,13 +361,12 @@ final class StatementRunController {
guard isHighlightEnabled,
let textView = controller.textView,
let selection = controller.cursorPositions.first,
selection.range.length == 0 else {
selection.range.length == 0,
textView.textStorage.length <= sizeLimit else {
return nil
}

let text = textView.string
guard (text as NSString).length <= sizeLimit else { return nil }

let located = statement(at: selection.range.location, in: text, storage: textView.textStorage)
guard located.hasContent, located.contentRange.length > 0 else { return nil }
return located.contentRange
Expand Down
Loading