Avoid replacing if result of Replace All is 0

This commit is contained in:
1024jp 2017-09-26 12:47:55 +09:00
parent d659858db8
commit 535b27f4fc
2 changed files with 20 additions and 12 deletions

View File

@ -12,7 +12,9 @@ Develop
### Improvements
- Improve the progress indicator of Replace All.
- Improve Replace All action:
- Avoid recoloring after Replace All if no text replaced.
- Improve the progress indicator.
- Update “JavaScript” syntax style:
- Add “.pac” extension.
- Update build environment to Xcode 9 (SDK macOS 10.13).

View File

@ -235,6 +235,7 @@ final class TextFind {
/// - Parameters:
/// - replacementString: The string with which to replace.
/// - block: The Block enumerates the matches.
/// - flag: The current state of the replacing progress.
/// - stop: The Block can set the value to true to stop further processing of the array.
/// - Returns:
/// - replacementItems: ReplacementItem per selectedRange.
@ -265,21 +266,26 @@ final class TextFind {
}, scopeCompletionHandler: { (scopeRange: NSRange) in
block(.foundCount(items.count), &ioStop)
// build replacementString
var replacedString = (self.string as NSString).substring(with: scopeRange)
for item in items.reversed() {
block(.replacementProgress, &ioStop)
if ioStop { return }
let substringRange = NSRange(location: item.range.location - scopeRange.location, length: item.range.length)
replacedString = (replacedString as NSString).replacingCharacters(in: substringRange, with: item.string)
let length: Int
if items.isEmpty {
length = scopeRange.length
} else {
// build replacementString
var replacedString = (self.string as NSString).substring(with: scopeRange)
for item in items.reversed() {
block(.replacementProgress, &ioStop)
if ioStop { return }
let substringRange = NSRange(location: item.range.location - scopeRange.location, length: item.range.length)
replacedString = (replacedString as NSString).replacingCharacters(in: substringRange, with: item.string)
}
replacementItems.append(ReplacementItem(string: replacedString, range: scopeRange))
length = (replacedString as NSString).length
}
replacementItems.append(ReplacementItem(string: replacedString, range: scopeRange))
// build selectedRange
let locationDelta = zip(selectedRanges, self.selectedRanges).reduce(scopeRange.location) { $0 + ($1.0.length - $1.1.length) }
let selectedRange = NSRange(location: locationDelta, length: (replacedString as NSString).length)
let selectedRange = NSRange(location: locationDelta, length: length)
selectedRanges.append(selectedRange)
items.removeAll()