refactor some of the character highlighting, trying to rely only on the start-end pair of locations without old extra variables

This commit is contained in:
Riccardo Mottola 2021-10-21 23:36:13 +02:00
parent aff171ceff
commit 60db8c598f
2 changed files with 45 additions and 46 deletions

View file

@ -74,14 +74,10 @@
NSColor *backgroundColor; NSColor *backgroundColor;
NSColor *readOnlyColor; NSColor *readOnlyColor;
NSColor *textBackground; NSColor *textBackground;
// location of the highlit delimiter character
unsigned int highlitCharacterLocation;
// is YES if we are currently highlighting a delimiter character // location of the highlighted delimiter characters
// otherwise NO // NSNotFound means not set
BOOL isCharacterHighlit; NSUInteger highlited_chars[2];
int highlited_chars[2];
// the stored color and font attributes of the highlit character, so // the stored color and font attributes of the highlit character, so
// that they can be restored later on when the character is un-highlit // that they can be restored later on when the character is un-highlit

View file

@ -175,18 +175,14 @@
// Activate undo // Activate undo
[ev setAllowsUndo: YES]; [ev setAllowsUndo: YES];
[ev setDelegate:self];
[[NSNotificationCenter defaultCenter] [[NSNotificationCenter defaultCenter]
addObserver:self addObserver:self
selector:@selector(textDidChange:) selector:@selector(textDidChange:)
name:NSTextDidChangeNotification name:NSTextDidChangeNotification
object:ev]; object:ev];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(textViewDidChangeSelection:)
name:NSTextViewDidChangeSelectionNotification
object:ev];
return ev; return ev;
} }
@ -227,9 +223,8 @@
previousBGColor = nil; previousBGColor = nil;
previousFont = nil; previousFont = nil;
isCharacterHighlit = NO; highlited_chars[0] = NSNotFound;
highlited_chars[0] = -1; highlited_chars[1] = NSNotFound;
highlited_chars[1] = -1;
undoManager = [[NSUndoManager alloc] init]; undoManager = [[NSUndoManager alloc] init];
} }
@ -948,6 +943,23 @@
} }
} }
- (NSRange)textView:(NSTextView *)textView
willChangeSelectionFromCharacterRange:(NSRange)oldSelectedCharRange
toCharacterRange:(NSRange)newSelectedCharRange
{
NSLog(@"Will change selection from %@ to %@", NSStringFromRange(oldSelectedCharRange), NSStringFromRange(newSelectedCharRange));
NSLog(@"is pressing key %d", editorTextViewIsPressingKey);
if (editorTextViewIsPressingKey == NO)
{
// unhighlight also invalidates old locations
if (textView == _intEditorView || textView == _extEditorView)
[self unhighlightCharacter: textView];
}
return newSelectedCharRange;
}
- (void)textViewDidChangeSelection:(NSNotification *)notification - (void)textViewDidChangeSelection:(NSNotification *)notification
{ {
id object; id object;
@ -956,13 +968,11 @@
if (editorTextViewIsPressingKey == NO) if (editorTextViewIsPressingKey == NO)
{ {
id object;
object = [notification object];
if (object == _intEditorView || object == _extEditorView) if (object == _intEditorView || object == _extEditorView)
[self computeNewParenthesisNesting: object]; [self computeNewParenthesisNesting: object];
} }
NSLog(@"textViewDidChangeSelection");
// calculate current line // calculate current line
if ([object isKindOfClass:[NSTextView class]]) if ([object isKindOfClass:[NSTextView class]])
{ {
@ -1356,19 +1366,17 @@ NSUInteger FindDelimiterInString(NSString * string,
- (void)unhighlightCharacter: (NSTextView *)editorView - (void)unhighlightCharacter: (NSTextView *)editorView
{ {
int i; unsigned i;
NSTextStorage *textStorage = [editorView textStorage]; NSTextStorage *textStorage = [editorView textStorage];
[textStorage beginEditing]; [textStorage beginEditing];
// if (isCharacterHighlit) for (i = 0; i < 2; i++)
for (i = 0; i < 2 && highlited_chars[i] != -1; i++)
{ {
if (highlited_chars[i] == NSNotFound)
continue;
NSRange r = NSMakeRange(highlited_chars[i], 1); NSRange r = NSMakeRange(highlited_chars[i], 1);
// NSRange r = NSMakeRange(highlitCharacterLocation, i);
isCharacterHighlit = NO;
// restore the character's color and font attributes // restore the character's color and font attributes
if (previousFont != nil) if (previousFont != nil)
@ -1406,45 +1414,39 @@ NSUInteger FindDelimiterInString(NSString * string,
range:r]; range:r];
} }
highlited_chars[i] = -1; highlited_chars[i] = NSNotFound;
} }
[textStorage endEditing]; [textStorage endEditing];
} }
- (void)highlightCharacterAt:(NSUInteger)location inEditor: (NSTextView *)editorView - (void)highlightCharacterPair:(NSTextView *)editorView
{ {
int i; unsigned i;
for (i = 0; i < 2 && highlited_chars[i] != -1; i++) {}; for (i = 0; i < 2; i++)
// if (isCharacterHighlit == NO)
if (i < 2)
{ {
if (highlited_chars[i] == NSNotFound)
return;
NSTextStorage *textStorage = [editorView textStorage]; NSTextStorage *textStorage = [editorView textStorage];
NSRange r = NSMakeRange(location, 1); NSRange r = NSMakeRange(highlited_chars[i], 1);
NSRange tmp; NSRange tmp;
// NSLog(@"highlight");
// highlitCharacterLocation = location;
highlited_chars[i] = location;
isCharacterHighlit = YES;
NSAssert(textStorage, @"textstorage can't be nil"); NSAssert(textStorage, @"textstorage can't be nil");
[textStorage beginEditing]; [textStorage beginEditing];
// store the previous character's attributes // store the previous character's attributes
ASSIGN(previousFGColor, ASSIGN(previousFGColor,
[textStorage attribute:NSForegroundColorAttributeName [textStorage attribute:NSForegroundColorAttributeName
atIndex:location atIndex:r.location
effectiveRange:&tmp]); effectiveRange:&tmp]);
ASSIGN(previousBGColor, ASSIGN(previousBGColor,
[textStorage attribute:NSBackgroundColorAttributeName [textStorage attribute:NSBackgroundColorAttributeName
atIndex:location atIndex:r.location
effectiveRange:&tmp]); effectiveRange:&tmp]);
ASSIGN(previousFont, [textStorage attribute:NSFontAttributeName ASSIGN(previousFont, [textStorage attribute:NSFontAttributeName
atIndex:location atIndex:r.location
effectiveRange:&tmp]); effectiveRange:&tmp]);
[textStorage addAttribute:NSFontAttributeName [textStorage addAttribute:NSFontAttributeName
@ -1478,7 +1480,7 @@ NSUInteger FindDelimiterInString(NSString * string,
selectedRange = [editorView selectedRange]; selectedRange = [editorView selectedRange];
// make sure we un-highlight a previously highlit delimiter // make sure we un-highlight a previously highlit delimiter
[self unhighlightCharacter :editorView]; // [self unhighlightCharacter :editorView];
// if we have a character at the selected location, check // if we have a character at the selected location, check
// to see if it is a delimiter character // to see if it is a delimiter character
@ -1508,8 +1510,9 @@ NSUInteger FindDelimiterInString(NSString * string,
// and in case a delimiter is found, highlight it // and in case a delimiter is found, highlight it
if (result != NSNotFound) if (result != NSNotFound)
{ {
[self highlightCharacterAt:selectedRange.location inEditor:editorView]; highlited_chars[0] = selectedRange.location;
[self highlightCharacterAt:result inEditor:editorView]; highlited_chars[1] = result;
[self highlightCharacterPair :editorView];
} }
} }
} }