From 535b27f4fce4926b22c5ca51541193aea6b26196 Mon Sep 17 00:00:00 2001 From: 1024jp <1024jp@wolfrosch.com> Date: Tue, 26 Sep 2017 12:47:55 +0900 Subject: [PATCH] Avoid replacing if result of Replace All is 0 --- CHANGELOG.md | 4 +++- CotEditor/Sources/TextFind.swift | 28 +++++++++++++++++----------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c14434bba..dab998d26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/CotEditor/Sources/TextFind.swift b/CotEditor/Sources/TextFind.swift index fc5e688e3..a43df5bd2 100644 --- a/CotEditor/Sources/TextFind.swift +++ b/CotEditor/Sources/TextFind.swift @@ -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()