Skip to content

Commit

Permalink
remove RPARSERCONTEXTFORPOSITION, fix some int width issues and force…
Browse files Browse the repository at this point in the history
… re-size for document windows

git-svn-id: https://svn.r-project.org/R-packages/trunk/Mac-GUI@7927 694ef91d-65df-0310-b7bb-92e67a308ead
  • Loading branch information
s-u committed Feb 12, 2021
1 parent f675aea commit 92f97b1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 64 deletions.
15 changes: 3 additions & 12 deletions NSTextView_RAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@
#import "RegexKitLite.h"
#import "RGUI.h"

static inline int RPARSERCONTEXTFORPOSITION (RTextView* self, NSUInteger index)
{
typedef int (*RPARSERCONTEXTFORPOSITIONMethodPtr)(RTextView*, SEL, NSUInteger);
static RPARSERCONTEXTFORPOSITIONMethodPtr _RPARSERCONTEXTFORPOSITION;
if (!_RPARSERCONTEXTFORPOSITION) _RPARSERCONTEXTFORPOSITION = (RPARSERCONTEXTFORPOSITIONMethodPtr)[self methodForSelector:@selector(parserContextForPosition:)];
int r = _RPARSERCONTEXTFORPOSITION(self, @selector(parserContextForPosition:), index);
return r;
}

@implementation NSTextView (NSTextView_RAdditions)

