Skip to content
Closed
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 @@ -977,6 +977,23 @@ extension EditorTextView {
private static let additionalWordSeparators = CharacterSet(charactersIn: ".,:")


/// Returns whether the character at the given location is an additional word separator, such as `.` and `:`.
///
/// - Parameter location: The UTF-16 based character index.
/// - Returns: `true` if the character is an additional word separator; otherwise, `false`.
func isAdditionalWordSeparator(at location: Int) -> Bool {

let string = self.string as NSString

guard
(0..<string.length).contains(location),
let character = Unicode.Scalar(string.character(at: location))
else { return false }

return Self.additionalWordSeparators.contains(character)
}


/// Returns the word range that includes the given location.
///
/// - Parameter location: The character index to find the word range.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,30 @@ final class EditorTextView: NSTextView, CurrentLineHighlighting, MultiCursorEdit
}


override func smartInsert(beforeStringFor pasteString: String, replacing charRangeToReplace: NSRange) -> String? {

// keep words joined by additional word separators, such as `foo.bar` and `1,000`, when replacing a part of them
// -> NSTextView inserts a space after some punctuation, such as `.` and `,`, for natural language.
// (2026-12, macOS 26)
if !charRangeToReplace.isEmpty, self.isAdditionalWordSeparator(at: charRangeToReplace.lowerBound - 1) {
return nil
}

return super.smartInsert(beforeStringFor: pasteString, replacing: charRangeToReplace)
}


override func smartInsert(afterStringFor pasteString: String, replacing charRangeToReplace: NSRange) -> String? {

// keep words joined by additional word separators when replacing a part of them (see `smartInsert(beforeStringFor:replacing:)`)
if !charRangeToReplace.isEmpty, self.isAdditionalWordSeparator(at: charRangeToReplace.upperBound) {
return nil
}

return super.smartInsert(afterStringFor: pasteString, replacing: charRangeToReplace)
}


override var baseWritingDirection: NSWritingDirection {

willSet {
Expand Down
58 changes: 58 additions & 0 deletions Tests/Sources/TextView/CursorMovementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,64 @@ import LineEnding
textView.transpose(nil)
#expect(textView.string == "😀a")
}


/// Tests that smart insert doesn't add a space next to an additional word separator when replacing a part of a word.
///
/// - Parameters:
/// - string: The text view content.
/// - range: The range to replace.
@Test(arguments: [
(".111", NSRange(1..<4)),
("1,000", NSRange(2..<5)),
("12:30", NSRange(3..<5)),
("foo.bar", NSRange(0..<3)),
])
func smartInsertNextToSeparator(string: String, range: NSRange) {

let textView = makeEditorTextView(string: string)
textView.smartInsertDeleteEnabled = true

var before: NSString?
var after: NSString?
textView.smartInsert(for: "222", replacing: range, before: &before, after: &after)

#expect(before == nil)
#expect(after == nil)
}


/// Tests that smart insert keeps adding a space after a separator when inserting at an insertion point.
@Test func smartInsertAtInsertionPointAfterSeparator() {

let textView = makeEditorTextView(string: "Hello.")
textView.smartInsertDeleteEnabled = true

var before: NSString?
var after: NSString?
textView.smartInsert(for: "world", replacing: NSRange(6..<6), before: &before, after: &after)

#expect(before == " ")
}


/// Tests that smart pasting a word to replace a word next to an additional word separator doesn't add a space.
@Test func smartPasteNextToSeparator() {

let textView = makeEditorTextView(string: ".111", selectedRange: NSRange(1..<4))
textView.smartInsertDeleteEnabled = true
textView.selectionGranularity = .selectByWord

let smartPasteType = NSPasteboard.PasteboardType("NeXT smart paste pasteboard type")
let pasteboard = NSPasteboard.withUniqueName()
defer { pasteboard.releaseGlobally() }
pasteboard.declareTypes([.string, smartPasteType], owner: nil)
pasteboard.setString("222", forType: .string)
pasteboard.setData(Data(), forType: smartPasteType)

#expect(textView.readSelection(from: pasteboard, type: .string))
#expect(textView.string == ".222")
}
}


Expand Down