gdscript_editor.cpp 124 KB

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