gdscript_editor.cpp 129 KB

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