/**
Expand Down Expand Up @@ -148,7 +139,7 @@ - (IBAction)selectEnclosingBrackets:(id)sender
// look for the first non-balanced closing bracket
for(NSUInteger i=caretPosition; i<stringLength; i++) {
if(!breakCounter--) return;
if(RPARSERCONTEXTFORPOSITION((RTextView*)self, i) != pcExpression) continue;
if([(RTextView*)self parserContextForPosition: i] != pcExpression) continue;
switch(CFStringGetCharacterAtIndex(parserStringRef, i)) {
case ')':
if(!pcnt) {
Expand Down Expand Up @@ -187,7 +178,7 @@ - (IBAction)selectEnclosingBrackets:(id)sender
breakCounter = 10000;
for(NSInteger i=caretPosition; i>=0; i--) {
if(!breakCounter--) return;
if(RPARSERCONTEXTFORPOSITION((RTextView*)self, i) != pcExpression) continue;
if([(RTextView*)self parserContextForPosition: i] != pcExpression) continue;
c = CFStringGetCharacterAtIndex(parserStringRef, i);
if(c == co) {
if(!bracketCounter) {
Expand All @@ -206,7 +197,7 @@ - (IBAction)selectEnclosingBrackets:(id)sender
breakCounter = 10000;
for(NSUInteger i=caretPosition; i<stringLength; i++) {
if(!breakCounter--) return;
if(RPARSERCONTEXTFORPOSITION((RTextView*)self, i) != pcExpression) continue;
if([(RTextView*)self parserContextForPosition: i] != pcExpression) continue;
c = CFStringGetCharacterAtIndex(parserStringRef, i);
if(c == co) {
bracketCounter++;
Expand Down
40 changes: 21 additions & 19 deletions RDocumentWinCtrl.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,6 @@
return to_return;
}

static inline int RPARSERCONTEXTFORPOSITION (RTextView* self, NSUInteger index)
{
typedef int (*RPARSERCONTEXTFORPOSITIONMethodPtr)(RTextView*, SEL, NSUInteger);
static RPARSERCONTEXTFORPOSITIONMethodPtr _RPARSERCONTEXTFORPOSITION;
if (!_RPARSERCONTEXTFORPOSITION) _RPARSERCONTEXTFORPOSITION = (RPARSERCONTEXTFORPOSITIONMethodPtr)[self methodForSelector:@selector(parserContextForPosition:)];
int r = _RPARSERCONTEXTFORPOSITION(self, @selector(parserContextForPosition:), index);
return r;
}


@implementation RDocumentWinCtrl

//- (id)init { // NOTE: init is *not* used! put any initialization in windowDidLoad
Expand Down Expand Up @@ -363,8 +353,9 @@ - (void) windowDidLoad
[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:showBraceHighlightingKey options:NSKeyValueObservingOptionNew context:NULL];
[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:prefShowArgsHints options:NSKeyValueObservingOptionNew context:NULL];

[[self window] setBackgroundColor:[NSColor clearColor]];
[[self window] setOpaque:NO];
// FIXME: we did we use this?
//[[self window] setBackgroundColor:[NSColor clearColor]];
//[[self window] setOpaque:NO];

SLog(@" - load document contents into textView");
[(RDocument*)[self document] loadInitialContents];
Expand Down Expand Up @@ -412,13 +403,24 @@ - (void) windowDidLoad
[theRulerView release];

[(NoodleLineNumberView*)[[textView enclosingScrollView] verticalRulerView] setLineWrappingMode:[Preferences flagForKey:enableLineWrappingKey withDefault: YES]];

[[self window] makeFirstResponder:theRulerView];
}

[self functionReset];

// Needed for showing tooltips of folded items
[[self window] setAcceptsMouseMovedEvents:YES];

// FIXME: this is a hack for layout issues in Big Sur
// by forcing re-size we force layout to be updated - is there a better way?
NSRect clRect = [[self window] contentLayoutRect];
clRect.size.width += 1;
[[self window] setContentSize:clRect.size];
clRect.size.width -= 1;
[[self window] setContentSize:clRect.size];

// Make the text view fist responder so the user can start typing
[[self window] makeFirstResponder:textView];

SLog(@" - windowDidLoad is done");

Expand Down Expand Up @@ -786,7 +788,7 @@ - (void) functionRescan
unichar fc;
while (li>0 && ((fc=CFStringGetCharacterAtIndex((CFStringRef)s, li)) ==' ' || fc=='\t' || fc=='\r' || fc=='\n')) li--;

if(RPARSERCONTEXTFORPOSITION(textView, (li+2)) == pcComment)
if([textView parserContextForPosition: li + 2] == pcComment)
continue; // section declaration is commented out

// due to finial bracket decrease range length by 1
Expand Down Expand Up @@ -1001,7 +1003,7 @@ - (void) highlightBracesWithShift: (int) shift andWarn: (BOOL) warn
if (cursorLocation < 0 || cursorLocation >= completeStringLength) return;

// bail if current character is in quotes or comments
if(RPARSERCONTEXTFORPOSITION(textView, cursorLocation) != pcExpression) return;
if([textView parserContextForPosition:cursorLocation] != pcExpression) return;

unichar characterToCheck;
unichar openingChar = 0;
Expand All @@ -1019,7 +1021,7 @@ - (void) highlightBracesWithShift: (int) shift andWarn: (BOOL) warn
if (openingChar) {
while (cursorLocation--) {
if(!breakCounter--) return;
if(RPARSERCONTEXTFORPOSITION(textView, cursorLocation) == pcExpression) {
if([textView parserContextForPosition:cursorLocation] == pcExpression) {
c = CFStringGetCharacterAtIndex((CFStringRef)completeString, cursorLocation);
if (c == openingChar) {
if (!skipMatchingBrace) {
Expand All @@ -1041,7 +1043,7 @@ - (void) highlightBracesWithShift: (int) shift andWarn: (BOOL) warn
if (openingChar) {
while ((++cursorLocation)<maxLimit) {
if(!breakCounter--) return;
if(RPARSERCONTEXTFORPOSITION(textView, cursorLocation) == pcExpression) {
if([textView parserContextForPosition:cursorLocation] == pcExpression) {
c = CFStringGetCharacterAtIndex((CFStringRef)completeString, cursorLocation);
if (c == openingChar) {
if (!skipMatchingBrace) {
Expand Down Expand Up @@ -1137,7 +1139,7 @@ - (BOOL)textView:(NSTextView *)textViewSrc doCommandBySelector:(SEL)commandSelec
BOOL firstRun = YES;
unichar c;
for(i=0; i<[line length]; i++) {
if(RPARSERCONTEXTFORPOSITION(textView, i) == pcExpression) {
if([textView parserContextForPosition: i] == pcExpression) {
c=CFStringGetCharacterAtIndex((CFStringRef)line,i);
if(c==')') {
cntP--;
Expand Down Expand Up @@ -1622,7 +1624,7 @@ - (IBAction)tidyRCode: (id)sender
if(!fr.length) break;
r = [rtvstr rangeOfRegex:comre options:0 inRange:NSMakeRange(fr.location, [rtvstr length]-fr.location) capture:1L error:nil];
// check if first # is a comment character i.e. not quoted
if(r.location+1 < [rtvstr length] && RPARSERCONTEXTFORPOSITION(rtv, r.location+1) == pcComment) {
if(r.location+1 < [rtvstr length] && [rtv parserContextForPosition: r.location + 1] == pcComment) {
rs = [rtvstr substringWithRange:[rtvstr rangeOfRegex:comre options:0 inRange:NSMakeRange(fr.location, [rtvstr length]-fr.location) capture:2L error:nil]];
rs = [rs stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
rstr = [NSString stringWithFormat:@";c=\"@__@_@_@%@\"", rs];
Expand Down
2 changes: 1 addition & 1 deletion RTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ extern BOOL RTextView_autoCloseBrackets;

- (void)setConsoleMode:(BOOL)isConsole;

- (int) parserContextForPosition:(int)position;
- (int) parserContextForPosition:(NSInteger)position;
- (void) showHelpForCurrentFunction;
- (void) currentFunctionHint;
- (BOOL) wrapSelectionWithPrefix:(NSString *)prefix suffix:(NSString *)suffix;
Expand Down
54 changes: 22 additions & 32 deletions RTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,6 @@
static inline CGFloat RPointDistance(NSPoint a, NSPoint b) { return sqrtf( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ); }
static inline NSPoint RPointOnLine(NSPoint a, NSPoint b, CGFloat t) { return NSMakePoint(a.x*(1.0f-t) + b.x*t, a.y*(1.0f-t) + b.y*t); }

static inline int RPARSERCONTEXTFORPOSITION (RTextView* self, NSUInteger index)
{
typedef int (*RPARSERCONTEXTFORPOSITIONMethodPtr)(RTextView*, SEL, NSUInteger);
static RPARSERCONTEXTFORPOSITIONMethodPtr _RPARSERCONTEXTFORPOSITION;
if (!_RPARSERCONTEXTFORPOSITION) _RPARSERCONTEXTFORPOSITION = (RPARSERCONTEXTFORPOSITIONMethodPtr)[self methodForSelector:@selector(parserContextForPosition:)];
int r = _RPARSERCONTEXTFORPOSITION(self, @selector(parserContextForPosition:), index);
return r;
}


// declared external
BOOL RTextView_autoCloseBrackets = YES;

Expand All @@ -82,7 +72,7 @@ - (void)setAutomaticDashSubstitutionEnabled:(BOOL)flag;

@interface RTextView (Private)

- (void)selectMatchingPairAt:(int)position;
- (void)selectMatchingPairAt:(NSInteger)position;
- (NSString*)functionNameForCurrentScope;

@end
Expand Down Expand Up @@ -285,13 +275,13 @@ - (void)keyDown:(NSEvent *)theEvent

NSString *rc = [theEvent charactersIgnoringModifiers];
NSString *cc = [theEvent characters];
unsigned int modFlags = [theEvent modifierFlags];
unsigned long modFlags = [theEvent modifierFlags];
long allFlags = (NSShiftKeyMask|NSControlKeyMask|NSAlternateKeyMask|NSCommandKeyMask);
long curFlags = (modFlags & allFlags);

BOOL hilite = NO;

SLog(@"RTextView: keyDown: %@ *** \"%@\" %d", theEvent, rc, modFlags);
SLog(@"RTextView: keyDown: %@ *** \"%@\" %lx", theEvent, rc, modFlags);

if([rc length] && [undoBreakTokensSet characterIsMember:[rc characterAtIndex:0]]) [self breakUndoCoalescing];

Expand All @@ -301,7 +291,7 @@ - (void)keyDown:(NSEvent *)theEvent
return;
}
if ([rc isEqual:@"="]) {
int mf = modFlags&allFlags;
long mf = modFlags & allFlags;
if ( mf ==NSControlKeyMask) {
[self breakUndoCoalescing];
[self insertText:@"<-"];
Expand Down Expand Up @@ -551,7 +541,7 @@ - (BOOL) wrapSelectionWithPrefix:(NSString *)prefix suffix:(NSString *)suffix
*
* @param position The cursor position to test
*/
- (int)parserContextForPosition:(int)position
- (int)parserContextForPosition:(NSInteger)position
{

int context = pcExpression;
Expand All @@ -574,9 +564,9 @@ - (int)parserContextForPosition:(int)position
if (lineStart == position)
return context;

SLog(@"RTextView: parserContextForPosition: %d, line start: %ld", position, lineStart);
SLog(@"RTextView: parserContextForPosition: %ld, line start: %ld", (long)position, (long)lineStart);

int i = lineStart;
long i = lineStart;
BOOL skip = NO;
unichar c;
unichar commentSign = (isRdDocument) ? '%' : '#';
Expand Down Expand Up @@ -978,7 +968,7 @@ - (BOOL) shiftSelectionLeft
*
* @param position The cursor position to test
*/
- (void)selectMatchingPairAt:(int)position
- (void)selectMatchingPairAt:(NSInteger)position
{

if(position < 1 || position >= [[self string] length])
Expand Down Expand Up @@ -1017,8 +1007,8 @@ - (NSString*)functionNameForCurrentScope
NSRange parseRange = NSMakeRange(0, [parseString length]);
NSInteger breakCounter = 1000;

int parentheses = 0;
int index = 0;
int parentheses = 0;
NSInteger index = 0;

SLog(@"RTextView: functionNameForCurrentScope");

Expand All @@ -1036,11 +1026,11 @@ - (NSString*)functionNameForCurrentScope
// if a word was found and the word doesn't represent a numeric value
// and a ( follows then return word
if([helpString length] && ![[[NSNumber numberWithFloat:[helpString floatValue]] stringValue] isEqualToString:helpString]) {
int start = NSMaxRange(selectedRange);
NSInteger start = NSMaxRange(selectedRange);
if(start < [parseString length]) {
BOOL found = NO;
int i = 0;
int end = ([parseString length] > 100) ? 100 : [parseString length];
NSInteger i = 0;
NSInteger end = ([parseString length] > 100) ? 100 : [parseString length];
unichar c;
for(i = start; i < end; i++) {
if ((c = CFStringGetCharacterAtIndex((CFStringRef)parseString, i)) == '(') {
Expand Down Expand Up @@ -1103,7 +1093,7 @@ - (NSString*)functionNameForCurrentScope
return nil;
}

SLog(@" - first not closed ( found at index: %d", index);
SLog(@" - first not closed ( found at index: %ld", (long) index);

// check if we still are in the parse range; otherwise bail
if(parseRange.location > index) {
Expand All @@ -1125,7 +1115,7 @@ - (NSString*)functionNameForCurrentScope
}
}

SLog(@" - function name found at index: %d", index);
SLog(@" - function name found at index: %ld", (long) index);

// check if we still are in the parse range; otherwise bail
if(parseRange.location > index) {
Expand Down Expand Up @@ -1187,7 +1177,7 @@ - (IBAction)makeASCIIconform:(id)sender
c = CFStringGetCharacterAtIndex((CFStringRef)strToConvert, theCharIndex);
// if c is non-ASCII and inside of "" or '' quotes transform it
// - this ignores all c inside of comments
if (c > 127 && RPARSERCONTEXTFORPOSITION((RTextView*)self, theCharIndexInTextView) < pcStringBQ)
if (c > 127 && [(RTextView*)self parserContextForPosition: theCharIndexInTextView] < pcStringBQ)
[theEncodedString appendFormat: @"\\u%04x", c];
else
[theEncodedString appendFormat: @"%C", c];
Expand Down Expand Up @@ -1250,7 +1240,7 @@ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
return ([[self undoManager] canRedo]);

if ([menuItem action] == @selector(makeASCIIconform:))
return ([self selectedRange].length || (([(RTextView*)self getRangeForCurrentWord].length) && RPARSERCONTEXTFORPOSITION((RTextView*)self, [self selectedRange].location) < pcStringBQ)) ? YES : NO;
return ([self selectedRange].length || (([(RTextView*)self getRangeForCurrentWord].length) && [(RTextView*)self parserContextForPosition:[self selectedRange].location] < pcStringBQ)) ? YES : NO;

if ([menuItem action] == @selector(unescapeUnicode:))
return ([self selectedRange].length || ([(RTextView*)self getRangeForCurrentWord].length)) ? YES : NO;
Expand Down Expand Up @@ -1361,7 +1351,7 @@ - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender

[env setObject:filepath forKey:kShellVarNameDraggedFilePath];
[env setObject:[[filepath stringByReplacingOccurrencesOfRegex:[NSString stringWithFormat:@"^%@", curDir] withString:@""] stringByAbbreviatingWithTildeInPath] forKey:kShellVarNameDraggedRelativeFilePath];
[env setObject:[NSNumber numberWithInt:snip_cnt] forKey:kShellVarNameCurrentSnippetIndex];
[env setObject:[NSNumber numberWithInt:(int)snip_cnt] forKey:kShellVarNameCurrentSnippetIndex];

NSString *extension = [[filepath pathExtension] lowercaseString];

Expand Down Expand Up @@ -2134,10 +2124,10 @@ - (NSRange)getRangeForCurrentWordOfRange:(NSRange)curRange
if (curRange.length) return curRange;

NSString *str = [self string];
int curLocation = curRange.location;
int start = curLocation;
int end = curLocation;
unsigned int strLen = [[self string] length];
NSInteger curLocation = curRange.location;
NSInteger start = curLocation;
NSInteger end = curLocation;
NSUInteger strLen = [[self string] length];

if(start) {
start--;
Expand Down

0 comments on commit 92f97b1

Please sign in to comment.