Support snippet insertion in multi-cursor editing

This commit is contained in:
1024jp 2019-01-12 02:03:56 +09:00
parent fc5fae18de
commit eb80963842
2 changed files with 19 additions and 9 deletions

View File

@ -5,6 +5,11 @@ Change Log
3.7.0-beta.4 (unreleased)
--------------------------
### Improvements
- [beta] Support snippet insertion in multi-cursor editing.
### Fixes
- [beta] Suppress blinking Edit menu when perform `^⇧↑` or `^⇧↓` shortcut.

View File

@ -8,7 +8,7 @@
//
// ---------------------------------------------------------------------------
//
// © 2017 1024jp
// © 2017-2019 1024jp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -29,16 +29,21 @@ extension NSTextView {
func insert(snippet: Snippet) {
let range = self.rangeForUserTextChange
let ranges = (self.rangesForUserTextChange ?? self.selectedRanges).map { $0.rangeValue }
let strings = [String](repeating: snippet.string, count: ranges.count)
guard self.shouldChangeText(in: range, replacementString: snippet.string) else { return }
let selectedRanges: [NSRange]? = {
guard let selection = snippet.selection else { return nil }
let snippetLength = snippet.string.utf16.count
return ranges.map { range in
let location = ranges.prefix { $0 != range }.map { snippetLength - $0.length }.reduce(range.location, +)
return NSRange(location: location + selection.location, length: selection.length)
}
}()
self.replace(with: strings, ranges: ranges, selectedRanges: selectedRanges, actionName: "Insert Snippet".localized)
self.replaceCharacters(in: range, with: snippet.string)
self.didChangeText()
if let selection = snippet.selection {
self.selectedRange = NSRange(location: range.location + selection.location, length: selection.length)
}
self.undoManager?.setActionName("Insert Snippet".localized)
}
}