gdscript_editor.cpp 128 KB

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