gdscript_editor.cpp 124 KB

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