gdscript_editor.cpp 123 KB

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