gdscript_editor.cpp 125 KB

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