gdscript_editor.cpp 119 KB

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