gdscript_editor.cpp 109 KB

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