gd_editor.cpp 69 KB

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