gdscript_editor.cpp 129 KB

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