gd_editor.cpp 83 KB

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