gdscript_editor.cpp 109 KB

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