gdscript_editor.cpp 147 KB

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