gdscript_editor.cpp 147 KB

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