gdscript_editor.cpp 111 KB

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