]>
git.saurik.com Git - cycript.git/blob - Complete.cpp
fcf8f5dc364e6b4def21964068c067e5e2ca5f5b
1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "cycript.hpp"
27 #include "Replace.hpp"
30 static CYExpression
*ParseExpression(CYPool
&pool
, CYUTF8String code
) {
31 std::stringstream stream
;
32 stream
<< '(' << code
<< ')';
33 CYDriver
driver(pool
, *stream
.rdbuf());
34 if (driver
.Parse() || !driver
.errors_
.empty())
38 CYContext
context(options
);
40 CYStatement
*statement(driver
.script_
->code_
);
41 _assert(statement
!= NULL
);
42 _assert(statement
->next_
== NULL
);
44 CYExpress
*express(dynamic_cast<CYExpress
*>(driver
.script_
->code_
));
45 _assert(express
!= NULL
);
47 CYParenthetical
*parenthetical(dynamic_cast<CYParenthetical
*>(express
->expression_
));
48 _assert(parenthetical
!= NULL
);
50 return parenthetical
->expression_
;
53 _visible
char **CYComplete(const char *word
, const std::string
&line
, CYUTF8String (*run
)(CYPool
&pool
, const std::string
&)) {
56 std::stringbuf
stream(line
);
57 CYDriver
driver(pool
, stream
);
61 if (driver
.Parse() || !driver
.errors_
.empty())
64 if (driver
.mode_
== CYDriver::AutoNone
)
67 CYExpression
*expression
;
70 CYContext
context(options
);
72 std::ostringstream prefix
;
74 switch (driver
.mode_
) {
75 case CYDriver::AutoPrimary
:
76 expression
= $
CYThis();
79 case CYDriver::AutoDirect
:
80 expression
= driver
.context_
;
83 case CYDriver::AutoIndirect
:
84 expression
= $
CYIndirect(driver
.context_
);
87 case CYDriver::AutoMessage
: {
88 CYDriver::Context
&thing(driver
.contexts_
.back());
89 expression
= $
M($
C1($
V("object_getClass"), thing
.context_
), $
S("prototype"));
90 for (CYDriver::Context::Words::const_iterator
part(thing
.words_
.begin()); part
!= thing
.words_
.end(); ++part
)
91 prefix
<< (*part
)->word_
<< ':';
94 case CYDriver::AutoResolve
:
95 expression
= $
M(driver
.context_
, $
S("$cyr"));
98 case CYDriver::AutoStruct
:
99 expression
= $
CYThis();
103 case CYDriver::AutoEnum
:
104 expression
= $
CYThis();
112 std::string
begin(prefix
.str());
115 if (driver
.mode_
== CYDriver::AutoMessage
)
116 message
= $
CYTrue();
118 message
= $
CYFalse();
120 driver
.script_
= $
CYScript($
CYExpress($
C4(ParseExpression(pool
,
121 " function(value, prefix, word, message) {\n"
122 " var object = value;\n"
124 " var before = prefix.length;\n"
126 " var entire = prefix.length;\n"
128 " for (var name in object)\n"
129 " if (name.substring(0, entire) == prefix)\n"
130 " names.push(name);\n"
132 " if (object.hasOwnProperty(\"cy$complete\"))\n"
133 " names = names.concat(object.cy$complete.call(value, prefix, message));\n"
135 " var local = Object.getOwnPropertyNames(object);\n"
139 " for (var name of local)\n"
140 " if (name.substring(0, entire) == prefix)\n"
141 " names.push(name);\n"
142 " } while (object = typeof object === 'object' ? Object.getPrototypeOf(object) : object.__proto__);\n"
145 ), expression
, $
S(begin
.c_str()), $
S(word
), message
)));
147 driver
.script_
->Replace(context
);
150 CYOutput
out(str
, options
);
151 out
<< *driver
.script_
;
153 std::string
code(str
.str());
154 CYUTF8String
json(run(pool
, code
));
155 // XXX: if this fails we should not try to parse it
157 CYExpression
*result(ParseExpression(pool
, json
));
161 CYArray
*array(dynamic_cast<CYArray
*>(result
->Primitive(context
)));
165 // XXX: use an std::set?
166 typedef std::vector
<CYUTF8String
> Completions
;
167 Completions completions
;
172 for (CYElement
*element(array
->elements_
); element
!= NULL
; ) {
173 CYElementValue
*value(dynamic_cast<CYElementValue
*>(element
));
174 _assert(value
!= NULL
);
175 element
= value
->next_
;
177 _assert(value
->value_
!= NULL
);
178 CYString
*string(value
->value_
->String(context
));
180 CYThrow("string was actually %s", typeid(*value
->value_
).name());
182 CYUTF8String completion
;
183 if (string
->size_
!= 0)
184 completion
= {string
->value_
, string
->size_
};
185 else if (driver
.mode_
== CYDriver::AutoMessage
)
190 completion
.data
+= begin
.size();
191 completion
.size
-= begin
.size();
193 if (CYStartsWith(completion
, "$cy"))
195 completions
.push_back(completion
);
201 size_t limit(completion
.size
), size(common
.size());
203 common
= common
.substr(0, limit
);
206 for (limit
= 0; limit
!= size
; ++limit
)
207 if (common
[limit
] != completion
.data
[limit
])
210 common
= common
.substr(0, limit
);
214 size_t count(completions
.size());
218 size_t colon(common
.find(':'));
219 if (colon
!= std::string::npos
)
220 common
= common
.substr(0, colon
+ 1);
221 if (completions
.size() == 1)
224 char **results(reinterpret_cast<char **>(malloc(sizeof(char *) * (count
+ 2))));
226 results
[0] = strdup(common
.c_str());
228 for (Completions::const_iterator
i(completions
.begin()); i
!= completions
.end(); ++i
)
229 results
[++index
] = strdup(i
->data
);
230 results
[count
+ 1] = NULL
;