Fix cursor after auto quote close (fix #1384)

This commit is contained in:
1024jp 2022-11-03 09:56:36 +09:00
parent 83f9fa39ff
commit 062dce1edf
2 changed files with 26 additions and 1 deletions

View File

@ -15,7 +15,8 @@ Change Log
- Fix an issue on macOS 13 that the Find All button in the find panel was not localized.
- Fix an issue on CotEditor 4.4.0 that `contents of selection` of a document object returned its entire contents.
- Fix an issue that the last of multiple insertion points that locates at the end of the document was not drawn.
- Fix an issue that the last of multiple insertion points locates at the end of the document was not drawn.
- Fix an issue that the insertion point immediately exited automatically closed quote marks when the smart quote feature is enabled.

View File

@ -95,6 +95,7 @@ final class EditorTextView: NSTextView, Themable, CurrentLineHighlighting, Multi
private let matchingBracketPairs: [BracePair] = BracePair.braces + [.doubleQuotes]
private lazy var braceHighlightDebouncer = Debouncer { [weak self] in self?.highlightMatchingBrace() }
private var isTypingPairedQuotes = false
private var cursorType: CursorType = .bar
private var balancesBrackets = false
@ -557,6 +558,11 @@ final class EditorTextView: NSTextView, Themable, CurrentLineHighlighting, Multi
if !CharacterSet.alphanumerics.contains(self.character(after: self.rangeForUserTextChange) ?? Unicode.Scalar(0)),
!(pair.begin == pair.end && CharacterSet.alphanumerics.contains(self.character(before: self.rangeForUserTextChange) ?? Unicode.Scalar(0))) // for "
{
// raise frag to manipulate the cursor later in `handleTextCheckingResults(_:forRange:types:options:orthography:wordCount:)`
if self.isAutomaticQuoteSubstitutionEnabled, pair.begin == "\"" {
self.isTypingPairedQuotes = true
}
super.insertText(String(pair.begin) + String(pair.end), replacementRange: replacementRange)
self.setSelectedRangesWithUndo([NSRange(location: self.selectedRange.location - 1, length: 0)])
self.textStorage?.addAttribute(.autoBalancedClosingBracket, value: true,
@ -754,6 +760,24 @@ final class EditorTextView: NSTextView, Themable, CurrentLineHighlighting, Multi
}
/// Perform automatic corrections
override func handleTextCheckingResults(_ results: [NSTextCheckingResult], forRange range: NSRange, types checkingTypes: NSTextCheckingTypes, options: [NSSpellChecker.OptionKey: Any] = [:], orthography: NSOrthography, wordCount: Int) {
super.handleTextCheckingResults(results, forRange: range, types: checkingTypes, options: options, orthography: orthography, wordCount: wordCount)
// move the cursor back into the middle of quotes if the paired close quote was automatically inserted
// because the cursor is automatically moved after the close quote by this method (#1384)
if self.isTypingPairedQuotes,
self.isAutomaticQuoteSubstitutionEnabled,
self.selectedRange.isEmpty,
results.map(\.resultType).contains(where: { $0.contains(.quote) })
{
self.selectedRange.location -= 1
}
self.isTypingPairedQuotes = false
}
/// change multiple selection ranges
override var selectedRanges: [NSValue] {