gdscript_editor.cpp 137 KB

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