gdscript_editor.cpp 128 KB

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