-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomplete.py
More file actions
293 lines (271 loc) · 12 KB
/
complete.py
File metadata and controls
293 lines (271 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
import rpy2.robjects as robjects
class RCompleter:
def __init__(self, linebuffer="", end=-1):
self.linebuffer = linebuffer
self.end = end
self.start = 0
self.can_suggest = False
self.guess_token()
def guess_token(self):
# first check if we're in a string, then check if we have special chars
# if neither, we can probably just start from the start
if self.linebuffer == "":
self.can_suggest = False
self.start = 0
if self.end < 0:
self.end = len(self.linebuffer)
self.linebuffer = QString(self.linebuffer[0:self.end])
if self.inside_quotes():
# set 'start' to the location of the last quote
regex = QRegExp(r"['\"]")
else:
regex = QRegExp(r"[^\.\w:?$@[\]]+")
self.start = self.linebuffer.lastIndexOf(regex)+1
if self.start < 1:
self.start = 0
self.can_suggest = True
self.token = self.linebuffer[self.start:self.end]
def inside_quotes(self):
# simply count the number of quotes before the current cursor (end)
double = self.linebuffer.count("'")
single = self.linebuffer.count('"')
return ((double % 2) or (single % 2))
def file_completions(self, token = ""):
# uses R's Sys.glob to filter files and path.expand to expand paths
token = unicode(token)
expanded = robjects.r['path.expand'](token)[0]
comps = list(robjects.r['Sys.glob']("%s*" % expanded, dirmark=True))
if not expanded == token:
exp = robjects.r['path.expand']("~")[0]
comps = [comp.replace(exp, "~") for comp in comps]
return comps
def make_regexp_safe(self, token=""):
return QRegExp.escape(token)
def keyword_completions(self, token=""):
regexp = QRegExp(r"^%s" % self.make_regexp_safe(token))
strings = QStringList([
"NULL", "NA", "TRUE", "FALSE", "Inf", "NaN",
"NA_integer_", "NA_real_", "NA_character_", "NA_complex_",
"repeat ", "in ", "next ", "break ", "else "])
return list(strings.filter(regexp))
def attached_package_completions(self, token = ""):
strings = QStringList(list(robjects.r.search()))
strings = strings.filter(QRegExp(r"^package:(%s)" % self.make_regexp_safe(token)))
suffix = robjects.r["$"](robjects.r['rc.options'](),"package.suffix")[0]
return ["%s%s" % (i[8:],suffix) for i in strings]
def normal_completions(self, token="", suffix=None):
if token == "":
return list()
else:
kwargs = {"ignore.case": False}
comps = QStringList(list(robjects.r.apropos("^%s" % self.make_regexp_safe(token), **kwargs)))
function = [robjects.r.exists(unicode(i), mode="function")[0] for i in comps]
if suffix is None:
suffix = robjects.r["$"](robjects.r['rc.options'](),"function.suffix")[0]
for i, item in enumerate(comps):
if function[i]:
comps[i] = "%s%s" % (item,suffix)
#comps = ["%s%s" % (item,suffix) for i, item in enumerate(comps) if function[i] else item]
keys = QStringList(self.keyword_completions(token))
packs = QStringList(self.attached_package_completions(token))
comps << keys << packs
return comps
def help_completions(self, prefix="", suffix=""):
# this one might be slow?
nc = self.match_available_topics(suffix)
if len(nc) < 1:
return list()
return ["%s?%s" % (prefix, i) for i in nc]
def match_available_topics(self, token=""):
def read_aliases(path):
f = robjects.r['file.path'](unicode(path), "help", "aliases.rds")[0]
info = QFileInfo(f)
if info.exists():
return list(robjects.r['.readRDS'](f).names)
else:
f = robjects.r['file.path'](unicode(path), "help", "AnIndex")[0]
info = QFileInfo(f)
if info.exists():
# aliases.rds was introduced before 2.10.0, as can phase this out
return list(list(robjects.r.scan(f, what = list("", ""),
sep = "\t", quote = "", na_strings = "", quiet = True))[0])
else:
return list()
if token == "":
return list()
pkgpaths = QStringList(list(robjects.r.searchpaths()))
pkgs = [QString(i).startsWith('package:') for i in list(robjects.r.search())]
aliases = []
for i, item in enumerate(pkgpaths):
if pkgs[i]:
aliases.extend(read_aliases(item))
aliases = QStringList(aliases)
aliases.removeDuplicates()
return list(aliases.filter(QRegExp(r"^%s" % self.make_regexp_safe(token))))
def special_operations_location(self, token):
regexp = QRegExp(r"([\$@\?\[])")
token = QString(token)
tmp = regexp.lastIndexIn(token)
tmp2 = tmp
tmpcap = regexp.cap()
regexp = QRegExp(r":::")
tmp2 = regexp.lastIndexIn(token)
if tmp2 > tmp:
return (tmp2, regexp.cap())
regexp = QRegExp(r"::")
tmp2 = regexp.lastIndexIn(token)
if tmp2 > tmp:
return (tmp2, regexp.cap())
return (tmp, tmpcap)
def argument_names(self, fname):
args = QString(str(robjects.r('do.call(argsAnywhere, list("%s"))' % fname)))
args = args.remove("NULL").replace(QRegExp(r'^function'), "")
args = args.remove("\n").replace(QRegExp(r"[\(\)]"), "").split(",")
return [arg.trimmed() for arg in args]
def special_function_arguments(self, fun="", text=""):
if fun in ("library", "require"):
regexp = QRegExp(r"^%s" % self.make_regexp_safe(text))
packages = QStringList(list(robjects.r['installed.packages']().rownames))
return list(packages.filter(regexp))
elif fun == "data":
data = robjects.r.data().rx2("results")
data = [data.rx(i,4)[0] for i in range(1,data.nrow)]
return list(QStringList(data).filter(QRegExp(r"^%s" % self.make_regexp_safe(text))))
else:
return list()
def inside_function(self, line = "", cursor = 1, first = False):
line = QString(line[0:cursor])
openBracket = line.count("(")
closeBracket = line.count(")")
wp = openBracket-closeBracket
if wp > 0:
index = line.lastIndexOf("(")
prefix = line[0:index]
suffix = line[index+1:]
if suffix.count(QRegExp(r"[=,]")) < 1:
first = True
tmp = suffix.lastIndexOf("=")
if tmp > 0 and suffix[tmp:].count(",") < 1:
return (None, first)
else: ## guess function name
regexp = QRegExp(r"[^\.\w]")
possible = list(prefix.split(regexp, QString.SkipEmptyParts))
if len(possible) > 0:
return (possible[-1], first)
else:
return (None, first)
else: # not inside function
return (None, first)
def function_arguments(self, fun="", text=""):
add = robjects.r["$"](robjects.r['rc.options'](),"funarg.suffix")[0]
if fun == "" or text == "":
return list()
methods = []
fun = unicode(fun)
specialFunArgs = self.special_function_arguments(fun, text)
if robjects.r.exists(fun, mode = "function")[0]:
methods = list(robjects.r.methods(fun))
methods.insert(0,fun)
allArgs = QStringList()
for func in methods:
allArgs << self.argument_names(func)
allArgs.removeDuplicates()
allArgs = allArgs.filter(QRegExp(r"^%s" % make_regexp_safe(text)))
if len(allArgs) > 0 and not add == "":
allArlgs = allArgs.replaceInStrings(QRegExp(r"*"), "\\1%s" % add)
allArgs << specialFunArgs
return allArgs
return QStringList()
def special_completions(self, text=""):
# spl (locations of matches) is guaranteed to be non-empty
specialOp = self.special_operations_location(text)
opStart = specialOp[0]
op = specialOp[1]
opEnd = opStart+len(op)
if opStart < 0:
return list()
prefix = text[0:opStart]
suffix = text[opEnd:]
if op == "?":
return self.help_completions(prefix, suffix)
def try_evaluate(s):
return robjects.r("try(eval(parse(text = '%s')), silent = TRUE)" % s)
if op == "$":
object = try_evaluate(prefix)
if robjects.r.inherits(object, "try-error")[0]: ## nothing else to do
comps = [suffix]
else:
comps = list(robjects.r['.DollarNames'](object,
pattern = "^%s" % self.make_regexp_safe(suffix)))
elif op == "@":
object = try_evaluate(prefix)
if robjects.r.inherits(object, "try-error")[0]: ## nothing else to do
comps = [suffix]
else:
slots = QStringList(robjects.r('methods::slotNames')(try_evaluate('meuse')))
comps = list(slots.filter(QRegExp(r"^%s" % self.make_regexp_safe(suffix))))
elif op == "::":
nse = robjects.r['try'](robjects.r.getNamespaceExports(prefix), silent = True)
if robjects.r.inherits(nse, "try-error")[0]: ## nothing else to do
comps = [suffix]
else:
comps = list(QStringList(list(nse)).filter(QRegExp(r"^%s" % self.make_regexp_safe(suffix))))
elif op == ":::":
ns = robjects.r['try'](robjects.r.getNamespace(prefix), silent = True)
if robjects.r.inherits(ns, "try-error")[0]: ## nothing else to do
comps = [suffix]
else:
comps = robjects.r.ls(ns, all_names = True, pattern = "^%s" % self.make_regexp_safe(suffix))
#elif op in ("[", "[["):
#comps = normal_completions(suffix)
#if len(comps) < 1:
#comps = [suffix]
else:
comps = [suffix]
if len(comps) < 1:
comps = [""]
return ["%s%s%s" % (prefix, op, comp) for comp in comps]
def complete_token(self):
if self.inside_quotes():
st = self.start
probablyNotFilename = (st > 2L and \
self.linebuffer[st-1] in ("[", ":", "$"))
if probablyNotFilename:
return list()
else:
comps = self.file_completions(self.token)
return comps
else:
comps = QStringList()
first = False
guessedFunction = self.inside_function()
fguess = guessedFunction[0]
first = guessedFunction[1]
if not fguess is None:
comps << QStringList(self.function_arguments(fguess, token))
if first and not fguess == "" and \
fguess in ("library", "require", "data"):
return comps
lastArithOp = QString(self.token).lastIndexOf(QRegExp(r"[\"'^/*+-]"))
haveArithOp = lastArithOp > 0
if haveArithOp:
prefix = token[0:lastArithOp]
text = token[lastArithOp+1:]
comps << QStringList(self.special_completions(self.token))
normal = QStringList(self.normal_completions(self.token))
if haveArithOp and len(normal) > 0:
comps << QStringList(["%s%s" % (prefix, comp) for comp in normal])
else:
comps << normal
return list(comps)
def main():
#robjects.r("test <- data.frame('one'=c(1,2,3,4,5), 'twobaloo'=c(1,2,3,4,5))")
linebuffer = 'save(x, file="/home/cf")'
completer = RCompleter(linebuffer, 22)
if completer.can_suggest:
comps = completer.complete_token()
print comps
if __name__ == '__main__':
main()