mirror of
https://github.com/gnustep/apps-projectcenter.git
synced 2025-02-14 15:41:34 +00:00
instead of using getLineStart which returns a character after scanning lines, we directly scan lines and count the newlines, so the string is needs to be scanned once only to know hoe many newlines. Some IMP caching to improve this ineffcient o(n) at every cursor movement
This commit is contained in:
parent
bae62a963b
commit
aff171ceff
1 changed files with 29 additions and 8 deletions
|
@ -967,15 +967,36 @@
|
||||||
if ([object isKindOfClass:[NSTextView class]])
|
if ([object isKindOfClass:[NSTextView class]])
|
||||||
{
|
{
|
||||||
NSTextView *tv = (NSTextView *)object;
|
NSTextView *tv = (NSTextView *)object;
|
||||||
NSArray *selArray;
|
NSString *str = [tv string];
|
||||||
NSRange lastSelection;
|
NSRange selection;
|
||||||
NSRange selRange;
|
NSUInteger selLine = NSNotFound;
|
||||||
NSUInteger selLine;
|
|
||||||
|
|
||||||
selArray = [tv selectedRanges];
|
// for speed reasons we cache [NSString characterAtIndex:index]
|
||||||
lastSelection = [[selArray lastObject] rangeValue];
|
SEL charAtIndexSel = @selector(characterAtIndex:);
|
||||||
NSLog(@"last selection is %@", [selArray lastObject]);
|
unichar (*charAtIndexFunc)(NSString *, SEL, NSUInteger);
|
||||||
[[tv string] getLineStart:NULL end:&selLine contentsEnd:NULL forRange:lastSelection];
|
charAtIndexFunc = (unichar (*)())[str methodForSelector:charAtIndexSel];
|
||||||
|
|
||||||
|
selection = [tv selectedRange];
|
||||||
|
// now we calculate given the selection the line count, splitting on \n
|
||||||
|
// calling lineRangeForRange / paragraphForRange does the same thing
|
||||||
|
// we want to avoid to scan the string twice
|
||||||
|
{
|
||||||
|
NSUInteger i;
|
||||||
|
unichar ch;
|
||||||
|
NSUInteger nlCount;
|
||||||
|
|
||||||
|
nlCount = 0;
|
||||||
|
for (i = 0; i < selection.location; i++)
|
||||||
|
{
|
||||||
|
// ch = [str characterAtIndex:i];
|
||||||
|
ch = (*charAtIndexFunc)(str, charAtIndexSel, i);
|
||||||
|
if (ch == (unichar)0x000A) // new line
|
||||||
|
nlCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
selLine = nlCount + 1;
|
||||||
|
}
|
||||||
|
NSLog(@"%u corresponds to %u", selection.location, selLine);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue