Skip to content

Fix smart insert adding a space after word separators - #2208

Closed
ryochin wants to merge 1 commit into
coteditor:mainfrom
ryochin:fix/smart-insert-word-separator
Closed

ryochin wants to merge 1 commit into
coteditor:mainfrom
ryochin:fix/smart-insert-word-separator

Conversation

@ryochin

@ryochin ryochin commented Dec 2, 2026

Copy link
Copy Markdown

Summary

When "Smart copy/paste" is enabled, replacing a part of a word that is joined by an additional word separator (., ,, or :) with a pasted word inserts an unwanted space after the separator.

Steps to reproduce

  1. In Settings > Mode, enable "Smart copy/paste" for the mode used by the document (enabled by default for General).
  2. In a CotEditor document, type .111 on the first line and 222 on the second line.
  3. Double-click 222 to select it and copy it.
    • The word needs to be copied within CotEditor so that the pasteboard includes the smart paste type, which NSTextView adds only when copying a word-level selection with Smart copy/paste enabled.
  4. Double-click 111 to select it and paste.
  • Expected: .222
  • Actual: . 222

The same happens with 1,000 and 12:30, but not with /.

Cause

NSTextView.smartInsert(beforeStringFor:replacing:) returns a space when the preceding character is punctuation such as ., ,, ;, :, ), or =. This is intended for natural language (e.g., pasting a word after Hello. results in Hello. world), and plain NSTextView behaves the same way.

However, CotEditor treats ., ,, and : as additional word separators (EditorTextView.additionalWordSeparators) so that a part of a token like foo.bar, 1,000, or 12:30 can be selected by double-clicking. Replacing such a part splits the token with the space.

Changes

  • Override smartInsert(beforeStringFor:replacing:) and smartInsert(afterStringFor:replacing:) in EditorTextView to suppress the space when the adjacent character is an additional word separator and a non-empty range is being replaced.
  • Keep AppKit's default behavior when inserting at an insertion point, so Hello. + world still results in Hello. world.
  • Add isAdditionalWordSeparator(at:) so additionalWordSeparators can stay private.
  • The scope is intentionally limited to CotEditor's additional word separators. Other punctuation, such as = and ), is left to AppKit.

Tests

Added to CursorMovementTests:

  • smartInsertNextToSeparator(string:range:): replacing a part of .111, 1,000, 12:30, and foo.bar adds no spaces.
  • smartInsertAtInsertionPointAfterSeparator(): inserting at an insertion point after . still adds a space.
  • smartPasteNextToSeparator(): an actual smart paste via readSelection(from:type:) turns .111 into .222.

I confirmed that the first and third tests fail without the fix and that all tests in CursorMovementTests pass with it. I also checked the fix manually in a local build.

When smart insert/delete is enabled, replacing a part of a word joined
by an additional word separator, such as the "111" in ".111", by pasting
a word inserted an unwanted space after the separator (". 222").

NSTextView adds a space after some punctuation for natural language, but
CotEditor treats ".", ",", and ":" as word separators so that a part of
a token like "foo.bar", "1,000", or "12:30" can be selected by
double-clicking. Now, smart insert no longer adds spaces next to these
separators when replacing a selection, while the default behavior is
kept when inserting at an insertion point.
@ryochin
ryochin requested a review from 1024jp as a code owner December 2, 2026 02:53
@1024jp 1024jp self-assigned this Dec 4, 2026
@1024jp 1024jp added this to the 7.2.1 milestone Dec 4, 2026
@1024jp 1024jp added the better to fix unwanted behaviors not critical but better to fix label Dec 4, 2026
@1024jp 1024jp closed this in 1b408a5 Dec 4, 2026
@1024jp

1024jp commented Dec 4, 2026

Copy link
Copy Markdown
Member

@ryochin Thank you for the detailed report and the suggestion for the fix!

I've committed a more compact version of the fix in 1b408a5, so let me close this PR. The main differences are:

  • Only smartInsert(beforeStringFor:replacing:) is overridden. AppKit already returns nil from smartInsert(afterStringFor:replacing:) when the following character is ., ,, or :, so the other override had no effect.
  • The check uses additionalWordSeparators directly instead of adding a new helper method.
  • The tests are reduced to a single case that covers the fix.

I also credited you in the changelog. Thanks again for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

better to fix unwanted behaviors not critical but better to fix

Development

Successfully merging this pull request may close these issues.

2 participants