gdscript_editor.cpp 125 KB

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