diff --git a/CotEditor/Sources/Document Window/Text View/EditorTextView+CursorMovement.swift b/CotEditor/Sources/Document Window/Text View/EditorTextView+CursorMovement.swift index c42932d829..31727744c5 100644 --- a/CotEditor/Sources/Document Window/Text View/EditorTextView+CursorMovement.swift +++ b/CotEditor/Sources/Document Window/Text View/EditorTextView+CursorMovement.swift @@ -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? { + + // 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 { diff --git a/Tests/Sources/TextView/CursorMovementTests.swift b/Tests/Sources/TextView/CursorMovementTests.swift index 354147fb3b..611502cb21 100644 --- a/Tests/Sources/TextView/CursorMovementTests.swift +++ b/Tests/Sources/TextView/CursorMovementTests.swift @@ -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") + } }