gdscript_editor.cpp 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138
  1. /*************************************************************************/
  2. /* gdscript_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "gdscript.h"
  31. #include "core/config/engine.h"
  32. #include "core/core_constants.h"
  33. #include "core/os/file_access.h"
  34. #include "gdscript_analyzer.h"
  35. #include "gdscript_compiler.h"
  36. #include "gdscript_parser.h"
  37. #include "gdscript_tokenizer.h"
  38. #ifdef TOOLS_ENABLED
  39. #include "core/config/project_settings.h"
  40. #include "editor/editor_file_system.h"
  41. #include "editor/editor_settings.h"
  42. #endif
  43. void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  44. p_delimiters->push_back("#");
  45. }
  46. void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  47. p_delimiters->push_back("\" \"");
  48. p_delimiters->push_back("' '");
  49. p_delimiters->push_back("\"\"\" \"\"\"");
  50. }
  51. String GDScriptLanguage::_get_processed_template(const String &p_template, const String &p_base_class_name) const {
  52. String processed_template = p_template;
  53. #ifdef TOOLS_ENABLED
  54. if (EDITOR_DEF("text_editor/completion/add_type_hints", false)) {
  55. processed_template = processed_template.replace("%INT_TYPE%", ": int");
  56. processed_template = processed_template.replace("%STRING_TYPE%", ": String");
  57. processed_template = processed_template.replace("%FLOAT_TYPE%", ": float");
  58. processed_template = processed_template.replace("%VOID_RETURN%", " -> void");
  59. } else {
  60. processed_template = processed_template.replace("%INT_TYPE%", "");
  61. processed_template = processed_template.replace("%STRING_TYPE%", "");
  62. processed_template = processed_template.replace("%FLOAT_TYPE%", "");
  63. processed_template = processed_template.replace("%VOID_RETURN%", "");
  64. }
  65. #else
  66. processed_template = processed_template.replace("%INT_TYPE%", "");
  67. processed_template = processed_template.replace("%STRING_TYPE%", "");
  68. processed_template = processed_template.replace("%FLOAT_TYPE%", "");
  69. processed_template = processed_template.replace("%VOID_RETURN%", "");
  70. #endif
  71. processed_template = processed_template.replace("%BASE%", p_base_class_name);
  72. processed_template = processed_template.replace("%TS%", _get_indentation());
  73. return processed_template;
  74. }
  75. Ref<Script> GDScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  76. String _template = "extends %BASE%\n"
  77. "\n"
  78. "\n"
  79. "# Declare member variables here. Examples:\n"
  80. "# var a%INT_TYPE% = 2\n"
  81. "# var b%STRING_TYPE% = \"text\"\n"
  82. "\n"
  83. "\n"
  84. "# Called when the node enters the scene tree for the first time.\n"
  85. "func _ready()%VOID_RETURN%:\n"
  86. "%TS%pass # Replace with function body.\n"
  87. "\n"
  88. "\n"
  89. "# Called every frame. 'delta' is the elapsed time since the previous frame.\n"
  90. "#func _process(delta%FLOAT_TYPE%)%VOID_RETURN%:\n"
  91. "#%TS%pass\n";
  92. _template = _get_processed_template(_template, p_base_class_name);
  93. Ref<GDScript> script;
  94. script.instance();
  95. script->set_source_code(_template);
  96. return script;
  97. }
  98. bool GDScriptLanguage::is_using_templates() {
  99. return true;
  100. }
  101. void GDScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
  102. String _template = _get_processed_template(p_script->get_source_code(), p_base_class_name);
  103. p_script->set_source_code(_template);
  104. }
  105. static void get_function_names_recursively(const GDScriptParser::ClassNode *p_class, const String &p_prefix, Map<int, String> &r_funcs) {
  106. for (int i = 0; i < p_class->members.size(); i++) {
  107. if (p_class->members[i].type == GDScriptParser::ClassNode::Member::FUNCTION) {
  108. const GDScriptParser::FunctionNode *function = p_class->members[i].function;
  109. r_funcs[function->start_line] = p_prefix.empty() ? String(function->identifier->name) : p_prefix + "." + String(function->identifier->name);
  110. } else if (p_class->members[i].type == GDScriptParser::ClassNode::Member::CLASS) {
  111. String new_prefix = p_class->members[i].m_class->identifier->name;
  112. get_function_names_recursively(p_class->members[i].m_class, p_prefix.empty() ? new_prefix : p_prefix + "." + new_prefix, r_funcs);
  113. }
  114. }
  115. }
  116. 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, List<ScriptLanguage::Warning> *r_warnings, Set<int> *r_safe_lines) const {
  117. GDScriptParser parser;
  118. GDScriptAnalyzer analyzer(&parser);
  119. Error err = parser.parse(p_script, p_path, false);
  120. if (err == OK) {
  121. err = analyzer.analyze();
  122. }
  123. #ifdef DEBUG_ENABLED
  124. if (r_warnings) {
  125. for (const List<GDScriptWarning>::Element *E = parser.get_warnings().front(); E; E = E->next()) {
  126. const GDScriptWarning &warn = E->get();
  127. ScriptLanguage::Warning w;
  128. w.start_line = warn.start_line;
  129. w.end_line = warn.end_line;
  130. w.leftmost_column = warn.leftmost_column;
  131. w.rightmost_column = warn.rightmost_column;
  132. w.code = (int)warn.code;
  133. w.string_code = GDScriptWarning::get_name_from_code(warn.code);
  134. w.message = warn.get_message();
  135. r_warnings->push_back(w);
  136. }
  137. }
  138. #endif
  139. if (err) {
  140. GDScriptParser::ParserError parse_error = parser.get_errors().front()->get();
  141. r_line_error = parse_error.line;
  142. r_col_error = parse_error.column;
  143. r_test_error = parse_error.message;
  144. return false;
  145. } else {
  146. const GDScriptParser::ClassNode *cl = parser.get_tree();
  147. Map<int, String> funcs;
  148. get_function_names_recursively(cl, "", funcs);
  149. for (Map<int, String>::Element *E = funcs.front(); E; E = E->next()) {
  150. r_functions->push_back(E->get() + ":" + itos(E->key()));
  151. }
  152. }
  153. #ifdef DEBUG_ENABLED
  154. if (r_safe_lines) {
  155. const Set<int> &unsafe_lines = parser.get_unsafe_lines();
  156. for (int i = 1; i <= parser.get_last_line_number(); i++) {
  157. if (!unsafe_lines.has(i)) {
  158. r_safe_lines->insert(i);
  159. }
  160. }
  161. }
  162. #endif
  163. return true;
  164. }
  165. bool GDScriptLanguage::has_named_classes() const {
  166. return false;
  167. }
  168. bool GDScriptLanguage::supports_builtin_mode() const {
  169. return true;
  170. }
  171. int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  172. GDScriptTokenizer tokenizer;
  173. tokenizer.set_source_code(p_code);
  174. int indent = 0;
  175. GDScriptTokenizer::Token current = tokenizer.scan();
  176. while (current.type != GDScriptTokenizer::Token::TK_EOF && current.type != GDScriptTokenizer::Token::ERROR) {
  177. if (current.type == GDScriptTokenizer::Token::INDENT) {
  178. indent++;
  179. } else if (current.type == GDScriptTokenizer::Token::DEDENT) {
  180. indent--;
  181. }
  182. if (indent == 0 && current.type == GDScriptTokenizer::Token::FUNC) {
  183. current = tokenizer.scan();
  184. if (current.is_identifier()) {
  185. String identifier = current.get_identifier();
  186. if (identifier == p_function) {
  187. return current.start_line;
  188. }
  189. }
  190. }
  191. current = tokenizer.scan();
  192. }
  193. return -1;
  194. }
  195. Script *GDScriptLanguage::create_script() const {
  196. return memnew(GDScript);
  197. }
  198. /* DEBUGGER FUNCTIONS */
  199. bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
  200. //break because of parse error
  201. if (EngineDebugger::is_active() && Thread::get_caller_id() == Thread::get_main_id()) {
  202. _debug_parse_err_line = p_line;
  203. _debug_parse_err_file = p_file;
  204. _debug_error = p_error;
  205. EngineDebugger::get_script_debugger()->debug(this, false, true);
  206. return true;
  207. } else {
  208. return false;
  209. }
  210. }
  211. bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
  212. if (EngineDebugger::is_active() && Thread::get_caller_id() == Thread::get_main_id()) {
  213. _debug_parse_err_line = -1;
  214. _debug_parse_err_file = "";
  215. _debug_error = p_error;
  216. bool is_error_breakpoint = p_error != "Breakpoint";
  217. EngineDebugger::get_script_debugger()->debug(this, p_allow_continue, is_error_breakpoint);
  218. return true;
  219. } else {
  220. return false;
  221. }
  222. }
  223. String GDScriptLanguage::debug_get_error() const {
  224. return _debug_error;
  225. }
  226. int GDScriptLanguage::debug_get_stack_level_count() const {
  227. if (_debug_parse_err_line >= 0) {
  228. return 1;
  229. }
  230. return _debug_call_stack_pos;
  231. }
  232. int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
  233. if (_debug_parse_err_line >= 0) {
  234. return _debug_parse_err_line;
  235. }
  236. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, -1);
  237. int l = _debug_call_stack_pos - p_level - 1;
  238. return *(_call_stack[l].line);
  239. }
  240. String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
  241. if (_debug_parse_err_line >= 0) {
  242. return "";
  243. }
  244. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  245. int l = _debug_call_stack_pos - p_level - 1;
  246. return _call_stack[l].function->get_name();
  247. }
  248. String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
  249. if (_debug_parse_err_line >= 0) {
  250. return _debug_parse_err_file;
  251. }
  252. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  253. int l = _debug_call_stack_pos - p_level - 1;
  254. return _call_stack[l].function->get_source();
  255. }
  256. 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) {
  257. if (_debug_parse_err_line >= 0) {
  258. return;
  259. }
  260. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  261. int l = _debug_call_stack_pos - p_level - 1;
  262. GDScriptFunction *f = _call_stack[l].function;
  263. List<Pair<StringName, int>> locals;
  264. f->debug_get_stack_member_state(*_call_stack[l].line, &locals);
  265. for (List<Pair<StringName, int>>::Element *E = locals.front(); E; E = E->next()) {
  266. p_locals->push_back(E->get().first);
  267. p_values->push_back(_call_stack[l].stack[E->get().second]);
  268. }
  269. }
  270. 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) {
  271. if (_debug_parse_err_line >= 0) {
  272. return;
  273. }
  274. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  275. int l = _debug_call_stack_pos - p_level - 1;
  276. GDScriptInstance *instance = _call_stack[l].instance;
  277. if (!instance) {
  278. return;
  279. }
  280. Ref<GDScript> script = instance->get_script();
  281. ERR_FAIL_COND(script.is_null());
  282. const Map<StringName, GDScript::MemberInfo> &mi = script->debug_get_member_indices();
  283. for (const Map<StringName, GDScript::MemberInfo>::Element *E = mi.front(); E; E = E->next()) {
  284. p_members->push_back(E->key());
  285. p_values->push_back(instance->debug_get_member_by_index(E->get().index));
  286. }
  287. }
  288. ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) {
  289. if (_debug_parse_err_line >= 0) {
  290. return nullptr;
  291. }
  292. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, nullptr);
  293. int l = _debug_call_stack_pos - p_level - 1;
  294. ScriptInstance *instance = _call_stack[l].instance;
  295. return instance;
  296. }
  297. void GDScriptLanguage::debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  298. const Map<StringName, int> &name_idx = GDScriptLanguage::get_singleton()->get_global_map();
  299. const Variant *globals = GDScriptLanguage::get_singleton()->get_global_array();
  300. List<Pair<String, Variant>> cinfo;
  301. get_public_constants(&cinfo);
  302. for (const Map<StringName, int>::Element *E = name_idx.front(); E; E = E->next()) {
  303. if (ClassDB::class_exists(E->key()) || Engine::get_singleton()->has_singleton(E->key())) {
  304. continue;
  305. }
  306. bool is_script_constant = false;
  307. for (List<Pair<String, Variant>>::Element *CE = cinfo.front(); CE; CE = CE->next()) {
  308. if (CE->get().first == E->key()) {
  309. is_script_constant = true;
  310. break;
  311. }
  312. }
  313. if (is_script_constant) {
  314. continue;
  315. }
  316. const Variant &var = globals[E->value()];
  317. if (Object *obj = var) {
  318. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  319. continue;
  320. }
  321. }
  322. bool skip = false;
  323. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  324. if (E->key() == CoreConstants::get_global_constant_name(i)) {
  325. skip = true;
  326. break;
  327. }
  328. }
  329. if (skip) {
  330. continue;
  331. }
  332. p_globals->push_back(E->key());
  333. p_values->push_back(var);
  334. }
  335. }
  336. String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  337. return "";
  338. }
  339. void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  340. p_extensions->push_back("gd");
  341. }
  342. void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  343. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  344. p_functions->push_back(GDScriptFunctions::get_info(GDScriptFunctions::Function(i)));
  345. }
  346. //not really "functions", but..
  347. {
  348. MethodInfo mi;
  349. mi.name = "preload";
  350. mi.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
  351. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
  352. p_functions->push_back(mi);
  353. }
  354. {
  355. MethodInfo mi;
  356. mi.name = "assert";
  357. mi.return_val.type = Variant::NIL;
  358. mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition"));
  359. mi.arguments.push_back(PropertyInfo(Variant::STRING, "message"));
  360. mi.default_arguments.push_back(String());
  361. p_functions->push_back(mi);
  362. }
  363. }
  364. void GDScriptLanguage::get_public_constants(List<Pair<String, Variant>> *p_constants) const {
  365. Pair<String, Variant> pi;
  366. pi.first = "PI";
  367. pi.second = Math_PI;
  368. p_constants->push_back(pi);
  369. Pair<String, Variant> tau;
  370. tau.first = "TAU";
  371. tau.second = Math_TAU;
  372. p_constants->push_back(tau);
  373. Pair<String, Variant> infinity;
  374. infinity.first = "INF";
  375. infinity.second = Math_INF;
  376. p_constants->push_back(infinity);
  377. Pair<String, Variant> nan;
  378. nan.first = "NAN";
  379. nan.second = Math_NAN;
  380. p_constants->push_back(nan);
  381. }
  382. String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const {
  383. #ifdef TOOLS_ENABLED
  384. bool th = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
  385. #else
  386. bool th = false;
  387. #endif
  388. String s = "func " + p_name + "(";
  389. if (p_args.size()) {
  390. for (int i = 0; i < p_args.size(); i++) {
  391. if (i > 0) {
  392. s += ", ";
  393. }
  394. s += p_args[i].get_slice(":", 0);
  395. if (th) {
  396. String type = p_args[i].get_slice(":", 1);
  397. if (!type.empty() && type != "var") {
  398. s += ": " + type;
  399. }
  400. }
  401. }
  402. }
  403. s += String(")") + (th ? " -> void" : "") + ":\n" + _get_indentation() + "pass # Replace with function body.\n";
  404. return s;
  405. }
  406. //////// COMPLETION //////////
  407. #ifdef TOOLS_ENABLED
  408. #define COMPLETION_RECURSION_LIMIT 200
  409. struct GDScriptCompletionIdentifier {
  410. GDScriptParser::DataType type;
  411. String enumeration;
  412. Variant value;
  413. const GDScriptParser::ExpressionNode *assigned_expression = nullptr;
  414. };
  415. // TODO: Move this to a central location (maybe core?).
  416. static const char *underscore_classes[] = {
  417. "ClassDB",
  418. "Directory",
  419. "Engine",
  420. "File",
  421. "Geometry",
  422. "GodotSharp",
  423. "JSON",
  424. "Marshalls",
  425. "Mutex",
  426. "OS",
  427. "ResourceLoader",
  428. "ResourceSaver",
  429. "Semaphore",
  430. "Thread",
  431. "VisualScriptEditor",
  432. nullptr,
  433. };
  434. static StringName _get_real_class_name(const StringName &p_source) {
  435. const char **class_name = underscore_classes;
  436. while (*class_name != nullptr) {
  437. if (p_source == *class_name) {
  438. return String("_") + p_source;
  439. }
  440. class_name++;
  441. }
  442. return p_source;
  443. }
  444. static String _get_visual_datatype(const PropertyInfo &p_info, bool p_is_arg = true) {
  445. if (p_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  446. String enum_name = p_info.class_name;
  447. if (enum_name.find(".") == -1) {
  448. return enum_name;
  449. }
  450. return enum_name.get_slice(".", 1);
  451. }
  452. String n = p_info.name;
  453. int idx = n.find(":");
  454. if (idx != -1) {
  455. return n.substr(idx + 1, n.length());
  456. }
  457. if (p_info.type == Variant::OBJECT) {
  458. if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  459. return p_info.hint_string;
  460. } else {
  461. return p_info.class_name.operator String();
  462. }
  463. }
  464. if (p_info.type == Variant::NIL) {
  465. if (p_is_arg || (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  466. return "Variant";
  467. } else {
  468. return "void";
  469. }
  470. }
  471. return Variant::get_type_name(p_info.type);
  472. }
  473. static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx, bool p_is_annotation = false) {
  474. String arghint;
  475. if (!p_is_annotation) {
  476. arghint += _get_visual_datatype(p_info.return_val, false) + " ";
  477. }
  478. arghint += p_info.name + "(";
  479. int def_args = p_info.arguments.size() - p_info.default_arguments.size();
  480. int i = 0;
  481. for (const List<PropertyInfo>::Element *E = p_info.arguments.front(); E; E = E->next()) {
  482. if (i > 0) {
  483. arghint += ", ";
  484. }
  485. if (i == p_arg_idx) {
  486. arghint += String::chr(0xFFFF);
  487. }
  488. arghint += E->get().name + ": " + _get_visual_datatype(E->get(), true);
  489. if (i - def_args >= 0) {
  490. arghint += String(" = ") + p_info.default_arguments[i - def_args].get_construct_string();
  491. }
  492. if (i == p_arg_idx) {
  493. arghint += String::chr(0xFFFF);
  494. }
  495. i++;
  496. }
  497. if (p_info.flags & METHOD_FLAG_VARARG) {
  498. if (p_info.arguments.size() > 0) {
  499. arghint += ", ";
  500. }
  501. if (p_arg_idx >= p_info.arguments.size()) {
  502. arghint += String::chr(0xFFFF);
  503. }
  504. arghint += "...";
  505. if (p_arg_idx >= p_info.arguments.size()) {
  506. arghint += String::chr(0xFFFF);
  507. }
  508. }
  509. arghint += ")";
  510. return arghint;
  511. }
  512. static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_function, int p_arg_idx) {
  513. String arghint = p_function->get_datatype().to_string() + " " + p_function->identifier->name.operator String() + "(";
  514. for (int i = 0; i < p_function->parameters.size(); i++) {
  515. if (i > 0) {
  516. arghint += ", ";
  517. }
  518. if (i == p_arg_idx) {
  519. arghint += String::chr(0xFFFF);
  520. }
  521. const GDScriptParser::ParameterNode *par = p_function->parameters[i];
  522. arghint += par->identifier->name.operator String() + ": " + par->get_datatype().to_string();
  523. if (par->default_value) {
  524. String def_val = "<unknown>";
  525. if (par->default_value->type == GDScriptParser::Node::LITERAL) {
  526. const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(par->default_value);
  527. def_val = literal->value.get_construct_string();
  528. } else if (par->default_value->type == GDScriptParser::Node::IDENTIFIER) {
  529. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(par->default_value);
  530. def_val = id->name.operator String();
  531. }
  532. arghint += " = " + def_val;
  533. }
  534. if (i == p_arg_idx) {
  535. arghint += String::chr(0xFFFF);
  536. }
  537. }
  538. arghint += ")";
  539. return arghint;
  540. }
  541. static void _get_directory_contents(EditorFileSystemDirectory *p_dir, Map<String, ScriptCodeCompletionOption> &r_list) {
  542. const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
  543. for (int i = 0; i < p_dir->get_file_count(); i++) {
  544. ScriptCodeCompletionOption option(p_dir->get_file_path(i), ScriptCodeCompletionOption::KIND_FILE_PATH);
  545. option.insert_text = quote_style + option.display + quote_style;
  546. r_list.insert(option.display, option);
  547. }
  548. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  549. _get_directory_contents(p_dir->get_subdir(i), r_list);
  550. }
  551. }
  552. static void _find_annotation_arguments(const GDScriptParser::AnnotationNode *p_annotation, int p_argument, const String p_quote_style, Map<String, ScriptCodeCompletionOption> &r_result) {
  553. if (p_annotation->name == "@export_range" || p_annotation->name == "@export_exp_range") {
  554. if (p_argument == 3 || p_argument == 4) {
  555. // Slider hint.
  556. ScriptCodeCompletionOption slider1("or_greater", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  557. slider1.insert_text = p_quote_style + slider1.display + p_quote_style;
  558. r_result.insert(slider1.display, slider1);
  559. ScriptCodeCompletionOption slider2("or_lesser", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  560. slider2.insert_text = p_quote_style + slider2.display + p_quote_style;
  561. r_result.insert(slider2.display, slider2);
  562. }
  563. } else if (p_annotation->name == "@export_exp_easing") {
  564. if (p_argument == 0 || p_argument == 1) {
  565. // Easing hint.
  566. ScriptCodeCompletionOption hint1("attenuation", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  567. hint1.insert_text = p_quote_style + hint1.display + p_quote_style;
  568. r_result.insert(hint1.display, hint1);
  569. ScriptCodeCompletionOption hint2("inout", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  570. hint2.insert_text = p_quote_style + hint2.display + p_quote_style;
  571. r_result.insert(hint2.display, hint2);
  572. }
  573. } else if (p_annotation->name == "@export_node_path") {
  574. ScriptCodeCompletionOption node("Node", ScriptCodeCompletionOption::KIND_CLASS);
  575. r_result.insert(node.display, node);
  576. List<StringName> node_types;
  577. ClassDB::get_inheriters_from_class("Node", &node_types);
  578. for (const List<StringName>::Element *E = node_types.front(); E != nullptr; E = E->next()) {
  579. if (!ClassDB::is_class_exposed(E->get())) {
  580. continue;
  581. }
  582. ScriptCodeCompletionOption option(E->get(), ScriptCodeCompletionOption::KIND_CLASS);
  583. r_result.insert(option.display, option);
  584. }
  585. }
  586. }
  587. static void _list_available_types(bool p_inherit_only, GDScriptParser::CompletionContext &p_context, Map<String, ScriptCodeCompletionOption> &r_result) {
  588. List<StringName> native_types;
  589. ClassDB::get_class_list(&native_types);
  590. for (const List<StringName>::Element *E = native_types.front(); E != nullptr; E = E->next()) {
  591. if (ClassDB::is_class_exposed(E->get()) && !Engine::get_singleton()->has_singleton(E->get())) {
  592. ScriptCodeCompletionOption option(E->get(), ScriptCodeCompletionOption::KIND_CLASS);
  593. r_result.insert(option.display, option);
  594. }
  595. }
  596. if (p_context.current_class) {
  597. if (!p_inherit_only && p_context.current_class->base_type.is_set()) {
  598. // Native enums from base class
  599. List<StringName> enums;
  600. ClassDB::get_enum_list(p_context.current_class->base_type.native_type, &enums);
  601. for (const List<StringName>::Element *E = enums.front(); E != nullptr; E = E->next()) {
  602. ScriptCodeCompletionOption option(E->get(), ScriptCodeCompletionOption::KIND_ENUM);
  603. r_result.insert(option.display, option);
  604. }
  605. }
  606. // Check current class for potential types
  607. const GDScriptParser::ClassNode *current = p_context.current_class;
  608. while (current) {
  609. for (int i = 0; i < current->members.size(); i++) {
  610. const GDScriptParser::ClassNode::Member &member = current->members[i];
  611. switch (member.type) {
  612. case GDScriptParser::ClassNode::Member::CLASS: {
  613. ScriptCodeCompletionOption option(member.m_class->identifier->name, ScriptCodeCompletionOption::KIND_CLASS);
  614. r_result.insert(option.display, option);
  615. } break;
  616. case GDScriptParser::ClassNode::Member::ENUM: {
  617. if (!p_inherit_only) {
  618. ScriptCodeCompletionOption option(member.m_enum->identifier->name, ScriptCodeCompletionOption::KIND_ENUM);
  619. r_result.insert(option.display, option);
  620. }
  621. } break;
  622. case GDScriptParser::ClassNode::Member::CONSTANT: {
  623. if (member.constant->get_datatype().is_meta_type && p_context.current_class->outer != nullptr) {
  624. ScriptCodeCompletionOption option(member.constant->identifier->name, ScriptCodeCompletionOption::KIND_CLASS);
  625. r_result.insert(option.display, option);
  626. }
  627. } break;
  628. default:
  629. break;
  630. }
  631. }
  632. current = current->outer;
  633. }
  634. }
  635. // Global scripts
  636. List<StringName> global_classes;
  637. ScriptServer::get_global_class_list(&global_classes);
  638. for (const List<StringName>::Element *E = global_classes.front(); E != nullptr; E = E->next()) {
  639. ScriptCodeCompletionOption option(E->get(), ScriptCodeCompletionOption::KIND_CLASS);
  640. r_result.insert(option.display, option);
  641. }
  642. // Autoload singletons
  643. Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  644. for (const Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E != nullptr; E = E->next()) {
  645. const ProjectSettings::AutoloadInfo &info = E->get();
  646. if (!info.is_singleton || info.path.get_extension().to_lower() != "gd") {
  647. continue;
  648. }
  649. ScriptCodeCompletionOption option(info.name, ScriptCodeCompletionOption::KIND_CLASS);
  650. r_result.insert(option.display, option);
  651. }
  652. }
  653. static void _find_identifiers_in_suite(const GDScriptParser::SuiteNode *p_suite, Map<String, ScriptCodeCompletionOption> &r_result) {
  654. for (int i = 0; i < p_suite->locals.size(); i++) {
  655. ScriptCodeCompletionOption option(p_suite->locals[i].name, ScriptCodeCompletionOption::KIND_VARIABLE);
  656. r_result.insert(option.display, option);
  657. }
  658. if (p_suite->parent_block) {
  659. _find_identifiers_in_suite(p_suite->parent_block, r_result);
  660. }
  661. }
  662. static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, Map<String, ScriptCodeCompletionOption> &r_result, int p_recursion_depth);
  663. static void _find_identifiers_in_class(const GDScriptParser::ClassNode *p_class, bool p_only_functions, bool p_static, bool p_parent_only, Map<String, ScriptCodeCompletionOption> &r_result, int p_recursion_depth) {
  664. ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
  665. if (!p_parent_only) {
  666. bool outer = false;
  667. const GDScriptParser::ClassNode *clss = p_class;
  668. while (clss) {
  669. for (int i = 0; i < clss->members.size(); i++) {
  670. const GDScriptParser::ClassNode::Member &member = clss->members[i];
  671. ScriptCodeCompletionOption option;
  672. switch (member.type) {
  673. case GDScriptParser::ClassNode::Member::VARIABLE:
  674. if (p_only_functions || outer || (p_static)) {
  675. continue;
  676. }
  677. option = ScriptCodeCompletionOption(member.variable->identifier->name, ScriptCodeCompletionOption::KIND_MEMBER);
  678. break;
  679. case GDScriptParser::ClassNode::Member::CONSTANT:
  680. if (p_only_functions) {
  681. continue;
  682. }
  683. option = ScriptCodeCompletionOption(member.constant->identifier->name, ScriptCodeCompletionOption::KIND_CONSTANT);
  684. if (member.constant->initializer) {
  685. option.default_value = member.constant->initializer->reduced_value;
  686. }
  687. break;
  688. case GDScriptParser::ClassNode::Member::CLASS:
  689. if (p_only_functions) {
  690. continue;
  691. }
  692. option = ScriptCodeCompletionOption(member.m_class->identifier->name, ScriptCodeCompletionOption::KIND_CLASS);
  693. break;
  694. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  695. if (p_only_functions) {
  696. continue;
  697. }
  698. option = ScriptCodeCompletionOption(member.enum_value.identifier->name, ScriptCodeCompletionOption::KIND_CONSTANT);
  699. break;
  700. case GDScriptParser::ClassNode::Member::ENUM:
  701. if (p_only_functions) {
  702. continue;
  703. }
  704. option = ScriptCodeCompletionOption(member.m_enum->identifier->name, ScriptCodeCompletionOption::KIND_ENUM);
  705. break;
  706. case GDScriptParser::ClassNode::Member::FUNCTION:
  707. if (outer || (p_static && !member.function->is_static) || member.function->identifier->name.operator String().begins_with("@")) {
  708. continue;
  709. }
  710. option = ScriptCodeCompletionOption(member.function->identifier->name, ScriptCodeCompletionOption::KIND_FUNCTION);
  711. if (member.function->parameters.size() > 0) {
  712. option.insert_text += "(";
  713. } else {
  714. option.insert_text += "()";
  715. }
  716. break;
  717. case GDScriptParser::ClassNode::Member::SIGNAL:
  718. if (p_only_functions || outer) {
  719. continue;
  720. }
  721. option = ScriptCodeCompletionOption(member.signal->identifier->name, ScriptCodeCompletionOption::KIND_SIGNAL);
  722. break;
  723. case GDScriptParser::ClassNode::Member::UNDEFINED:
  724. break;
  725. }
  726. r_result.insert(option.display, option);
  727. }
  728. outer = true;
  729. clss = clss->outer;
  730. }
  731. }
  732. // Parents.
  733. GDScriptCompletionIdentifier base_type;
  734. base_type.type = p_class->base_type;
  735. base_type.type.is_meta_type = p_static;
  736. _find_identifiers_in_base(base_type, p_only_functions, r_result, p_recursion_depth + 1);
  737. }
  738. static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, Map<String, ScriptCodeCompletionOption> &r_result, int p_recursion_depth) {
  739. ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
  740. GDScriptParser::DataType base_type = p_base.type;
  741. bool _static = base_type.is_meta_type;
  742. if (_static && base_type.kind != GDScriptParser::DataType::BUILTIN) {
  743. ScriptCodeCompletionOption option("new", ScriptCodeCompletionOption::KIND_FUNCTION);
  744. option.insert_text += "(";
  745. r_result.insert(option.display, option);
  746. }
  747. while (!base_type.has_no_type()) {
  748. switch (base_type.kind) {
  749. case GDScriptParser::DataType::CLASS: {
  750. _find_identifiers_in_class(base_type.class_type, p_only_functions, _static, false, r_result, p_recursion_depth + 1);
  751. // This already finds all parent identifiers, so we are done.
  752. base_type = GDScriptParser::DataType();
  753. } break;
  754. case GDScriptParser::DataType::SCRIPT: {
  755. Ref<Script> scr = base_type.script_type;
  756. if (scr.is_valid()) {
  757. if (!p_only_functions) {
  758. if (!_static) {
  759. List<PropertyInfo> members;
  760. scr->get_script_property_list(&members);
  761. for (List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  762. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_MEMBER);
  763. r_result.insert(option.display, option);
  764. }
  765. }
  766. Map<StringName, Variant> constants;
  767. scr->get_constants(&constants);
  768. for (Map<StringName, Variant>::Element *E = constants.front(); E; E = E->next()) {
  769. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CONSTANT);
  770. r_result.insert(option.display, option);
  771. }
  772. List<MethodInfo> signals;
  773. scr->get_script_signal_list(&signals);
  774. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  775. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_SIGNAL);
  776. r_result.insert(option.display, option);
  777. }
  778. }
  779. List<MethodInfo> methods;
  780. scr->get_script_method_list(&methods);
  781. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  782. if (E->get().name.begins_with("@")) {
  783. continue;
  784. }
  785. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_FUNCTION);
  786. if (E->get().arguments.size()) {
  787. option.insert_text += "(";
  788. } else {
  789. option.insert_text += "()";
  790. }
  791. r_result.insert(option.display, option);
  792. }
  793. Ref<Script> base_script = scr->get_base_script();
  794. if (base_script.is_valid()) {
  795. base_type.script_type = base_script;
  796. } else {
  797. base_type.kind = GDScriptParser::DataType::NATIVE;
  798. base_type.native_type = scr->get_instance_base_type();
  799. }
  800. } else {
  801. return;
  802. }
  803. } break;
  804. case GDScriptParser::DataType::NATIVE: {
  805. StringName type = _get_real_class_name(base_type.native_type);
  806. if (!ClassDB::class_exists(type)) {
  807. return;
  808. }
  809. if (!p_only_functions) {
  810. List<String> constants;
  811. ClassDB::get_integer_constant_list(type, &constants);
  812. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  813. ScriptCodeCompletionOption option(E->get(), ScriptCodeCompletionOption::KIND_CONSTANT);
  814. r_result.insert(option.display, option);
  815. }
  816. if (!_static || Engine::get_singleton()->has_singleton(type)) {
  817. List<PropertyInfo> pinfo;
  818. ClassDB::get_property_list(type, &pinfo);
  819. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  820. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY)) {
  821. continue;
  822. }
  823. if (E->get().name.find("/") != -1) {
  824. continue;
  825. }
  826. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_MEMBER);
  827. r_result.insert(option.display, option);
  828. }
  829. }
  830. }
  831. if (!_static || Engine::get_singleton()->has_singleton(type)) {
  832. List<MethodInfo> methods;
  833. bool is_autocompleting_getters = GLOBAL_GET("debug/gdscript/completion/autocomplete_setters_and_getters").booleanize();
  834. ClassDB::get_method_list(type, &methods, false, !is_autocompleting_getters);
  835. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  836. if (E->get().name.begins_with("_")) {
  837. continue;
  838. }
  839. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_FUNCTION);
  840. if (E->get().arguments.size()) {
  841. option.insert_text += "(";
  842. } else {
  843. option.insert_text += "()";
  844. }
  845. r_result.insert(option.display, option);
  846. }
  847. }
  848. return;
  849. } break;
  850. case GDScriptParser::DataType::BUILTIN: {
  851. Callable::CallError err;
  852. Variant tmp;
  853. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  854. if (err.error != Callable::CallError::CALL_OK) {
  855. return;
  856. }
  857. if (!p_only_functions) {
  858. List<PropertyInfo> members;
  859. if (p_base.value.get_type() != Variant::NIL) {
  860. p_base.value.get_property_list(&members);
  861. } else {
  862. tmp.get_property_list(&members);
  863. }
  864. for (List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  865. if (String(E->get().name).find("/") == -1) {
  866. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_MEMBER);
  867. r_result.insert(option.display, option);
  868. }
  869. }
  870. }
  871. List<MethodInfo> methods;
  872. tmp.get_method_list(&methods);
  873. for (const List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  874. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_FUNCTION);
  875. if (E->get().arguments.size()) {
  876. option.insert_text += "(";
  877. } else {
  878. option.insert_text += "()";
  879. }
  880. r_result.insert(option.display, option);
  881. }
  882. return;
  883. } break;
  884. default: {
  885. return;
  886. } break;
  887. }
  888. }
  889. }
  890. static void _find_identifiers(GDScriptParser::CompletionContext &p_context, bool p_only_functions, Map<String, ScriptCodeCompletionOption> &r_result, int p_recursion_depth) {
  891. if (!p_only_functions && p_context.current_suite) {
  892. // This includes function parameters, since they are also locals.
  893. _find_identifiers_in_suite(p_context.current_suite, r_result);
  894. }
  895. if (p_context.current_class) {
  896. _find_identifiers_in_class(p_context.current_class, p_only_functions, (!p_context.current_function || p_context.current_function->is_static), false, r_result, p_recursion_depth + 1);
  897. }
  898. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  899. MethodInfo function = GDScriptFunctions::get_info(GDScriptFunctions::Function(i));
  900. ScriptCodeCompletionOption option(String(GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i))), ScriptCodeCompletionOption::KIND_FUNCTION);
  901. if (function.arguments.size() || (function.flags & METHOD_FLAG_VARARG)) {
  902. option.insert_text += "(";
  903. } else {
  904. option.insert_text += "()";
  905. }
  906. r_result.insert(option.display, option);
  907. }
  908. if (p_only_functions) {
  909. return;
  910. }
  911. static const char *_type_names[Variant::VARIANT_MAX] = {
  912. "null", "bool", "int", "float", "String", "StringName", "Vector2", "Vector2i", "Rect2", "Rect2i", "Vector3", "Vector3i", "Transform2D", "Plane", "Quat", "AABB", "Basis", "Transform",
  913. "Color", "NodePath", "RID", "Signal", "Callable", "Object", "Dictionary", "Array", "PackedByteArray", "PackedInt32Array", "PackedInt64Array", "PackedFloat32Array", "PackedFloat64Array", "PackedStringArray",
  914. "PackedVector2Array", "PackedVector3Array", "PackedColorArray"
  915. };
  916. static_assert((sizeof(_type_names) / sizeof(*_type_names)) == Variant::VARIANT_MAX, "Completion for builtin types is incomplete");
  917. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  918. ScriptCodeCompletionOption option(_type_names[i], ScriptCodeCompletionOption::KIND_CLASS);
  919. r_result.insert(option.display, option);
  920. }
  921. static const char *_keywords[] = {
  922. "and", "in", "not", "or", "false", "PI", "TAU", "INF", "NAN", "self", "true", "as", "assert",
  923. "breakpoint", "class", "extends", "is", "func", "preload", "signal", "tool", "await",
  924. "const", "enum", "static", "super", "var", "break", "continue", "if", "elif",
  925. "else", "for", "pass", "return", "match", "while",
  926. 0
  927. };
  928. const char **kw = _keywords;
  929. while (*kw) {
  930. ScriptCodeCompletionOption option(*kw, ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  931. r_result.insert(option.display, option);
  932. kw++;
  933. }
  934. Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  935. for (const Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E != nullptr; E = E->next()) {
  936. if (!E->value().is_singleton) {
  937. continue;
  938. }
  939. ScriptCodeCompletionOption option(E->key(), ScriptCodeCompletionOption::KIND_CONSTANT);
  940. r_result.insert(option.display, option);
  941. }
  942. // Native classes and global constants.
  943. for (const Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  944. ScriptCodeCompletionOption option;
  945. if (ClassDB::class_exists(E->key()) || Engine::get_singleton()->has_singleton(E->key())) {
  946. option = ScriptCodeCompletionOption(E->key().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  947. } else {
  948. option = ScriptCodeCompletionOption(E->key().operator String(), ScriptCodeCompletionOption::KIND_CONSTANT);
  949. }
  950. r_result.insert(option.display, option);
  951. }
  952. }
  953. static GDScriptCompletionIdentifier _type_from_variant(const Variant &p_value) {
  954. GDScriptCompletionIdentifier ci;
  955. ci.value = p_value;
  956. ci.type.is_constant = true;
  957. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  958. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  959. ci.type.builtin_type = p_value.get_type();
  960. if (ci.type.builtin_type == Variant::OBJECT) {
  961. Object *obj = p_value.operator Object *();
  962. if (!obj) {
  963. return ci;
  964. }
  965. ci.type.native_type = obj->get_class_name();
  966. Ref<Script> scr = p_value;
  967. if (scr.is_valid()) {
  968. ci.type.is_meta_type = true;
  969. } else {
  970. ci.type.is_meta_type = false;
  971. scr = obj->get_script();
  972. }
  973. if (scr.is_valid()) {
  974. ci.type.script_type = scr;
  975. ci.type.kind = GDScriptParser::DataType::SCRIPT;
  976. ci.type.native_type = scr->get_instance_base_type();
  977. } else {
  978. ci.type.kind = GDScriptParser::DataType::NATIVE;
  979. }
  980. }
  981. return ci;
  982. }
  983. static GDScriptCompletionIdentifier _type_from_property(const PropertyInfo &p_property) {
  984. GDScriptCompletionIdentifier ci;
  985. if (p_property.type == Variant::NIL) {
  986. // Variant
  987. return ci;
  988. }
  989. if (p_property.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  990. ci.enumeration = p_property.class_name;
  991. }
  992. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  993. ci.type.builtin_type = p_property.type;
  994. if (p_property.type == Variant::OBJECT) {
  995. ci.type.kind = GDScriptParser::DataType::NATIVE;
  996. ci.type.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  997. } else {
  998. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  999. }
  1000. return ci;
  1001. }
  1002. static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  1003. static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  1004. static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type);
  1005. static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::ExpressionNode *p_expression, GDScriptCompletionIdentifier &r_type) {
  1006. bool found = false;
  1007. if (p_expression->is_constant) {
  1008. // Already has a value, so just use that.
  1009. r_type = _type_from_variant(p_expression->reduced_value);
  1010. found = true;
  1011. } else {
  1012. switch (p_expression->type) {
  1013. case GDScriptParser::Node::LITERAL: {
  1014. const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(p_expression);
  1015. r_type = _type_from_variant(literal->value);
  1016. found = true;
  1017. } break;
  1018. case GDScriptParser::Node::SELF: {
  1019. if (p_context.current_class) {
  1020. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1021. r_type.type.type_source = GDScriptParser::DataType::INFERRED;
  1022. r_type.type.is_constant = true;
  1023. r_type.type.class_type = p_context.current_class;
  1024. r_type.value = p_context.base;
  1025. found = true;
  1026. }
  1027. } break;
  1028. case GDScriptParser::Node::IDENTIFIER: {
  1029. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  1030. found = _guess_identifier_type(p_context, id->name, r_type);
  1031. } break;
  1032. case GDScriptParser::Node::DICTIONARY: {
  1033. // Try to recreate the dictionary.
  1034. const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
  1035. Dictionary d;
  1036. bool full = true;
  1037. for (int i = 0; i < dn->elements.size(); i++) {
  1038. GDScriptCompletionIdentifier key;
  1039. if (_guess_expression_type(p_context, dn->elements[i].key, key)) {
  1040. if (!key.type.is_constant) {
  1041. full = false;
  1042. break;
  1043. }
  1044. GDScriptCompletionIdentifier value;
  1045. if (_guess_expression_type(p_context, dn->elements[i].value, value)) {
  1046. if (!value.type.is_constant) {
  1047. full = false;
  1048. break;
  1049. }
  1050. d[key.value] = value.value;
  1051. } else {
  1052. full = false;
  1053. break;
  1054. }
  1055. } else {
  1056. full = false;
  1057. break;
  1058. }
  1059. }
  1060. if (full) {
  1061. r_type.value = d;
  1062. r_type.type.is_constant = true;
  1063. }
  1064. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1065. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1066. r_type.type.builtin_type = Variant::DICTIONARY;
  1067. found = true;
  1068. } break;
  1069. case GDScriptParser::Node::ARRAY: {
  1070. // Try to recreate the array
  1071. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
  1072. Array a;
  1073. bool full = true;
  1074. a.resize(an->elements.size());
  1075. for (int i = 0; i < an->elements.size(); i++) {
  1076. GDScriptCompletionIdentifier value;
  1077. if (_guess_expression_type(p_context, an->elements[i], value)) {
  1078. if (value.type.is_constant) {
  1079. a[i] = value.value;
  1080. } else {
  1081. full = false;
  1082. break;
  1083. }
  1084. } else {
  1085. full = false;
  1086. break;
  1087. }
  1088. }
  1089. if (full) {
  1090. // If not fully constant, setting this value is detrimental to the inference.
  1091. r_type.value = a;
  1092. }
  1093. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1094. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1095. r_type.type.builtin_type = Variant::ARRAY;
  1096. found = true;
  1097. } break;
  1098. case GDScriptParser::Node::CAST: {
  1099. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  1100. GDScriptCompletionIdentifier value;
  1101. if (_guess_expression_type(p_context, cn->operand, r_type)) {
  1102. r_type.type = cn->get_datatype();
  1103. found = true;
  1104. }
  1105. } break;
  1106. case GDScriptParser::Node::CALL: {
  1107. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_expression);
  1108. if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
  1109. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1110. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1111. r_type.type.builtin_type = GDScriptParser::get_builtin_type(call->function_name);
  1112. found = true;
  1113. break;
  1114. } else if (GDScriptParser::get_builtin_function(call->function_name) < GDScriptFunctions::FUNC_MAX) {
  1115. MethodInfo mi = GDScriptFunctions::get_info(GDScriptParser::get_builtin_function(call->function_name));
  1116. r_type = _type_from_property(mi.return_val);
  1117. found = true;
  1118. break;
  1119. } else {
  1120. GDScriptParser::CompletionContext c = p_context;
  1121. c.current_line = call->start_line;
  1122. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  1123. GDScriptCompletionIdentifier base;
  1124. if (callee_type == GDScriptParser::Node::IDENTIFIER || call->is_super) {
  1125. // Simple call, so base is 'self'.
  1126. if (p_context.current_class) {
  1127. base.type.kind = GDScriptParser::DataType::CLASS;
  1128. base.type.type_source = GDScriptParser::DataType::INFERRED;
  1129. base.type.is_constant = true;
  1130. base.type.class_type = p_context.current_class;
  1131. base.value = p_context.base;
  1132. } else {
  1133. break;
  1134. }
  1135. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT && static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->is_attribute) {
  1136. if (!_guess_expression_type(c, static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->base, base)) {
  1137. found = false;
  1138. break;
  1139. }
  1140. } else {
  1141. break;
  1142. }
  1143. // Try call if constant methods with constant arguments
  1144. if (base.type.is_constant && base.value.get_type() == Variant::OBJECT) {
  1145. GDScriptParser::DataType native_type = base.type;
  1146. while (native_type.kind == GDScriptParser::DataType::CLASS) {
  1147. native_type = native_type.class_type->base_type;
  1148. }
  1149. while (native_type.kind == GDScriptParser::DataType::SCRIPT) {
  1150. if (native_type.script_type.is_valid()) {
  1151. Ref<Script> parent = native_type.script_type->get_base_script();
  1152. if (parent.is_valid()) {
  1153. native_type.script_type = parent;
  1154. } else {
  1155. native_type.kind = GDScriptParser::DataType::NATIVE;
  1156. native_type.native_type = native_type.script_type->get_instance_base_type();
  1157. if (!ClassDB::class_exists(native_type.native_type)) {
  1158. native_type.native_type = String("_") + native_type.native_type;
  1159. if (!ClassDB::class_exists(native_type.native_type)) {
  1160. native_type.kind = GDScriptParser::DataType::UNRESOLVED;
  1161. }
  1162. }
  1163. }
  1164. }
  1165. }
  1166. if (native_type.kind == GDScriptParser::DataType::NATIVE) {
  1167. MethodBind *mb = ClassDB::get_method(native_type.native_type, call->function_name);
  1168. if (mb && mb->is_const()) {
  1169. bool all_is_const = true;
  1170. Vector<Variant> args;
  1171. GDScriptParser::CompletionContext c2 = p_context;
  1172. c2.current_line = call->start_line;
  1173. for (int i = 0; all_is_const && i < call->arguments.size(); i++) {
  1174. GDScriptCompletionIdentifier arg;
  1175. if (!call->arguments[i]->is_constant) {
  1176. all_is_const = false;
  1177. }
  1178. }
  1179. Object *baseptr = base.value;
  1180. if (all_is_const && String(call->function_name) == "get_node" && ClassDB::is_parent_class(native_type.native_type, "Node") && args.size()) {
  1181. String arg1 = args[0];
  1182. if (arg1.begins_with("/root/")) {
  1183. String which = arg1.get_slice("/", 2);
  1184. if (which != "") {
  1185. // Try singletons first
  1186. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(which)) {
  1187. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[which]);
  1188. found = true;
  1189. } else {
  1190. Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  1191. for (Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E; E = E->next()) {
  1192. String name = E->key();
  1193. if (name == which) {
  1194. String script = E->value().path;
  1195. if (!script.begins_with("res://")) {
  1196. script = "res://" + script;
  1197. }
  1198. if (!script.ends_with(".gd")) {
  1199. //not a script, try find the script anyway,
  1200. //may have some success
  1201. script = script.get_basename() + ".gd";
  1202. }
  1203. if (FileAccess::exists(script)) {
  1204. Error err = OK;
  1205. Ref<GDScriptParserRef> parser = GDScriptCache::get_parser(script, GDScriptParserRef::INTERFACE_SOLVED, err);
  1206. if (err == OK) {
  1207. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1208. r_type.type.script_path = script;
  1209. r_type.type.class_type = parser->get_parser()->get_tree();
  1210. r_type.type.is_constant = false;
  1211. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1212. r_type.value = Variant();
  1213. p_context.dependent_parsers.push_back(parser);
  1214. found = true;
  1215. }
  1216. }
  1217. break;
  1218. }
  1219. }
  1220. }
  1221. }
  1222. }
  1223. }
  1224. if (!found && all_is_const && baseptr) {
  1225. Vector<const Variant *> argptr;
  1226. for (int i = 0; i < args.size(); i++) {
  1227. argptr.push_back(&args[i]);
  1228. }
  1229. Callable::CallError ce;
  1230. Variant ret = mb->call(baseptr, (const Variant **)argptr.ptr(), argptr.size(), ce);
  1231. if (ce.error == Callable::CallError::CALL_OK && ret.get_type() != Variant::NIL) {
  1232. if (ret.get_type() != Variant::OBJECT || ret.operator Object *() != nullptr) {
  1233. r_type = _type_from_variant(ret);
  1234. found = true;
  1235. }
  1236. }
  1237. }
  1238. }
  1239. }
  1240. }
  1241. if (!found) {
  1242. found = _guess_method_return_type_from_base(c, base, call->function_name, r_type);
  1243. }
  1244. }
  1245. } break;
  1246. case GDScriptParser::Node::SUBSCRIPT: {
  1247. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_expression);
  1248. if (subscript->is_attribute) {
  1249. GDScriptParser::CompletionContext c = p_context;
  1250. c.current_line = subscript->start_line;
  1251. GDScriptCompletionIdentifier base;
  1252. if (!_guess_expression_type(c, subscript->base, base)) {
  1253. found = false;
  1254. break;
  1255. }
  1256. if (base.value.get_type() == Variant::DICTIONARY && base.value.operator Dictionary().has(String(subscript->attribute->name))) {
  1257. Variant value = base.value.operator Dictionary()[String(subscript->attribute->name)];
  1258. r_type = _type_from_variant(value);
  1259. found = true;
  1260. break;
  1261. }
  1262. const GDScriptParser::DictionaryNode *dn = nullptr;
  1263. if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  1264. dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
  1265. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
  1266. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  1267. }
  1268. if (dn) {
  1269. for (int i = 0; i < dn->elements.size(); i++) {
  1270. GDScriptCompletionIdentifier key;
  1271. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  1272. continue;
  1273. }
  1274. if (key.value == String(subscript->attribute->name)) {
  1275. r_type.assigned_expression = dn->elements[i].value;
  1276. found = _guess_expression_type(c, dn->elements[i].value, r_type);
  1277. break;
  1278. }
  1279. }
  1280. }
  1281. if (!found) {
  1282. found = _guess_identifier_type_from_base(c, base, subscript->attribute->name, r_type);
  1283. }
  1284. } else {
  1285. if (subscript->index == nullptr) {
  1286. found = false;
  1287. break;
  1288. }
  1289. GDScriptParser::CompletionContext c = p_context;
  1290. c.current_line = subscript->start_line;
  1291. GDScriptCompletionIdentifier base;
  1292. if (!_guess_expression_type(c, subscript->base, base)) {
  1293. found = false;
  1294. break;
  1295. }
  1296. GDScriptCompletionIdentifier index;
  1297. if (!_guess_expression_type(c, subscript->index, index)) {
  1298. found = false;
  1299. break;
  1300. }
  1301. if (base.value.in(index.value)) {
  1302. Variant value = base.value.get(index.value);
  1303. r_type = _type_from_variant(value);
  1304. found = true;
  1305. break;
  1306. }
  1307. // Look if it is a dictionary node.
  1308. const GDScriptParser::DictionaryNode *dn = nullptr;
  1309. if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  1310. dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
  1311. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
  1312. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  1313. }
  1314. if (dn) {
  1315. for (int i = 0; i < dn->elements.size(); i++) {
  1316. GDScriptCompletionIdentifier key;
  1317. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  1318. continue;
  1319. }
  1320. if (key.value == index.value) {
  1321. r_type.assigned_expression = dn->elements[i].value;
  1322. found = _guess_expression_type(p_context, dn->elements[i].value, r_type);
  1323. break;
  1324. }
  1325. }
  1326. }
  1327. // Look if it is an array node.
  1328. if (!found && index.value.is_num()) {
  1329. int idx = index.value;
  1330. const GDScriptParser::ArrayNode *an = nullptr;
  1331. if (subscript->base->type == GDScriptParser::Node::ARRAY) {
  1332. an = static_cast<const GDScriptParser::ArrayNode *>(subscript->base);
  1333. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::ARRAY) {
  1334. an = static_cast<const GDScriptParser::ArrayNode *>(base.assigned_expression);
  1335. }
  1336. if (an && idx >= 0 && an->elements.size() > idx) {
  1337. r_type.assigned_expression = an->elements[idx];
  1338. found = _guess_expression_type(c, an->elements[idx], r_type);
  1339. break;
  1340. }
  1341. }
  1342. // Look for valid indexing in other types
  1343. if (!found && (index.value.get_type() == Variant::STRING || index.value.get_type() == Variant::NODE_PATH)) {
  1344. StringName id = index.value;
  1345. found = _guess_identifier_type_from_base(c, base, id, r_type);
  1346. } else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) {
  1347. Callable::CallError err;
  1348. Variant base_val;
  1349. Variant::construct(base.type.builtin_type, base_val, nullptr, 0, err);
  1350. bool valid = false;
  1351. Variant res = base_val.get(index.value, &valid);
  1352. if (valid) {
  1353. r_type = _type_from_variant(res);
  1354. r_type.value = Variant();
  1355. r_type.type.is_constant = false;
  1356. found = true;
  1357. }
  1358. }
  1359. }
  1360. } break;
  1361. case GDScriptParser::Node::BINARY_OPERATOR: {
  1362. const GDScriptParser::BinaryOpNode *op = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression);
  1363. if (op->variant_op == Variant::OP_MAX) {
  1364. break;
  1365. }
  1366. GDScriptParser::CompletionContext context = p_context;
  1367. context.current_line = op->start_line;
  1368. GDScriptCompletionIdentifier p1;
  1369. GDScriptCompletionIdentifier p2;
  1370. if (!_guess_expression_type(context, op->left_operand, p1)) {
  1371. found = false;
  1372. break;
  1373. }
  1374. if (!_guess_expression_type(context, op->right_operand, p2)) {
  1375. found = false;
  1376. break;
  1377. }
  1378. Callable::CallError ce;
  1379. bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
  1380. Variant d1;
  1381. Variant::construct(p1.type.builtin_type, d1, nullptr, 0, ce);
  1382. Variant d2;
  1383. Variant::construct(p2.type.builtin_type, d2, nullptr, 0, ce);
  1384. Variant v1 = (v1_use_value) ? p1.value : d1;
  1385. bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
  1386. Variant v2 = (v2_use_value) ? p2.value : d2;
  1387. // avoid potential invalid ops
  1388. if ((op->variant_op == Variant::OP_DIVIDE || op->variant_op == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
  1389. v2 = 1;
  1390. v2_use_value = false;
  1391. }
  1392. if (op->variant_op == Variant::OP_DIVIDE && v2.get_type() == Variant::FLOAT) {
  1393. v2 = 1.0;
  1394. v2_use_value = false;
  1395. }
  1396. Variant res;
  1397. bool valid;
  1398. Variant::evaluate(op->variant_op, v1, v2, res, valid);
  1399. if (!valid) {
  1400. found = false;
  1401. break;
  1402. }
  1403. r_type = _type_from_variant(res);
  1404. if (!v1_use_value || !v2_use_value) {
  1405. r_type.value = Variant();
  1406. r_type.type.is_constant = false;
  1407. }
  1408. found = true;
  1409. } break;
  1410. default:
  1411. break;
  1412. }
  1413. }
  1414. // It may have found a null, but that's never useful
  1415. if (found && r_type.type.kind == GDScriptParser::DataType::BUILTIN && r_type.type.builtin_type == Variant::NIL) {
  1416. found = false;
  1417. }
  1418. // Check type hint last. For collections we want chance to get the actual value first
  1419. // This way we can detect types from the content of dictionaries and arrays
  1420. if (!found && p_expression->get_datatype().is_hard_type()) {
  1421. r_type.type = p_expression->get_datatype();
  1422. if (!r_type.assigned_expression) {
  1423. r_type.assigned_expression = p_expression;
  1424. }
  1425. found = true;
  1426. }
  1427. return found;
  1428. }
  1429. static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  1430. // Look in blocks first.
  1431. int last_assign_line = -1;
  1432. const GDScriptParser::ExpressionNode *last_assigned_expression = nullptr;
  1433. GDScriptParser::DataType id_type;
  1434. GDScriptParser::SuiteNode *suite = p_context.current_suite;
  1435. bool is_function_parameter = false;
  1436. if (suite) {
  1437. if (suite->has_local(p_identifier)) {
  1438. const GDScriptParser::SuiteNode::Local &local = suite->get_local(p_identifier);
  1439. id_type = local.get_datatype();
  1440. // Check initializer as the first assignment.
  1441. switch (local.type) {
  1442. case GDScriptParser::SuiteNode::Local::VARIABLE:
  1443. if (local.variable->initializer) {
  1444. last_assign_line = local.variable->initializer->end_line;
  1445. last_assigned_expression = local.variable->initializer;
  1446. }
  1447. break;
  1448. case GDScriptParser::SuiteNode::Local::CONSTANT:
  1449. if (local.constant->initializer) {
  1450. last_assign_line = local.constant->initializer->end_line;
  1451. last_assigned_expression = local.constant->initializer;
  1452. }
  1453. break;
  1454. case GDScriptParser::SuiteNode::Local::PARAMETER:
  1455. if (local.parameter->default_value) {
  1456. last_assign_line = local.parameter->default_value->end_line;
  1457. last_assigned_expression = local.parameter->default_value;
  1458. }
  1459. is_function_parameter = true;
  1460. break;
  1461. default:
  1462. break;
  1463. }
  1464. }
  1465. }
  1466. while (suite) {
  1467. for (int i = 0; i < suite->statements.size(); i++) {
  1468. if (suite->statements[i]->start_line > p_context.current_line) {
  1469. break;
  1470. }
  1471. switch (suite->statements[i]->type) {
  1472. case GDScriptParser::Node::ASSIGNMENT: {
  1473. const GDScriptParser::AssignmentNode *assign = static_cast<const GDScriptParser::AssignmentNode *>(suite->statements[i]);
  1474. if (assign->end_line > last_assign_line && assign->assignee && assign->assigned_value && assign->assignee->type == GDScriptParser::Node::IDENTIFIER) {
  1475. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(assign->assignee);
  1476. if (id->name == p_identifier) {
  1477. last_assign_line = assign->assigned_value->end_line;
  1478. last_assigned_expression = assign->assigned_value;
  1479. }
  1480. }
  1481. } break;
  1482. default:
  1483. // TODO: Check sub blocks (control flow statements) as they might also reassign stuff.
  1484. break;
  1485. }
  1486. }
  1487. if (suite->parent_if && suite->parent_if->condition && suite->parent_if->condition->type == GDScriptParser::Node::BINARY_OPERATOR && static_cast<const GDScriptParser::BinaryOpNode *>(suite->parent_if->condition)->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) {
  1488. // Operator `is` used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
  1489. // Super dirty hack, but very useful.
  1490. // Credit: Zylann.
  1491. // TODO: this could be hacked to detect ANDed conditions too...
  1492. const GDScriptParser::BinaryOpNode *op = static_cast<const GDScriptParser::BinaryOpNode *>(suite->parent_if->condition);
  1493. if (op->left_operand && op->right_operand && op->left_operand->type == GDScriptParser::Node::IDENTIFIER && static_cast<const GDScriptParser::IdentifierNode *>(op->left_operand)->name == p_identifier) {
  1494. // Bingo.
  1495. GDScriptParser::CompletionContext c = p_context;
  1496. c.current_line = op->left_operand->start_line;
  1497. c.current_suite = suite;
  1498. GDScriptCompletionIdentifier is_type;
  1499. if (_guess_expression_type(c, op->right_operand, is_type)) {
  1500. id_type = is_type.type;
  1501. id_type.is_meta_type = false;
  1502. if (last_assign_line < c.current_line) {
  1503. // Override last assignment.
  1504. last_assign_line = c.current_line;
  1505. last_assigned_expression = nullptr;
  1506. }
  1507. }
  1508. }
  1509. }
  1510. suite = suite->parent_block;
  1511. }
  1512. if (last_assigned_expression && last_assign_line != p_context.current_line) {
  1513. GDScriptParser::CompletionContext c = p_context;
  1514. c.current_line = last_assign_line;
  1515. r_type.assigned_expression = last_assigned_expression;
  1516. if (_guess_expression_type(c, last_assigned_expression, r_type)) {
  1517. return true;
  1518. }
  1519. }
  1520. if (is_function_parameter && p_context.current_function && p_context.current_class) {
  1521. // Check if it's override of native function, then we can assume the type from the signature.
  1522. GDScriptParser::DataType base_type = p_context.current_class->base_type;
  1523. while (base_type.is_set()) {
  1524. switch (base_type.kind) {
  1525. case GDScriptParser::DataType::CLASS:
  1526. if (base_type.class_type->has_function(p_context.current_function->identifier->name)) {
  1527. GDScriptParser::FunctionNode *parent_function = base_type.class_type->get_member(p_context.current_function->identifier->name).function;
  1528. const GDScriptParser::ParameterNode *parameter = parent_function->parameters[parent_function->parameters_indices[p_identifier]];
  1529. if ((!id_type.is_set() || id_type.is_variant()) && parameter->get_datatype().is_hard_type()) {
  1530. id_type = parameter->get_datatype();
  1531. }
  1532. if (parameter->default_value) {
  1533. GDScriptParser::CompletionContext c = p_context;
  1534. c.current_function = parent_function;
  1535. c.current_class = base_type.class_type;
  1536. c.base = nullptr;
  1537. if (_guess_expression_type(c, parameter->default_value, r_type)) {
  1538. return true;
  1539. }
  1540. }
  1541. base_type = base_type.class_type->base_type;
  1542. }
  1543. break;
  1544. case GDScriptParser::DataType::NATIVE: {
  1545. if (id_type.is_set() && !id_type.is_variant()) {
  1546. base_type = GDScriptParser::DataType();
  1547. break;
  1548. }
  1549. StringName real_native = _get_real_class_name(base_type.native_type);
  1550. MethodInfo info;
  1551. if (ClassDB::get_method_info(real_native, p_context.current_function->identifier->name, &info)) {
  1552. for (const List<PropertyInfo>::Element *E = info.arguments.front(); E; E = E->next()) {
  1553. if (E->get().name == p_identifier) {
  1554. r_type = _type_from_property(E->get());
  1555. return true;
  1556. }
  1557. }
  1558. }
  1559. base_type = GDScriptParser::DataType();
  1560. } break;
  1561. default:
  1562. break;
  1563. }
  1564. }
  1565. }
  1566. if (id_type.is_set() && !id_type.is_variant()) {
  1567. r_type.type = id_type;
  1568. return true;
  1569. }
  1570. // Check current class (including inheritance).
  1571. if (p_context.current_class) {
  1572. GDScriptCompletionIdentifier base;
  1573. base.value = p_context.base;
  1574. base.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1575. base.type.kind = GDScriptParser::DataType::CLASS;
  1576. base.type.class_type = p_context.current_class;
  1577. base.type.is_meta_type = p_context.current_function && p_context.current_function->is_static;
  1578. if (_guess_identifier_type_from_base(p_context, base, p_identifier, r_type)) {
  1579. return true;
  1580. }
  1581. }
  1582. // Check global scripts.
  1583. if (ScriptServer::is_global_class(p_identifier)) {
  1584. String script = ScriptServer::get_global_class_path(p_identifier);
  1585. if (script.to_lower().ends_with(".gd")) {
  1586. Error err = OK;
  1587. Ref<GDScriptParserRef> parser = GDScriptCache::get_parser(script, GDScriptParserRef::INTERFACE_SOLVED, err);
  1588. if (err == OK) {
  1589. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1590. r_type.type.script_path = script;
  1591. r_type.type.class_type = parser->get_parser()->get_tree();
  1592. r_type.type.is_constant = false;
  1593. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1594. r_type.value = Variant();
  1595. p_context.dependent_parsers.push_back(parser);
  1596. return true;
  1597. }
  1598. } else {
  1599. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier));
  1600. if (scr.is_valid()) {
  1601. r_type = _type_from_variant(scr);
  1602. r_type.type.is_meta_type = true;
  1603. return true;
  1604. }
  1605. }
  1606. return false;
  1607. }
  1608. // Check autoloads.
  1609. if (ProjectSettings::get_singleton()->has_autoload(p_identifier)) {
  1610. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier]);
  1611. return true;
  1612. }
  1613. // Check ClassDB.
  1614. StringName class_name = _get_real_class_name(p_identifier);
  1615. if (ClassDB::class_exists(class_name) && ClassDB::is_class_exposed(class_name)) {
  1616. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1617. r_type.type.kind = GDScriptParser::DataType::NATIVE;
  1618. r_type.type.native_type = p_identifier;
  1619. r_type.type.is_constant = true;
  1620. r_type.type.is_meta_type = !Engine::get_singleton()->has_singleton(class_name);
  1621. r_type.value = Variant();
  1622. }
  1623. return false;
  1624. }
  1625. static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  1626. GDScriptParser::DataType base_type = p_base.type;
  1627. bool is_static = base_type.is_meta_type;
  1628. while (base_type.is_set()) {
  1629. switch (base_type.kind) {
  1630. case GDScriptParser::DataType::CLASS:
  1631. if (base_type.class_type->has_member(p_identifier)) {
  1632. const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_identifier);
  1633. switch (member.type) {
  1634. case GDScriptParser::ClassNode::Member::CONSTANT:
  1635. r_type.type = member.constant->get_datatype();
  1636. if (member.constant->initializer && member.constant->initializer->is_constant) {
  1637. r_type.value = member.constant->initializer->reduced_value;
  1638. }
  1639. return true;
  1640. case GDScriptParser::ClassNode::Member::VARIABLE:
  1641. if (!is_static) {
  1642. if (member.variable->initializer) {
  1643. const GDScriptParser::ExpressionNode *init = member.variable->initializer;
  1644. if (init->is_constant) {
  1645. r_type.value = init->reduced_value;
  1646. r_type = _type_from_variant(init->reduced_value);
  1647. return true;
  1648. } else if (init->start_line == p_context.current_line) {
  1649. return false;
  1650. } else if (_guess_expression_type(p_context, init, r_type)) {
  1651. return true;
  1652. } else if (init->get_datatype().is_set() && !init->get_datatype().is_variant()) {
  1653. r_type.type = init->get_datatype();
  1654. return true;
  1655. }
  1656. } else if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
  1657. r_type.type = member.variable->get_datatype();
  1658. return true;
  1659. }
  1660. }
  1661. // TODO: Check assignments in constructor.
  1662. return false;
  1663. case GDScriptParser::ClassNode::Member::ENUM:
  1664. r_type.type = member.m_enum->get_datatype();
  1665. r_type.enumeration = member.m_enum->identifier->name;
  1666. return true;
  1667. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  1668. r_type = _type_from_variant(member.enum_value.value);
  1669. return true;
  1670. case GDScriptParser::ClassNode::Member::SIGNAL:
  1671. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1672. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1673. r_type.type.builtin_type = Variant::SIGNAL;
  1674. return true;
  1675. case GDScriptParser::ClassNode::Member::FUNCTION:
  1676. if (is_static && !member.function->is_static) {
  1677. return false;
  1678. }
  1679. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1680. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1681. r_type.type.builtin_type = Variant::CALLABLE;
  1682. return true;
  1683. case GDScriptParser::ClassNode::Member::CLASS:
  1684. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1685. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1686. r_type.type.class_type = member.m_class;
  1687. return true;
  1688. case GDScriptParser::ClassNode::Member::UNDEFINED:
  1689. return false; // Unreachable.
  1690. }
  1691. return false;
  1692. }
  1693. base_type = base_type.class_type->base_type;
  1694. break;
  1695. case GDScriptParser::DataType::SCRIPT: {
  1696. Ref<Script> scr = base_type.script_type;
  1697. if (scr.is_valid()) {
  1698. Map<StringName, Variant> constants;
  1699. scr->get_constants(&constants);
  1700. if (constants.has(p_identifier)) {
  1701. r_type = _type_from_variant(constants[p_identifier]);
  1702. return true;
  1703. }
  1704. if (!is_static) {
  1705. List<PropertyInfo> members;
  1706. scr->get_script_property_list(&members);
  1707. for (const List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  1708. const PropertyInfo &prop = E->get();
  1709. if (prop.name == p_identifier) {
  1710. r_type = _type_from_property(prop);
  1711. return true;
  1712. }
  1713. }
  1714. }
  1715. Ref<Script> parent = scr->get_base_script();
  1716. if (parent.is_valid()) {
  1717. base_type.script_type = parent;
  1718. } else {
  1719. base_type.kind = GDScriptParser::DataType::NATIVE;
  1720. base_type.native_type = scr->get_instance_base_type();
  1721. }
  1722. } else {
  1723. return false;
  1724. }
  1725. } break;
  1726. case GDScriptParser::DataType::NATIVE: {
  1727. StringName class_name = _get_real_class_name(base_type.native_type);
  1728. if (!ClassDB::class_exists(class_name)) {
  1729. return false;
  1730. }
  1731. // Skip constants since they're all integers. Type does not matter because int has no members.
  1732. PropertyInfo prop;
  1733. if (ClassDB::get_property_info(class_name, p_identifier, &prop)) {
  1734. StringName getter = ClassDB::get_property_getter(class_name, p_identifier);
  1735. if (getter != StringName()) {
  1736. MethodBind *g = ClassDB::get_method(class_name, getter);
  1737. if (g) {
  1738. r_type = _type_from_property(g->get_return_info());
  1739. return true;
  1740. }
  1741. } else {
  1742. r_type = _type_from_property(prop);
  1743. return true;
  1744. }
  1745. }
  1746. return false;
  1747. } break;
  1748. case GDScriptParser::DataType::BUILTIN: {
  1749. Callable::CallError err;
  1750. Variant tmp;
  1751. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  1752. if (err.error != Callable::CallError::CALL_OK) {
  1753. return false;
  1754. }
  1755. bool valid = false;
  1756. Variant res = tmp.get(p_identifier, &valid);
  1757. if (valid) {
  1758. r_type = _type_from_variant(res);
  1759. r_type.value = Variant();
  1760. r_type.type.is_constant = false;
  1761. return true;
  1762. }
  1763. return false;
  1764. } break;
  1765. default: {
  1766. return false;
  1767. } break;
  1768. }
  1769. }
  1770. return false;
  1771. }
  1772. static void _find_last_return_in_block(GDScriptParser::CompletionContext &p_context, int &r_last_return_line, const GDScriptParser::ExpressionNode **r_last_returned_value) {
  1773. if (!p_context.current_suite) {
  1774. return;
  1775. }
  1776. for (int i = 0; i < p_context.current_suite->statements.size(); i++) {
  1777. if (p_context.current_suite->statements[i]->start_line < r_last_return_line) {
  1778. break;
  1779. }
  1780. GDScriptParser::CompletionContext c = p_context;
  1781. switch (p_context.current_suite->statements[i]->type) {
  1782. case GDScriptParser::Node::FOR:
  1783. c.current_suite = static_cast<const GDScriptParser::ForNode *>(p_context.current_suite->statements[i])->loop;
  1784. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1785. break;
  1786. case GDScriptParser::Node::WHILE:
  1787. c.current_suite = static_cast<const GDScriptParser::WhileNode *>(p_context.current_suite->statements[i])->loop;
  1788. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1789. break;
  1790. case GDScriptParser::Node::IF: {
  1791. const GDScriptParser::IfNode *_if = static_cast<const GDScriptParser::IfNode *>(p_context.current_suite->statements[i]);
  1792. c.current_suite = _if->true_block;
  1793. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1794. if (_if->false_block) {
  1795. c.current_suite = _if->false_block;
  1796. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1797. }
  1798. } break;
  1799. case GDScriptParser::Node::MATCH: {
  1800. const GDScriptParser::MatchNode *match = static_cast<const GDScriptParser::MatchNode *>(p_context.current_suite->statements[i]);
  1801. for (int j = 0; j < match->branches.size(); j++) {
  1802. c.current_suite = match->branches[j]->block;
  1803. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1804. }
  1805. } break;
  1806. case GDScriptParser::Node::RETURN: {
  1807. const GDScriptParser::ReturnNode *ret = static_cast<const GDScriptParser::ReturnNode *>(p_context.current_suite->statements[i]);
  1808. if (ret->return_value) {
  1809. if (ret->start_line > r_last_return_line) {
  1810. r_last_return_line = ret->start_line;
  1811. *r_last_returned_value = ret->return_value;
  1812. }
  1813. }
  1814. } break;
  1815. default:
  1816. break;
  1817. }
  1818. }
  1819. }
  1820. static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type) {
  1821. GDScriptParser::DataType base_type = p_base.type;
  1822. bool is_static = base_type.is_meta_type;
  1823. if (is_static && p_method == "new") {
  1824. r_type.type = base_type;
  1825. r_type.type.is_meta_type = false;
  1826. r_type.type.is_constant = false;
  1827. return true;
  1828. }
  1829. while (base_type.is_set() && !base_type.is_variant()) {
  1830. switch (base_type.kind) {
  1831. case GDScriptParser::DataType::CLASS:
  1832. if (base_type.class_type->has_function(p_method)) {
  1833. const GDScriptParser::FunctionNode *method = base_type.class_type->get_member(p_method).function;
  1834. if (!is_static || method->is_static) {
  1835. int last_return_line = -1;
  1836. const GDScriptParser::ExpressionNode *last_returned_value = nullptr;
  1837. GDScriptParser::CompletionContext c = p_context;
  1838. c.current_class = base_type.class_type;
  1839. c.current_function = const_cast<GDScriptParser::FunctionNode *>(method);
  1840. c.current_suite = method->body;
  1841. _find_last_return_in_block(c, last_return_line, &last_returned_value);
  1842. if (last_returned_value) {
  1843. c.current_line = c.current_suite->end_line;
  1844. if (_guess_expression_type(c, last_returned_value, r_type)) {
  1845. return true;
  1846. }
  1847. if (method->get_datatype().is_set() && !method->get_datatype().is_variant()) {
  1848. r_type.type = method->get_datatype();
  1849. return true;
  1850. }
  1851. }
  1852. }
  1853. }
  1854. base_type = base_type.class_type->base_type;
  1855. break;
  1856. case GDScriptParser::DataType::SCRIPT: {
  1857. Ref<Script> scr = base_type.script_type;
  1858. if (scr.is_valid()) {
  1859. List<MethodInfo> methods;
  1860. scr->get_script_method_list(&methods);
  1861. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1862. MethodInfo &mi = E->get();
  1863. if (mi.name == p_method) {
  1864. r_type = _type_from_property(mi.return_val);
  1865. return true;
  1866. }
  1867. }
  1868. Ref<Script> base_script = scr->get_base_script();
  1869. if (base_script.is_valid()) {
  1870. base_type.script_type = base_script;
  1871. } else {
  1872. base_type.kind = GDScriptParser::DataType::NATIVE;
  1873. base_type.native_type = scr->get_instance_base_type();
  1874. }
  1875. } else {
  1876. return false;
  1877. }
  1878. } break;
  1879. case GDScriptParser::DataType::NATIVE: {
  1880. StringName native = _get_real_class_name(base_type.native_type);
  1881. if (!ClassDB::class_exists(native)) {
  1882. return false;
  1883. }
  1884. MethodBind *mb = ClassDB::get_method(native, p_method);
  1885. if (mb) {
  1886. r_type = _type_from_property(mb->get_return_info());
  1887. return true;
  1888. }
  1889. return false;
  1890. } break;
  1891. case GDScriptParser::DataType::BUILTIN: {
  1892. Callable::CallError err;
  1893. Variant tmp;
  1894. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  1895. if (err.error != Callable::CallError::CALL_OK) {
  1896. return false;
  1897. }
  1898. List<MethodInfo> methods;
  1899. tmp.get_method_list(&methods);
  1900. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1901. MethodInfo &mi = E->get();
  1902. if (mi.name == p_method) {
  1903. r_type = _type_from_property(mi.return_val);
  1904. return true;
  1905. }
  1906. }
  1907. return false;
  1908. } break;
  1909. default: {
  1910. return false;
  1911. }
  1912. }
  1913. }
  1914. return false;
  1915. }
  1916. static void _find_enumeration_candidates(GDScriptParser::CompletionContext &p_context, const String &p_enum_hint, Map<String, ScriptCodeCompletionOption> &r_result) {
  1917. if (p_enum_hint.find(".") == -1) {
  1918. // Global constant or in the current class.
  1919. StringName current_enum = p_enum_hint;
  1920. if (p_context.current_class && p_context.current_class->has_member(current_enum) && p_context.current_class->get_member(current_enum).type == GDScriptParser::ClassNode::Member::ENUM) {
  1921. const GDScriptParser::EnumNode *_enum = p_context.current_class->get_member(current_enum).m_enum;
  1922. for (int i = 0; i < _enum->values.size(); i++) {
  1923. ScriptCodeCompletionOption option(_enum->values[i].identifier->name, ScriptCodeCompletionOption::KIND_ENUM);
  1924. r_result.insert(option.display, option);
  1925. }
  1926. } else {
  1927. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  1928. if (CoreConstants::get_global_constant_enum(i) == current_enum) {
  1929. ScriptCodeCompletionOption option(CoreConstants::get_global_constant_name(i), ScriptCodeCompletionOption::KIND_ENUM);
  1930. r_result.insert(option.display, option);
  1931. }
  1932. }
  1933. }
  1934. } else {
  1935. String class_name = p_enum_hint.get_slice(".", 0);
  1936. String enum_name = p_enum_hint.get_slice(".", 1);
  1937. if (!ClassDB::class_exists(class_name)) {
  1938. return;
  1939. }
  1940. List<StringName> enum_constants;
  1941. ClassDB::get_enum_constants(class_name, enum_name, &enum_constants);
  1942. for (List<StringName>::Element *E = enum_constants.front(); E; E = E->next()) {
  1943. String candidate = class_name + "." + E->get();
  1944. ScriptCodeCompletionOption option(candidate, ScriptCodeCompletionOption::KIND_ENUM);
  1945. r_result.insert(option.display, option);
  1946. }
  1947. }
  1948. }
  1949. static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, int p_argidx, bool p_static, Map<String, ScriptCodeCompletionOption> &r_result, String &r_arghint) {
  1950. Variant base = p_base.value;
  1951. GDScriptParser::DataType base_type = p_base.type;
  1952. const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
  1953. while (base_type.is_set() && !base_type.is_variant()) {
  1954. switch (base_type.kind) {
  1955. case GDScriptParser::DataType::CLASS: {
  1956. if (base_type.class_type->has_member(p_method)) {
  1957. const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_method);
  1958. if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
  1959. r_arghint = _make_arguments_hint(member.function, p_argidx);
  1960. return;
  1961. }
  1962. }
  1963. base_type = base_type.class_type->base_type;
  1964. } break;
  1965. case GDScriptParser::DataType::NATIVE: {
  1966. StringName class_name = _get_real_class_name(base_type.native_type);
  1967. if (!ClassDB::class_exists(class_name)) {
  1968. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  1969. break;
  1970. }
  1971. MethodInfo info;
  1972. int method_args = 0;
  1973. if (ClassDB::get_method_info(class_name, p_method, &info)) {
  1974. method_args = info.arguments.size();
  1975. if (base.get_type() == Variant::OBJECT) {
  1976. Object *obj = base.operator Object *();
  1977. if (obj) {
  1978. List<String> options;
  1979. obj->get_argument_options(p_method, p_argidx, &options);
  1980. for (List<String>::Element *F = options.front(); F; F = F->next()) {
  1981. ScriptCodeCompletionOption option(F->get(), ScriptCodeCompletionOption::KIND_FUNCTION);
  1982. r_result.insert(option.display, option);
  1983. }
  1984. }
  1985. }
  1986. if (p_argidx < method_args) {
  1987. PropertyInfo arg_info = info.arguments[p_argidx];
  1988. if (arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  1989. _find_enumeration_candidates(p_context, arg_info.class_name, r_result);
  1990. }
  1991. }
  1992. r_arghint = _make_arguments_hint(info, p_argidx);
  1993. return;
  1994. }
  1995. if (ClassDB::is_parent_class(class_name, "Node") && (p_method == "get_node" || p_method == "has_node") && p_argidx == 0) {
  1996. // Get autoloads
  1997. List<PropertyInfo> props;
  1998. ProjectSettings::get_singleton()->get_property_list(&props);
  1999. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2000. String s = E->get().name;
  2001. if (!s.begins_with("autoload/")) {
  2002. continue;
  2003. }
  2004. String name = s.get_slice("/", 1);
  2005. ScriptCodeCompletionOption option("/root/" + name, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2006. option.insert_text = quote_style + option.display + quote_style;
  2007. r_result.insert(option.display, option);
  2008. }
  2009. }
  2010. if (p_argidx == 0 && method_args > 0 && ClassDB::is_parent_class(class_name, "InputEvent") && p_method.operator String().find("action") != -1) {
  2011. // Get input actions
  2012. List<PropertyInfo> props;
  2013. ProjectSettings::get_singleton()->get_property_list(&props);
  2014. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2015. String s = E->get().name;
  2016. if (!s.begins_with("input/")) {
  2017. continue;
  2018. }
  2019. String name = s.get_slice("/", 1);
  2020. ScriptCodeCompletionOption option(name, ScriptCodeCompletionOption::KIND_CONSTANT);
  2021. option.insert_text = quote_style + option.display + quote_style;
  2022. r_result.insert(option.display, option);
  2023. }
  2024. }
  2025. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2026. } break;
  2027. case GDScriptParser::DataType::BUILTIN: {
  2028. if (base.get_type() == Variant::NIL) {
  2029. Callable::CallError err;
  2030. Variant::construct(base_type.builtin_type, base, nullptr, 0, err);
  2031. if (err.error != Callable::CallError::CALL_OK) {
  2032. return;
  2033. }
  2034. }
  2035. List<MethodInfo> methods;
  2036. base.get_method_list(&methods);
  2037. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  2038. if (E->get().name == p_method) {
  2039. r_arghint = _make_arguments_hint(E->get(), p_argidx);
  2040. return;
  2041. }
  2042. }
  2043. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2044. } break;
  2045. default: {
  2046. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2047. } break;
  2048. }
  2049. }
  2050. }
  2051. static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptParser::Node *p_call, int p_argidx, Map<String, ScriptCodeCompletionOption> &r_result, bool &r_forced, String &r_arghint) {
  2052. const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
  2053. if (p_call->type == GDScriptParser::Node::PRELOAD) {
  2054. if (p_argidx == 0 && bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) {
  2055. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result);
  2056. }
  2057. MethodInfo mi(PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), "preload", PropertyInfo(Variant::STRING, "path"));
  2058. r_arghint = _make_arguments_hint(mi, p_argidx);
  2059. return;
  2060. } else if (p_call->type != GDScriptParser::Node::CALL) {
  2061. return;
  2062. }
  2063. Variant base;
  2064. GDScriptParser::DataType base_type;
  2065. bool _static = false;
  2066. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_call);
  2067. GDScriptParser::Node::Type callee_type = GDScriptParser::Node::NONE;
  2068. GDScriptCompletionIdentifier connect_base;
  2069. if (GDScriptParser::get_builtin_function(call->function_name) < GDScriptFunctions::FUNC_MAX) {
  2070. MethodInfo info = GDScriptFunctions::get_info(GDScriptParser::get_builtin_function(call->function_name));
  2071. if ((info.name == "load" || info.name == "preload") && bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) {
  2072. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result);
  2073. }
  2074. r_arghint = _make_arguments_hint(info, p_argidx);
  2075. return;
  2076. } else if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
  2077. // Complete constructor
  2078. List<MethodInfo> constructors;
  2079. Variant::get_constructor_list(GDScriptParser::get_builtin_type(call->function_name), &constructors);
  2080. int i = 0;
  2081. for (List<MethodInfo>::Element *E = constructors.front(); E; E = E->next()) {
  2082. if (p_argidx >= E->get().arguments.size()) {
  2083. continue;
  2084. }
  2085. if (i > 0) {
  2086. r_arghint += "\n";
  2087. }
  2088. r_arghint += _make_arguments_hint(E->get(), p_argidx);
  2089. i++;
  2090. }
  2091. return;
  2092. } else if (call->is_super || callee_type == GDScriptParser::Node::IDENTIFIER) {
  2093. base = p_context.base;
  2094. if (p_context.current_class) {
  2095. base_type = p_context.current_class->get_datatype();
  2096. _static = !p_context.current_function || p_context.current_function->is_static;
  2097. }
  2098. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
  2099. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(call->callee);
  2100. if (subscript->is_attribute) {
  2101. GDScriptCompletionIdentifier ci;
  2102. if (_guess_expression_type(p_context, subscript->base, ci)) {
  2103. base_type = ci.type;
  2104. base = ci.value;
  2105. } else {
  2106. return;
  2107. }
  2108. _static = base_type.is_meta_type;
  2109. }
  2110. } else {
  2111. return;
  2112. }
  2113. GDScriptCompletionIdentifier ci;
  2114. ci.type = base_type;
  2115. ci.value = base;
  2116. _find_call_arguments(p_context, ci, call->function_name, p_argidx, _static, r_result, r_arghint);
  2117. r_forced = r_result.size() > 0;
  2118. }
  2119. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptCodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) {
  2120. const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
  2121. GDScriptParser parser;
  2122. GDScriptAnalyzer analyzer(&parser);
  2123. parser.parse(p_code, p_path, true);
  2124. analyzer.analyze();
  2125. r_forced = false;
  2126. Map<String, ScriptCodeCompletionOption> options;
  2127. GDScriptParser::CompletionContext completion_context = parser.get_completion_context();
  2128. completion_context.base = p_owner;
  2129. bool is_function = false;
  2130. switch (completion_context.type) {
  2131. case GDScriptParser::COMPLETION_NONE:
  2132. break;
  2133. case GDScriptParser::COMPLETION_ANNOTATION: {
  2134. List<MethodInfo> annotations;
  2135. parser.get_annotation_list(&annotations);
  2136. for (const List<MethodInfo>::Element *E = annotations.front(); E != nullptr; E = E->next()) {
  2137. ScriptCodeCompletionOption option(E->get().name.substr(1), ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  2138. if (E->get().arguments.size() > 0) {
  2139. option.insert_text += "(";
  2140. }
  2141. options.insert(option.display, option);
  2142. }
  2143. r_forced = true;
  2144. } break;
  2145. case GDScriptParser::COMPLETION_ANNOTATION_ARGUMENTS: {
  2146. if (completion_context.node == nullptr || completion_context.node->type != GDScriptParser::Node::ANNOTATION) {
  2147. break;
  2148. }
  2149. const GDScriptParser::AnnotationNode *annotation = static_cast<const GDScriptParser::AnnotationNode *>(completion_context.node);
  2150. _find_annotation_arguments(annotation, completion_context.current_argument, quote_style, options);
  2151. r_forced = true;
  2152. } break;
  2153. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  2154. List<StringName> constants;
  2155. Variant::get_constants_for_type(completion_context.builtin_type, &constants);
  2156. for (const List<StringName>::Element *E = constants.front(); E != nullptr; E = E->next()) {
  2157. ScriptCodeCompletionOption option(E->get(), ScriptCodeCompletionOption::KIND_CONSTANT);
  2158. bool valid = false;
  2159. Variant default_value = Variant::get_constant_value(completion_context.builtin_type, E->get(), &valid);
  2160. if (valid) {
  2161. option.default_value = default_value;
  2162. }
  2163. options.insert(option.display, option);
  2164. }
  2165. } break;
  2166. case GDScriptParser::COMPLETION_INHERIT_TYPE: {
  2167. _list_available_types(true, completion_context, options);
  2168. r_forced = true;
  2169. } break;
  2170. case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID: {
  2171. ScriptCodeCompletionOption option("void", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  2172. options.insert(option.display, option);
  2173. }
  2174. [[fallthrough]];
  2175. case GDScriptParser::COMPLETION_TYPE_NAME: {
  2176. _list_available_types(false, completion_context, options);
  2177. r_forced = true;
  2178. } break;
  2179. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION_OR_TYPE: {
  2180. _list_available_types(false, completion_context, options);
  2181. ScriptCodeCompletionOption get("get", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  2182. options.insert(get.display, get);
  2183. ScriptCodeCompletionOption set("set", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  2184. options.insert(set.display, set);
  2185. r_forced = true;
  2186. } break;
  2187. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION: {
  2188. ScriptCodeCompletionOption get("get", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  2189. options.insert(get.display, get);
  2190. ScriptCodeCompletionOption set("set", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  2191. options.insert(set.display, set);
  2192. r_forced = true;
  2193. } break;
  2194. case GDScriptParser::COMPLETION_PROPERTY_METHOD: {
  2195. if (!completion_context.current_class) {
  2196. break;
  2197. }
  2198. for (int i = 0; i < completion_context.current_class->members.size(); i++) {
  2199. const GDScriptParser::ClassNode::Member &member = completion_context.current_class->members[i];
  2200. if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) {
  2201. continue;
  2202. }
  2203. if (member.function->is_static) {
  2204. continue;
  2205. }
  2206. ScriptCodeCompletionOption option(member.function->identifier->name, ScriptCodeCompletionOption::KIND_FUNCTION);
  2207. options.insert(option.display, option);
  2208. }
  2209. r_forced = true;
  2210. } break;
  2211. case GDScriptParser::COMPLETION_ASSIGN: {
  2212. GDScriptCompletionIdentifier type;
  2213. if (!completion_context.node || completion_context.node->type != GDScriptParser::Node::ASSIGNMENT) {
  2214. break;
  2215. }
  2216. if (!_guess_expression_type(completion_context, static_cast<const GDScriptParser::AssignmentNode *>(completion_context.node)->assignee, type)) {
  2217. _find_identifiers(completion_context, false, options, 0);
  2218. r_forced = true;
  2219. break;
  2220. }
  2221. if (!type.enumeration.empty()) {
  2222. _find_enumeration_candidates(completion_context, type.enumeration, options);
  2223. r_forced = options.size() > 0;
  2224. } else {
  2225. _find_identifiers(completion_context, false, options, 0);
  2226. r_forced = true;
  2227. }
  2228. } break;
  2229. case GDScriptParser::COMPLETION_METHOD:
  2230. is_function = true;
  2231. [[fallthrough]];
  2232. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2233. _find_identifiers(completion_context, is_function, options, 0);
  2234. } break;
  2235. case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD:
  2236. is_function = true;
  2237. [[fallthrough]];
  2238. case GDScriptParser::COMPLETION_ATTRIBUTE: {
  2239. r_forced = true;
  2240. const GDScriptParser::SubscriptNode *attr = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
  2241. if (attr->base) {
  2242. GDScriptCompletionIdentifier base;
  2243. if (!_guess_expression_type(completion_context, attr->base, base)) {
  2244. break;
  2245. }
  2246. _find_identifiers_in_base(base, is_function, options, 0);
  2247. }
  2248. } break;
  2249. case GDScriptParser::COMPLETION_SUBSCRIPT: {
  2250. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
  2251. GDScriptCompletionIdentifier base;
  2252. if (!_guess_expression_type(completion_context, subscript->base, base)) {
  2253. break;
  2254. }
  2255. GDScriptParser::CompletionContext c = completion_context;
  2256. c.current_function = nullptr;
  2257. c.current_suite = nullptr;
  2258. c.base = base.value.get_type() == Variant::OBJECT ? base.value.operator Object *() : nullptr;
  2259. if (base.type.kind == GDScriptParser::DataType::CLASS) {
  2260. c.current_class = base.type.class_type;
  2261. } else {
  2262. c.current_class = nullptr;
  2263. }
  2264. _find_identifiers_in_base(base, false, options, 0);
  2265. } break;
  2266. case GDScriptParser::COMPLETION_TYPE_ATTRIBUTE: {
  2267. if (!completion_context.current_class) {
  2268. break;
  2269. }
  2270. const GDScriptParser::TypeNode *type = static_cast<const GDScriptParser::TypeNode *>(completion_context.node);
  2271. bool found = true;
  2272. GDScriptCompletionIdentifier base;
  2273. base.type.kind = GDScriptParser::DataType::CLASS;
  2274. base.type.type_source = GDScriptParser::DataType::INFERRED;
  2275. base.type.is_constant = true;
  2276. base.type.class_type = completion_context.current_class;
  2277. base.value = completion_context.base;
  2278. for (int i = 0; i < completion_context.current_argument; i++) {
  2279. GDScriptCompletionIdentifier ci;
  2280. if (!_guess_identifier_type_from_base(completion_context, base, type->type_chain[i]->name, ci)) {
  2281. found = false;
  2282. break;
  2283. }
  2284. base = ci;
  2285. }
  2286. // TODO: Improve this to only list types.
  2287. if (found) {
  2288. _find_identifiers_in_base(base, false, options, 0);
  2289. }
  2290. r_forced = true;
  2291. } break;
  2292. case GDScriptParser::COMPLETION_RESOURCE_PATH: {
  2293. if (EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths")) {
  2294. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
  2295. r_forced = true;
  2296. }
  2297. } break;
  2298. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  2299. if (!completion_context.node) {
  2300. break;
  2301. }
  2302. _find_call_arguments(completion_context, completion_context.node, completion_context.current_argument, options, r_forced, r_call_hint);
  2303. } break;
  2304. case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
  2305. GDScriptParser::DataType native_type = completion_context.current_class->base_type;
  2306. while (native_type.is_set() && native_type.kind != GDScriptParser::DataType::NATIVE) {
  2307. switch (native_type.kind) {
  2308. case GDScriptParser::DataType::CLASS: {
  2309. native_type = native_type.class_type->base_type;
  2310. } break;
  2311. default: {
  2312. native_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2313. } break;
  2314. }
  2315. }
  2316. if (!native_type.is_set()) {
  2317. break;
  2318. }
  2319. StringName class_name = _get_real_class_name(native_type.native_type);
  2320. if (!ClassDB::class_exists(class_name)) {
  2321. break;
  2322. }
  2323. bool use_type_hint = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints").operator bool();
  2324. List<MethodInfo> virtual_methods;
  2325. ClassDB::get_virtual_methods(class_name, &virtual_methods);
  2326. for (List<MethodInfo>::Element *E = virtual_methods.front(); E; E = E->next()) {
  2327. MethodInfo &mi = E->get();
  2328. String method_hint = mi.name;
  2329. if (method_hint.find(":") != -1) {
  2330. method_hint = method_hint.get_slice(":", 0);
  2331. }
  2332. method_hint += "(";
  2333. if (mi.arguments.size()) {
  2334. for (int i = 0; i < mi.arguments.size(); i++) {
  2335. if (i > 0) {
  2336. method_hint += ", ";
  2337. }
  2338. String arg = mi.arguments[i].name;
  2339. if (arg.find(":") != -1) {
  2340. arg = arg.substr(0, arg.find(":"));
  2341. }
  2342. method_hint += arg;
  2343. if (use_type_hint && mi.arguments[i].type != Variant::NIL) {
  2344. method_hint += ": ";
  2345. if (mi.arguments[i].type == Variant::OBJECT && mi.arguments[i].class_name != StringName()) {
  2346. method_hint += mi.arguments[i].class_name.operator String();
  2347. } else {
  2348. method_hint += Variant::get_type_name(mi.arguments[i].type);
  2349. }
  2350. }
  2351. }
  2352. }
  2353. method_hint += ")";
  2354. if (use_type_hint && (mi.return_val.type != Variant::NIL || !(mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
  2355. method_hint += " -> ";
  2356. if (mi.return_val.type == Variant::NIL) {
  2357. method_hint += "void";
  2358. } else if (mi.return_val.type == Variant::OBJECT && mi.return_val.class_name != StringName()) {
  2359. method_hint += mi.return_val.class_name.operator String();
  2360. } else {
  2361. method_hint += Variant::get_type_name(mi.return_val.type);
  2362. }
  2363. }
  2364. method_hint += ":";
  2365. ScriptCodeCompletionOption option(method_hint, ScriptCodeCompletionOption::KIND_FUNCTION);
  2366. options.insert(option.display, option);
  2367. }
  2368. } break;
  2369. case GDScriptParser::COMPLETION_GET_NODE: {
  2370. if (p_owner) {
  2371. List<String> opts;
  2372. p_owner->get_argument_options("get_node", 0, &opts);
  2373. for (List<String>::Element *E = opts.front(); E; E = E->next()) {
  2374. String opt = E->get().strip_edges();
  2375. if (opt.is_quoted()) {
  2376. r_forced = true;
  2377. String idopt = opt.unquote();
  2378. if (idopt.replace("/", "_").is_valid_identifier()) {
  2379. ScriptCodeCompletionOption option(idopt, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2380. options.insert(option.display, option);
  2381. } else {
  2382. ScriptCodeCompletionOption option(opt, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2383. options.insert(option.display, option);
  2384. }
  2385. }
  2386. }
  2387. // Get autoloads.
  2388. Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  2389. for (Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E; E = E->next()) {
  2390. String name = E->key();
  2391. ScriptCodeCompletionOption option(quote_style + "/root/" + name + quote_style, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2392. options.insert(option.display, option);
  2393. }
  2394. }
  2395. } break;
  2396. case GDScriptParser::COMPLETION_SUPER_METHOD: {
  2397. if (!completion_context.current_class) {
  2398. break;
  2399. }
  2400. _find_identifiers_in_class(completion_context.current_class, true, false, true, options, 0);
  2401. } break;
  2402. }
  2403. for (Map<String, ScriptCodeCompletionOption>::Element *E = options.front(); E; E = E->next()) {
  2404. r_options->push_back(E->get());
  2405. }
  2406. return OK;
  2407. }
  2408. #else
  2409. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptCodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) {
  2410. return OK;
  2411. }
  2412. #endif
  2413. //////// END COMPLETION //////////
  2414. String GDScriptLanguage::_get_indentation() const {
  2415. #ifdef TOOLS_ENABLED
  2416. if (Engine::get_singleton()->is_editor_hint()) {
  2417. bool use_space_indentation = EDITOR_DEF("text_editor/indent/type", false);
  2418. if (use_space_indentation) {
  2419. int indent_size = EDITOR_DEF("text_editor/indent/size", 4);
  2420. String space_indent = "";
  2421. for (int i = 0; i < indent_size; i++) {
  2422. space_indent += " ";
  2423. }
  2424. return space_indent;
  2425. }
  2426. }
  2427. #endif
  2428. return "\t";
  2429. }
  2430. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  2431. String indent = _get_indentation();
  2432. Vector<String> lines = p_code.split("\n");
  2433. List<int> indent_stack;
  2434. for (int i = 0; i < lines.size(); i++) {
  2435. String l = lines[i];
  2436. int tc = 0;
  2437. for (int j = 0; j < l.length(); j++) {
  2438. if (l[j] == ' ' || l[j] == '\t') {
  2439. tc++;
  2440. } else {
  2441. break;
  2442. }
  2443. }
  2444. String st = l.substr(tc, l.length()).strip_edges();
  2445. if (st == "" || st.begins_with("#")) {
  2446. continue; //ignore!
  2447. }
  2448. int ilevel = 0;
  2449. if (indent_stack.size()) {
  2450. ilevel = indent_stack.back()->get();
  2451. }
  2452. if (tc > ilevel) {
  2453. indent_stack.push_back(tc);
  2454. } else if (tc < ilevel) {
  2455. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  2456. indent_stack.pop_back();
  2457. }
  2458. if (indent_stack.size() && indent_stack.back()->get() != tc) {
  2459. indent_stack.push_back(tc); //this is not right but gets the job done
  2460. }
  2461. }
  2462. if (i >= p_from_line) {
  2463. l = "";
  2464. for (int j = 0; j < indent_stack.size(); j++) {
  2465. l += indent;
  2466. }
  2467. l += st;
  2468. } else if (i > p_to_line) {
  2469. break;
  2470. }
  2471. lines.write[i] = l;
  2472. }
  2473. p_code = "";
  2474. for (int i = 0; i < lines.size(); i++) {
  2475. if (i > 0) {
  2476. p_code += "\n";
  2477. }
  2478. p_code += lines[i];
  2479. }
  2480. }
  2481. #ifdef TOOLS_ENABLED
  2482. static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, const String &p_symbol, bool p_is_function, GDScriptLanguage::LookupResult &r_result) {
  2483. GDScriptParser::DataType base_type = p_base;
  2484. while (base_type.is_set()) {
  2485. switch (base_type.kind) {
  2486. case GDScriptParser::DataType::CLASS: {
  2487. if (base_type.class_type) {
  2488. if (base_type.class_type->has_member(p_symbol)) {
  2489. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2490. r_result.location = base_type.class_type->get_member(p_symbol).get_line();
  2491. return OK;
  2492. }
  2493. base_type = base_type.class_type->base_type;
  2494. }
  2495. } break;
  2496. case GDScriptParser::DataType::SCRIPT: {
  2497. Ref<Script> scr = base_type.script_type;
  2498. if (scr.is_valid()) {
  2499. int line = scr->get_member_line(p_symbol);
  2500. if (line >= 0) {
  2501. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2502. r_result.location = line;
  2503. r_result.script = scr;
  2504. return OK;
  2505. }
  2506. Ref<Script> base_script = scr->get_base_script();
  2507. if (base_script.is_valid()) {
  2508. base_type.script_type = base_script;
  2509. } else {
  2510. base_type.kind = GDScriptParser::DataType::NATIVE;
  2511. base_type.native_type = scr->get_instance_base_type();
  2512. }
  2513. } else {
  2514. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2515. }
  2516. } break;
  2517. case GDScriptParser::DataType::NATIVE: {
  2518. StringName class_name = _get_real_class_name(base_type.native_type);
  2519. if (!ClassDB::class_exists(class_name)) {
  2520. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2521. break;
  2522. }
  2523. if (ClassDB::has_method(class_name, p_symbol, true)) {
  2524. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2525. r_result.class_name = base_type.native_type;
  2526. r_result.class_member = p_symbol;
  2527. return OK;
  2528. }
  2529. List<MethodInfo> virtual_methods;
  2530. ClassDB::get_virtual_methods(class_name, &virtual_methods, true);
  2531. for (List<MethodInfo>::Element *E = virtual_methods.front(); E; E = E->next()) {
  2532. if (E->get().name == p_symbol) {
  2533. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2534. r_result.class_name = base_type.native_type;
  2535. r_result.class_member = p_symbol;
  2536. return OK;
  2537. }
  2538. }
  2539. StringName enum_name = ClassDB::get_integer_constant_enum(class_name, p_symbol, true);
  2540. if (enum_name != StringName()) {
  2541. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  2542. r_result.class_name = base_type.native_type;
  2543. r_result.class_member = enum_name;
  2544. return OK;
  2545. }
  2546. List<String> constants;
  2547. ClassDB::get_integer_constant_list(class_name, &constants, true);
  2548. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  2549. if (E->get() == p_symbol) {
  2550. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2551. r_result.class_name = base_type.native_type;
  2552. r_result.class_member = p_symbol;
  2553. return OK;
  2554. }
  2555. }
  2556. if (ClassDB::has_property(class_name, p_symbol, true)) {
  2557. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2558. r_result.class_name = base_type.native_type;
  2559. r_result.class_member = p_symbol;
  2560. return OK;
  2561. }
  2562. StringName parent = ClassDB::get_parent_class(class_name);
  2563. if (parent != StringName()) {
  2564. if (String(parent).begins_with("_")) {
  2565. base_type.native_type = String(parent).right(1);
  2566. } else {
  2567. base_type.native_type = parent;
  2568. }
  2569. } else {
  2570. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2571. }
  2572. } break;
  2573. case GDScriptParser::DataType::BUILTIN: {
  2574. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2575. if (Variant::has_constant(base_type.builtin_type, p_symbol)) {
  2576. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2577. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2578. r_result.class_member = p_symbol;
  2579. return OK;
  2580. }
  2581. Variant v;
  2582. REF v_ref;
  2583. if (base_type.builtin_type == Variant::OBJECT) {
  2584. v_ref.instance();
  2585. v = v_ref;
  2586. } else {
  2587. Callable::CallError err;
  2588. Variant::construct(base_type.builtin_type, v, NULL, 0, err);
  2589. if (err.error != Callable::CallError::CALL_OK) {
  2590. break;
  2591. }
  2592. }
  2593. if (v.has_method(p_symbol)) {
  2594. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2595. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2596. r_result.class_member = p_symbol;
  2597. return OK;
  2598. }
  2599. bool valid = false;
  2600. v.get(p_symbol, &valid);
  2601. if (valid) {
  2602. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2603. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2604. r_result.class_member = p_symbol;
  2605. return OK;
  2606. }
  2607. } break;
  2608. default: {
  2609. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2610. } break;
  2611. }
  2612. }
  2613. return ERR_CANT_RESOLVE;
  2614. }
  2615. Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) {
  2616. //before parsing, try the usual stuff
  2617. if (ClassDB::class_exists(p_symbol)) {
  2618. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2619. r_result.class_name = p_symbol;
  2620. return OK;
  2621. } else {
  2622. String under_prefix = "_" + p_symbol;
  2623. if (ClassDB::class_exists(under_prefix)) {
  2624. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2625. r_result.class_name = p_symbol;
  2626. return OK;
  2627. }
  2628. }
  2629. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2630. Variant::Type t = Variant::Type(i);
  2631. if (Variant::get_type_name(t) == p_symbol) {
  2632. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2633. r_result.class_name = Variant::get_type_name(t);
  2634. return OK;
  2635. }
  2636. }
  2637. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  2638. if (GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i)) == p_symbol) {
  2639. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2640. r_result.class_name = "@GDScript";
  2641. r_result.class_member = p_symbol;
  2642. return OK;
  2643. }
  2644. }
  2645. if ("PI" == p_symbol || "TAU" == p_symbol || "INF" == p_symbol || "NAN" == p_symbol) {
  2646. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2647. r_result.class_name = "@GDScript";
  2648. r_result.class_member = p_symbol;
  2649. return OK;
  2650. }
  2651. GDScriptParser parser;
  2652. parser.parse(p_code, p_path, true);
  2653. GDScriptAnalyzer analyzer(&parser);
  2654. analyzer.analyze();
  2655. GDScriptParser::CompletionContext context = parser.get_completion_context();
  2656. if (context.current_class && context.current_class->extends.size() > 0) {
  2657. bool success = false;
  2658. ClassDB::get_integer_constant(context.current_class->extends[0], p_symbol, &success);
  2659. if (success) {
  2660. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2661. r_result.class_name = context.current_class->extends[0];
  2662. r_result.class_member = p_symbol;
  2663. return OK;
  2664. }
  2665. }
  2666. bool is_function = false;
  2667. switch (context.type) {
  2668. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  2669. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2670. r_result.class_name = Variant::get_type_name(context.builtin_type);
  2671. r_result.class_member = p_symbol;
  2672. return OK;
  2673. } break;
  2674. case GDScriptParser::COMPLETION_SUPER_METHOD:
  2675. case GDScriptParser::COMPLETION_METHOD: {
  2676. is_function = true;
  2677. [[fallthrough]];
  2678. }
  2679. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2680. GDScriptParser::DataType base_type;
  2681. if (context.current_class) {
  2682. if (context.type != GDScriptParser::COMPLETION_SUPER_METHOD) {
  2683. base_type = context.current_class->get_datatype();
  2684. } else {
  2685. base_type = context.current_class->base_type;
  2686. }
  2687. } else {
  2688. break;
  2689. }
  2690. if (!is_function && context.current_suite) {
  2691. // Lookup local variables.
  2692. const GDScriptParser::SuiteNode *suite = context.current_suite;
  2693. while (suite) {
  2694. if (suite->has_local(p_symbol)) {
  2695. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2696. r_result.location = suite->get_local(p_symbol).start_line;
  2697. return OK;
  2698. }
  2699. suite = suite->parent_block;
  2700. }
  2701. }
  2702. if (_lookup_symbol_from_base(base_type, p_symbol, is_function, r_result) == OK) {
  2703. return OK;
  2704. }
  2705. if (!is_function) {
  2706. // Guess in autoloads as singletons.
  2707. if (ProjectSettings::get_singleton()->has_autoload(p_symbol)) {
  2708. const ProjectSettings::AutoloadInfo &singleton = ProjectSettings::get_singleton()->get_autoload(p_symbol);
  2709. if (singleton.is_singleton) {
  2710. String script = singleton.path;
  2711. if (!script.ends_with(".gd")) {
  2712. // Not a script, try find the script anyway,
  2713. // may have some success.
  2714. script = script.get_basename() + ".gd";
  2715. }
  2716. if (FileAccess::exists(script)) {
  2717. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2718. r_result.location = 0;
  2719. r_result.script = ResourceLoader::load(script);
  2720. return OK;
  2721. }
  2722. }
  2723. }
  2724. // Global.
  2725. Map<StringName, int> classes = GDScriptLanguage::get_singleton()->get_global_map();
  2726. if (classes.has(p_symbol)) {
  2727. Variant value = GDScriptLanguage::get_singleton()->get_global_array()[classes[p_symbol]];
  2728. if (value.get_type() == Variant::OBJECT) {
  2729. Object *obj = value;
  2730. if (obj) {
  2731. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  2732. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2733. r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  2734. } else {
  2735. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2736. r_result.class_name = obj->get_class();
  2737. }
  2738. // proxy class remove the underscore.
  2739. if (r_result.class_name.begins_with("_")) {
  2740. r_result.class_name = r_result.class_name.right(1);
  2741. }
  2742. return OK;
  2743. }
  2744. } else {
  2745. /*
  2746. // Because get_integer_constant_enum and get_integer_constant don't work on @GlobalScope
  2747. // We cannot determine the exact nature of the identifier here
  2748. // Otherwise these codes would work
  2749. StringName enumName = ClassDB::get_integer_constant_enum("@GlobalScope", p_symbol, true);
  2750. if (enumName != NULL) {
  2751. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  2752. r_result.class_name = "@GlobalScope";
  2753. r_result.class_member = enumName;
  2754. return OK;
  2755. }
  2756. else {
  2757. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2758. r_result.class_name = "@GlobalScope";
  2759. r_result.class_member = p_symbol;
  2760. return OK;
  2761. }*/
  2762. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_TBD_GLOBALSCOPE;
  2763. r_result.class_name = "@GlobalScope";
  2764. r_result.class_member = p_symbol;
  2765. return OK;
  2766. }
  2767. }
  2768. }
  2769. } break;
  2770. case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD: {
  2771. is_function = true;
  2772. [[fallthrough]];
  2773. }
  2774. case GDScriptParser::COMPLETION_ATTRIBUTE: {
  2775. if (context.node->type != GDScriptParser::Node::SUBSCRIPT) {
  2776. break;
  2777. }
  2778. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(context.node);
  2779. if (!subscript->is_attribute) {
  2780. break;
  2781. }
  2782. GDScriptCompletionIdentifier base;
  2783. if (!_guess_expression_type(context, subscript->base, base)) {
  2784. break;
  2785. }
  2786. if (_lookup_symbol_from_base(base.type, p_symbol, is_function, r_result) == OK) {
  2787. return OK;
  2788. }
  2789. } break;
  2790. case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
  2791. GDScriptParser::DataType base_type = context.current_class->base_type;
  2792. if (_lookup_symbol_from_base(base_type, p_symbol, true, r_result) == OK) {
  2793. return OK;
  2794. }
  2795. } break;
  2796. case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID:
  2797. case GDScriptParser::COMPLETION_TYPE_NAME: {
  2798. GDScriptParser::DataType base_type = context.current_class->get_datatype();
  2799. if (_lookup_symbol_from_base(base_type, p_symbol, false, r_result) == OK) {
  2800. return OK;
  2801. }
  2802. } break;
  2803. default: {
  2804. }
  2805. }
  2806. return ERR_CANT_RESOLVE;
  2807. }
  2808. #endif