gdscript_editor.cpp 118 KB

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