gdscript_editor.cpp 118 KB

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