gdscript_editor.cpp 125 KB

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