gdscript_editor.cpp 117 KB

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