gdscript_editor.cpp 120 KB

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