gd_editor.cpp 69 KB

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