gdscript_editor.cpp 109 KB

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