gdscript_editor.cpp 130 KB

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