gd_editor.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369
  1. /*************************************************************************/
  2. /* gd_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gd_compiler.h"
  31. #include "gd_script.h"
  32. #include "global_constants.h"
  33. #include "globals.h"
  34. #include "os/file_access.h"
  35. void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  36. p_delimiters->push_back("#");
  37. p_delimiters->push_back("\"\"\" \"\"\"");
  38. }
  39. void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  40. p_delimiters->push_back("\" \"");
  41. p_delimiters->push_back("' '");
  42. }
  43. String GDScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  44. String _template = String() +
  45. "extends %BASE%\n\n" +
  46. "# class member variables go here, for example:\n" +
  47. "# var a = 2\n" +
  48. "# var b = \"textvar\"\n\n" +
  49. "func _ready():\n" +
  50. "\t# Called every time the node is added to the scene.\n" +
  51. "\t# Initialization here\n" +
  52. "\tpass\n";
  53. return _template.replace("%BASE%", p_base_class_name);
  54. }
  55. bool GDScriptLanguage::validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions) const {
  56. GDParser parser;
  57. Error err = parser.parse(p_script, p_path.get_base_dir(), true, p_path);
  58. if (err) {
  59. r_line_error = parser.get_error_line();
  60. r_col_error = parser.get_error_column();
  61. r_test_error = parser.get_error();
  62. return false;
  63. } else {
  64. const GDParser::Node *root = parser.get_parse_tree();
  65. ERR_FAIL_COND_V(root->type != GDParser::Node::TYPE_CLASS, false);
  66. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode *>(root);
  67. Map<int, String> funcs;
  68. for (int i = 0; i < cl->functions.size(); i++) {
  69. funcs[cl->functions[i]->line] = cl->functions[i]->name;
  70. }
  71. for (int i = 0; i < cl->static_functions.size(); i++) {
  72. funcs[cl->static_functions[i]->line] = cl->static_functions[i]->name;
  73. }
  74. for (Map<int, String>::Element *E = funcs.front(); E; E = E->next()) {
  75. r_functions->push_back(E->get() + ":" + itos(E->key()));
  76. }
  77. }
  78. return true;
  79. }
  80. bool GDScriptLanguage::has_named_classes() const {
  81. return false;
  82. }
  83. int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  84. GDTokenizerText tokenizer;
  85. tokenizer.set_code(p_code);
  86. int indent = 0;
  87. while (tokenizer.get_token() != GDTokenizer::TK_EOF && tokenizer.get_token() != GDTokenizer::TK_ERROR) {
  88. if (tokenizer.get_token() == GDTokenizer::TK_NEWLINE) {
  89. indent = tokenizer.get_token_line_indent();
  90. }
  91. //print_line("TOKEN: "+String(GDTokenizer::get_token_name(tokenizer.get_token())));
  92. if (indent == 0 && tokenizer.get_token() == GDTokenizer::TK_PR_FUNCTION && tokenizer.get_token(1) == GDTokenizer::TK_IDENTIFIER) {
  93. String identifier = tokenizer.get_token_identifier(1);
  94. if (identifier == p_function) {
  95. return tokenizer.get_token_line();
  96. }
  97. }
  98. tokenizer.advance();
  99. //print_line("NEXT: "+String(GDTokenizer::get_token_name(tokenizer.get_token())));
  100. }
  101. return -1;
  102. }
  103. Script *GDScriptLanguage::create_script() const {
  104. return memnew(GDScript);
  105. }
  106. /* DEBUGGER FUNCTIONS */
  107. bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
  108. //break because of parse error
  109. if (ScriptDebugger::get_singleton() && Thread::get_caller_ID() == Thread::get_main_ID()) {
  110. _debug_parse_err_line = p_line;
  111. _debug_parse_err_file = p_file;
  112. _debug_error = p_error;
  113. ScriptDebugger::get_singleton()->debug(this, false);
  114. return true;
  115. } else {
  116. return false;
  117. }
  118. }
  119. bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
  120. if (ScriptDebugger::get_singleton() && Thread::get_caller_ID() == Thread::get_main_ID()) {
  121. _debug_parse_err_line = -1;
  122. _debug_parse_err_file = "";
  123. _debug_error = p_error;
  124. ScriptDebugger::get_singleton()->debug(this, p_allow_continue);
  125. return true;
  126. } else {
  127. return false;
  128. }
  129. }
  130. String GDScriptLanguage::debug_get_error() const {
  131. return _debug_error;
  132. }
  133. int GDScriptLanguage::debug_get_stack_level_count() const {
  134. if (_debug_parse_err_line >= 0)
  135. return 1;
  136. return _debug_call_stack_pos;
  137. }
  138. int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
  139. if (_debug_parse_err_line >= 0)
  140. return _debug_parse_err_line;
  141. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, -1);
  142. int l = _debug_call_stack_pos - p_level - 1;
  143. return *(_call_stack[l].line);
  144. }
  145. String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
  146. if (_debug_parse_err_line >= 0)
  147. return "";
  148. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  149. int l = _debug_call_stack_pos - p_level - 1;
  150. return _call_stack[l].function->get_name();
  151. }
  152. String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
  153. if (_debug_parse_err_line >= 0)
  154. return _debug_parse_err_file;
  155. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  156. int l = _debug_call_stack_pos - p_level - 1;
  157. return _call_stack[l].function->get_source();
  158. }
  159. void GDScriptLanguage::debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  160. if (_debug_parse_err_line >= 0)
  161. return;
  162. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  163. int l = _debug_call_stack_pos - p_level - 1;
  164. GDFunction *f = _call_stack[l].function;
  165. List<Pair<StringName, int> > locals;
  166. f->debug_get_stack_member_state(*_call_stack[l].line, &locals);
  167. for (List<Pair<StringName, int> >::Element *E = locals.front(); E; E = E->next()) {
  168. p_locals->push_back(E->get().first);
  169. p_values->push_back(_call_stack[l].stack[E->get().second]);
  170. }
  171. }
  172. void GDScriptLanguage::debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  173. if (_debug_parse_err_line >= 0)
  174. return;
  175. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  176. int l = _debug_call_stack_pos - p_level - 1;
  177. GDInstance *instance = _call_stack[l].instance;
  178. if (!instance)
  179. return;
  180. Ref<GDScript> script = instance->get_script();
  181. ERR_FAIL_COND(script.is_null());
  182. const Map<StringName, GDScript::MemberInfo> &mi = script->debug_get_member_indices();
  183. for (const Map<StringName, GDScript::MemberInfo>::Element *E = mi.front(); E; E = E->next()) {
  184. p_members->push_back(E->key());
  185. p_values->push_back(instance->debug_get_member_by_index(E->get().index));
  186. }
  187. }
  188. ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) {
  189. ERR_FAIL_COND_V(_debug_parse_err_line >= 0, NULL);
  190. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, NULL);
  191. int l = _debug_call_stack_pos - p_level - 1;
  192. GDInstance *instance = _call_stack[l].instance;
  193. return instance;
  194. }
  195. void GDScriptLanguage::debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  196. const Map<StringName, int> &name_idx = GDScriptLanguage::get_singleton()->get_global_map();
  197. const Variant *globals = GDScriptLanguage::get_singleton()->get_global_array();
  198. for (const Map<StringName, int>::Element *E = name_idx.front(); E; E = E->next()) {
  199. if (ObjectTypeDB::type_exists(E->key()) || Globals::get_singleton()->has_singleton(E->key()) || E->key() == "PI")
  200. continue;
  201. const Variant &var = globals[E->value()];
  202. if (Object *obj = var) {
  203. if (obj->cast_to<GDNativeClass>())
  204. continue;
  205. }
  206. bool skip = false;
  207. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  208. if (E->key() == GlobalConstants::get_global_constant_name(i)) {
  209. skip = true;
  210. break;
  211. }
  212. }
  213. if (skip)
  214. continue;
  215. p_globals->push_back(E->key());
  216. p_values->push_back(var);
  217. }
  218. }
  219. String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  220. if (_debug_parse_err_line >= 0)
  221. return "";
  222. return "";
  223. }
  224. void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  225. p_extensions->push_back("gd");
  226. }
  227. void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  228. for (int i = 0; i < GDFunctions::FUNC_MAX; i++) {
  229. p_functions->push_back(GDFunctions::get_info(GDFunctions::Function(i)));
  230. }
  231. //not really "functions", but..
  232. {
  233. MethodInfo mi;
  234. mi.name = "preload:Resource";
  235. mi.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
  236. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
  237. p_functions->push_back(mi);
  238. }
  239. {
  240. MethodInfo mi;
  241. mi.name = "yield:GDFunctionState";
  242. mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  243. mi.arguments.push_back(PropertyInfo(Variant::STRING, "signal"));
  244. mi.default_arguments.push_back(Variant::NIL);
  245. mi.default_arguments.push_back(Variant::STRING);
  246. p_functions->push_back(mi);
  247. }
  248. {
  249. MethodInfo mi;
  250. mi.name = "assert";
  251. mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition"));
  252. p_functions->push_back(mi);
  253. }
  254. }
  255. void GDScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  256. Pair<String, Variant> pi;
  257. pi.first = "PI";
  258. pi.second = Math_PI;
  259. p_constants->push_back(pi);
  260. }
  261. String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const StringArray &p_args) const {
  262. String s = "func " + p_name + "(";
  263. if (p_args.size()) {
  264. s += " ";
  265. for (int i = 0; i < p_args.size(); i++) {
  266. if (i > 0)
  267. s += ", ";
  268. s += p_args[i];
  269. }
  270. s += " ";
  271. }
  272. s += "):\n\tpass # replace with function body\n";
  273. return s;
  274. }
  275. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  276. struct GDCompletionIdentifier {
  277. StringName obj_type;
  278. Ref<GDScript> script;
  279. Variant::Type type;
  280. Variant value; //im case there is a value, also return it
  281. };
  282. static GDCompletionIdentifier _get_type_from_variant(const Variant &p_variant) {
  283. GDCompletionIdentifier t;
  284. t.type = p_variant.get_type();
  285. t.value = p_variant;
  286. if (p_variant.get_type() == Variant::OBJECT) {
  287. Object *obj = p_variant;
  288. if (obj) {
  289. //if (obj->cast_to<GDNativeClass>()) {
  290. // t.obj_type=obj->cast_to<GDNativeClass>()->get_name();
  291. // t.value=Variant();
  292. //} else {
  293. t.obj_type = obj->get_type();
  294. //}
  295. }
  296. }
  297. return t;
  298. }
  299. static GDCompletionIdentifier _get_type_from_pinfo(const PropertyInfo &p_info) {
  300. GDCompletionIdentifier t;
  301. t.type = p_info.type;
  302. if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  303. t.obj_type = p_info.hint_string;
  304. }
  305. return t;
  306. }
  307. struct GDCompletionContext {
  308. const GDParser::ClassNode *_class;
  309. const GDParser::FunctionNode *function;
  310. const GDParser::BlockNode *block;
  311. Object *base;
  312. String base_path;
  313. };
  314. static Ref<Reference> _get_parent_class(GDCompletionContext &context) {
  315. if (context._class->extends_used) {
  316. //do inheritance
  317. String path = context._class->extends_file;
  318. Ref<GDScript> script;
  319. Ref<GDNativeClass> native;
  320. if (path != "") {
  321. //path (and optionally subclasses)
  322. if (path.is_rel_path()) {
  323. path = context.base_path.plus_file(path);
  324. }
  325. if (ScriptCodeCompletionCache::get_sigleton())
  326. script = ScriptCodeCompletionCache::get_sigleton()->get_cached_resource(path);
  327. else
  328. script = ResourceLoader::load(path);
  329. if (script.is_null()) {
  330. return REF();
  331. }
  332. if (!script->is_valid()) {
  333. return REF();
  334. }
  335. //print_line("EXTENDS PATH: "+path+" script is "+itos(script.is_valid())+" indices is "+itos(script->member_indices.size())+" valid? "+itos(script->valid));
  336. if (context._class->extends_class.size()) {
  337. for (int i = 0; i < context._class->extends_class.size(); i++) {
  338. String sub = context._class->extends_class[i];
  339. if (script->get_subclasses().has(sub)) {
  340. script = script->get_subclasses()[sub];
  341. } else {
  342. return REF();
  343. }
  344. }
  345. }
  346. if (script.is_valid())
  347. return script;
  348. } else {
  349. if (context._class->extends_class.size() == 0) {
  350. ERR_PRINT("BUG");
  351. return REF();
  352. }
  353. String base = context._class->extends_class[0];
  354. if (context._class->extends_class.size() > 1) {
  355. return REF();
  356. }
  357. //if not found, try engine classes
  358. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  359. return REF();
  360. }
  361. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  362. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  363. return native;
  364. }
  365. }
  366. return Ref<Reference>();
  367. }
  368. static GDCompletionIdentifier _get_native_class(GDCompletionContext &context) {
  369. //eeh...
  370. GDCompletionIdentifier id;
  371. id.type = Variant::NIL;
  372. REF pc = _get_parent_class(context);
  373. if (!pc.is_valid()) {
  374. return id;
  375. }
  376. Ref<GDNativeClass> nc = pc;
  377. Ref<GDScript> s = pc;
  378. if (s.is_null() && nc.is_null()) {
  379. return id;
  380. }
  381. while (!s.is_null()) {
  382. nc = s->get_native();
  383. s = s->get_base();
  384. }
  385. if (nc.is_null()) {
  386. return id;
  387. }
  388. id.type = Variant::OBJECT;
  389. if (context.base)
  390. id.value = context.base;
  391. id.obj_type = nc->get_name();
  392. return id;
  393. }
  394. static bool _guess_identifier_type(GDCompletionContext &context, int p_line, const StringName &p_identifier, GDCompletionIdentifier &r_type);
  395. static bool _guess_expression_type(GDCompletionContext &context, const GDParser::Node *p_node, int p_line, GDCompletionIdentifier &r_type) {
  396. if (p_node->type == GDParser::Node::TYPE_CONSTANT) {
  397. const GDParser::ConstantNode *cn = static_cast<const GDParser::ConstantNode *>(p_node);
  398. r_type = _get_type_from_variant(cn->value);
  399. return true;
  400. } else if (p_node->type == GDParser::Node::TYPE_DICTIONARY) {
  401. r_type.type = Variant::DICTIONARY;
  402. //what the heck, fill it anyway
  403. const GDParser::DictionaryNode *an = static_cast<const GDParser::DictionaryNode *>(p_node);
  404. Dictionary d;
  405. for (int i = 0; i < an->elements.size(); i++) {
  406. GDCompletionIdentifier k;
  407. if (_guess_expression_type(context, an->elements[i].key, p_line, k) && k.value.get_type() != Variant::NIL) {
  408. GDCompletionIdentifier v;
  409. if (_guess_expression_type(context, an->elements[i].value, p_line, v)) {
  410. d[k.value] = v.value;
  411. }
  412. }
  413. }
  414. r_type.value = d;
  415. return true;
  416. } else if (p_node->type == GDParser::Node::TYPE_ARRAY) {
  417. r_type.type = Variant::ARRAY;
  418. //what the heck, fill it anyway
  419. const GDParser::ArrayNode *an = static_cast<const GDParser::ArrayNode *>(p_node);
  420. Array arr;
  421. arr.resize(an->elements.size());
  422. for (int i = 0; i < an->elements.size(); i++) {
  423. GDCompletionIdentifier ci;
  424. if (_guess_expression_type(context, an->elements[i], p_line, ci)) {
  425. arr[i] = ci.value;
  426. }
  427. }
  428. r_type.value = arr;
  429. return true;
  430. } else if (p_node->type == GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  431. MethodInfo mi = GDFunctions::get_info(static_cast<const GDParser::BuiltInFunctionNode *>(p_node)->function);
  432. r_type = _get_type_from_pinfo(mi.return_val);
  433. return true;
  434. } else if (p_node->type == GDParser::Node::TYPE_IDENTIFIER) {
  435. return _guess_identifier_type(context, p_line - 1, static_cast<const GDParser::IdentifierNode *>(p_node)->name, r_type);
  436. } else if (p_node->type == GDParser::Node::TYPE_SELF) {
  437. //eeh...
  438. r_type = _get_native_class(context);
  439. return r_type.type != Variant::NIL;
  440. } else if (p_node->type == GDParser::Node::TYPE_OPERATOR) {
  441. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(p_node);
  442. if (op->op == GDParser::OperatorNode::OP_CALL) {
  443. if (op->arguments[0]->type == GDParser::Node::TYPE_TYPE) {
  444. const GDParser::TypeNode *tn = static_cast<const GDParser::TypeNode *>(op->arguments[0]);
  445. r_type.type = tn->vtype;
  446. return true;
  447. } else if (op->arguments[0]->type == GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  448. const GDParser::BuiltInFunctionNode *bin = static_cast<const GDParser::BuiltInFunctionNode *>(op->arguments[0]);
  449. return _guess_expression_type(context, bin, p_line, r_type);
  450. } else if (op->arguments.size() > 1 && op->arguments[1]->type == GDParser::Node::TYPE_IDENTIFIER) {
  451. GDCompletionIdentifier base;
  452. if (!_guess_expression_type(context, op->arguments[0], p_line, base))
  453. return false;
  454. StringName id = static_cast<const GDParser::IdentifierNode *>(op->arguments[1])->name;
  455. if (base.type == Variant::OBJECT) {
  456. if (id.operator String() == "new" && base.value.get_type() == Variant::OBJECT) {
  457. Object *obj = base.value;
  458. if (obj && obj->cast_to<GDNativeClass>()) {
  459. GDNativeClass *gdnc = obj->cast_to<GDNativeClass>();
  460. r_type.type = Variant::OBJECT;
  461. r_type.value = Variant();
  462. r_type.obj_type = gdnc->get_name();
  463. return true;
  464. }
  465. }
  466. if (ObjectTypeDB::has_method(base.obj_type, id)) {
  467. #ifdef TOOLS_ENABLED
  468. MethodBind *mb = ObjectTypeDB::get_method(base.obj_type, id);
  469. PropertyInfo pi = mb->get_argument_info(-1);
  470. //try calling the function if constant and all args are constant, should not crash..
  471. Object *baseptr = base.value;
  472. if (mb->is_const() && pi.type == Variant::OBJECT) {
  473. bool all_valid = true;
  474. Vector<Variant> args;
  475. for (int i = 2; i < op->arguments.size(); i++) {
  476. GDCompletionIdentifier arg;
  477. if (_guess_expression_type(context, op->arguments[i], p_line, arg)) {
  478. if (arg.value.get_type() != Variant::NIL && arg.value.get_type() != Variant::OBJECT) { // calling with object seems dangerous, i don' t know
  479. args.push_back(arg.value);
  480. } else {
  481. all_valid = false;
  482. break;
  483. }
  484. } else {
  485. all_valid = false;
  486. }
  487. }
  488. if (all_valid && String(id) == "get_node" && ObjectTypeDB::is_type(base.obj_type, "Node") && args.size()) {
  489. String arg1 = args[0];
  490. if (arg1.begins_with("/root/")) {
  491. String which = arg1.get_slice("/", 2);
  492. if (which != "") {
  493. List<PropertyInfo> props;
  494. Globals::get_singleton()->get_property_list(&props);
  495. //print_line("find singleton");
  496. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  497. String s = E->get().name;
  498. if (!s.begins_with("autoload/"))
  499. continue;
  500. //print_line("found "+s);
  501. String name = s.get_slice("/", 1);
  502. //print_line("name: "+name+", which: "+which);
  503. if (name == which) {
  504. String script = Globals::get_singleton()->get(s);
  505. if (!script.begins_with("res://")) {
  506. script = "res://" + script;
  507. }
  508. if (!script.ends_with(".gd")) {
  509. //not a script, try find the script anyway,
  510. //may have some success
  511. script = script.basename() + ".gd";
  512. }
  513. if (FileAccess::exists(script)) {
  514. //print_line("is a script");
  515. Ref<Script> scr;
  516. if (ScriptCodeCompletionCache::get_sigleton())
  517. scr = ScriptCodeCompletionCache::get_sigleton()->get_cached_resource(script);
  518. else
  519. scr = ResourceLoader::load(script);
  520. r_type.obj_type = "Node";
  521. r_type.type = Variant::OBJECT;
  522. r_type.script = scr;
  523. r_type.value = Variant();
  524. return true;
  525. }
  526. }
  527. }
  528. }
  529. }
  530. }
  531. if (baseptr) {
  532. if (all_valid) {
  533. Vector<const Variant *> argptr;
  534. for (int i = 0; i < args.size(); i++) {
  535. argptr.push_back(&args[i]);
  536. }
  537. Variant::CallError ce;
  538. Variant ret = mb->call(baseptr, argptr.ptr(), argptr.size(), ce);
  539. if (ce.error == Variant::CallError::CALL_OK && ret.get_type() != Variant::NIL) {
  540. if (ret.get_type() != Variant::OBJECT || ret.operator Object *() != NULL) {
  541. r_type = _get_type_from_variant(ret);
  542. return true;
  543. }
  544. }
  545. }
  546. }
  547. }
  548. r_type.type = pi.type;
  549. if (pi.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  550. r_type.obj_type = pi.hint_string;
  551. }
  552. return true;
  553. #else
  554. return false;
  555. #endif
  556. } else {
  557. return false;
  558. }
  559. } else {
  560. //method for some variant..
  561. Variant::CallError ce;
  562. Variant v = Variant::construct(base.type, NULL, 0, ce);
  563. List<MethodInfo> mi;
  564. v.get_method_list(&mi);
  565. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  566. if (!E->get().name.begins_with("_") && E->get().name == id.operator String()) {
  567. MethodInfo mi = E->get();
  568. r_type.type = mi.return_val.type;
  569. if (mi.return_val.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  570. r_type.obj_type = mi.return_val.hint_string;
  571. }
  572. return true;
  573. }
  574. }
  575. }
  576. }
  577. } else if (op->op == GDParser::OperatorNode::OP_INDEX || op->op == GDParser::OperatorNode::OP_INDEX_NAMED) {
  578. GDCompletionIdentifier p1;
  579. GDCompletionIdentifier p2;
  580. if (op->op == GDParser::OperatorNode::OP_INDEX_NAMED) {
  581. if (op->arguments[1]->type == GDParser::Node::TYPE_IDENTIFIER) {
  582. String id = static_cast<const GDParser::IdentifierNode *>(op->arguments[1])->name;
  583. p2.type = Variant::STRING;
  584. p2.value = id;
  585. }
  586. } else {
  587. if (op->arguments[1]) {
  588. if (!_guess_expression_type(context, op->arguments[1], p_line, p2)) {
  589. return false;
  590. }
  591. }
  592. }
  593. if (op->arguments[0]->type == GDParser::Node::TYPE_ARRAY) {
  594. const GDParser::ArrayNode *an = static_cast<const GDParser::ArrayNode *>(op->arguments[0]);
  595. if (p2.value.is_num()) {
  596. int index = p2.value;
  597. if (index < 0 || index >= an->elements.size())
  598. return false;
  599. return _guess_expression_type(context, an->elements[index], p_line, r_type);
  600. }
  601. } else if (op->arguments[0]->type == GDParser::Node::TYPE_DICTIONARY) {
  602. const GDParser::DictionaryNode *dn = static_cast<const GDParser::DictionaryNode *>(op->arguments[0]);
  603. if (p2.value.get_type() == Variant::NIL)
  604. return false;
  605. for (int i = 0; i < dn->elements.size(); i++) {
  606. GDCompletionIdentifier k;
  607. if (!_guess_expression_type(context, dn->elements[i].key, p_line, k)) {
  608. return false;
  609. }
  610. if (k.value.get_type() == Variant::NIL)
  611. return false;
  612. if (k.value == p2.value) {
  613. return _guess_expression_type(context, dn->elements[i].value, p_line, r_type);
  614. }
  615. }
  616. } else {
  617. if (op->arguments[0]) {
  618. if (!_guess_expression_type(context, op->arguments[0], p_line, p1)) {
  619. return false;
  620. }
  621. }
  622. if (p1.value.get_type() == Variant::OBJECT) {
  623. //??
  624. } else if (p1.value.get_type() != Variant::NIL) {
  625. bool valid;
  626. Variant ret = p1.value.get(p2.value, &valid);
  627. if (valid) {
  628. r_type = _get_type_from_variant(ret);
  629. return true;
  630. }
  631. } else {
  632. if (p1.type != Variant::NIL) {
  633. Variant::CallError ce;
  634. Variant base = Variant::construct(p1.type, NULL, 0, ce);
  635. bool valid;
  636. Variant ret = base.get(p2.value, &valid);
  637. if (valid) {
  638. r_type = _get_type_from_variant(ret);
  639. return true;
  640. }
  641. }
  642. }
  643. }
  644. } else {
  645. Variant::Operator vop = Variant::OP_MAX;
  646. switch (op->op) {
  647. case GDParser::OperatorNode::OP_ADD: vop = Variant::OP_ADD; break;
  648. case GDParser::OperatorNode::OP_SUB: vop = Variant::OP_SUBSTRACT; break;
  649. case GDParser::OperatorNode::OP_MUL: vop = Variant::OP_MULTIPLY; break;
  650. case GDParser::OperatorNode::OP_DIV: vop = Variant::OP_DIVIDE; break;
  651. case GDParser::OperatorNode::OP_MOD: vop = Variant::OP_MODULE; break;
  652. case GDParser::OperatorNode::OP_SHIFT_LEFT: vop = Variant::OP_SHIFT_LEFT; break;
  653. case GDParser::OperatorNode::OP_SHIFT_RIGHT: vop = Variant::OP_SHIFT_RIGHT; break;
  654. case GDParser::OperatorNode::OP_BIT_AND: vop = Variant::OP_BIT_AND; break;
  655. case GDParser::OperatorNode::OP_BIT_OR: vop = Variant::OP_BIT_OR; break;
  656. case GDParser::OperatorNode::OP_BIT_XOR: vop = Variant::OP_BIT_XOR; break;
  657. default: {}
  658. }
  659. if (vop == Variant::OP_MAX)
  660. return false;
  661. GDCompletionIdentifier p1;
  662. GDCompletionIdentifier p2;
  663. if (op->arguments[0]) {
  664. if (!_guess_expression_type(context, op->arguments[0], p_line, p1)) {
  665. return false;
  666. }
  667. }
  668. if (op->arguments.size() > 1) {
  669. if (!_guess_expression_type(context, op->arguments[1], p_line, p2)) {
  670. return false;
  671. }
  672. }
  673. Variant::CallError ce;
  674. bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
  675. Variant v1 = (v1_use_value) ? p1.value : Variant::construct(p1.type, NULL, 0, ce);
  676. bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
  677. Variant v2 = (v2_use_value) ? p2.value : Variant::construct(p2.type, NULL, 0, ce);
  678. // avoid potential invalid ops
  679. if ((vop == Variant::OP_DIVIDE || vop == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
  680. v2 = 1;
  681. v2_use_value = false;
  682. }
  683. if (vop == Variant::OP_DIVIDE && v2.get_type() == Variant::REAL) {
  684. v2 = 1.0;
  685. v2_use_value = false;
  686. }
  687. Variant r;
  688. bool valid;
  689. Variant::evaluate(vop, v1, v2, r, valid);
  690. if (!valid)
  691. return false;
  692. r_type.type = r.get_type();
  693. if (v1_use_value && v2_use_value)
  694. r_type.value = r;
  695. return true;
  696. }
  697. }
  698. return false;
  699. }
  700. static bool _guess_identifier_type_in_block(GDCompletionContext &context, int p_line, const StringName &p_identifier, GDCompletionIdentifier &r_type) {
  701. const GDParser::Node *last_assign = NULL;
  702. int last_assign_line = -1;
  703. for (int i = 0; i < context.block->statements.size(); i++) {
  704. if (context.block->statements[i]->line > p_line)
  705. continue;
  706. if (context.block->statements[i]->type == GDParser::BlockNode::TYPE_LOCAL_VAR) {
  707. const GDParser::LocalVarNode *lv = static_cast<const GDParser::LocalVarNode *>(context.block->statements[i]);
  708. if (lv->assign && lv->name == p_identifier) {
  709. last_assign = lv->assign;
  710. last_assign_line = context.block->statements[i]->line;
  711. }
  712. }
  713. if (context.block->statements[i]->type == GDParser::BlockNode::TYPE_OPERATOR) {
  714. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(context.block->statements[i]);
  715. if (op->op == GDParser::OperatorNode::OP_ASSIGN) {
  716. if (op->arguments.size() && op->arguments[0]->type == GDParser::Node::TYPE_IDENTIFIER) {
  717. const GDParser::IdentifierNode *id = static_cast<const GDParser::IdentifierNode *>(op->arguments[0]);
  718. if (id->name == p_identifier) {
  719. last_assign = op->arguments[1];
  720. last_assign_line = context.block->statements[i]->line;
  721. }
  722. }
  723. }
  724. }
  725. }
  726. //use the last assignment, (then backwards?)
  727. if (last_assign) {
  728. return _guess_expression_type(context, last_assign, last_assign_line, r_type);
  729. }
  730. return false;
  731. }
  732. static bool _guess_identifier_from_assignment_in_function(GDCompletionContext &context, int p_src_line, const StringName &p_identifier, const StringName &p_function, GDCompletionIdentifier &r_type) {
  733. const GDParser::FunctionNode *func = NULL;
  734. for (int i = 0; i < context._class->functions.size(); i++) {
  735. if (context._class->functions[i]->name == p_function) {
  736. func = context._class->functions[i];
  737. break;
  738. }
  739. }
  740. if (!func)
  741. return false;
  742. for (int i = 0; i < func->body->statements.size(); i++) {
  743. if (func->body->statements[i]->line == p_src_line) {
  744. break;
  745. }
  746. if (func->body->statements[i]->type == GDParser::BlockNode::TYPE_OPERATOR) {
  747. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(func->body->statements[i]);
  748. if (op->op == GDParser::OperatorNode::OP_ASSIGN) {
  749. if (op->arguments.size() && op->arguments[0]->type == GDParser::Node::TYPE_IDENTIFIER) {
  750. const GDParser::IdentifierNode *id = static_cast<const GDParser::IdentifierNode *>(op->arguments[0]);
  751. if (id->name == p_identifier) {
  752. return _guess_expression_type(context, op->arguments[1], func->body->statements[i]->line, r_type);
  753. }
  754. }
  755. }
  756. }
  757. }
  758. return false;
  759. }
  760. static bool _guess_identifier_type(GDCompletionContext &context, int p_line, const StringName &p_identifier, GDCompletionIdentifier &r_type) {
  761. //go to block first
  762. const GDParser::BlockNode *block = context.block;
  763. while (block) {
  764. GDCompletionContext c = context;
  765. c.block = block;
  766. if (_guess_identifier_type_in_block(c, p_line, p_identifier, r_type)) {
  767. return true;
  768. }
  769. block = block->parent_block;
  770. }
  771. //guess from argument if virtual
  772. if (context.function && context.function->name != StringName()) {
  773. int argindex = -1;
  774. for (int i = 0; i < context.function->arguments.size(); i++) {
  775. if (context.function->arguments[i] == p_identifier) {
  776. argindex = i;
  777. break;
  778. }
  779. }
  780. if (argindex != -1) {
  781. GDCompletionIdentifier id = _get_native_class(context);
  782. if (id.type == Variant::OBJECT && id.obj_type != StringName()) {
  783. //this kinda sucks but meh
  784. List<MethodInfo> vmethods;
  785. ObjectTypeDB::get_virtual_methods(id.obj_type, &vmethods);
  786. for (List<MethodInfo>::Element *E = vmethods.front(); E; E = E->next()) {
  787. if (E->get().name == context.function->name && argindex < E->get().arguments.size()) {
  788. PropertyInfo arg = E->get().arguments[argindex];
  789. int scp = arg.name.find(":");
  790. if (scp != -1) {
  791. r_type.type = Variant::OBJECT;
  792. r_type.obj_type = arg.name.substr(scp + 1, arg.name.length());
  793. return true;
  794. } else {
  795. r_type.type = arg.type;
  796. if (arg.hint == PROPERTY_HINT_RESOURCE_TYPE)
  797. r_type.obj_type = arg.hint_string;
  798. return true;
  799. }
  800. }
  801. }
  802. }
  803. }
  804. }
  805. //guess type in constant
  806. for (int i = 0; i < context._class->constant_expressions.size(); i++) {
  807. if (context._class->constant_expressions[i].identifier == p_identifier) {
  808. ERR_FAIL_COND_V(context._class->constant_expressions[i].expression->type != GDParser::Node::TYPE_CONSTANT, false);
  809. r_type = _get_type_from_variant(static_cast<const GDParser::ConstantNode *>(context._class->constant_expressions[i].expression)->value);
  810. return true;
  811. }
  812. }
  813. if (!(context.function && context.function->_static)) {
  814. for (int i = 0; i < context._class->variables.size(); i++) {
  815. if (context._class->variables[i].identifier == p_identifier) {
  816. if (context._class->variables[i]._export.type != Variant::NIL) {
  817. r_type = _get_type_from_pinfo(context._class->variables[i]._export);
  818. return true;
  819. } else if (context._class->variables[i].expression) {
  820. bool rtype = _guess_expression_type(context, context._class->variables[i].expression, context._class->variables[i].line, r_type);
  821. if (rtype && r_type.type != Variant::NIL)
  822. return true;
  823. //return _guess_expression_type(context,context._class->variables[i].expression,context._class->variables[i].line,r_type);
  824. }
  825. //try to guess from assignment in construtor or _ready
  826. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_ready", r_type))
  827. return true;
  828. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_enter_tree", r_type))
  829. return true;
  830. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_init", r_type))
  831. return true;
  832. return false;
  833. }
  834. }
  835. }
  836. //autoloads as singletons
  837. List<PropertyInfo> props;
  838. Globals::get_singleton()->get_property_list(&props);
  839. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  840. String s = E->get().name;
  841. if (!s.begins_with("autoload/"))
  842. continue;
  843. String name = s.get_slice("/", 1);
  844. if (name == String(p_identifier)) {
  845. String path = Globals::get_singleton()->get(s);
  846. if (path.begins_with("*")) {
  847. String script = path.substr(1, path.length());
  848. if (!script.ends_with(".gd")) {
  849. //not a script, try find the script anyway,
  850. //may have some success
  851. script = script.basename() + ".gd";
  852. }
  853. if (FileAccess::exists(script)) {
  854. //print_line("is a script");
  855. Ref<Script> scr;
  856. if (ScriptCodeCompletionCache::get_sigleton())
  857. scr = ScriptCodeCompletionCache::get_sigleton()->get_cached_resource(script);
  858. else
  859. scr = ResourceLoader::load(script);
  860. r_type.obj_type = "Node";
  861. r_type.type = Variant::OBJECT;
  862. r_type.script = scr;
  863. r_type.value = Variant();
  864. return true;
  865. }
  866. }
  867. }
  868. }
  869. //global
  870. for (Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  871. if (E->key() == p_identifier) {
  872. r_type = _get_type_from_variant(GDScriptLanguage::get_singleton()->get_global_array()[E->get()]);
  873. return true;
  874. }
  875. }
  876. return false;
  877. }
  878. static void _find_identifiers_in_block(GDCompletionContext &context, int p_line, bool p_only_functions, Set<String> &result) {
  879. if (p_only_functions)
  880. return;
  881. for (int i = 0; i < context.block->statements.size(); i++) {
  882. if (context.block->statements[i]->line > p_line)
  883. continue;
  884. if (context.block->statements[i]->type == GDParser::BlockNode::TYPE_LOCAL_VAR) {
  885. const GDParser::LocalVarNode *lv = static_cast<const GDParser::LocalVarNode *>(context.block->statements[i]);
  886. result.insert(lv->name.operator String());
  887. }
  888. }
  889. }
  890. static void _find_identifiers_in_class(GDCompletionContext &context, bool p_static, bool p_only_functions, Set<String> &result) {
  891. if (!p_static && !p_only_functions) {
  892. for (int i = 0; i < context._class->variables.size(); i++) {
  893. result.insert(context._class->variables[i].identifier);
  894. }
  895. }
  896. if (!p_only_functions) {
  897. for (int i = 0; i < context._class->constant_expressions.size(); i++) {
  898. result.insert(context._class->constant_expressions[i].identifier);
  899. }
  900. for (int i = 0; i < context._class->subclasses.size(); i++) {
  901. result.insert(context._class->subclasses[i]->name);
  902. }
  903. }
  904. for (int i = 0; i < context._class->static_functions.size(); i++) {
  905. if (context._class->static_functions[i]->arguments.size())
  906. result.insert(context._class->static_functions[i]->name.operator String() + "(");
  907. else
  908. result.insert(context._class->static_functions[i]->name.operator String() + "()");
  909. }
  910. if (!p_static) {
  911. for (int i = 0; i < context._class->functions.size(); i++) {
  912. if (context._class->functions[i]->arguments.size())
  913. result.insert(context._class->functions[i]->name.operator String() + "(");
  914. else
  915. result.insert(context._class->functions[i]->name.operator String() + "()");
  916. }
  917. }
  918. //globals
  919. Ref<Reference> base = _get_parent_class(context);
  920. while (true) {
  921. Ref<GDScript> script = base;
  922. Ref<GDNativeClass> nc = base;
  923. if (script.is_valid()) {
  924. if (!p_static && !p_only_functions) {
  925. for (const Set<StringName>::Element *E = script->get_members().front(); E; E = E->next()) {
  926. result.insert(E->get().operator String());
  927. }
  928. }
  929. if (!p_only_functions) {
  930. for (const Map<StringName, Variant>::Element *E = script->get_constants().front(); E; E = E->next()) {
  931. result.insert(E->key().operator String());
  932. }
  933. }
  934. for (const Map<StringName, GDFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  935. if (!p_static || E->get()->is_static()) {
  936. if (E->get()->get_argument_count())
  937. result.insert(E->key().operator String() + "(");
  938. else
  939. result.insert(E->key().operator String() + "()");
  940. }
  941. }
  942. if (!p_only_functions) {
  943. for (const Map<StringName, Ref<GDScript> >::Element *E = script->get_subclasses().front(); E; E = E->next()) {
  944. result.insert(E->key().operator String());
  945. }
  946. }
  947. base = script->get_base();
  948. if (base.is_null())
  949. base = script->get_native();
  950. } else if (nc.is_valid()) {
  951. if (!p_only_functions) {
  952. StringName type = nc->get_name();
  953. List<String> constants;
  954. ObjectTypeDB::get_integer_constant_list(type, &constants);
  955. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  956. result.insert(E->get());
  957. }
  958. List<MethodInfo> methods;
  959. ObjectTypeDB::get_method_list(type, &methods);
  960. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  961. if (E->get().name.begins_with("_"))
  962. continue;
  963. if (E->get().arguments.size())
  964. result.insert(E->get().name + "(");
  965. else
  966. result.insert(E->get().name + "()");
  967. }
  968. }
  969. break;
  970. } else
  971. break;
  972. }
  973. }
  974. static void _find_identifiers(GDCompletionContext &context, int p_line, bool p_only_functions, Set<String> &result) {
  975. const GDParser::BlockNode *block = context.block;
  976. if (context.function) {
  977. const GDParser::FunctionNode *f = context.function;
  978. for (int i = 0; i < f->arguments.size(); i++) {
  979. result.insert(f->arguments[i].operator String());
  980. }
  981. }
  982. while (block) {
  983. GDCompletionContext c = context;
  984. c.block = block;
  985. _find_identifiers_in_block(c, p_line, p_only_functions, result);
  986. block = block->parent_block;
  987. }
  988. const GDParser::ClassNode *clss = context._class;
  989. bool _static = context.function && context.function->_static;
  990. while (clss) {
  991. GDCompletionContext c = context;
  992. c._class = clss;
  993. c.block = NULL;
  994. c.function = NULL;
  995. _find_identifiers_in_class(c, _static, p_only_functions, result);
  996. clss = clss->owner;
  997. }
  998. for (int i = 0; i < GDFunctions::FUNC_MAX; i++) {
  999. result.insert(GDFunctions::get_func_name(GDFunctions::Function(i)));
  1000. }
  1001. static const char *_type_names[Variant::VARIANT_MAX] = {
  1002. "null", "bool", "int", "float", "String", "Vector2", "Rect2", "Vector3", "Matrix32", "Plane", "Quat", "AABB", "Matrix3", "Transform",
  1003. "Color", "Image", "NodePath", "RID", "Object", "InputEvent", "Dictionary", "Array", "RawArray", "IntArray", "FloatArray", "StringArray",
  1004. "Vector2Array", "Vector3Array", "ColorArray"
  1005. };
  1006. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1007. result.insert(_type_names[i]);
  1008. }
  1009. //autoload singletons
  1010. List<PropertyInfo> props;
  1011. Globals::get_singleton()->get_property_list(&props);
  1012. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1013. String s = E->get().name;
  1014. if (!s.begins_with("autoload/"))
  1015. continue;
  1016. String name = s.get_slice("/", 1);
  1017. String path = Globals::get_singleton()->get(s);
  1018. if (path.begins_with("*")) {
  1019. result.insert(name);
  1020. }
  1021. }
  1022. for (const Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  1023. result.insert(E->key().operator String());
  1024. }
  1025. }
  1026. static String _get_visual_datatype(const PropertyInfo &p_info, bool p_isarg = true) {
  1027. String n = p_info.name;
  1028. int idx = n.find(":");
  1029. if (idx != -1) {
  1030. return n.substr(idx + 1, n.length());
  1031. }
  1032. if (p_info.type == Variant::OBJECT && p_info.hint == PROPERTY_HINT_RESOURCE_TYPE)
  1033. return p_info.hint_string;
  1034. if (p_info.type == Variant::NIL) {
  1035. if (p_isarg)
  1036. return "var";
  1037. else
  1038. return "void";
  1039. }
  1040. return Variant::get_type_name(p_info.type);
  1041. }
  1042. static void _make_function_hint(const GDParser::FunctionNode *p_func, int p_argidx, String &arghint) {
  1043. arghint = "func " + p_func->name + "(";
  1044. for (int i = 0; i < p_func->arguments.size(); i++) {
  1045. if (i > 0)
  1046. arghint += ", ";
  1047. else
  1048. arghint += " ";
  1049. if (i == p_argidx) {
  1050. arghint += String::chr(0xFFFF);
  1051. }
  1052. arghint += p_func->arguments[i].operator String();
  1053. int deffrom = p_func->arguments.size() - p_func->default_values.size();
  1054. if (i >= deffrom) {
  1055. int defidx = deffrom - i;
  1056. if (defidx >= 0 && defidx < p_func->default_values.size()) {
  1057. if (p_func->default_values[defidx]->type == GDParser::Node::TYPE_OPERATOR) {
  1058. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(p_func->default_values[defidx]);
  1059. if (op->op == GDParser::OperatorNode::OP_ASSIGN) {
  1060. const GDParser::ConstantNode *cn = static_cast<const GDParser::ConstantNode *>(op->arguments[1]);
  1061. arghint += "=" + cn->value.get_construct_string();
  1062. }
  1063. } else {
  1064. }
  1065. }
  1066. }
  1067. if (i == p_argidx) {
  1068. arghint += String::chr(0xFFFF);
  1069. }
  1070. }
  1071. if (p_func->arguments.size() > 0)
  1072. arghint += " ";
  1073. arghint += ")";
  1074. }
  1075. static void _find_type_arguments(const GDParser::Node *p_node, int p_line, const StringName &p_method, const GDCompletionIdentifier &id, int p_argidx, Set<String> &result, String &arghint) {
  1076. //print_line("find type arguments?");
  1077. if (id.type == Variant::INPUT_EVENT && String(p_method) == "is_action" && p_argidx == 0) {
  1078. List<PropertyInfo> pinfo;
  1079. Globals::get_singleton()->get_property_list(&pinfo);
  1080. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1081. const PropertyInfo &pi = E->get();
  1082. if (!pi.name.begins_with("input/"))
  1083. continue;
  1084. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  1085. result.insert("\"" + name + "\"");
  1086. }
  1087. } else if (id.type == Variant::OBJECT && id.obj_type != StringName()) {
  1088. MethodBind *m = ObjectTypeDB::get_method(id.obj_type, p_method);
  1089. if (!m) {
  1090. //not in static method, see script
  1091. //print_line("not in static: "+String(p_method));
  1092. Ref<GDScript> on_script;
  1093. if (id.value.get_type()) {
  1094. Object *obj = id.value;
  1095. if (obj) {
  1096. GDScript *scr = obj->cast_to<GDScript>();
  1097. if (scr) {
  1098. while (scr) {
  1099. for (const Map<StringName, GDFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1100. if (E->get()->is_static() && p_method == E->get()->get_name()) {
  1101. arghint = "static func " + String(p_method) + "(";
  1102. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1103. if (i > 0)
  1104. arghint += ", ";
  1105. else
  1106. arghint += " ";
  1107. if (i == p_argidx) {
  1108. arghint += String::chr(0xFFFF);
  1109. }
  1110. arghint += "var " + E->get()->get_argument_name(i);
  1111. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1112. if (i >= deffrom) {
  1113. int defidx = deffrom - i;
  1114. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1115. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1116. }
  1117. }
  1118. if (i == p_argidx) {
  1119. arghint += String::chr(0xFFFF);
  1120. }
  1121. }
  1122. arghint += ")";
  1123. return; //found
  1124. }
  1125. }
  1126. if (scr->get_base().is_valid())
  1127. scr = scr->get_base().ptr();
  1128. else
  1129. scr = NULL;
  1130. }
  1131. } else {
  1132. on_script = obj->get_script();
  1133. }
  1134. }
  1135. }
  1136. //print_line("but it has a script?");
  1137. if (!on_script.is_valid() && id.script.is_valid()) {
  1138. //print_line("yes");
  1139. on_script = id.script;
  1140. }
  1141. if (on_script.is_valid()) {
  1142. GDScript *scr = on_script.ptr();
  1143. if (scr) {
  1144. while (scr) {
  1145. String code = scr->get_source_code();
  1146. //print_line("has source code!");
  1147. if (code != "") {
  1148. //if there is code, parse it. This way is slower but updates in real-time
  1149. GDParser p;
  1150. //Error parse(const String& p_code, const String& p_base_path="", bool p_just_validate=false,const String& p_self_path="",bool p_for_completion=false);
  1151. Error err = p.parse(scr->get_source_code(), scr->get_path().get_base_dir(), true, "", false);
  1152. if (err == OK) {
  1153. //print_line("checking the functions...");
  1154. //only if ok, otherwise use what is cached on the script
  1155. //GDParser::ClassNode *base = p.
  1156. const GDParser::Node *root = p.get_parse_tree();
  1157. ERR_FAIL_COND(root->type != GDParser::Node::TYPE_CLASS);
  1158. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode *>(root);
  1159. const GDParser::FunctionNode *func = NULL;
  1160. bool st = false;
  1161. for (int i = 0; i < cl->functions.size(); i++) {
  1162. //print_line(String(cl->functions[i]->name)+" vs "+String(p_method));
  1163. if (cl->functions[i]->name == p_method) {
  1164. func = cl->functions[i];
  1165. }
  1166. }
  1167. for (int i = 0; i < cl->static_functions.size(); i++) {
  1168. //print_line(String(cl->static_functions[i]->name)+" vs "+String(p_method));
  1169. if (cl->static_functions[i]->name == p_method) {
  1170. func = cl->static_functions[i];
  1171. st = true;
  1172. }
  1173. }
  1174. if (func) {
  1175. arghint = "func " + String(p_method) + "(";
  1176. if (st)
  1177. arghint = "static " + arghint;
  1178. for (int i = 0; i < func->arguments.size(); i++) {
  1179. if (i > 0)
  1180. arghint += ", ";
  1181. else
  1182. arghint += " ";
  1183. if (i == p_argidx) {
  1184. arghint += String::chr(0xFFFF);
  1185. }
  1186. arghint += "var " + String(func->arguments[i]);
  1187. int deffrom = func->arguments.size() - func->default_values.size();
  1188. if (i >= deffrom) {
  1189. int defidx = deffrom - i;
  1190. if (defidx >= 0 && defidx < func->default_values.size() && func->default_values[defidx]->type == GDParser::Node::TYPE_OPERATOR) {
  1191. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(func->default_values[defidx]);
  1192. if (op->op == GDParser::OperatorNode::OP_ASSIGN) {
  1193. const GDParser::ConstantNode *cn = static_cast<const GDParser::ConstantNode *>(op->arguments[1]);
  1194. arghint += "=" + cn->value.get_construct_string();
  1195. }
  1196. }
  1197. }
  1198. if (i == p_argidx) {
  1199. arghint += String::chr(0xFFFF);
  1200. }
  1201. }
  1202. arghint += " )";
  1203. return;
  1204. }
  1205. } else {
  1206. //print_line("failed parsing?");
  1207. code = "";
  1208. }
  1209. }
  1210. if (code == "") {
  1211. for (const Map<StringName, GDFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1212. if (p_method == E->get()->get_name()) {
  1213. arghint = "func " + String(p_method) + "(";
  1214. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1215. if (i > 0)
  1216. arghint += ", ";
  1217. else
  1218. arghint += " ";
  1219. if (i == p_argidx) {
  1220. arghint += String::chr(0xFFFF);
  1221. }
  1222. arghint += "var " + E->get()->get_argument_name(i);
  1223. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1224. if (i >= deffrom) {
  1225. int defidx = deffrom - i;
  1226. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1227. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1228. }
  1229. }
  1230. if (i == p_argidx) {
  1231. arghint += String::chr(0xFFFF);
  1232. }
  1233. }
  1234. arghint += ")";
  1235. return; //found
  1236. }
  1237. }
  1238. #if 0
  1239. //use class directly, no code was found
  1240. if (!isfunction) {
  1241. for (const Map<StringName,Variant>::Element *E=scr->get_constants().front();E;E=E->next()) {
  1242. options.insert(E->key());
  1243. }
  1244. }
  1245. for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
  1246. options.insert(String(E->key())+"(");
  1247. }
  1248. for (const Set<StringName>::Element *E=scr->get_members().front();E;E=E->next()) {
  1249. options.insert(E->get());
  1250. }
  1251. #endif
  1252. }
  1253. if (scr->get_base().is_valid())
  1254. scr = scr->get_base().ptr();
  1255. else
  1256. scr = NULL;
  1257. }
  1258. }
  1259. }
  1260. } else {
  1261. //regular method
  1262. if (p_method.operator String() == "connect" || (p_method.operator String() == "emit_signal" && p_argidx == 0)) {
  1263. if (p_argidx == 0) {
  1264. List<MethodInfo> sigs;
  1265. ObjectTypeDB::get_signal_list(id.obj_type, &sigs);
  1266. for (List<MethodInfo>::Element *E = sigs.front(); E; E = E->next()) {
  1267. result.insert("\"" + E->get().name + "\"");
  1268. }
  1269. }
  1270. /*if (p_argidx==2) {
  1271. ERR_FAIL_COND(p_node->type!=GDParser::Node::TYPE_OPERATOR);
  1272. const GDParser::OperatorNode *op=static_cast<const GDParser::OperatorNode *>(p_node);
  1273. if (op->arguments.size()>)
  1274. }*/
  1275. } else {
  1276. if (p_argidx == 0 && (String(p_method) == "get_node" || String(p_method) == "has_node") && ObjectTypeDB::is_type(id.obj_type, "Node")) {
  1277. List<PropertyInfo> props;
  1278. Globals::get_singleton()->get_property_list(&props);
  1279. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1280. String s = E->get().name;
  1281. if (!s.begins_with("autoload/"))
  1282. continue;
  1283. // print_line("found "+s);
  1284. String name = s.get_slice("/", 1);
  1285. result.insert("\"/root/" + name + "\"");
  1286. }
  1287. }
  1288. Object *obj = id.value;
  1289. if (obj) {
  1290. List<String> options;
  1291. obj->get_argument_options(p_method, p_argidx, &options);
  1292. for (List<String>::Element *E = options.front(); E; E = E->next()) {
  1293. result.insert(E->get());
  1294. }
  1295. }
  1296. }
  1297. arghint = _get_visual_datatype(m->get_argument_info(-1), false) + " " + p_method.operator String() + String("(");
  1298. for (int i = 0; i < m->get_argument_count(); i++) {
  1299. if (i > 0)
  1300. arghint += ", ";
  1301. else
  1302. arghint += " ";
  1303. if (i == p_argidx) {
  1304. arghint += String::chr(0xFFFF);
  1305. }
  1306. String n = m->get_argument_info(i).name;
  1307. int dp = n.find(":");
  1308. if (dp != -1)
  1309. n = n.substr(0, dp);
  1310. arghint += _get_visual_datatype(m->get_argument_info(i)) + " " + n;
  1311. int deffrom = m->get_argument_count() - m->get_default_argument_count();
  1312. if (i >= deffrom) {
  1313. int defidx = i - deffrom;
  1314. if (defidx >= 0 && defidx < m->get_default_argument_count()) {
  1315. Variant v = m->get_default_argument(i);
  1316. arghint += "=" + v.get_construct_string();
  1317. }
  1318. }
  1319. if (i == p_argidx) {
  1320. arghint += String::chr(0xFFFF);
  1321. }
  1322. }
  1323. if (m->get_argument_count() > 0)
  1324. arghint += " ";
  1325. arghint += ")";
  1326. }
  1327. }
  1328. }
  1329. static void _find_call_arguments(GDCompletionContext &context, const GDParser::Node *p_node, int p_line, int p_argidx, Set<String> &result, String &arghint) {
  1330. if (!p_node || p_node->type != GDParser::Node::TYPE_OPERATOR) {
  1331. return;
  1332. }
  1333. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(p_node);
  1334. if (op->op != GDParser::OperatorNode::OP_CALL) {
  1335. return;
  1336. }
  1337. if (op->arguments[0]->type == GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  1338. //complete built-in function
  1339. const GDParser::BuiltInFunctionNode *fn = static_cast<const GDParser::BuiltInFunctionNode *>(op->arguments[0]);
  1340. MethodInfo mi = GDFunctions::get_info(fn->function);
  1341. arghint = _get_visual_datatype(mi.return_val, false) + " " + GDFunctions::get_func_name(fn->function) + String("(");
  1342. for (int i = 0; i < mi.arguments.size(); i++) {
  1343. if (i > 0)
  1344. arghint += ", ";
  1345. else
  1346. arghint += " ";
  1347. if (i == p_argidx) {
  1348. arghint += String::chr(0xFFFF);
  1349. }
  1350. arghint += _get_visual_datatype(mi.arguments[i]) + " " + mi.arguments[i].name;
  1351. if (i == p_argidx) {
  1352. arghint += String::chr(0xFFFF);
  1353. }
  1354. }
  1355. if (mi.arguments.size() > 0)
  1356. arghint += " ";
  1357. arghint += ")";
  1358. } else if (op->arguments[0]->type == GDParser::Node::TYPE_TYPE) {
  1359. //complete constructor
  1360. const GDParser::TypeNode *tn = static_cast<const GDParser::TypeNode *>(op->arguments[0]);
  1361. List<MethodInfo> mil;
  1362. Variant::get_constructor_list(tn->vtype, &mil);
  1363. for (List<MethodInfo>::Element *E = mil.front(); E; E = E->next()) {
  1364. MethodInfo mi = E->get();
  1365. if (mi.arguments.size() == 0)
  1366. continue;
  1367. if (E->prev())
  1368. arghint += "\n";
  1369. arghint += Variant::get_type_name(tn->vtype) + " " + Variant::get_type_name(tn->vtype) + String("(");
  1370. for (int i = 0; i < mi.arguments.size(); i++) {
  1371. if (i > 0)
  1372. arghint += ", ";
  1373. else
  1374. arghint += " ";
  1375. if (i == p_argidx) {
  1376. arghint += String::chr(0xFFFF);
  1377. }
  1378. arghint += _get_visual_datatype(mi.arguments[i]) + " " + mi.arguments[i].name;
  1379. if (i == p_argidx) {
  1380. arghint += String::chr(0xFFFF);
  1381. }
  1382. }
  1383. if (mi.arguments.size() > 0)
  1384. arghint += " ";
  1385. arghint += ")";
  1386. }
  1387. } else if (op->arguments.size() >= 2 && op->arguments[1]->type == GDParser::Node::TYPE_IDENTIFIER) {
  1388. //make sure identifier exists...
  1389. const GDParser::IdentifierNode *id = static_cast<const GDParser::IdentifierNode *>(op->arguments[1]);
  1390. if (op->arguments[0]->type == GDParser::Node::TYPE_SELF) {
  1391. //self, look up
  1392. for (int i = 0; i < context._class->static_functions.size(); i++) {
  1393. if (context._class->static_functions[i]->name == id->name) {
  1394. _make_function_hint(context._class->static_functions[i], p_argidx, arghint);
  1395. return;
  1396. }
  1397. }
  1398. if (context.function && !context.function->_static) {
  1399. for (int i = 0; i < context._class->functions.size(); i++) {
  1400. if (context._class->functions[i]->name == id->name) {
  1401. _make_function_hint(context._class->functions[i], p_argidx, arghint);
  1402. return;
  1403. }
  1404. }
  1405. }
  1406. Ref<Reference> base = _get_parent_class(context);
  1407. while (true) {
  1408. Ref<GDScript> script = base;
  1409. Ref<GDNativeClass> nc = base;
  1410. if (script.is_valid()) {
  1411. for (const Map<StringName, GDFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  1412. if (E->key() == id->name) {
  1413. if (context.function && context.function->_static && !E->get()->is_static())
  1414. continue;
  1415. arghint = "func " + id->name.operator String() + String("(");
  1416. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1417. if (i > 0)
  1418. arghint += ", ";
  1419. else
  1420. arghint += " ";
  1421. if (i == p_argidx) {
  1422. arghint += String::chr(0xFFFF);
  1423. }
  1424. arghint += E->get()->get_argument_name(i);
  1425. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1426. if (i >= deffrom) {
  1427. int defidx = deffrom - i;
  1428. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1429. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1430. }
  1431. }
  1432. if (i == p_argidx) {
  1433. arghint += String::chr(0xFFFF);
  1434. }
  1435. }
  1436. if (E->get()->get_argument_count() > 0)
  1437. arghint += " ";
  1438. arghint += ")";
  1439. return;
  1440. }
  1441. }
  1442. base = script->get_base();
  1443. if (base.is_null())
  1444. base = script->get_native();
  1445. } else if (nc.is_valid()) {
  1446. if (context.function && !context.function->_static) {
  1447. GDCompletionIdentifier ci;
  1448. ci.type = Variant::OBJECT;
  1449. ci.obj_type = nc->get_name();
  1450. if (!context._class->owner)
  1451. ci.value = context.base;
  1452. _find_type_arguments(p_node, p_line, id->name, ci, p_argidx, result, arghint);
  1453. //guess type..
  1454. /*
  1455. List<MethodInfo> methods;
  1456. ObjectTypeDB::get_method_list(type,&methods);
  1457. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1458. //if (E->get().arguments.size())
  1459. // result.insert(E->get().name+"(");
  1460. //else
  1461. // result.insert(E->get().name+"()");
  1462. }*/
  1463. }
  1464. break;
  1465. } else
  1466. break;
  1467. }
  1468. } else {
  1469. //indexed lookup
  1470. GDCompletionIdentifier ci;
  1471. if (_guess_expression_type(context, op->arguments[0], p_line, ci)) {
  1472. _find_type_arguments(p_node, p_line, id->name, ci, p_argidx, result, arghint);
  1473. return;
  1474. }
  1475. }
  1476. }
  1477. #if 0
  1478. bool _static=context.function->_static;
  1479. for(int i=0;i<context._class->static_functions.size();i++) {
  1480. if (context._class->static_functions[i]->arguments.size())
  1481. result.insert(context._class->static_functions[i]->name.operator String()+"(");
  1482. else
  1483. result.insert(context._class->static_functions[i]->name.operator String()+"()");
  1484. }
  1485. if (!p_static) {
  1486. for(int i=0;i<context._class->functions.size();i++) {
  1487. if (context._class->functions[i]->arguments.size())
  1488. result.insert(context._class->functions[i]->name.operator String()+"(");
  1489. else
  1490. result.insert(context._class->functions[i]->name.operator String()+"()");
  1491. }
  1492. }
  1493. Ref<Reference> base = _get_parent_class(context);
  1494. while(true) {
  1495. Ref<GDScript> script = base;
  1496. Ref<GDNativeClass> nc = base;
  1497. if (script.is_valid()) {
  1498. if (!p_static && !p_only_functions) {
  1499. for (const Set<StringName>::Element *E=script->get_members().front();E;E=E->next()) {
  1500. result.insert(E->get().operator String());
  1501. }
  1502. }
  1503. if (!p_only_functions) {
  1504. for (const Map<StringName,Variant>::Element *E=script->get_constants().front();E;E=E->next()) {
  1505. result.insert(E->key().operator String());
  1506. }
  1507. }
  1508. for (const Map<StringName,GDFunction>::Element *E=script->get_member_functions().front();E;E=E->next()) {
  1509. if (!p_static || E->get().is_static()) {
  1510. if (E->get().get_argument_count())
  1511. result.insert(E->key().operator String()+"(");
  1512. else
  1513. result.insert(E->key().operator String()+"()");
  1514. }
  1515. }
  1516. if (!p_only_functions) {
  1517. for (const Map<StringName,Ref<GDScript> >::Element *E=script->get_subclasses().front();E;E=E->next()) {
  1518. result.insert(E->key().operator String());
  1519. }
  1520. }
  1521. base=script->get_base();
  1522. if (base.is_null())
  1523. base=script->get_native();
  1524. } else if (nc.is_valid()) {
  1525. if (!p_only_functions) {
  1526. StringName type = nc->get_name();
  1527. List<String> constants;
  1528. ObjectTypeDB::get_integer_constant_list(type,&constants);
  1529. for(List<String>::Element *E=constants.front();E;E=E->next()) {
  1530. result.insert(E->get());
  1531. }
  1532. List<MethodInfo> methods;
  1533. ObjectTypeDB::get_method_list(type,&methods);
  1534. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1535. if (E->get().arguments.size())
  1536. result.insert(E->get().name+"(");
  1537. else
  1538. result.insert(E->get().name+"()");
  1539. }
  1540. }
  1541. break;
  1542. } else
  1543. break;
  1544. }
  1545. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  1546. result.insert(GDFunctions::get_func_name(GDFunctions::Function(i)));
  1547. }
  1548. #endif
  1549. }
  1550. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, String &r_call_hint) {
  1551. GDParser p;
  1552. Error err = p.parse(p_code, p_base_path, false, "", true);
  1553. bool isfunction = false;
  1554. Set<String> options;
  1555. GDCompletionContext context;
  1556. context._class = p.get_completion_class();
  1557. context.block = p.get_completion_block();
  1558. context.function = p.get_completion_function();
  1559. context.base = p_owner;
  1560. context.base_path = p_base_path;
  1561. switch (p.get_completion_type()) {
  1562. case GDParser::COMPLETION_NONE: {
  1563. } break;
  1564. case GDParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  1565. List<StringName> constants;
  1566. Variant::get_numeric_constants_for_type(p.get_completion_built_in_constant(), &constants);
  1567. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  1568. options.insert(E->get().operator String());
  1569. }
  1570. } break;
  1571. case GDParser::COMPLETION_FUNCTION:
  1572. isfunction = true;
  1573. case GDParser::COMPLETION_IDENTIFIER: {
  1574. _find_identifiers(context, p.get_completion_line(), isfunction, options);
  1575. } break;
  1576. case GDParser::COMPLETION_PARENT_FUNCTION: {
  1577. } break;
  1578. case GDParser::COMPLETION_METHOD:
  1579. isfunction = true;
  1580. case GDParser::COMPLETION_INDEX: {
  1581. const GDParser::Node *node = p.get_completion_node();
  1582. if (node->type != GDParser::Node::TYPE_OPERATOR)
  1583. break;
  1584. GDCompletionIdentifier t;
  1585. if (_guess_expression_type(context, static_cast<const GDParser::OperatorNode *>(node)->arguments[0], p.get_completion_line(), t)) {
  1586. if (t.type == Variant::OBJECT && t.obj_type == "GDNativeClass") {
  1587. //native enum
  1588. Ref<GDNativeClass> gdn = t.value;
  1589. if (gdn.is_valid()) {
  1590. StringName cn = gdn->get_name();
  1591. List<String> cnames;
  1592. ObjectTypeDB::get_integer_constant_list(cn, &cnames);
  1593. for (List<String>::Element *E = cnames.front(); E; E = E->next()) {
  1594. options.insert(E->get());
  1595. }
  1596. }
  1597. } else if (t.type == Variant::OBJECT && t.obj_type != StringName()) {
  1598. Ref<GDScript> on_script;
  1599. if (t.value.get_type()) {
  1600. Object *obj = t.value;
  1601. if (obj) {
  1602. GDScript *scr = obj->cast_to<GDScript>();
  1603. if (scr) {
  1604. while (scr) {
  1605. if (!isfunction) {
  1606. for (const Map<StringName, Variant>::Element *E = scr->get_constants().front(); E; E = E->next()) {
  1607. options.insert(E->key());
  1608. }
  1609. }
  1610. for (const Map<StringName, GDFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1611. if (E->get()->is_static())
  1612. options.insert(E->key());
  1613. }
  1614. if (scr->get_base().is_valid())
  1615. scr = scr->get_base().ptr();
  1616. else
  1617. scr = NULL;
  1618. }
  1619. } else {
  1620. on_script = obj->get_script();
  1621. }
  1622. }
  1623. }
  1624. if (!on_script.is_valid() && t.script.is_valid()) {
  1625. on_script = t.script;
  1626. }
  1627. if (on_script.is_valid()) {
  1628. GDScript *scr = on_script.ptr();
  1629. if (scr) {
  1630. while (scr) {
  1631. String code = scr->get_source_code();
  1632. if (code != "") {
  1633. //if there is code, parse it. This way is slower but updates in real-time
  1634. GDParser p;
  1635. Error err = p.parse(scr->get_source_code(), scr->get_path().get_base_dir(), true, "", false);
  1636. if (err == OK) {
  1637. //only if ok, otherwise use what is cached on the script
  1638. //GDParser::ClassNode *base = p.
  1639. const GDParser::Node *root = p.get_parse_tree();
  1640. ERR_FAIL_COND_V(root->type != GDParser::Node::TYPE_CLASS, ERR_PARSE_ERROR);
  1641. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode *>(root);
  1642. for (int i = 0; i < cl->functions.size(); i++) {
  1643. if (cl->functions[i]->arguments.size())
  1644. options.insert(String(cl->functions[i]->name) + "(");
  1645. else
  1646. options.insert(String(cl->functions[i]->name) + "()");
  1647. }
  1648. for (int i = 0; i < cl->static_functions.size(); i++) {
  1649. if (cl->static_functions[i]->arguments.size())
  1650. options.insert(String(cl->static_functions[i]->name) + "(");
  1651. else
  1652. options.insert(String(cl->static_functions[i]->name) + "()");
  1653. }
  1654. if (!isfunction) {
  1655. for (int i = 0; i < cl->variables.size(); i++) {
  1656. options.insert(String(cl->variables[i].identifier));
  1657. }
  1658. for (int i = 0; i < cl->constant_expressions.size(); i++) {
  1659. options.insert(String(cl->constant_expressions[i].identifier));
  1660. }
  1661. }
  1662. } else {
  1663. code = ""; //well, then no code
  1664. }
  1665. }
  1666. if (code == "") {
  1667. //use class directly, no code was found
  1668. if (!isfunction) {
  1669. for (const Map<StringName, Variant>::Element *E = scr->get_constants().front(); E; E = E->next()) {
  1670. options.insert(E->key());
  1671. }
  1672. }
  1673. for (const Map<StringName, GDFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1674. if (E->get()->get_argument_count())
  1675. options.insert(String(E->key()) + "()");
  1676. else
  1677. options.insert(String(E->key()) + "(");
  1678. }
  1679. for (const Set<StringName>::Element *E = scr->get_members().front(); E; E = E->next()) {
  1680. options.insert(E->get());
  1681. }
  1682. }
  1683. if (scr->get_base().is_valid())
  1684. scr = scr->get_base().ptr();
  1685. else
  1686. scr = NULL;
  1687. }
  1688. }
  1689. }
  1690. if (!isfunction) {
  1691. ObjectTypeDB::get_integer_constant_list(t.obj_type, r_options);
  1692. }
  1693. List<MethodInfo> mi;
  1694. ObjectTypeDB::get_method_list(t.obj_type, &mi);
  1695. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  1696. if (E->get().name.begins_with("_"))
  1697. continue;
  1698. if (E->get().arguments.size())
  1699. options.insert(E->get().name + "(");
  1700. else
  1701. options.insert(E->get().name + "()");
  1702. }
  1703. } else {
  1704. if (t.type == Variant::INPUT_EVENT) {
  1705. //this is hardcoded otherwise it's not obvious
  1706. Set<String> exclude;
  1707. for (int i = 0; i < InputEvent::TYPE_MAX; i++) {
  1708. InputEvent ie;
  1709. ie.type = InputEvent::Type(i);
  1710. static const char *evnames[] = {
  1711. "# Common",
  1712. "# Key",
  1713. "# MouseMotion",
  1714. "# MouseButton",
  1715. "# JoystickMotion",
  1716. "# JoystickButton",
  1717. "# ScreenTouch",
  1718. "# ScreenDrag",
  1719. "# Action"
  1720. };
  1721. r_options->push_back(evnames[i]);
  1722. Variant v = ie;
  1723. if (i == 0) {
  1724. List<MethodInfo> mi;
  1725. v.get_method_list(&mi);
  1726. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  1727. r_options->push_back(E->get().name + "(");
  1728. }
  1729. }
  1730. List<PropertyInfo> pi;
  1731. v.get_property_list(&pi);
  1732. for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
  1733. if (i == 0)
  1734. exclude.insert(E->get().name);
  1735. else if (exclude.has(E->get().name))
  1736. continue;
  1737. r_options->push_back(E->get().name);
  1738. }
  1739. }
  1740. return OK;
  1741. } else {
  1742. if (t.value.get_type() == Variant::NIL) {
  1743. Variant::CallError ce;
  1744. t.value = Variant::construct(t.type, NULL, 0, ce);
  1745. }
  1746. if (!isfunction) {
  1747. List<PropertyInfo> pl;
  1748. t.value.get_property_list(&pl);
  1749. for (List<PropertyInfo>::Element *E = pl.front(); E; E = E->next()) {
  1750. if (E->get().name.find("/") == -1)
  1751. options.insert(E->get().name);
  1752. }
  1753. }
  1754. List<MethodInfo> mi;
  1755. t.value.get_method_list(&mi);
  1756. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  1757. if (E->get().arguments.size())
  1758. options.insert(E->get().name + "(");
  1759. else
  1760. options.insert(E->get().name + "()");
  1761. }
  1762. }
  1763. }
  1764. }
  1765. } break;
  1766. case GDParser::COMPLETION_CALL_ARGUMENTS: {
  1767. _find_call_arguments(context, p.get_completion_node(), p.get_completion_line(), p.get_completion_argument_index(), options, r_call_hint);
  1768. } break;
  1769. case GDParser::COMPLETION_VIRTUAL_FUNC: {
  1770. GDCompletionIdentifier cid = _get_native_class(context);
  1771. if (cid.obj_type != StringName()) {
  1772. List<MethodInfo> vm;
  1773. ObjectTypeDB::get_virtual_methods(cid.obj_type, &vm);
  1774. for (List<MethodInfo>::Element *E = vm.front(); E; E = E->next()) {
  1775. MethodInfo &mi = E->get();
  1776. String m = mi.name;
  1777. if (m.find(":") != -1)
  1778. m = m.substr(0, m.find(":"));
  1779. m += "(";
  1780. if (mi.arguments.size()) {
  1781. for (int i = 0; i < mi.arguments.size(); i++) {
  1782. if (i > 0)
  1783. m += ", ";
  1784. String n = mi.arguments[i].name;
  1785. if (n.find(":") != -1)
  1786. n = n.substr(0, n.find(":"));
  1787. m += n;
  1788. }
  1789. }
  1790. m += "):";
  1791. options.insert(m);
  1792. }
  1793. }
  1794. } break;
  1795. }
  1796. for (Set<String>::Element *E = options.front(); E; E = E->next()) {
  1797. r_options->push_back(E->get());
  1798. }
  1799. return OK;
  1800. }
  1801. #else
  1802. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, String &r_call_hint) {
  1803. return OK;
  1804. }
  1805. #endif
  1806. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  1807. Vector<String> lines = p_code.split("\n");
  1808. List<int> indent_stack;
  1809. for (int i = 0; i < lines.size(); i++) {
  1810. String l = lines[i];
  1811. int tc = 0;
  1812. for (int j = 0; j < l.length(); j++) {
  1813. if (l[j] == ' ' || l[j] == '\t') {
  1814. tc++;
  1815. } else {
  1816. break;
  1817. }
  1818. }
  1819. String st = l.substr(tc, l.length()).strip_edges();
  1820. if (st == "" || st.begins_with("#"))
  1821. continue; //ignore!
  1822. int ilevel = 0;
  1823. if (indent_stack.size()) {
  1824. ilevel = indent_stack.back()->get();
  1825. }
  1826. if (tc > ilevel) {
  1827. indent_stack.push_back(tc);
  1828. } else if (tc < ilevel) {
  1829. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  1830. indent_stack.pop_back();
  1831. }
  1832. if (indent_stack.size() && indent_stack.back()->get() != tc)
  1833. indent_stack.push_back(tc); //this is not right but gets the job done
  1834. }
  1835. if (i >= p_from_line) {
  1836. l = "";
  1837. for (int j = 0; j < indent_stack.size(); j++)
  1838. l += "\t";
  1839. l += st;
  1840. } else if (i > p_to_line) {
  1841. break;
  1842. }
  1843. //print_line(itos(indent_stack.size())+","+itos(tc)+": "+l);
  1844. lines[i] = l;
  1845. }
  1846. p_code = "";
  1847. for (int i = 0; i < lines.size(); i++) {
  1848. if (i > 0)
  1849. p_code += "\n";
  1850. p_code += lines[i];
  1851. }
  1852. }