Skip to content

Commit

Permalink
• Improved "function list" for R scripts to list object declarations …
Browse files Browse the repository at this point in the history
…for setMethod, setReplaceMethod, setClass as "- a_method (a_class)" and "- (a_class)" resp.

git-svn-id: https://svn.r-project.org/R-packages/trunk/Mac-GUI@6045 694ef91d-65df-0310-b7bb-92e67a308ead
  • Loading branch information
Hans-Jörg Bibiko committed Jan 27, 2012
1 parent 2adcb4b commit ab16242
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 27 deletions.
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
NEWS for R.app GUI for Mac OS X

Last-update: 2012-01-27 [HJBB]
* Improved "function list" for R scripts to list
object declarations for setMethod, setReplaceMethod,
setClass as "- a_method (a_class)" and "- (a_class)"
resp.

Last-update: 2012-01-24 [HJBB]
* Added Format > Transformation >
- "Unicode Characters in R Strings to \uxxxx"
Expand Down
85 changes: 69 additions & 16 deletions RDocumentWinCtrl.m
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,10 @@ - (void) functionRescan
if([[[self document] fileType] isEqualToString:ftRSource]) {

NSInteger level = 0; // counter for function declaration inside a function declaration
NSInteger levelChecked = 0; // checked level counter if level > 16

// Dummy string for generating n times the string " " for structuring the menu
NSString *levelTemplate = @" ";
NSArray *d = nil;

// initialise flex
size_t tokenEnd, token;
Expand All @@ -577,11 +578,12 @@ - (void) functionRescan
// NSLog(@"t %d",token);
switch (token) {
case RSYM_FUNCTION: // a valid function name was found
tokenRange = NSMakeRange(yyuoffset, yyuleng);
levelChecked = (level>16) ? 48 : (level*3);
fn = [NSString stringWithFormat:@" %@%@%@", [levelTemplate substringWithRange:NSMakeRange(0,levelChecked)], (level)?@"":@"", [s substringWithRange:tokenRange]];
fn = [NSString stringWithFormat:@" %@%@%@",
[levelTemplate substringWithRange:NSMakeRange(0,(level>16) ? 48 : (level*3))],
(level)?@"":@"",
[s substringWithRange:NSMakeRange(yyuoffset, yyuleng)]];
mi = nil;
SLog(@" - found identifier %d:%d \"%@\"", yyuoffset, yyuleng, fn);
SLog(@" - found function %d:%d \"%@\"", yyuoffset, yyuleng, fn);
fnf++;
if (yyuoffset<=sr.location) sit=pim;
mi = [[NSMenuItem alloc] initWithTitle:fn action:@selector(functionGo:) keyEquivalent:@""];
Expand All @@ -592,11 +594,12 @@ - (void) functionRescan
pim++;
break;
case RSYM_INV_FUNCTION: // an invalid function name was found
tokenRange = NSMakeRange(yyuoffset, yyuleng);
levelChecked = (level>16) ? 48 : (level*3);
fn = [NSString stringWithFormat:@" %@%@%@", [levelTemplate substringWithRange:NSMakeRange(0,levelChecked)], (level)?@"":@"", [s substringWithRange:tokenRange]];
fn = [NSString stringWithFormat:@" %@%@%@",
[levelTemplate substringWithRange:NSMakeRange(0,(level>16) ? 48 : (level*3))],
(level)?@"":@"",
[s substringWithRange:NSMakeRange(yyuoffset, yyuleng)]];
mi = nil;
SLog(@" - found identifier %d:%d \"%@\"", yyuoffset, yyuleng, fn);
SLog(@" - found invalid function %d:%d \"%@\"", yyuoffset, yyuleng, fn);
fnf++;
if (yyuoffset<=sr.location) sit=pim;
mi = [[NSMenuItem alloc] initWithTitle:fn action:@selector(functionGo:) keyEquivalent:@""];
Expand All @@ -609,14 +612,65 @@ - (void) functionRescan
[mi release];
pim++;
break;
case RSYM_METHOD: // not yet implemented
case RSYM_METHOD1: // setMethod(f, sig)
d = [s captureComponentsMatchedByRegex:@"(?m)([\"'])([^\"']+)\\1[^\"']+?([\"'])([^\"']+)\\3" range:NSMakeRange(yyuoffset, yyuleng)];
if(d && [d count] == 5) {
fn = [NSString stringWithFormat:@" %@%@- %@ (%@)",
[levelTemplate substringWithRange:NSMakeRange(0,(level>16) ? 48 : (level*3))],
(level)?@"":@"",
[d objectAtIndex:2],
[d lastObject]];
mi = nil;
SLog(@" - found method1 %d:%d \"%@\"", yyuoffset, yyuleng, fn);
fnf++;
if (yyuoffset<=sr.location) sit=pim;
mi = [[NSMenuItem alloc] initWithTitle:fn action:@selector(functionGo:) keyEquivalent:@""];
[mi setTag:yyuoffset];
[mi setTarget:self];
[fnm addItem:mi];
[mi release];
pim++;
}
break;
case RSYM_METHOD2: // setMethod(sig, f)
d = [s captureComponentsMatchedByRegex:@"(?m)([\"'])([^\"']+)\\1[^\"']+?([\"'])([^\"']+)\\3" range:NSMakeRange(yyuoffset, yyuleng)];
if(d && [d count] == 5) {
fn = [NSString stringWithFormat:@" %@%@- %@ (%@)",
[levelTemplate substringWithRange:NSMakeRange(0,(level>16) ? 48 : (level*3))],
(level)?@"":@"",
[d lastObject],
[d objectAtIndex:2]];
mi = nil;
SLog(@" - found method2 %d:%d \"%@\"", yyuoffset, yyuleng, fn);
fnf++;
if (yyuoffset<=sr.location) sit=pim;
mi = [[NSMenuItem alloc] initWithTitle:fn action:@selector(functionGo:) keyEquivalent:@""];
[mi setTag:yyuoffset];
[mi setTarget:self];
[fnm addItem:mi];
[mi release];
pim++;
}
break;
case RSYM_CLASS: // setClass
tokenRange = NSMakeRange(yyuoffset, yyuleng);
fn = [NSString stringWithFormat:@" %@%@- (%@)",
[levelTemplate substringWithRange:NSMakeRange(0,(level>16) ? 48 : (level*3))],
(level)?@"":@"",
[[s substringWithRange:tokenRange] stringByMatching:@"([\"'])([^\"']+)\\1" capture:2L]];
mi = nil;
SLog(@" - found class %d:%d \"%@\"", yyuoffset, yyuleng, fn);
fnf++;
if (yyuoffset<=sr.location) sit=pim;
mi = [[NSMenuItem alloc] initWithTitle:fn action:@selector(functionGo:) keyEquivalent:@""];
[mi setTag:yyuoffset];
[mi setTarget:self];
[fnm addItem:mi];
[mi release];
pim++;
break;
case RSYM_PRAGMA: // a literal pragma mark was found; it will displayed in blue to structure large R scripts
tokenRange = NSMakeRange(yyuoffset, yyuleng);
fn = [s substringWithRange:tokenRange];
NSRange r = [fn rangeOfRegex:@"^(#pragma\\s+mark\\s+)(.*?)\\s*$" capture:2L];
fn = [fn substringWithRange:r];
fn = [[s substringWithRange:NSMakeRange(yyuoffset, yyuleng)] stringByMatching:@"^(#pragma\\s+mark\\s+)(.*?)\\s*$" capture:2L];
mi = nil;
SLog(@" - found pragma %d:%d \"%@\"", yyuoffset, yyuleng, fn);
fnf++;
Expand All @@ -632,7 +686,6 @@ - (void) functionRescan
pim++;
break;
case RSYM_PRAGMA_LINE: // insert a menu separator line
tokenRange = NSMakeRange(yyuoffset, yyuleng);
mi = nil;
SLog(@" - found identifier for separator");
fnf++;
Expand Down Expand Up @@ -660,7 +713,7 @@ - (void) functionRescan
tokenEnd = NSMaxRange(tokenRange) - 1;

}

}
else if([[[self document] fileType] isEqualToString:ftRdDoc]) {
while (1) {
Expand Down
2 changes: 1 addition & 1 deletion RScriptEditorTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ - (void)textStorageDidProcessEditing:(NSNotification *)notification
// Improve undo behaviour, i.e. it depends how fast the user types
[self performSelector:@selector(breakUndoCoalescing) withObject:nil afterDelay:0.8];

[[self delegate] performSelector:@selector(functionRescan) withObject:nil afterDelay:0.05];
[[self delegate] performSelector:@selector(functionRescan) withObject:nil afterDelay:0.2];

}

Expand Down
14 changes: 8 additions & 6 deletions RSymbolTokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
*/

#define RSYM_FUNCTION 1
#define RSYM_METHOD 2
#define RSYM_PRAGMA 3
#define RSYM_PRAGMA_LINE 4
#define RSYM_INV_FUNCTION 5
#define RSYM_LEVEL_UP 6
#define RSYM_LEVEL_DOWN 7
#define RSYM_METHOD1 2
#define RSYM_METHOD2 3
#define RSYM_CLASS 4
#define RSYM_PRAGMA 5
#define RSYM_PRAGMA_LINE 6
#define RSYM_INV_FUNCTION 7
#define RSYM_LEVEL_UP 8
#define RSYM_LEVEL_DOWN 9
#define RSYM_OTHER 10
32 changes: 28 additions & 4 deletions RSymbolTokens.l
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

size_t utf8strlenSym(const char * _s);
size_t yyuoffset, yyuleng;
int bcnt = 0;

//keep track of the current utf-8 character (not byte) offset and token length
#define YY_USER_ACTION { yyuoffset += yyuleng; yyuleng = utf8strlenSym(yytext); }
Expand All @@ -54,6 +55,8 @@ size_t yyuoffset, yyuleng;

s [ \t\n\r]
sw [ \t]
nf [\(,]
alphanum [0-9a-zA-Z_\.À-]
finv1 \.?[0-9]+[0-9a-zA-Z_\.À-]*
finv2 [0-9]+\.[0-9a-zA-Z_\.À-]*
finv3 _[0-9a-zA-Z_\.À-]*
Expand All @@ -62,6 +65,7 @@ assign [ \t\n\r]*(<<?-|=)[ \t\n\r]*
line [\-]+

%x paren
%x classparen

%%

Expand All @@ -76,21 +80,41 @@ line [\-]+

#.* ;

; ;
{s} ;

\( { BEGIN(paren); }
<paren>\"([^"\\]|\\(.|[\n\r]))*\"? ; /* ignore double quoted strings */
<paren>'([^'\\]|\\(.|[\n\r]))*'? ; /* ignore single quoted strings */
<paren>`[^`]*`? ; /* ignore backtick quoted string */
<paren>#.* ;
<paren>[^\)"'`#]+ ;
<paren>[^\)"'`#]+ ;
<paren>\) { BEGIN(INITIAL); }

