gdscript_editor.cpp 119 KB

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