gdscript_editor.cpp 109 KB

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