gdscript_editor.cpp 129 KB

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