gdscript_editor.cpp 131 KB

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