| 714 |
iacus |
1 |
/*
|
|
|
2 |
* R.app : a Cocoa front end to: "R A Computer Language for Statistical Data Analysis"
|
|
|
3 |
*
|
|
|
4 |
* R.app Copyright notes:
|
| 1254 |
iacus |
5 |
* Copyright (C) 2004-5 The R Foundation
|
| 714 |
iacus |
6 |
* written by Stefano M. Iacus and Simon Urbanek
|
|
|
7 |
*
|
|
|
8 |
*
|
|
|
9 |
* R Copyright notes:
|
|
|
10 |
* Copyright (C) 1995-1996 Robert Gentleman and Ross Ihaka
|
|
|
11 |
* Copyright (C) 1998-2001 The R Development Core Team
|
|
|
12 |
* Copyright (C) 2002-2004 The R Foundation
|
|
|
13 |
*
|
|
|
14 |
* This program is free software; you can redistribute it and/or modify
|
|
|
15 |
* it under the terms of the GNU General Public License as published by
|
|
|
16 |
* the Free Software Foundation; either version 2 of the License, or
|
|
|
17 |
* (at your option) any later version.
|
|
|
18 |
*
|
|
|
19 |
* This program is distributed in the hope that it will be useful,
|
|
|
20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
22 |
* GNU General Public License for more details.
|
|
|
23 |
*
|
|
|
24 |
* A copy of the GNU General Public License is available via WWW at
|
|
|
25 |
* http://www.gnu.org/copyleft/gpl.html. You can also obtain it by
|
|
|
26 |
* writing to the Free Software Foundation, Inc., 59 Temple Place,
|
|
|
27 |
* Suite 330, Boston, MA 02111-1307 USA.
|
|
|
28 |
*/
|
| 568 |
iacus |
29 |
|
|
|
30 |
#import "CodeCompletion.h"
|
| 816 |
urbaneks |
31 |
#import "../REngine/REngine.h"
|
| 5705 |
urbaneks |
32 |
#import "../RegexKitLite.h"
|
|
|
33 |
#import "FileCompletion.h"
|
| 1499 |
urbaneks |
34 |
#import "RGUI.h"
|
| 5705 |
urbaneks |
35 |
#import "../RController.h"
|
| 568 |
iacus |
36 |
|
| 5705 |
urbaneks |
37 |
|
| 568 |
iacus |
38 |
@implementation CodeCompletion
|
|
|
39 |
|
| 5987 |
bibiko |
40 |
// + (NSString*) complete: (NSString*) part {
|
|
|
41 |
// return nil;
|
|
|
42 |
// if (preventReentrance && insideR>0) {
|
|
|
43 |
// SLog(@"CodeCompletion.complete: returning nil completion to prevent R re-entrance [***]");
|
|
|
44 |
// return nil;
|
|
|
45 |
// }
|
|
|
46 |
// REngine *re = [REngine mainEngine];
|
|
|
47 |
// if (![re beginProtected]) {
|
|
|
48 |
// SLog(@"CodeCompletion.complete: returning nil completion because protected REngine entry failed [***]");
|
|
|
49 |
// return nil;
|
|
|
50 |
// }
|
|
|
51 |
// // first get the length of the search path so we can go environment by environment
|
|
|
52 |
// RSEXP *x = [re evaluateString:@"length(search())"];
|
|
|
53 |
// int pos=1, maxpos;
|
|
|
54 |
// if (x==nil || ((maxpos = [x integer])==0)) { [re endProtected]; return nil; }
|
|
|
55 |
//
|
|
|
56 |
// // ok, we got the search path length; search in each environment and if something matches, get it, otherwise go to the next one
|
|
|
57 |
// while (pos<=maxpos) {
|
|
|
58 |
// // use ls to get the names of the objects in the specific environment
|
|
|
59 |
// NSString *ls=[NSString stringWithFormat:@"ls(pos=%d, all.names=TRUE, pattern=\"^%@.*\")", pos, part];
|
|
|
60 |
// RSEXP *x = [re evaluateString:ls];
|
|
|
61 |
// //NSLog(@"attepmting to find %@ via %@", part, ls);
|
|
|
62 |
// if (x==nil) {
|
|
|
63 |
// [re endProtected];
|
|
|
64 |
// return nil;
|
|
|
65 |
// }
|
|
|
66 |
// NSArray *a = [x array];
|
|
|
67 |
// if (a == nil) {
|
|
|
68 |
// [x release];
|
|
|
69 |
// [re endProtected];
|
|
|
70 |
// return nil;
|
|
|
71 |
// }
|
|
|
72 |
//
|
|
|
73 |
// { // the following code works also if pattern is not specified; with pattern present we could make it even easier, but currently we use it just to narrow the search (e.g. "." could still be matched by something else ...)
|
|
|
74 |
// int i=0, matches=0;
|
|
|
75 |
// NSString *common=nil;
|
|
|
76 |
// while (i<[a count]) {
|
|
|
77 |
// NSString *sx = (NSString*) [a objectAtIndex:i];
|
|
|
78 |
// if ([sx hasPrefix: part]) {
|
|
|
79 |
// if (matches==0)
|
|
|
80 |
// common = [[NSString alloc] initWithString: sx];
|
|
|
81 |
// else {
|
|
|
82 |
// NSString *cpref=[[NSString alloc] initWithString:[common commonPrefixWithString:sx options:0]];
|
|
|
83 |
// [common release];
|
|
|
84 |
// common = cpref;
|
|
|
85 |
// }
|
|
|
86 |
// matches++;
|
|
|
87 |
// }
|
|
|
88 |
// i++;
|
|
|
89 |
// }
|
|
|
90 |
// if (common) { // attempt to get class of the object - it will fail if that's just a partial object, but who cares..
|
|
|
91 |
// x = [re evaluateString:[NSString stringWithFormat:@"try(class(%@),silent=TRUE)",common]];
|
|
|
92 |
// [re endProtected];
|
|
|
93 |
// if (x && [x string] && [[x string] isEqualToString:@"function"])
|
|
|
94 |
// return [[common autorelease] stringByAppendingString:@"("];
|
|
|
95 |
// else
|
|
|
96 |
// return [common autorelease];
|
|
|
97 |
// }
|
|
|
98 |
// }
|
|
|
99 |
// pos++;
|
|
|
100 |
// }
|
|
|
101 |
// [re endProtected];
|
|
|
102 |
// return nil;
|
|
|
103 |
// }
|
|
|
104 |
//
|
|
|
105 |
// + (NSArray*) completeAll: (NSString*) part cutPrefix: (int) prefix {
|
|
|
106 |
// if (preventReentrance && insideR>0) {
|
|
|
107 |
// SLog(@"CodeCompletion.completeAll: returning nil completion to prevent R re-entrance [***]");
|
|
|
108 |
// return nil;
|
|
|
109 |
// }
|
|
|
110 |
//
|
|
|
111 |
// REngine *re = [REngine mainEngine];
|
|
|
112 |
// if (![re beginProtected]) {
|
|
|
113 |
// SLog(@"CodeCompletion.completeAll: returning nil completion because protected REngine entry failed [***]");
|
|
|
114 |
// return nil;
|
|
|
115 |
// }
|
|
|
116 |
//
|
|
|
117 |
// // first get the length of the search path so we can go environment by environment
|
|
|
118 |
// RSEXP *x = [re evaluateString:@"length(search())"];
|
|
|
119 |
// int pos=1, maxpos, matches=0;
|
|
|
120 |
// NSMutableArray *ca = nil;
|
|
|
121 |
// NSString *common=nil;
|
|
|
122 |
//
|
|
|
123 |
// if (x==nil || ((maxpos = [x integer])==0)) { [re endProtected]; return nil; }
|
|
|
124 |
//
|
|
|
125 |
// ca = [[NSMutableArray alloc] initWithCapacity: 8];
|
|
|
126 |
//
|
|
|
127 |
// // ok, we got the search path length; search in each environment and if something matches, get it, otherwise go to the next one
|
|
|
128 |
// while (pos<=maxpos) {
|
|
|
129 |
// // use ls to get the names of the objects in the specific environment
|
|
|
130 |
// NSString *ls=[NSString stringWithFormat:@"ls(pos=%d, all.names=TRUE, pattern=\"^%@.*\")", pos, part];
|
|
|
131 |
// RSEXP *x = [re evaluateString:ls];
|
|
|
132 |
// //NSLog(@"attepmting to find %@ via %@", part, ls);
|
|
|
133 |
// if (x==nil) {
|
|
|
134 |
// [re endProtected];
|
|
|
135 |
// return nil;
|
|
|
136 |
// }
|
|
|
137 |
//
|
|
|
138 |
// NSArray *a = [x array];
|
|
|
139 |
//
|
|
|
140 |
// if (a == nil) {
|
|
|
141 |
// [x release];
|
|
|
142 |
// [re endProtected];
|
|
|
143 |
// return nil;
|
|
|
144 |
// }
|
|
|
145 |
//
|
|
|
146 |
// { // the following code works also if pattern is not specified; with pattern present we could make it even easier, but currently we use it just to narrow the search (e.g. "." could still be matched by something else ...)
|
|
|
147 |
// int i=0, firstMatch=-1;
|
|
|
148 |
// while (i<[a count]) {
|
|
|
149 |
// NSString *sx = (NSString*) [a objectAtIndex:i];
|
|
|
150 |
// if ([sx hasPrefix: part]) {
|
|
|
151 |
// if (matches==0) {
|
|
|
152 |
// firstMatch=i;
|
|
|
153 |
// common=[[NSString alloc] initWithString: sx];
|
|
|
154 |
// } else {
|
|
|
155 |
// NSString *cpref=[[NSString alloc] initWithString:[common commonPrefixWithString:sx options:0]];
|
|
|
156 |
// [common release];
|
|
|
157 |
// common=cpref;
|
|
|
158 |
// }
|
|
|
159 |
// if (prefix<[sx length]) {
|
|
|
160 |
// [ca addObject: [sx substringFromIndex:prefix]];
|
|
|
161 |
// matches++;
|
|
|
162 |
// }
|
|
|
163 |
// }
|
|
|
164 |
// i++;
|
|
|
165 |
// }
|
|
|
166 |
// }
|
|
|
167 |
// pos++;
|
|
|
168 |
// }
|
|
|
169 |
// if (common) {
|
|
|
170 |
// if (matches==1) {
|
|
|
171 |
// // attempt to get class of the object - it will fail if that's just a partial object, but who cares..
|
|
|
172 |
// x = [re evaluateString:[NSString stringWithFormat:@"try(class(%@),silent=TRUE)",common]];
|
|
|
173 |
// [ca release];
|
|
|
174 |
// if (x!=nil && [x string]!=nil && [[x string] isEqualToString:@"function"]) {
|
|
|
175 |
// [re endProtected];
|
|
|
176 |
// return [NSArray arrayWithObject: [[[common autorelease] stringByAppendingString:@"("] substringFromIndex:prefix]];
|
|
|
177 |
// } else {
|
|
|
178 |
// [re endProtected];
|
|
|
179 |
// return [NSArray arrayWithObject: [[common autorelease] substringFromIndex:prefix]];
|
|
|
180 |
// }
|
|
|
181 |
// } else {
|
|
|
182 |
// [common release];
|
|
|
183 |
// [re endProtected];
|
|
|
184 |
// return ca;
|
|
|
185 |
// }
|
|
|
186 |
// }
|
|
|
187 |
// [re endProtected];
|
|
|
188 |
// [ca release];
|
|
|
189 |
// return nil;
|
|
|
190 |
// }
|
| 568 |
iacus |
191 |
|
| 5705 |
urbaneks |
192 |
+ (NSArray*) retrieveSuggestionsForScopeRange:(NSRange)scopeRange inTextView:(NSTextView*)textView
|
|
|
193 |
{
|
|
|
194 |
|
|
|
195 |
if (preventReentrance && insideR>0) {
|
|
|
196 |
SLog(@"CodeCompletion.retrieveSuggestionForScope: returning nil completion to prevent R re-entrance [***]");
|
|
|
197 |
return nil;
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
REngine *re = [REngine mainEngine];
|
|
|
201 |
if (![re beginProtected]) {
|
|
|
202 |
SLog(@"CodeCompletion.retrieveSuggestionForScope: returning nil completion because protected REngine entry failed [***]");
|
|
|
203 |
return nil;
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
NSString *linebuffer = [[[textView textStorage] string] substringWithRange:scopeRange];
|
|
|
207 |
|
|
|
208 |
SLog(@" - passed string for completion:\n%@", [linebuffer description]);
|
|
|
209 |
if(!linebuffer || ![linebuffer length]) {
|
|
|
210 |
[re endProtected];
|
|
|
211 |
return nil;
|
|
|
212 |
}
|
|
|
213 |
|
| 6232 |
bibiko |
214 |
// delete all lines consisting of comments
|
|
|
215 |
linebuffer = [linebuffer stringByReplacingOccurrencesOfRegex:@"(?sm)^\\s*#.*?$" withString:@""];
|
| 5862 |
bibiko |
216 |
|
| 5705 |
urbaneks |
217 |
// convert scope string to single line
|
|
|
218 |
linebuffer = [linebuffer stringByReplacingOccurrencesOfRegex:@"[\n\r\t]+" withString:@" "];
|
|
|
219 |
|
|
|
220 |
// first we need to find out whether we're in a text part or code part
|
|
|
221 |
unichar c;
|
|
|
222 |
int tl = [linebuffer length], tp=0, quotes=0, dquotes=0, lastQuote=-1;
|
|
|
223 |
while (tp < tl) {
|
|
|
224 |
c = CFStringGetCharacterAtIndex((CFStringRef)linebuffer, tp);
|
|
|
225 |
if (c=='\\')
|
|
|
226 |
tp++; // skip the next char after a backslash (we don't have to worry about \023 and friends)
|
|
|
227 |
else {
|
|
|
228 |
if (dquotes==0 && c=='\'') {
|
|
|
229 |
quotes^=1;
|
|
|
230 |
if (quotes) lastQuote=tp;
|
|
|
231 |
}
|
|
|
232 |
if (quotes==0 && c=='"') {
|
|
|
233 |
dquotes^=1;
|
|
|
234 |
if (dquotes) lastQuote=tp;
|
|
|
235 |
}
|
|
|
236 |
}
|
|
|
237 |
tp++;
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
// if we're inside any quotes, bail via file completion
|
|
|
241 |
if (quotes+dquotes>0) {
|
|
|
242 |
SLog(@" - cursor is inside quotes - call file completion");
|
|
|
243 |
[re endProtected];
|
|
|
244 |
return [FileCompletion completeAll:[linebuffer substringFromIndex:lastQuote+1] cutPrefix:0];
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
// use internal rcompgen to retrieve completion suggestions;
|
|
|
248 |
// can be modified by the user via rc.setting s() and rc.options() resp.
|
|
|
249 |
|
| 6230 |
bibiko |
250 |
// remove all content in quotes and replace '' by "" for passing it through rcompgen.completion('%@')
|
|
|
251 |
linebuffer = [linebuffer stringByReplacingOccurrencesOfRegex:@"([\"']).*?(?<!\\\\)\\1" withString:@"\"\""];
|
| 5995 |
bibiko |
252 |
|
|
|
253 |
// escape backslashes
|
|
|
254 |
linebuffer = [linebuffer stringByReplacingOccurrencesOfRegex:@"\\\\" withString:@"\\"];
|
|
|
255 |
|
| 5705 |
urbaneks |
256 |
SLog(@" - rcompgen completion will be invoked with:\n%@", linebuffer);
|
|
|
257 |
RSEXP *xx = [re evaluateString:[NSString stringWithFormat:@"rcompgen.completion('%@')", linebuffer]];
|
|
|
258 |
[re endProtected];
|
|
|
259 |
if(xx) {
|
|
|
260 |
|
|
|
261 |
NSArray *ca = [xx array];
|
|
|
262 |
|
|
|
263 |
// if only one suggestion was found set function hint in status bar
|
|
|
264 |
if (ca && [ca count]==1) {
|
|
|
265 |
|
|
|
266 |
NSString *foundItem = [ca objectAtIndex:0];
|
|
|
267 |
|
|
|
268 |
// ignore all spaces, equal signs, and opened paranthesis at the end
|
|
|
269 |
BOOL showIt = YES;
|
|
|
270 |
int i = [foundItem length]-1;
|
|
|
271 |
unichar c;
|
|
|
272 |
while(i>0) {
|
|
|
273 |
c = CFStringGetCharacterAtIndex((CFStringRef)foundItem, i);
|
|
|
274 |
if(c == ' ' || c == '(') {
|
|
|
275 |
i--;
|
|
|
276 |
}
|
|
|
277 |
else if (c == '=') {
|
|
|
278 |
// if suggestion ends with a = do not show the hint, since
|
|
|
279 |
// it's a parameter
|
|
|
280 |
showIt = NO;
|
|
|
281 |
break;
|
|
|
282 |
}
|
|
|
283 |
else
|
|
|
284 |
break;
|
|
|
285 |
}
|
|
|
286 |
i++;
|
|
|
287 |
foundItem = [foundItem substringToIndex:i];
|
|
|
288 |
SLog(@" - show function hint '%@' with display %d", foundItem, showIt);
|
|
|
289 |
if(showIt && [[(NSTextView*)[[NSApp keyWindow] firstResponder] delegate] respondsToSelector:@selector(hintForFunction:)])
|
|
|
290 |
// (RController*) is only a dummy to avoid compiler warnings
|
|
|
291 |
[(RController*)[(NSTextView*)[[NSApp keyWindow] firstResponder] delegate] hintForFunction:foundItem];
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
SLog(@" - found %d suggestions", [ca count]);
|
|
|
295 |
[xx release];
|
|
|
296 |
return (ca && [ca count]) ? ca : nil;
|
|
|
297 |
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
return nil;
|
|
|
301 |
|
|
|
302 |
}
|
|
|
303 |
|
| 568 |
iacus |
304 |
@end
|