<classparen>\"([^"\\]|\\(.|[\n\r]))*\"? ; /* ignore double quoted strings */
<classparen>'([^'\\]|\\(.|[\n\r]))*'? ; /* ignore single quoted strings */
<classparen>`[^`]*`? ; /* ignore backtick quoted string */
<classparen>#.* ;
<classparen>\( { bcnt++; }
<classparen>[^\(\)"'`#]+ ;
<classparen>\) { bcnt--; if(bcnt <= 0)BEGIN(INITIAL); }

\{ { return RSYM_LEVEL_DOWN; }
\} { return RSYM_LEVEL_UP; }

{finv1}/{assign}function{s}*\( { return RSYM_INV_FUNCTION; }
{finv2}/{assign}function{s}*\( { return RSYM_INV_FUNCTION; }
{finv3}/{assign}function{s}*\( { return RSYM_INV_FUNCTION; }
{fname}/{assign}function{s}*\( { return RSYM_FUNCTION; }
; ;
{s} ;
{nf}{s}*{fname}/{assign}function{s}*\( ;
{fname}/{assign}function{s}*\( { return RSYM_FUNCTION; }

set(Replace)?Method{s}*\({s}*(f{s}*=)?.*{s}*,{s}*(signature{s}*=)?.*{s}*, { bcnt=1; BEGIN(classparen); return RSYM_METHOD1; }
set(Replace)?Method{s}*\({s}*signature{s}*=.*{s}*,{s}*f{s}*=.*{s}*, { bcnt=1; BEGIN(classparen); return RSYM_METHOD2; }

setClass{s}*\({s}*(Class{s}*=)?{s}*["][^"]+["]{s}*\) { return RSYM_CLASS; }
setClass{s}*\({s}*(Class{s}*=)?{s}*['][^']+[']{s}*\) { return RSYM_CLASS; }
setClass{s}*\({s}*(Class{s}*=)?{s}*["][^"]+["]{s}*, { bcnt=1; BEGIN(classparen); return RSYM_CLASS; }
setClass{s}*\({s}*(Class{s}*=)?{s}*['][^']+[']{s}*, { bcnt=1; BEGIN(classparen); return RSYM_CLASS; }

<<EOF>> {
BEGIN(INITIAL); /* make sure we return to initial state when finished! */
Expand Down

0 comments on commit ab16242

Please sign in to comment.