gdscript_editor.cpp 141 KB

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