gd_editor.cpp 88 KB

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