Change rule of matching brace highlight like Xcode

This commit is contained in:
1024jp 2017-09-26 20:44:45 +09:00
parent 85c5b127d7
commit 87676eb259
2 changed files with 15 additions and 22 deletions

View File

@ -15,6 +15,8 @@ Develop
- Improve Replace All action:
- Avoid recoloring after Replace All if no text replaced.
- Improve the progress indicator.
- Change to highlight matching braces just like Xcode.
- No more beep for unbalanced braces.
- Update “JavaScript” syntax style:
- Add “.pac” extension.
- Update build environment to Xcode 9 (SDK macOS 10.13).

View File

@ -71,7 +71,6 @@ final class EditorTextViewController: NSViewController, NSTextViewDelegate {
// MARK: Private Properties
private var lastCursorLocation = 0
private lazy var currentLineUpdateTask: Debouncer = Debouncer(delay: 0.01, tolerance: 0.5) { [weak self] in self?.updateCurrentLineRect() }
private enum MenuItemTag: Int {
@ -295,41 +294,33 @@ final class EditorTextViewController: NSViewController, NSTextViewDelegate {
}
/// find the matching open brace and highlight it
/// find the matching brace and highlight it
private func highlightMatchingBrace(in textView: NSTextView) {
guard
UserDefaults.standard[.highlightBraces],
let string = textView.string, !string.isEmpty,
textView.selectedRange.location != NSNotFound
let string = textView.string, !string.isEmpty
else { return }
let cursorLocation = textView.selectedRange.location
let difference = cursorLocation - self.lastCursorLocation
self.lastCursorLocation = cursorLocation
// The brace will be highlighted only when the cursor moves forward, just like on Xcode. (2006-09-10)
// -> If the difference is more than one, the cursor would be moved with the mouse or programmatically
// and we shouldn't check for matching braces then.
guard difference == 1 else { return }
guard
cursorLocation != NSNotFound,
let cursorIndex = String.UTF16Index(encodedOffset: cursorLocation).samePosition(in: string)
else { return }
// check the caracter just before the cursor
guard let cursorIndex = String.UTF16Index(encodedOffset: cursorLocation).samePosition(in: string) else { return }
// check the character just before the cursor
let lastIndex = string.index(before: cursorIndex)
let lastCharacter = string.characters[lastIndex]
let bracePairs: [BracePair] = UserDefaults.standard[.highlightLtGt] ? (BracePair.braces + [.ltgt]) : BracePair.braces
guard let pair = bracePairs.first(where: { $0.end == lastCharacter }) else { return }
guard let index = string.indexOfBeginBrace(for: pair, at: lastIndex) else {
// do not beep when the typed brace is `>`
// -> Since `>` (and `<`) can often be used alone unlike other braces.
if pair != .ltgt {
NSBeep()
}
return
}
guard
let pair = bracePairs.first(where: { $0.begin == lastCharacter || $0.end == lastCharacter }),
let index = (pair.begin == lastCharacter)
? string.indexOfEndBrace(for: pair, at: lastIndex)
: string.indexOfBeginBrace(for: pair, at: lastIndex)
else { return }
let location = index.samePosition(in: string.utf16)!.encodedOffset