gdscript_editor.cpp 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193
  1. /*************************************************************************/
  2. /* gdscript_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript.h"
  31. #include "core/engine.h"
  32. #include "editor/editor_settings.h"
  33. #include "gdscript_compiler.h"
  34. #include "global_constants.h"
  35. #include "os/file_access.h"
  36. #ifdef TOOLS_ENABLED
  37. #include "editor/editor_file_system.h"
  38. #include "editor/editor_settings.h"
  39. #include "engine.h"
  40. #endif
  41. void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  42. p_delimiters->push_back("#");
  43. p_delimiters->push_back("\"\"\" \"\"\"");
  44. }
  45. void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  46. p_delimiters->push_back("\" \"");
  47. p_delimiters->push_back("' '");
  48. }
  49. Ref<Script> GDScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  50. #ifdef TOOLS_ENABLED
  51. bool th = EDITOR_DEF("text_editor/completion/add_type_hints", false);
  52. #else
  53. bool th = false;
  54. #endif
  55. String _template = "extends %BASE%\n"
  56. "\n"
  57. "# Declare member variables here. Examples:\n"
  58. "# var a %INT_TYPE%= 2\n"
  59. "# var b %STRING_TYPE%= \"text\"\n"
  60. "\n"
  61. "# Called when the node enters the scene tree for the first time.\n"
  62. "func _ready()%VOID_RETURN%:\n"
  63. "%TS%pass # Replace with function body.\n"
  64. "\n"
  65. "# Called every frame. 'delta' is the elapsed time since the previous frame.\n"
  66. "#func _process(delta%FLOAT_TYPE%)%VOID_RETURN%:\n"
  67. "#%TS%pass\n";
  68. #ifdef TOOLS_ENABLED
  69. if (EDITOR_DEF("text_editor/completion/add_type_hints", false)) {
  70. _template = _template.replace("%INT_TYPE%", ": int ");
  71. _template = _template.replace("%STRING_TYPE%", ": String ");
  72. _template = _template.replace("%FLOAT_TYPE%", " : float");
  73. _template = _template.replace("%VOID_RETURN%", " -> void");
  74. } else {
  75. _template = _template.replace("%INT_TYPE%", "");
  76. _template = _template.replace("%STRING_TYPE%", "");
  77. _template = _template.replace("%FLOAT_TYPE%", "");
  78. _template = _template.replace("%VOID_RETURN%", "");
  79. }
  80. #else
  81. _template = _template.replace("%INT_TYPE%", "");
  82. _template = _template.replace("%STRING_TYPE%", "");
  83. _template = _template.replace("%FLOAT_TYPE%", "");
  84. _template = _template.replace("%VOID_RETURN%", "");
  85. #endif
  86. _template = _template.replace("%BASE%", p_base_class_name);
  87. _template = _template.replace("%TS%", _get_indentation());
  88. Ref<GDScript> script;
  89. script.instance();
  90. script->set_source_code(_template);
  91. return script;
  92. }
  93. bool GDScriptLanguage::is_using_templates() {
  94. return true;
  95. }
  96. void GDScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
  97. String src = p_script->get_source_code();
  98. src = src.replace("%BASE%", p_base_class_name);
  99. src = src.replace("%TS%", _get_indentation());
  100. p_script->set_source_code(src);
  101. }
  102. 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) const {
  103. GDScriptParser parser;
  104. Error err = parser.parse(p_script, p_path.get_base_dir(), true, p_path);
  105. if (err) {
  106. r_line_error = parser.get_error_line();
  107. r_col_error = parser.get_error_column();
  108. r_test_error = parser.get_error();
  109. return false;
  110. } else {
  111. const GDScriptParser::Node *root = parser.get_parse_tree();
  112. ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, false);
  113. const GDScriptParser::ClassNode *cl = static_cast<const GDScriptParser::ClassNode *>(root);
  114. Map<int, String> funcs;
  115. for (int i = 0; i < cl->functions.size(); i++) {
  116. funcs[cl->functions[i]->line] = cl->functions[i]->name;
  117. }
  118. for (int i = 0; i < cl->static_functions.size(); i++) {
  119. funcs[cl->static_functions[i]->line] = cl->static_functions[i]->name;
  120. }
  121. for (int i = 0; i < cl->subclasses.size(); i++) {
  122. for (int j = 0; j < cl->subclasses[i]->functions.size(); j++) {
  123. funcs[cl->subclasses[i]->functions[j]->line] = String(cl->subclasses[i]->name) + "." + String(cl->subclasses[i]->functions[j]->name);
  124. }
  125. }
  126. for (Map<int, String>::Element *E = funcs.front(); E; E = E->next()) {
  127. r_functions->push_back(E->get() + ":" + itos(E->key()));
  128. }
  129. }
  130. return true;
  131. }
  132. bool GDScriptLanguage::has_named_classes() const {
  133. return false;
  134. }
  135. bool GDScriptLanguage::supports_builtin_mode() const {
  136. return true;
  137. }
  138. int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  139. GDScriptTokenizerText tokenizer;
  140. tokenizer.set_code(p_code);
  141. int indent = 0;
  142. while (tokenizer.get_token() != GDScriptTokenizer::TK_EOF && tokenizer.get_token() != GDScriptTokenizer::TK_ERROR) {
  143. if (tokenizer.get_token() == GDScriptTokenizer::TK_NEWLINE) {
  144. indent = tokenizer.get_token_line_indent();
  145. }
  146. //print_line("TOKEN: "+String(GDScriptTokenizer::get_token_name(tokenizer.get_token())));
  147. if (indent == 0 && tokenizer.get_token() == GDScriptTokenizer::TK_PR_FUNCTION && tokenizer.get_token(1) == GDScriptTokenizer::TK_IDENTIFIER) {
  148. String identifier = tokenizer.get_token_identifier(1);
  149. if (identifier == p_function) {
  150. return tokenizer.get_token_line();
  151. }
  152. }
  153. tokenizer.advance();
  154. //print_line("NEXT: "+String(GDScriptTokenizer::get_token_name(tokenizer.get_token())));
  155. }
  156. return -1;
  157. }
  158. Script *GDScriptLanguage::create_script() const {
  159. return memnew(GDScript);
  160. }
  161. /* DEBUGGER FUNCTIONS */
  162. bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
  163. //break because of parse error
  164. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  165. _debug_parse_err_line = p_line;
  166. _debug_parse_err_file = p_file;
  167. _debug_error = p_error;
  168. ScriptDebugger::get_singleton()->debug(this, false);
  169. return true;
  170. } else {
  171. return false;
  172. }
  173. }
  174. bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
  175. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  176. _debug_parse_err_line = -1;
  177. _debug_parse_err_file = "";
  178. _debug_error = p_error;
  179. ScriptDebugger::get_singleton()->debug(this, p_allow_continue);
  180. return true;
  181. } else {
  182. return false;
  183. }
  184. }
  185. String GDScriptLanguage::debug_get_error() const {
  186. return _debug_error;
  187. }
  188. int GDScriptLanguage::debug_get_stack_level_count() const {
  189. if (_debug_parse_err_line >= 0)
  190. return 1;
  191. return _debug_call_stack_pos;
  192. }
  193. int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
  194. if (_debug_parse_err_line >= 0)
  195. return _debug_parse_err_line;
  196. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, -1);
  197. int l = _debug_call_stack_pos - p_level - 1;
  198. return *(_call_stack[l].line);
  199. }
  200. String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
  201. if (_debug_parse_err_line >= 0)
  202. return "";
  203. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  204. int l = _debug_call_stack_pos - p_level - 1;
  205. return _call_stack[l].function->get_name();
  206. }
  207. String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
  208. if (_debug_parse_err_line >= 0)
  209. return _debug_parse_err_file;
  210. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  211. int l = _debug_call_stack_pos - p_level - 1;
  212. return _call_stack[l].function->get_source();
  213. }
  214. 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) {
  215. if (_debug_parse_err_line >= 0)
  216. return;
  217. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  218. int l = _debug_call_stack_pos - p_level - 1;
  219. GDScriptFunction *f = _call_stack[l].function;
  220. List<Pair<StringName, int> > locals;
  221. f->debug_get_stack_member_state(*_call_stack[l].line, &locals);
  222. for (List<Pair<StringName, int> >::Element *E = locals.front(); E; E = E->next()) {
  223. p_locals->push_back(E->get().first);
  224. p_values->push_back(_call_stack[l].stack[E->get().second]);
  225. }
  226. }
  227. 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) {
  228. if (_debug_parse_err_line >= 0)
  229. return;
  230. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  231. int l = _debug_call_stack_pos - p_level - 1;
  232. GDScriptInstance *instance = _call_stack[l].instance;
  233. if (!instance)
  234. return;
  235. Ref<GDScript> script = instance->get_script();
  236. ERR_FAIL_COND(script.is_null());
  237. const Map<StringName, GDScript::MemberInfo> &mi = script->debug_get_member_indices();
  238. for (const Map<StringName, GDScript::MemberInfo>::Element *E = mi.front(); E; E = E->next()) {
  239. p_members->push_back(E->key());
  240. p_values->push_back(instance->debug_get_member_by_index(E->get().index));
  241. }
  242. }
  243. ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) {
  244. ERR_FAIL_COND_V(_debug_parse_err_line >= 0, NULL);
  245. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, NULL);
  246. int l = _debug_call_stack_pos - p_level - 1;
  247. ScriptInstance *instance = _call_stack[l].instance;
  248. return instance;
  249. }
  250. void GDScriptLanguage::debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  251. const Map<StringName, int> &name_idx = GDScriptLanguage::get_singleton()->get_global_map();
  252. const Variant *globals = GDScriptLanguage::get_singleton()->get_global_array();
  253. List<Pair<String, Variant> > cinfo;
  254. get_public_constants(&cinfo);
  255. for (const Map<StringName, int>::Element *E = name_idx.front(); E; E = E->next()) {
  256. if (ClassDB::class_exists(E->key()) || Engine::get_singleton()->has_singleton(E->key()))
  257. continue;
  258. bool is_script_constant = false;
  259. for (List<Pair<String, Variant> >::Element *CE = cinfo.front(); CE; CE = CE->next()) {
  260. if (CE->get().first == E->key()) {
  261. is_script_constant = true;
  262. break;
  263. }
  264. }
  265. if (is_script_constant)
  266. continue;
  267. const Variant &var = globals[E->value()];
  268. if (Object *obj = var) {
  269. if (Object::cast_to<GDScriptNativeClass>(obj))
  270. continue;
  271. }
  272. bool skip = false;
  273. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  274. if (E->key() == GlobalConstants::get_global_constant_name(i)) {
  275. skip = true;
  276. break;
  277. }
  278. }
  279. if (skip)
  280. continue;
  281. p_globals->push_back(E->key());
  282. p_values->push_back(var);
  283. }
  284. }
  285. String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  286. if (_debug_parse_err_line >= 0)
  287. return "";
  288. return "";
  289. }
  290. void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  291. p_extensions->push_back("gd");
  292. }
  293. void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  294. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  295. p_functions->push_back(GDScriptFunctions::get_info(GDScriptFunctions::Function(i)));
  296. }
  297. //not really "functions", but..
  298. {
  299. MethodInfo mi;
  300. mi.name = "preload";
  301. mi.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
  302. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
  303. p_functions->push_back(mi);
  304. }
  305. {
  306. MethodInfo mi;
  307. mi.name = "yield";
  308. mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  309. mi.arguments.push_back(PropertyInfo(Variant::STRING, "signal"));
  310. mi.default_arguments.push_back(Variant());
  311. mi.default_arguments.push_back(String());
  312. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "GDScriptFunctionState");
  313. p_functions->push_back(mi);
  314. }
  315. {
  316. MethodInfo mi;
  317. mi.name = "assert";
  318. mi.return_val.type = Variant::NIL;
  319. mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition"));
  320. p_functions->push_back(mi);
  321. }
  322. }
  323. void GDScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  324. Pair<String, Variant> pi;
  325. pi.first = "PI";
  326. pi.second = Math_PI;
  327. p_constants->push_back(pi);
  328. Pair<String, Variant> tau;
  329. tau.first = "TAU";
  330. tau.second = Math_TAU;
  331. p_constants->push_back(tau);
  332. Pair<String, Variant> infinity;
  333. infinity.first = "INF";
  334. infinity.second = Math_INF;
  335. p_constants->push_back(infinity);
  336. Pair<String, Variant> nan;
  337. nan.first = "NAN";
  338. nan.second = Math_NAN;
  339. p_constants->push_back(nan);
  340. }
  341. String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  342. #ifdef TOOLS_ENABLED
  343. bool th = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
  344. #else
  345. bool th = false;
  346. #endif
  347. String s = "func " + p_name + "(";
  348. if (p_args.size()) {
  349. for (int i = 0; i < p_args.size(); i++) {
  350. if (i > 0)
  351. s += ", ";
  352. s += p_args[i].get_slice(":", 0);
  353. if (th) {
  354. String type = p_args[i].get_slice(":", 1);
  355. if (!type.empty() && type != "var") {
  356. s += " : " + type;
  357. }
  358. }
  359. }
  360. }
  361. s += String(")") + (th ? " -> void" : "") + ":\n" + _get_indentation() + "pass # Replace with function body.\n";
  362. return s;
  363. }
  364. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  365. struct GDScriptCompletionIdentifier {
  366. String enumeration;
  367. StringName obj_type;
  368. Ref<GDScript> script;
  369. Variant::Type type;
  370. Variant value; //im case there is a value, also return it
  371. GDScriptCompletionIdentifier() :
  372. type(Variant::NIL) {}
  373. };
  374. static GDScriptCompletionIdentifier _get_type_from_variant(const Variant &p_variant, bool p_allow_gdnative_class = false) {
  375. GDScriptCompletionIdentifier t;
  376. t.type = p_variant.get_type();
  377. t.value = p_variant;
  378. if (p_variant.get_type() == Variant::OBJECT) {
  379. Object *obj = p_variant;
  380. if (obj) {
  381. if (p_allow_gdnative_class && Object::cast_to<GDScriptNativeClass>(obj)) {
  382. t.obj_type = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  383. t.value = Variant();
  384. } else {
  385. t.obj_type = obj->get_class();
  386. }
  387. }
  388. }
  389. return t;
  390. }
  391. static GDScriptCompletionIdentifier _get_type_from_pinfo(const PropertyInfo &p_info) {
  392. GDScriptCompletionIdentifier t;
  393. t.type = p_info.type;
  394. if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  395. t.obj_type = p_info.hint_string;
  396. }
  397. return t;
  398. }
  399. static bool _get_type_from_parser_type(const GDScriptParser::DataType &p_datatype, GDScriptCompletionIdentifier &r_type) {
  400. if (p_datatype.has_type && p_datatype.kind != GDScriptParser::DataType::UNRESOLVED) {
  401. switch (p_datatype.kind) {
  402. case GDScriptParser::DataType::BUILTIN: {
  403. r_type.type = p_datatype.builtin_type;
  404. return true;
  405. } break;
  406. case GDScriptParser::DataType::NATIVE: {
  407. r_type.type = Variant::OBJECT;
  408. r_type.obj_type = p_datatype.native_type;
  409. return true;
  410. } break;
  411. case GDScriptParser::DataType::SCRIPT:
  412. case GDScriptParser::DataType::GDSCRIPT: {
  413. r_type.type = Variant::OBJECT;
  414. r_type.script = p_datatype.script_type;
  415. r_type.obj_type = r_type.script->get_instance_base_type();
  416. return true;
  417. } break;
  418. }
  419. }
  420. return false;
  421. }
  422. struct GDScriptCompletionContext {
  423. const GDScriptParser::ClassNode *_class;
  424. const GDScriptParser::FunctionNode *function;
  425. const GDScriptParser::BlockNode *block;
  426. Object *base;
  427. String base_path;
  428. };
  429. static Ref<Reference> _get_parent_class(GDScriptCompletionContext &context) {
  430. if (context._class->extends_used) {
  431. //do inheritance
  432. String path = context._class->extends_file;
  433. Ref<GDScript> script;
  434. Ref<GDScriptNativeClass> native;
  435. if (path != "") {
  436. //path (and optionally subclasses)
  437. if (path.is_rel_path()) {
  438. path = context.base_path.plus_file(path);
  439. }
  440. if (ScriptCodeCompletionCache::get_singleton())
  441. script = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
  442. else
  443. script = ResourceLoader::load(path);
  444. if (script.is_null()) {
  445. return REF();
  446. }
  447. if (!script->is_valid()) {
  448. return REF();
  449. }
  450. //print_line("EXTENDS PATH: "+path+" script is "+itos(script.is_valid())+" indices is "+itos(script->member_indices.size())+" valid? "+itos(script->valid));
  451. if (context._class->extends_class.size()) {
  452. for (int i = 0; i < context._class->extends_class.size(); i++) {
  453. String sub = context._class->extends_class[i];
  454. if (script->get_subclasses().has(sub)) {
  455. script = script->get_subclasses()[sub];
  456. } else {
  457. return REF();
  458. }
  459. }
  460. }
  461. if (script.is_valid())
  462. return script;
  463. } else {
  464. if (context._class->extends_class.size() == 0) {
  465. ERR_PRINT("BUG");
  466. return REF();
  467. }
  468. String base = context._class->extends_class[0];
  469. if (context._class->extends_class.size() > 1) {
  470. return REF();
  471. }
  472. //if not found, try engine classes
  473. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  474. return REF();
  475. }
  476. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  477. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  478. return native;
  479. }
  480. }
  481. return Ref<Reference>();
  482. }
  483. static GDScriptCompletionIdentifier _get_native_class(GDScriptCompletionContext &context) {
  484. GDScriptCompletionIdentifier id;
  485. REF pc = _get_parent_class(context);
  486. if (!pc.is_valid()) {
  487. return id;
  488. }
  489. Ref<GDScriptNativeClass> nc = pc;
  490. Ref<GDScript> s = pc;
  491. if (s.is_null() && nc.is_null()) {
  492. return id;
  493. }
  494. while (!s.is_null()) {
  495. nc = s->get_native();
  496. s = s->get_base();
  497. }
  498. if (nc.is_null()) {
  499. return id;
  500. }
  501. id.type = Variant::OBJECT;
  502. if (context.base)
  503. id.value = context.base;
  504. id.obj_type = nc->get_name();
  505. return id;
  506. }
  507. static bool _guess_identifier_type(GDScriptCompletionContext &context, int p_line, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type, bool p_for_indexing);
  508. static bool _guess_expression_type(GDScriptCompletionContext &context, const GDScriptParser::Node *p_node, int p_line, GDScriptCompletionIdentifier &r_type, bool p_for_indexing = false) {
  509. GDScriptParser::DataType datatype = p_node->get_datatype();
  510. if (_get_type_from_parser_type(datatype, r_type)) {
  511. return true;
  512. }
  513. if (p_node->type == GDScriptParser::Node::TYPE_CONSTANT) {
  514. const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(p_node);
  515. r_type = _get_type_from_variant(cn->value);
  516. return true;
  517. } else if (p_node->type == GDScriptParser::Node::TYPE_DICTIONARY) {
  518. r_type.type = Variant::DICTIONARY;
  519. //what the heck, fill it anyway
  520. const GDScriptParser::DictionaryNode *an = static_cast<const GDScriptParser::DictionaryNode *>(p_node);
  521. Dictionary d;
  522. for (int i = 0; i < an->elements.size(); i++) {
  523. GDScriptCompletionIdentifier k;
  524. if (_guess_expression_type(context, an->elements[i].key, p_line, k) && k.value.get_type() != Variant::NIL) {
  525. GDScriptCompletionIdentifier v;
  526. if (_guess_expression_type(context, an->elements[i].value, p_line, v)) {
  527. d[k.value] = v.value;
  528. }
  529. }
  530. }
  531. r_type.value = d;
  532. return true;
  533. } else if (p_node->type == GDScriptParser::Node::TYPE_ARRAY) {
  534. r_type.type = Variant::ARRAY;
  535. //what the heck, fill it anyway
  536. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_node);
  537. Array arr;
  538. arr.resize(an->elements.size());
  539. for (int i = 0; i < an->elements.size(); i++) {
  540. GDScriptCompletionIdentifier ci;
  541. if (_guess_expression_type(context, an->elements[i], p_line, ci)) {
  542. arr[i] = ci.value;
  543. }
  544. }
  545. r_type.value = arr;
  546. return true;
  547. } else if (p_node->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
  548. MethodInfo mi = GDScriptFunctions::get_info(static_cast<const GDScriptParser::BuiltInFunctionNode *>(p_node)->function);
  549. r_type = _get_type_from_pinfo(mi.return_val);
  550. return true;
  551. } else if (p_node->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  552. return _guess_identifier_type(context, p_line - 1, static_cast<const GDScriptParser::IdentifierNode *>(p_node)->name, r_type, p_for_indexing);
  553. } else if (p_node->type == GDScriptParser::Node::TYPE_SELF) {
  554. //eeh...
  555. r_type = _get_native_class(context);
  556. return r_type.type != Variant::NIL;
  557. } else if (p_node->type == GDScriptParser::Node::TYPE_OPERATOR) {
  558. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_node);
  559. if (op->op == GDScriptParser::OperatorNode::OP_CALL) {
  560. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) {
  561. const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(op->arguments[0]);
  562. r_type.type = tn->vtype;
  563. return true;
  564. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
  565. const GDScriptParser::BuiltInFunctionNode *bin = static_cast<const GDScriptParser::BuiltInFunctionNode *>(op->arguments[0]);
  566. return _guess_expression_type(context, bin, p_line, r_type);
  567. } else if (op->arguments.size() > 1 && op->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  568. StringName id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1])->name;
  569. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER && String(id) == "new") {
  570. //shortcut
  571. StringName identifier = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name;
  572. if (ClassDB::class_exists(identifier)) {
  573. r_type.type = Variant::OBJECT;
  574. r_type.value = Variant();
  575. r_type.obj_type = identifier;
  576. return true;
  577. }
  578. }
  579. GDScriptCompletionIdentifier base;
  580. if (!_guess_expression_type(context, op->arguments[0], p_line, base))
  581. return false;
  582. if (base.type == Variant::OBJECT) {
  583. if (id.operator String() == "new" && base.value.get_type() == Variant::OBJECT) {
  584. Object *obj = base.value;
  585. if (obj && Object::cast_to<GDScriptNativeClass>(obj)) {
  586. GDScriptNativeClass *gdnc = Object::cast_to<GDScriptNativeClass>(obj);
  587. r_type.type = Variant::OBJECT;
  588. r_type.value = Variant();
  589. r_type.obj_type = gdnc->get_name();
  590. return true;
  591. } else {
  592. if (base.obj_type != StringName()) {
  593. r_type.type = Variant::OBJECT;
  594. r_type.value = Variant();
  595. r_type.obj_type = base.obj_type;
  596. return true;
  597. }
  598. }
  599. }
  600. if (ClassDB::has_method(base.obj_type, id)) {
  601. #ifdef TOOLS_ENABLED
  602. MethodBind *mb = ClassDB::get_method(base.obj_type, id);
  603. PropertyInfo pi = mb->get_return_info();
  604. //try calling the function if constant and all args are constant, should not crash..
  605. Object *baseptr = base.value;
  606. if (mb->is_const() && pi.type == Variant::OBJECT) {
  607. bool all_valid = true;
  608. Vector<Variant> args;
  609. for (int i = 2; i < op->arguments.size(); i++) {
  610. GDScriptCompletionIdentifier arg;
  611. if (_guess_expression_type(context, op->arguments[i], p_line, arg)) {
  612. if (arg.value.get_type() != Variant::NIL && arg.value.get_type() != Variant::OBJECT) { // calling with object seems dangerous, i don' t know
  613. args.push_back(arg.value);
  614. } else {
  615. all_valid = false;
  616. break;
  617. }
  618. } else {
  619. all_valid = false;
  620. }
  621. }
  622. if (all_valid && String(id) == "get_node" && ClassDB::is_parent_class(base.obj_type, "Node") && args.size()) {
  623. String arg1 = args[0];
  624. if (arg1.begins_with("/root/")) {
  625. String which = arg1.get_slice("/", 2);
  626. if (which != "") {
  627. List<PropertyInfo> props;
  628. ProjectSettings::get_singleton()->get_property_list(&props);
  629. //print_line("find singleton");
  630. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  631. String s = E->get().name;
  632. if (!s.begins_with("autoload/"))
  633. continue;
  634. //print_line("found "+s);
  635. String name = s.get_slice("/", 1);
  636. //print_line("name: "+name+", which: "+which);
  637. if (name == which) {
  638. String script = ProjectSettings::get_singleton()->get(s);
  639. if (!script.begins_with("res://")) {
  640. script = "res://" + script;
  641. }
  642. if (!script.ends_with(".gd")) {
  643. //not a script, try find the script anyway,
  644. //may have some success
  645. script = script.get_basename() + ".gd";
  646. }
  647. if (FileAccess::exists(script)) {
  648. //print_line("is a script");
  649. Ref<Script> scr;
  650. if (ScriptCodeCompletionCache::get_singleton())
  651. scr = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(script);
  652. else
  653. scr = ResourceLoader::load(script);
  654. r_type.obj_type = "Node";
  655. r_type.type = Variant::OBJECT;
  656. r_type.script = scr;
  657. r_type.value = Variant();
  658. return true;
  659. }
  660. }
  661. }
  662. }
  663. }
  664. }
  665. if (baseptr) {
  666. if (all_valid) {
  667. Vector<const Variant *> argptr;
  668. for (int i = 0; i < args.size(); i++) {
  669. argptr.push_back(&args[i]);
  670. }
  671. Variant::CallError ce;
  672. Variant ret = mb->call(baseptr, (const Variant **)argptr.ptr(), argptr.size(), ce);
  673. if (ce.error == Variant::CallError::CALL_OK && ret.get_type() != Variant::NIL) {
  674. if (ret.get_type() != Variant::OBJECT || ret.operator Object *() != NULL) {
  675. r_type = _get_type_from_variant(ret);
  676. return true;
  677. }
  678. }
  679. }
  680. }
  681. }
  682. r_type.type = pi.type;
  683. if (pi.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  684. r_type.obj_type = pi.hint_string;
  685. }
  686. return true;
  687. #else
  688. return false;
  689. #endif
  690. } else {
  691. return false;
  692. }
  693. } else {
  694. //method for some variant..
  695. Variant::CallError ce;
  696. Variant v = Variant::construct(base.type, NULL, 0, ce);
  697. List<MethodInfo> mi;
  698. v.get_method_list(&mi);
  699. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  700. if (!E->get().name.begins_with("_") && E->get().name == id.operator String()) {
  701. MethodInfo mi = E->get();
  702. r_type.type = mi.return_val.type;
  703. if (mi.return_val.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  704. r_type.obj_type = mi.return_val.hint_string;
  705. }
  706. return true;
  707. }
  708. }
  709. }
  710. }
  711. } else if (op->op == GDScriptParser::OperatorNode::OP_INDEX || op->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED) {
  712. GDScriptCompletionIdentifier p1;
  713. GDScriptCompletionIdentifier p2;
  714. if (op->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED) {
  715. if (op->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  716. String id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1])->name;
  717. p2.type = Variant::STRING;
  718. p2.value = id;
  719. }
  720. } else {
  721. if (op->arguments[1]) {
  722. if (!_guess_expression_type(context, op->arguments[1], p_line, p2)) {
  723. return false;
  724. }
  725. }
  726. }
  727. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_ARRAY) {
  728. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(op->arguments[0]);
  729. if (p2.value.is_num()) {
  730. int index = p2.value;
  731. if (index < 0 || index >= an->elements.size())
  732. return false;
  733. return _guess_expression_type(context, an->elements[index], p_line, r_type);
  734. }
  735. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_DICTIONARY) {
  736. const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(op->arguments[0]);
  737. if (p2.value.get_type() == Variant::NIL)
  738. return false;
  739. for (int i = 0; i < dn->elements.size(); i++) {
  740. GDScriptCompletionIdentifier k;
  741. if (!_guess_expression_type(context, dn->elements[i].key, p_line, k)) {
  742. return false;
  743. }
  744. if (k.value.get_type() == Variant::NIL)
  745. return false;
  746. if (k.value == p2.value) {
  747. return _guess_expression_type(context, dn->elements[i].value, p_line, r_type);
  748. }
  749. }
  750. } else {
  751. if (op->arguments[0]) {
  752. if (!_guess_expression_type(context, op->arguments[0], p_line, p1)) {
  753. return false;
  754. }
  755. }
  756. if (p1.value.get_type() == Variant::OBJECT) {
  757. //??
  758. if (p1.obj_type != StringName() && p2.type == Variant::STRING) {
  759. StringName base_type = p1.obj_type;
  760. if (p1.obj_type == "GDScriptNativeClass") {
  761. //native enum
  762. Ref<GDScriptNativeClass> gdn = p1.value;
  763. if (gdn.is_valid()) {
  764. base_type = gdn->get_name();
  765. }
  766. }
  767. StringName index = p2.value;
  768. bool valid;
  769. Variant::Type t = ClassDB::get_property_type(base_type, index, &valid);
  770. if (t != Variant::NIL && valid) {
  771. r_type.type = t;
  772. if (t == Variant::INT || t == Variant::OBJECT) {
  773. //check for enum!
  774. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  775. StringName getter = ClassDB::get_property_getter(base_type, index);
  776. if (getter != StringName()) {
  777. MethodBind *mb = ClassDB::get_method(base_type, getter);
  778. if (mb) {
  779. PropertyInfo rt = mb->get_return_info();
  780. if ((rt.usage & PROPERTY_USAGE_CLASS_IS_ENUM) && t == Variant::INT) {
  781. r_type.enumeration = rt.class_name;
  782. } else if (t == Variant::OBJECT) {
  783. r_type.obj_type = rt.class_name;
  784. }
  785. }
  786. }
  787. #endif
  788. }
  789. return true;
  790. }
  791. }
  792. } else if (p1.value.get_type() != Variant::NIL) {
  793. bool valid;
  794. Variant ret = p1.value.get(p2.value, &valid);
  795. if (valid) {
  796. r_type = _get_type_from_variant(ret);
  797. return true;
  798. }
  799. } else {
  800. if (p1.type != Variant::NIL) {
  801. Variant::CallError ce;
  802. Variant base = Variant::construct(p1.type, NULL, 0, ce);
  803. bool valid;
  804. Variant ret = base.get(p2.value, &valid);
  805. if (valid) {
  806. r_type = _get_type_from_variant(ret);
  807. return true;
  808. }
  809. }
  810. }
  811. }
  812. } else {
  813. Variant::Operator vop = Variant::OP_MAX;
  814. switch (op->op) {
  815. case GDScriptParser::OperatorNode::OP_ADD: vop = Variant::OP_ADD; break;
  816. case GDScriptParser::OperatorNode::OP_SUB: vop = Variant::OP_SUBTRACT; break;
  817. case GDScriptParser::OperatorNode::OP_MUL: vop = Variant::OP_MULTIPLY; break;
  818. case GDScriptParser::OperatorNode::OP_DIV: vop = Variant::OP_DIVIDE; break;
  819. case GDScriptParser::OperatorNode::OP_MOD: vop = Variant::OP_MODULE; break;
  820. case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: vop = Variant::OP_SHIFT_LEFT; break;
  821. case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: vop = Variant::OP_SHIFT_RIGHT; break;
  822. case GDScriptParser::OperatorNode::OP_BIT_AND: vop = Variant::OP_BIT_AND; break;
  823. case GDScriptParser::OperatorNode::OP_BIT_OR: vop = Variant::OP_BIT_OR; break;
  824. case GDScriptParser::OperatorNode::OP_BIT_XOR: vop = Variant::OP_BIT_XOR; break;
  825. default: {}
  826. }
  827. if (vop == Variant::OP_MAX)
  828. return false;
  829. GDScriptCompletionIdentifier p1;
  830. GDScriptCompletionIdentifier p2;
  831. if (op->arguments[0]) {
  832. if (!_guess_expression_type(context, op->arguments[0], p_line, p1)) {
  833. return false;
  834. }
  835. }
  836. if (op->arguments.size() > 1) {
  837. if (!_guess_expression_type(context, op->arguments[1], p_line, p2)) {
  838. return false;
  839. }
  840. }
  841. Variant::CallError ce;
  842. bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
  843. Variant v1 = (v1_use_value) ? p1.value : Variant::construct(p1.type, NULL, 0, ce);
  844. bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
  845. Variant v2 = (v2_use_value) ? p2.value : Variant::construct(p2.type, NULL, 0, ce);
  846. // avoid potential invalid ops
  847. if ((vop == Variant::OP_DIVIDE || vop == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
  848. v2 = 1;
  849. v2_use_value = false;
  850. }
  851. if (vop == Variant::OP_DIVIDE && v2.get_type() == Variant::REAL) {
  852. v2 = 1.0;
  853. v2_use_value = false;
  854. }
  855. Variant r;
  856. bool valid;
  857. Variant::evaluate(vop, v1, v2, r, valid);
  858. if (!valid)
  859. return false;
  860. r_type.type = r.get_type();
  861. if (v1_use_value && v2_use_value)
  862. r_type.value = r;
  863. return true;
  864. }
  865. }
  866. return false;
  867. }
  868. static bool _guess_identifier_type_in_block(GDScriptCompletionContext &context, int p_line, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  869. if (context.block->if_condition && context.block->if_condition->type == GDScriptParser::Node::TYPE_OPERATOR && static_cast<const GDScriptParser::OperatorNode *>(context.block->if_condition)->op == GDScriptParser::OperatorNode::OP_IS) {
  870. //is used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
  871. //super dirty hack, but very useful
  872. //credit: Zylann
  873. //TODO: this could be hacked to detect ANDed conditions too..
  874. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(context.block->if_condition);
  875. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER && static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name == p_identifier) {
  876. //bingo
  877. if (_guess_expression_type(context, op->arguments[1], op->line, r_type)) {
  878. return true;
  879. }
  880. }
  881. }
  882. // Check type hint first
  883. const GDScriptParser::BlockNode *blk = context.block;
  884. while (blk) {
  885. if (blk->variables.has(p_identifier)) {
  886. GDScriptParser::DataType var_type = blk->variables[p_identifier]->get_datatype();
  887. if (_get_type_from_parser_type(var_type, r_type)) {
  888. return true;
  889. }
  890. }
  891. blk = blk->parent_block;
  892. }
  893. GDScriptCompletionIdentifier gdi = _get_native_class(context);
  894. if (gdi.obj_type != StringName()) {
  895. bool valid;
  896. Variant::Type t = ClassDB::get_property_type(gdi.obj_type, p_identifier, &valid);
  897. if (t != Variant::NIL && valid) {
  898. r_type.type = t;
  899. if (t == Variant::INT) {
  900. //check for enum!
  901. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  902. StringName getter = ClassDB::get_property_getter(gdi.obj_type, p_identifier);
  903. if (getter != StringName()) {
  904. MethodBind *mb = ClassDB::get_method(gdi.obj_type, getter);
  905. if (mb) {
  906. PropertyInfo rt = mb->get_return_info();
  907. if (rt.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  908. r_type.enumeration = rt.class_name;
  909. }
  910. }
  911. }
  912. #endif
  913. }
  914. return true;
  915. }
  916. }
  917. const GDScriptParser::Node *last_assign = NULL;
  918. int last_assign_line = -1;
  919. for (int i = 0; i < context.block->statements.size(); i++) {
  920. if (context.block->statements[i]->line > p_line)
  921. continue;
  922. if (context.block->statements[i]->type == GDScriptParser::BlockNode::TYPE_LOCAL_VAR) {
  923. const GDScriptParser::LocalVarNode *lv = static_cast<const GDScriptParser::LocalVarNode *>(context.block->statements[i]);
  924. if (lv->assign && lv->name == p_identifier) {
  925. last_assign = lv->assign;
  926. last_assign_line = context.block->statements[i]->line;
  927. }
  928. }
  929. if (context.block->statements[i]->type == GDScriptParser::BlockNode::TYPE_OPERATOR) {
  930. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(context.block->statements[i]);
  931. if (op->op == GDScriptParser::OperatorNode::OP_ASSIGN) {
  932. if (op->arguments.size() && op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  933. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0]);
  934. if (id->name == p_identifier) {
  935. last_assign = op->arguments[1];
  936. last_assign_line = context.block->statements[i]->line;
  937. }
  938. }
  939. }
  940. }
  941. }
  942. //use the last assignment, (then backwards?)
  943. if (last_assign && last_assign_line != p_line) {
  944. return _guess_expression_type(context, last_assign, last_assign_line, r_type);
  945. }
  946. return false;
  947. }
  948. static bool _guess_identifier_from_assignment_in_function(GDScriptCompletionContext &context, int p_src_line, const StringName &p_identifier, const StringName &p_function, GDScriptCompletionIdentifier &r_type) {
  949. const GDScriptParser::FunctionNode *func = NULL;
  950. for (int i = 0; i < context._class->functions.size(); i++) {
  951. if (context._class->functions[i]->name == p_function) {
  952. func = context._class->functions[i];
  953. break;
  954. }
  955. }
  956. if (!func)
  957. return false;
  958. for (int i = 0; i < func->body->statements.size(); i++) {
  959. if (func->body->statements[i]->line == p_src_line) {
  960. break;
  961. }
  962. if (func->body->statements[i]->type == GDScriptParser::BlockNode::TYPE_OPERATOR) {
  963. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(func->body->statements[i]);
  964. if (op->op == GDScriptParser::OperatorNode::OP_ASSIGN) {
  965. if (op->arguments.size() && op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  966. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0]);
  967. if (id->name == p_identifier) {
  968. return _guess_expression_type(context, op->arguments[1], func->body->statements[i]->line, r_type);
  969. }
  970. }
  971. }
  972. }
  973. }
  974. return false;
  975. }
  976. static bool _guess_identifier_type(GDScriptCompletionContext &context, int p_line, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type, bool p_for_indexing) {
  977. //go to block first
  978. const GDScriptParser::BlockNode *block = context.block;
  979. while (block) {
  980. GDScriptCompletionContext c = context;
  981. c.block = block;
  982. if (_guess_identifier_type_in_block(c, p_line, p_identifier, r_type)) {
  983. return true;
  984. }
  985. block = block->parent_block;
  986. }
  987. //guess from argument if virtual
  988. if (context.function && context.function->name != StringName()) {
  989. int argindex = -1;
  990. for (int i = 0; i < context.function->arguments.size(); i++) {
  991. if (context.function->arguments[i] == p_identifier) {
  992. argindex = i;
  993. if (_get_type_from_parser_type(context.function->argument_types[i], r_type)) {
  994. return true;
  995. }
  996. break;
  997. }
  998. }
  999. if (argindex != -1) {
  1000. GDScriptCompletionIdentifier id = _get_native_class(context);
  1001. if (id.type == Variant::OBJECT && id.obj_type != StringName()) {
  1002. //this kinda sucks but meh
  1003. List<MethodInfo> vmethods;
  1004. ClassDB::get_virtual_methods(id.obj_type, &vmethods);
  1005. for (List<MethodInfo>::Element *E = vmethods.front(); E; E = E->next()) {
  1006. if (E->get().name == context.function->name && argindex < E->get().arguments.size()) {
  1007. PropertyInfo arg = E->get().arguments[argindex];
  1008. int scp = String(arg.name).find(":");
  1009. if (scp != -1) {
  1010. r_type.type = Variant::OBJECT;
  1011. r_type.obj_type = String(arg.name).substr(scp + 1, String(arg.name).length());
  1012. return true;
  1013. } else {
  1014. r_type.type = arg.type;
  1015. if (arg.hint == PROPERTY_HINT_RESOURCE_TYPE)
  1016. r_type.obj_type = arg.hint_string;
  1017. return true;
  1018. }
  1019. }
  1020. }
  1021. }
  1022. }
  1023. }
  1024. //guess type in constant
  1025. if (context._class->constant_expressions.has(p_identifier)) {
  1026. ERR_FAIL_COND_V(context._class->constant_expressions[p_identifier].expression->type != GDScriptParser::Node::TYPE_CONSTANT, false);
  1027. r_type = _get_type_from_variant(static_cast<const GDScriptParser::ConstantNode *>(context._class->constant_expressions[p_identifier].expression)->value);
  1028. return true;
  1029. }
  1030. if (!(context.function && context.function->_static)) {
  1031. for (int i = 0; i < context._class->variables.size(); i++) {
  1032. if (context._class->variables[i].identifier == p_identifier) {
  1033. if (context._class->variables[i]._export.type != Variant::NIL) {
  1034. r_type = _get_type_from_pinfo(context._class->variables[i]._export);
  1035. return true;
  1036. } else if (context._class->variables[i].expression) {
  1037. if (p_line <= context._class->variables[i].line)
  1038. return false;
  1039. bool rtype = _guess_expression_type(context, context._class->variables[i].expression, context._class->variables[i].line, r_type);
  1040. if (rtype && r_type.type != Variant::NIL)
  1041. return true;
  1042. //return _guess_expression_type(context,context._class->variables[i].expression,context._class->variables[i].line,r_type);
  1043. }
  1044. //try to guess from assignment in constructor or _ready
  1045. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_ready", r_type))
  1046. return true;
  1047. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_enter_tree", r_type))
  1048. return true;
  1049. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_init", r_type))
  1050. return true;
  1051. return false;
  1052. }
  1053. }
  1054. }
  1055. //autoloads as singletons
  1056. List<PropertyInfo> props;
  1057. ProjectSettings::get_singleton()->get_property_list(&props);
  1058. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1059. String s = E->get().name;
  1060. if (!s.begins_with("autoload/"))
  1061. continue;
  1062. String name = s.get_slice("/", 1);
  1063. if (name == String(p_identifier)) {
  1064. String path = ProjectSettings::get_singleton()->get(s);
  1065. if (path.begins_with("*")) {
  1066. String script = path.substr(1, path.length());
  1067. if (!script.ends_with(".gd")) {
  1068. //not a script, try find the script anyway,
  1069. //may have some success
  1070. script = script.get_basename() + ".gd";
  1071. }
  1072. if (FileAccess::exists(script)) {
  1073. //print_line("is a script");
  1074. Ref<Script> scr;
  1075. if (ScriptCodeCompletionCache::get_singleton())
  1076. scr = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(script);
  1077. else
  1078. scr = ResourceLoader::load(script);
  1079. r_type.obj_type = "Node";
  1080. r_type.type = Variant::OBJECT;
  1081. r_type.script = scr;
  1082. r_type.value = Variant();
  1083. return true;
  1084. }
  1085. }
  1086. }
  1087. }
  1088. //global
  1089. for (Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  1090. if (E->key() == p_identifier) {
  1091. r_type = _get_type_from_variant(GDScriptLanguage::get_singleton()->get_global_array()[E->get()], !p_for_indexing);
  1092. return true;
  1093. }
  1094. }
  1095. return false;
  1096. }
  1097. static void _find_identifiers_in_block(GDScriptCompletionContext &context, int p_line, bool p_only_functions, Set<String> &result) {
  1098. if (p_only_functions)
  1099. return;
  1100. for (int i = 0; i < context.block->statements.size(); i++) {
  1101. GDScriptParser::Node *statement = context.block->statements[i];
  1102. if (statement->line > p_line)
  1103. continue;
  1104. GDScriptParser::BlockNode::Type statementType = statement->type;
  1105. if (statementType == GDScriptParser::BlockNode::TYPE_LOCAL_VAR) {
  1106. const GDScriptParser::LocalVarNode *lv = static_cast<const GDScriptParser::LocalVarNode *>(statement);
  1107. result.insert(lv->name.operator String());
  1108. } else if (statementType == GDScriptParser::BlockNode::TYPE_CONTROL_FLOW) {
  1109. const GDScriptParser::ControlFlowNode *cf = static_cast<const GDScriptParser::ControlFlowNode *>(statement);
  1110. if (cf->cf_type == GDScriptParser::ControlFlowNode::CF_FOR) {
  1111. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(cf->arguments[0]);
  1112. result.insert(id->name.operator String());
  1113. }
  1114. }
  1115. }
  1116. }
  1117. static void _find_identifiers_in_class(GDScriptCompletionContext &context, bool p_static, bool p_only_functions, Set<String> &result) {
  1118. if (!p_static && !p_only_functions) {
  1119. for (int i = 0; i < context._class->variables.size(); i++) {
  1120. result.insert(context._class->variables[i].identifier);
  1121. }
  1122. }
  1123. if (!p_only_functions) {
  1124. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = context._class->constant_expressions.front(); E; E = E->next()) {
  1125. result.insert(E->key());
  1126. }
  1127. for (int i = 0; i < context._class->subclasses.size(); i++) {
  1128. result.insert(context._class->subclasses[i]->name);
  1129. }
  1130. }
  1131. for (int i = 0; i < context._class->static_functions.size(); i++) {
  1132. if (context._class->static_functions[i]->arguments.size())
  1133. result.insert(context._class->static_functions[i]->name.operator String() + "(");
  1134. else
  1135. result.insert(context._class->static_functions[i]->name.operator String() + "()");
  1136. }
  1137. if (!p_static) {
  1138. for (int i = 0; i < context._class->functions.size(); i++) {
  1139. if (context._class->functions[i]->arguments.size())
  1140. result.insert(context._class->functions[i]->name.operator String() + "(");
  1141. else
  1142. result.insert(context._class->functions[i]->name.operator String() + "()");
  1143. }
  1144. }
  1145. //globals
  1146. Ref<Reference> base = _get_parent_class(context);
  1147. while (true) {
  1148. Ref<GDScript> script = base;
  1149. Ref<GDScriptNativeClass> nc = base;
  1150. if (script.is_valid()) {
  1151. if (!p_static && !p_only_functions) {
  1152. for (const Set<StringName>::Element *E = script->get_members().front(); E; E = E->next()) {
  1153. result.insert(E->get().operator String());
  1154. }
  1155. }
  1156. if (!p_only_functions) {
  1157. for (const Map<StringName, Variant>::Element *E = script->get_constants().front(); E; E = E->next()) {
  1158. result.insert(E->key().operator String());
  1159. }
  1160. }
  1161. for (const Map<StringName, GDScriptFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  1162. if (!p_static || E->get()->is_static()) {
  1163. if (E->get()->get_argument_count())
  1164. result.insert(E->key().operator String() + "(");
  1165. else
  1166. result.insert(E->key().operator String() + "()");
  1167. }
  1168. }
  1169. if (!p_only_functions) {
  1170. for (const Map<StringName, Ref<GDScript> >::Element *E = script->get_subclasses().front(); E; E = E->next()) {
  1171. result.insert(E->key().operator String());
  1172. }
  1173. }
  1174. base = script->get_base();
  1175. if (base.is_null())
  1176. base = script->get_native();
  1177. } else if (nc.is_valid()) {
  1178. StringName type = nc->get_name();
  1179. if (!p_only_functions) {
  1180. List<String> constants;
  1181. ClassDB::get_integer_constant_list(type, &constants);
  1182. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  1183. result.insert(E->get());
  1184. }
  1185. List<PropertyInfo> pinfo;
  1186. ClassDB::get_property_list(type, &pinfo);
  1187. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1188. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY))
  1189. continue;
  1190. if (String(E->get().name).find("/") != -1)
  1191. continue;
  1192. result.insert(E->get().name);
  1193. }
  1194. }
  1195. List<MethodInfo> methods;
  1196. ClassDB::get_method_list(type, &methods, false, true);
  1197. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1198. if (E->get().name.begins_with("_"))
  1199. continue;
  1200. if (E->get().arguments.size())
  1201. result.insert(E->get().name + "(");
  1202. else
  1203. result.insert(E->get().name + "()");
  1204. }
  1205. break;
  1206. } else
  1207. break;
  1208. }
  1209. }
  1210. static void _find_identifiers(GDScriptCompletionContext &context, int p_line, bool p_only_functions, Set<String> &result) {
  1211. const GDScriptParser::BlockNode *block = context.block;
  1212. if (context.function) {
  1213. const GDScriptParser::FunctionNode *f = context.function;
  1214. for (int i = 0; i < f->arguments.size(); i++) {
  1215. result.insert(f->arguments[i].operator String());
  1216. }
  1217. }
  1218. while (block) {
  1219. GDScriptCompletionContext c = context;
  1220. c.block = block;
  1221. _find_identifiers_in_block(c, p_line, p_only_functions, result);
  1222. block = block->parent_block;
  1223. }
  1224. const GDScriptParser::ClassNode *clss = context._class;
  1225. bool _static = context.function && context.function->_static;
  1226. while (clss) {
  1227. GDScriptCompletionContext c = context;
  1228. c._class = clss;
  1229. c.block = NULL;
  1230. c.function = NULL;
  1231. _find_identifiers_in_class(c, _static, p_only_functions, result);
  1232. if (!p_only_functions && clss->name != StringName()) {
  1233. result.insert(clss->name.operator String());
  1234. }
  1235. clss = clss->owner;
  1236. }
  1237. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  1238. result.insert(GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i)));
  1239. }
  1240. static const char *_type_names[Variant::VARIANT_MAX] = {
  1241. "null", "bool", "int", "float", "String", "Vector2", "Rect2", "Vector3", "Transform2D", "Plane", "Quat", "AABB", "Basis", "Transform",
  1242. "Color", "NodePath", "RID", "Object", "Dictionary", "Array", "PoolByteArray", "PoolIntArray", "PoolRealArray", "PoolStringArray",
  1243. "PoolVector2Array", "PoolVector3Array", "PoolColorArray"
  1244. };
  1245. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1246. result.insert(_type_names[i]);
  1247. }
  1248. List<String> reserved_words;
  1249. GDScriptLanguage::get_singleton()->get_reserved_words(&reserved_words);
  1250. for (List<String>::Element *E = reserved_words.front(); E; E = E->next()) {
  1251. result.insert(E->get());
  1252. }
  1253. //autoload singletons
  1254. for (const Map<StringName, Variant>::Element *E = GDScriptLanguage::get_singleton()->get_named_globals_map().front(); E; E = E->next()) {
  1255. result.insert(E->key().operator String());
  1256. }
  1257. for (const Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  1258. result.insert(E->key().operator String());
  1259. }
  1260. }
  1261. static void _find_types(GDScriptCompletionContext &p_context, Set<String> &r_result, bool p_is_function) {
  1262. const GDScriptParser::ClassNode *clss = p_context._class;
  1263. while (clss) {
  1264. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = clss->constant_expressions.front(); E; E = E->next()) {
  1265. r_result.insert(E->key().operator String());
  1266. }
  1267. for (int i = 0; i < clss->subclasses.size(); i++) {
  1268. if (clss->subclasses[i]->name != StringName()) {
  1269. r_result.insert(clss->subclasses[i]->name.operator String());
  1270. }
  1271. }
  1272. clss = clss->owner;
  1273. }
  1274. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1275. r_result.insert(Variant::get_type_name((Variant::Type)i));
  1276. }
  1277. List<StringName> native_classes;
  1278. ClassDB::get_class_list(&native_classes);
  1279. for (List<StringName>::Element *E = native_classes.front(); E; E = E->next()) {
  1280. String class_name = E->get().operator String();
  1281. if (class_name.begins_with("_")) {
  1282. class_name = class_name.right(1);
  1283. }
  1284. if (Engine::get_singleton()->has_singleton(class_name)) {
  1285. continue;
  1286. }
  1287. r_result.insert(class_name);
  1288. }
  1289. if (p_is_function) {
  1290. r_result.insert("void");
  1291. }
  1292. }
  1293. static String _get_visual_datatype(const PropertyInfo &p_info, bool p_isarg = true) {
  1294. String n = p_info.name;
  1295. int idx = n.find(":");
  1296. if (idx != -1) {
  1297. return n.substr(idx + 1, n.length());
  1298. }
  1299. if (p_info.type == Variant::OBJECT && p_info.hint == PROPERTY_HINT_RESOURCE_TYPE)
  1300. return p_info.hint_string;
  1301. if (p_info.type == Variant::OBJECT && p_info.class_name != StringName())
  1302. return p_info.class_name.operator String();
  1303. if (p_info.type == Variant::NIL) {
  1304. if (p_isarg)
  1305. return "var";
  1306. else
  1307. return "void";
  1308. }
  1309. return Variant::get_type_name(p_info.type);
  1310. }
  1311. static void _make_function_hint(const GDScriptParser::FunctionNode *p_func, int p_argidx, String &arghint) {
  1312. GDScriptParser::DataType rettype = p_func->return_type;
  1313. if (rettype.has_type && rettype.kind == GDScriptParser::DataType::UNRESOLVED) {
  1314. // Can be unresolved due to incomplete parse, use the unresolved name instead
  1315. arghint = rettype.native_type.operator String();
  1316. } else {
  1317. arghint = rettype.to_string();
  1318. }
  1319. arghint += " " + p_func->name + "(";
  1320. for (int i = 0; i < p_func->arguments.size(); i++) {
  1321. if (i > 0)
  1322. arghint += ", ";
  1323. else
  1324. arghint += " ";
  1325. if (i == p_argidx) {
  1326. arghint += String::chr(0xFFFF);
  1327. }
  1328. GDScriptParser::DataType argtype = p_func->argument_types[i];
  1329. if (argtype.has_type && argtype.kind == GDScriptParser::DataType::UNRESOLVED) {
  1330. // Can be unresolved due to incomplete parse, use the unresolved name instead
  1331. arghint += argtype.native_type.operator String();
  1332. } else {
  1333. arghint += argtype.to_string();
  1334. }
  1335. arghint += " " + p_func->arguments[i].operator String();
  1336. int deffrom = p_func->arguments.size() - p_func->default_values.size();
  1337. if (i >= deffrom) {
  1338. int defidx = deffrom - i;
  1339. if (defidx >= 0 && defidx < p_func->default_values.size()) {
  1340. if (p_func->default_values[defidx]->type == GDScriptParser::Node::TYPE_OPERATOR) {
  1341. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_func->default_values[defidx]);
  1342. if (op->op == GDScriptParser::OperatorNode::OP_ASSIGN) {
  1343. const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(op->arguments[1]);
  1344. arghint += " = " + cn->value.get_construct_string();
  1345. }
  1346. }
  1347. }
  1348. }
  1349. if (i == p_argidx) {
  1350. arghint += String::chr(0xFFFF);
  1351. }
  1352. }
  1353. if (p_func->arguments.size() > 0)
  1354. arghint += " ";
  1355. arghint += ")";
  1356. }
  1357. void get_directory_contents(EditorFileSystemDirectory *p_dir, Set<String> &r_list) {
  1358. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  1359. get_directory_contents(p_dir->get_subdir(i), r_list);
  1360. }
  1361. for (int i = 0; i < p_dir->get_file_count(); i++) {
  1362. r_list.insert("\"" + p_dir->get_file_path(i) + "\"");
  1363. }
  1364. }
  1365. static void _find_type_arguments(GDScriptCompletionContext &context, const GDScriptParser::Node *p_node, int p_line, const StringName &p_method, const GDScriptCompletionIdentifier &id, int p_argidx, Set<String> &result, bool &r_forced, String &arghint) {
  1366. //print_line("find type arguments?");
  1367. if (id.type == Variant::OBJECT && id.obj_type != StringName()) {
  1368. MethodBind *m = ClassDB::get_method(id.obj_type, p_method);
  1369. if (!m) {
  1370. //not in static method, see script
  1371. //print_line("not in static: "+String(p_method));
  1372. Ref<GDScript> on_script;
  1373. if (id.value.get_type()) {
  1374. Object *obj = id.value;
  1375. GDScript *scr = Object::cast_to<GDScript>(obj);
  1376. if (scr) {
  1377. while (scr) {
  1378. for (const Map<StringName, GDScriptFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1379. if (E->get()->is_static() && p_method == E->get()->get_name()) {
  1380. arghint = "static func " + String(p_method) + "(";
  1381. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1382. if (i > 0)
  1383. arghint += ", ";
  1384. else
  1385. arghint += " ";
  1386. if (i == p_argidx) {
  1387. arghint += String::chr(0xFFFF);
  1388. }
  1389. arghint += "var " + E->get()->get_argument_name(i);
  1390. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1391. if (i >= deffrom) {
  1392. int defidx = deffrom - i;
  1393. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1394. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1395. }
  1396. }
  1397. if (i == p_argidx) {
  1398. arghint += String::chr(0xFFFF);
  1399. }
  1400. }
  1401. arghint += ")";
  1402. return; //found
  1403. }
  1404. }
  1405. if (scr->get_base().is_valid())
  1406. scr = scr->get_base().ptr();
  1407. else
  1408. scr = NULL;
  1409. }
  1410. } else {
  1411. if (obj) {
  1412. on_script = obj->get_script();
  1413. }
  1414. }
  1415. }
  1416. //print_line("but it has a script?");
  1417. if (!on_script.is_valid() && id.script.is_valid()) {
  1418. //print_line("yes");
  1419. on_script = id.script;
  1420. }
  1421. if (on_script.is_valid()) {
  1422. GDScript *scr = on_script.ptr();
  1423. if (scr) {
  1424. while (scr) {
  1425. String code = scr->get_source_code();
  1426. //print_line("has source code!");
  1427. if (code != "") {
  1428. //if there is code, parse it. This way is slower but updates in real-time
  1429. GDScriptParser p;
  1430. //Error parse(const String& p_code, const String& p_base_path="", bool p_just_validate=false,const String& p_self_path="",bool p_for_completion=false);
  1431. Error err = p.parse(scr->get_source_code(), scr->get_path().get_base_dir(), true, "", false);
  1432. if (err == OK) {
  1433. //print_line("checking the functions...");
  1434. //only if ok, otherwise use what is cached on the script
  1435. //GDScriptParser::ClassNode *base = p.
  1436. const GDScriptParser::Node *root = p.get_parse_tree();
  1437. ERR_FAIL_COND(root->type != GDScriptParser::Node::TYPE_CLASS);
  1438. const GDScriptParser::ClassNode *cl = static_cast<const GDScriptParser::ClassNode *>(root);
  1439. const GDScriptParser::FunctionNode *func = NULL;
  1440. bool st = false;
  1441. for (int i = 0; i < cl->functions.size(); i++) {
  1442. //print_line(String(cl->functions[i]->name)+" vs "+String(p_method));
  1443. if (cl->functions[i]->name == p_method) {
  1444. func = cl->functions[i];
  1445. }
  1446. }
  1447. for (int i = 0; i < cl->static_functions.size(); i++) {
  1448. //print_line(String(cl->static_functions[i]->name)+" vs "+String(p_method));
  1449. if (cl->static_functions[i]->name == p_method) {
  1450. func = cl->static_functions[i];
  1451. st = true;
  1452. }
  1453. }
  1454. if (func) {
  1455. _make_function_hint(func, p_argidx, arghint);
  1456. return;
  1457. }
  1458. } else {
  1459. //print_line("failed parsing?");
  1460. code = "";
  1461. }
  1462. }
  1463. if (code == "") {
  1464. for (const Map<StringName, GDScriptFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1465. if (p_method == E->get()->get_name()) {
  1466. arghint = "func " + String(p_method) + "(";
  1467. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1468. if (i > 0)
  1469. arghint += ", ";
  1470. else
  1471. arghint += " ";
  1472. if (i == p_argidx) {
  1473. arghint += String::chr(0xFFFF);
  1474. }
  1475. arghint += "var " + E->get()->get_argument_name(i);
  1476. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1477. if (i >= deffrom) {
  1478. int defidx = deffrom - i;
  1479. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1480. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1481. }
  1482. }
  1483. if (i == p_argidx) {
  1484. arghint += String::chr(0xFFFF);
  1485. }
  1486. }
  1487. arghint += ")";
  1488. return; //found
  1489. }
  1490. }
  1491. }
  1492. if (scr->get_base().is_valid())
  1493. scr = scr->get_base().ptr();
  1494. else
  1495. scr = NULL;
  1496. }
  1497. }
  1498. }
  1499. } else {
  1500. //regular method
  1501. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  1502. if (p_argidx < m->get_argument_count()) {
  1503. PropertyInfo pi = m->get_argument_info(p_argidx);
  1504. if (pi.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  1505. String enumeration = pi.class_name;
  1506. if (enumeration.find(".") != -1) {
  1507. //class constant
  1508. List<StringName> constants;
  1509. String cls = enumeration.get_slice(".", 0);
  1510. String enm = enumeration.get_slice(".", 1);
  1511. ClassDB::get_enum_constants(cls, enm, &constants);
  1512. //constants.sort_custom<StringName::AlphCompare>();
  1513. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  1514. String add = cls + "." + E->get();
  1515. result.insert(add);
  1516. r_forced = true;
  1517. }
  1518. } else {
  1519. //global constant
  1520. StringName current_enum = enumeration;
  1521. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  1522. if (GlobalConstants::get_global_constant_enum(i) == current_enum) {
  1523. result.insert(GlobalConstants::get_global_constant_name(i));
  1524. r_forced = true;
  1525. }
  1526. }
  1527. //global
  1528. }
  1529. }
  1530. }
  1531. #endif
  1532. if (p_method.operator String() == "connect" || (p_method.operator String() == "emit_signal" && p_argidx == 0)) {
  1533. if (p_argidx == 0) {
  1534. List<MethodInfo> sigs;
  1535. ClassDB::get_signal_list(id.obj_type, &sigs);
  1536. if (id.script.is_valid()) {
  1537. id.script->get_script_signal_list(&sigs);
  1538. } else if (id.value.get_type() == Variant::OBJECT) {
  1539. Object *obj = id.value;
  1540. if (obj && !obj->get_script().is_null()) {
  1541. Ref<Script> scr = obj->get_script();
  1542. if (scr.is_valid()) {
  1543. scr->get_script_signal_list(&sigs);
  1544. }
  1545. }
  1546. }
  1547. for (List<MethodInfo>::Element *E = sigs.front(); E; E = E->next()) {
  1548. result.insert("\"" + E->get().name + "\"");
  1549. r_forced = true;
  1550. }
  1551. } else if (p_argidx == 2) {
  1552. if (context._class) {
  1553. for (int i = 0; i < context._class->functions.size(); i++) {
  1554. result.insert("\"" + context._class->functions[i]->name + "\"");
  1555. r_forced = true;
  1556. }
  1557. }
  1558. }
  1559. /*if (p_argidx==2) {
  1560. ERR_FAIL_COND(p_node->type!=GDScriptParser::Node::TYPE_OPERATOR);
  1561. const GDScriptParser::OperatorNode *op=static_cast<const GDScriptParser::OperatorNode *>(p_node);
  1562. if (op->arguments.size()>)
  1563. }*/
  1564. } else {
  1565. if (p_argidx == 0 && (String(p_method) == "get_node" || String(p_method) == "has_node") && ClassDB::is_parent_class(id.obj_type, "Node")) {
  1566. List<PropertyInfo> props;
  1567. ProjectSettings::get_singleton()->get_property_list(&props);
  1568. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1569. String s = E->get().name;
  1570. if (!s.begins_with("autoload/"))
  1571. continue;
  1572. //print_line("found "+s);
  1573. String name = s.get_slice("/", 1);
  1574. result.insert("\"/root/" + name + "\"");
  1575. r_forced = true;
  1576. }
  1577. }
  1578. Object *obj = id.value;
  1579. if (obj) {
  1580. List<String> options;
  1581. obj->get_argument_options(p_method, p_argidx, &options);
  1582. for (List<String>::Element *E = options.front(); E; E = E->next()) {
  1583. result.insert(E->get());
  1584. r_forced = true;
  1585. }
  1586. }
  1587. }
  1588. arghint = _get_visual_datatype(m->get_return_info(), false) + " " + p_method.operator String() + String("(");
  1589. for (int i = 0; i < m->get_argument_count(); i++) {
  1590. if (i > 0)
  1591. arghint += ", ";
  1592. else
  1593. arghint += " ";
  1594. if (i == p_argidx) {
  1595. arghint += String::chr(0xFFFF);
  1596. }
  1597. String n = m->get_argument_info(i).name;
  1598. int dp = n.find(":");
  1599. if (dp != -1)
  1600. n = n.substr(0, dp);
  1601. arghint += _get_visual_datatype(m->get_argument_info(i)) + " " + n;
  1602. int deffrom = m->get_argument_count() - m->get_default_argument_count();
  1603. if (i >= deffrom) {
  1604. int defidx = i - deffrom;
  1605. if (defidx >= 0 && defidx < m->get_default_argument_count()) {
  1606. Variant v = m->get_default_argument(i);
  1607. arghint += "=" + v.get_construct_string();
  1608. }
  1609. }
  1610. if (i == p_argidx) {
  1611. arghint += String::chr(0xFFFF);
  1612. }
  1613. }
  1614. if (m->get_argument_count() > 0)
  1615. arghint += " ";
  1616. arghint += ")";
  1617. }
  1618. }
  1619. }
  1620. static void _find_call_arguments(GDScriptCompletionContext &context, const GDScriptParser::Node *p_node, int p_line, int p_argidx, Set<String> &result, bool &r_forced, String &arghint) {
  1621. if (!p_node || p_node->type != GDScriptParser::Node::TYPE_OPERATOR) {
  1622. return;
  1623. }
  1624. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_node);
  1625. if (op->op != GDScriptParser::OperatorNode::OP_CALL) {
  1626. return;
  1627. }
  1628. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
  1629. //complete built-in function
  1630. const GDScriptParser::BuiltInFunctionNode *fn = static_cast<const GDScriptParser::BuiltInFunctionNode *>(op->arguments[0]);
  1631. MethodInfo mi = GDScriptFunctions::get_info(fn->function);
  1632. if (mi.name == "load" && bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) {
  1633. get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), result);
  1634. }
  1635. arghint = _get_visual_datatype(mi.return_val, false) + " " + GDScriptFunctions::get_func_name(fn->function) + String("(");
  1636. for (int i = 0; i < mi.arguments.size(); i++) {
  1637. if (i > 0)
  1638. arghint += ", ";
  1639. else
  1640. arghint += " ";
  1641. if (i == p_argidx || ((mi.flags & METHOD_FLAG_VARARG) && i > p_argidx)) {
  1642. arghint += String::chr(0xFFFF);
  1643. }
  1644. arghint += _get_visual_datatype(mi.arguments[i]) + " " + mi.arguments[i].name;
  1645. if (i == p_argidx || ((mi.flags & METHOD_FLAG_VARARG) && i > p_argidx)) {
  1646. arghint += String::chr(0xFFFF);
  1647. }
  1648. }
  1649. if (mi.arguments.size() > 0)
  1650. arghint += " ";
  1651. arghint += ")";
  1652. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) {
  1653. //complete constructor
  1654. const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(op->arguments[0]);
  1655. List<MethodInfo> mil;
  1656. Variant::get_constructor_list(tn->vtype, &mil);
  1657. for (List<MethodInfo>::Element *E = mil.front(); E; E = E->next()) {
  1658. MethodInfo mi = E->get();
  1659. if (mi.arguments.size() == 0)
  1660. continue;
  1661. if (E->prev())
  1662. arghint += "\n";
  1663. arghint += Variant::get_type_name(tn->vtype) + " " + Variant::get_type_name(tn->vtype) + String("(");
  1664. for (int i = 0; i < mi.arguments.size(); i++) {
  1665. if (i > 0)
  1666. arghint += ", ";
  1667. else
  1668. arghint += " ";
  1669. if (i == p_argidx) {
  1670. arghint += String::chr(0xFFFF);
  1671. }
  1672. arghint += _get_visual_datatype(mi.arguments[i]) + " " + mi.arguments[i].name;
  1673. if (i == p_argidx) {
  1674. arghint += String::chr(0xFFFF);
  1675. }
  1676. }
  1677. if (mi.arguments.size() > 0)
  1678. arghint += " ";
  1679. arghint += ")";
  1680. }
  1681. } else if (op->arguments.size() >= 2 && op->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  1682. //make sure identifier exists...
  1683. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1]);
  1684. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_SELF) {
  1685. //self, look up
  1686. for (int i = 0; i < context._class->static_functions.size(); i++) {
  1687. if (context._class->static_functions[i]->name == id->name) {
  1688. _make_function_hint(context._class->static_functions[i], p_argidx, arghint);
  1689. return;
  1690. }
  1691. }
  1692. if (context.function && !context.function->_static) {
  1693. for (int i = 0; i < context._class->functions.size(); i++) {
  1694. if (context._class->functions[i]->name == id->name) {
  1695. _make_function_hint(context._class->functions[i], p_argidx, arghint);
  1696. return;
  1697. }
  1698. }
  1699. }
  1700. Ref<Reference> base = _get_parent_class(context);
  1701. while (true) {
  1702. Ref<GDScript> script = base;
  1703. Ref<GDScriptNativeClass> nc = base;
  1704. if (script.is_valid()) {
  1705. for (const Map<StringName, GDScriptFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  1706. if (E->key() == id->name) {
  1707. if (context.function && context.function->_static && !E->get()->is_static())
  1708. continue;
  1709. arghint = "func " + id->name.operator String() + String("(");
  1710. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1711. if (i > 0)
  1712. arghint += ", ";
  1713. else
  1714. arghint += " ";
  1715. if (i == p_argidx) {
  1716. arghint += String::chr(0xFFFF);
  1717. }
  1718. arghint += E->get()->get_argument_name(i);
  1719. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1720. if (i >= deffrom) {
  1721. int defidx = deffrom - i;
  1722. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1723. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1724. }
  1725. }
  1726. if (i == p_argidx) {
  1727. arghint += String::chr(0xFFFF);
  1728. }
  1729. }
  1730. if (E->get()->get_argument_count() > 0)
  1731. arghint += " ";
  1732. arghint += ")";
  1733. return;
  1734. }
  1735. }
  1736. base = script->get_base();
  1737. if (base.is_null())
  1738. base = script->get_native();
  1739. } else if (nc.is_valid()) {
  1740. if (!(context.function && context.function->_static)) {
  1741. GDScriptCompletionIdentifier ci;
  1742. ci.type = Variant::OBJECT;
  1743. ci.obj_type = nc->get_name();
  1744. if (!context._class->owner)
  1745. ci.value = context.base;
  1746. _find_type_arguments(context, p_node, p_line, id->name, ci, p_argidx, result, r_forced, arghint);
  1747. //guess type..
  1748. /*
  1749. List<MethodInfo> methods;
  1750. ClassDB::get_method_list(type,&methods);
  1751. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1752. if (E->get().arguments.size())
  1753. result.insert(E->get().name+"(");
  1754. else
  1755. result.insert(E->get().name+"()");
  1756. }*/
  1757. }
  1758. break;
  1759. } else
  1760. break;
  1761. }
  1762. } else {
  1763. //indexed lookup
  1764. GDScriptCompletionIdentifier ci;
  1765. if (_guess_expression_type(context, op->arguments[0], p_line, ci)) {
  1766. _find_type_arguments(context, p_node, p_line, id->name, ci, p_argidx, result, r_forced, arghint);
  1767. return;
  1768. }
  1769. }
  1770. }
  1771. }
  1772. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, bool &r_forced, String &r_call_hint) {
  1773. GDScriptParser p;
  1774. p.parse(p_code, p_base_path, false, "", true);
  1775. bool isfunction = false;
  1776. Set<String> options;
  1777. r_forced = false;
  1778. GDScriptCompletionContext context;
  1779. context._class = p.get_completion_class();
  1780. context.block = p.get_completion_block();
  1781. context.function = p.get_completion_function();
  1782. context.base = p_owner;
  1783. context.base_path = p_base_path;
  1784. switch (p.get_completion_type()) {
  1785. case GDScriptParser::COMPLETION_NONE: {
  1786. } break;
  1787. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  1788. List<StringName> constants;
  1789. Variant::get_numeric_constants_for_type(p.get_completion_built_in_constant(), &constants);
  1790. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  1791. options.insert(E->get().operator String());
  1792. }
  1793. } break;
  1794. case GDScriptParser::COMPLETION_FUNCTION:
  1795. isfunction = true;
  1796. case GDScriptParser::COMPLETION_IDENTIFIER: {
  1797. _find_identifiers(context, p.get_completion_line(), isfunction, options);
  1798. } break;
  1799. case GDScriptParser::COMPLETION_PARENT_FUNCTION: {
  1800. } break;
  1801. case GDScriptParser::COMPLETION_GET_NODE: {
  1802. if (p_owner) {
  1803. List<String> opts;
  1804. p_owner->get_argument_options("get_node", 0, &opts);
  1805. for (List<String>::Element *E = opts.front(); E; E = E->next()) {
  1806. String opt = E->get().strip_edges();
  1807. if (opt.is_quoted()) {
  1808. r_forced = true;
  1809. String idopt = opt.unquote();
  1810. if (idopt.replace("/", "_").is_valid_identifier()) {
  1811. options.insert(idopt);
  1812. } else {
  1813. options.insert(opt);
  1814. }
  1815. }
  1816. }
  1817. }
  1818. } break;
  1819. case GDScriptParser::COMPLETION_METHOD:
  1820. isfunction = true;
  1821. case GDScriptParser::COMPLETION_INDEX: {
  1822. const GDScriptParser::Node *node = p.get_completion_node();
  1823. if (node->type != GDScriptParser::Node::TYPE_OPERATOR)
  1824. break;
  1825. GDScriptCompletionIdentifier t;
  1826. if (_guess_expression_type(context, static_cast<const GDScriptParser::OperatorNode *>(node)->arguments[0], p.get_completion_line(), t, true)) {
  1827. if (t.type == Variant::OBJECT && t.obj_type == "GDScriptNativeClass") {
  1828. //native enum
  1829. Ref<GDScriptNativeClass> gdn = t.value;
  1830. if (gdn.is_valid()) {
  1831. StringName cn = gdn->get_name();
  1832. List<String> cnames;
  1833. ClassDB::get_integer_constant_list(cn, &cnames);
  1834. for (List<String>::Element *E = cnames.front(); E; E = E->next()) {
  1835. options.insert(E->get());
  1836. }
  1837. List<PropertyInfo> pinfo;
  1838. ClassDB::get_property_list(cn, &pinfo);
  1839. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1840. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY))
  1841. continue;
  1842. if (String(E->get().name).find("/") != -1)
  1843. continue;
  1844. options.insert(E->get().name);
  1845. }
  1846. }
  1847. } else if (t.type == Variant::OBJECT && t.obj_type != StringName()) {
  1848. Ref<GDScript> on_script;
  1849. if (t.value.get_type()) {
  1850. Object *obj = t.value;
  1851. GDScript *scr = Object::cast_to<GDScript>(obj);
  1852. if (scr) {
  1853. while (scr) {
  1854. if (!isfunction) {
  1855. for (const Map<StringName, Variant>::Element *E = scr->get_constants().front(); E; E = E->next()) {
  1856. options.insert(E->key());
  1857. }
  1858. }
  1859. for (const Map<StringName, GDScriptFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1860. if (E->get()->is_static())
  1861. options.insert(E->key());
  1862. }
  1863. if (scr->get_base().is_valid())
  1864. scr = scr->get_base().ptr();
  1865. else
  1866. scr = NULL;
  1867. }
  1868. } else {
  1869. if (obj) {
  1870. on_script = obj->get_script();
  1871. }
  1872. }
  1873. }
  1874. if (!on_script.is_valid() && t.script.is_valid()) {
  1875. on_script = t.script;
  1876. }
  1877. if (on_script.is_valid()) {
  1878. GDScript *scr = on_script.ptr();
  1879. if (scr) {
  1880. while (scr) {
  1881. String code = scr->get_source_code();
  1882. if (code != "") {
  1883. //if there is code, parse it. This way is slower but updates in real-time
  1884. GDScriptParser p;
  1885. Error err = p.parse(scr->get_source_code(), scr->get_path().get_base_dir(), true, "", false);
  1886. if (err == OK) {
  1887. //only if ok, otherwise use what is cached on the script
  1888. //GDScriptParser::ClassNode *base = p.
  1889. const GDScriptParser::Node *root = p.get_parse_tree();
  1890. ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, ERR_PARSE_ERROR);
  1891. const GDScriptParser::ClassNode *cl = static_cast<const GDScriptParser::ClassNode *>(root);
  1892. for (int i = 0; i < cl->functions.size(); i++) {
  1893. if (cl->functions[i]->arguments.size())
  1894. options.insert(String(cl->functions[i]->name) + "(");
  1895. else
  1896. options.insert(String(cl->functions[i]->name) + "()");
  1897. }
  1898. for (int i = 0; i < cl->static_functions.size(); i++) {
  1899. if (cl->static_functions[i]->arguments.size())
  1900. options.insert(String(cl->static_functions[i]->name) + "(");
  1901. else
  1902. options.insert(String(cl->static_functions[i]->name) + "()");
  1903. }
  1904. if (!isfunction) {
  1905. for (int i = 0; i < cl->variables.size(); i++) {
  1906. options.insert(String(cl->variables[i].identifier));
  1907. }
  1908. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = cl->constant_expressions.front(); E; E = E->next()) {
  1909. options.insert(String(E->key()));
  1910. }
  1911. }
  1912. } else {
  1913. code = ""; //well, then no code
  1914. }
  1915. }
  1916. if (code == "") {
  1917. //use class directly, no code was found
  1918. if (!isfunction) {
  1919. for (const Map<StringName, Variant>::Element *E = scr->get_constants().front(); E; E = E->next()) {
  1920. options.insert(E->key());
  1921. }
  1922. }
  1923. for (const Map<StringName, GDScriptFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1924. if (E->get()->get_argument_count())
  1925. options.insert(String(E->key()) + "()");
  1926. else
  1927. options.insert(String(E->key()) + "(");
  1928. }
  1929. for (const Set<StringName>::Element *E = scr->get_members().front(); E; E = E->next()) {
  1930. options.insert(E->get());
  1931. }
  1932. }
  1933. if (scr->get_base().is_valid())
  1934. scr = scr->get_base().ptr();
  1935. else
  1936. scr = NULL;
  1937. }
  1938. }
  1939. }
  1940. if (!isfunction) {
  1941. ClassDB::get_integer_constant_list(t.obj_type, r_options);
  1942. List<PropertyInfo> pinfo;
  1943. ClassDB::get_property_list(t.obj_type, &pinfo);
  1944. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1945. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY))
  1946. continue;
  1947. if (String(E->get().name).find("/") != -1)
  1948. continue;
  1949. r_options->push_back(E->get().name);
  1950. }
  1951. }
  1952. List<MethodInfo> mi;
  1953. ClassDB::get_method_list(t.obj_type, &mi, false, true);
  1954. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  1955. if (E->get().name.begins_with("_"))
  1956. continue;
  1957. if (E->get().arguments.size())
  1958. options.insert(E->get().name + "(");
  1959. else
  1960. options.insert(E->get().name + "()");
  1961. }
  1962. } else {
  1963. //check InputEvent hint
  1964. {
  1965. if (t.value.get_type() == Variant::NIL) {
  1966. Variant::CallError ce;
  1967. t.value = Variant::construct(t.type, NULL, 0, ce);
  1968. }
  1969. if (!isfunction) {
  1970. List<PropertyInfo> pl;
  1971. t.value.get_property_list(&pl);
  1972. for (List<PropertyInfo>::Element *E = pl.front(); E; E = E->next()) {
  1973. if (String(E->get().name).find("/") == -1)
  1974. options.insert(E->get().name);
  1975. }
  1976. }
  1977. List<MethodInfo> mi;
  1978. t.value.get_method_list(&mi);
  1979. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  1980. if (E->get().arguments.size())
  1981. options.insert(E->get().name + "(");
  1982. else
  1983. options.insert(E->get().name + "()");
  1984. }
  1985. }
  1986. }
  1987. }
  1988. } break;
  1989. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  1990. _find_call_arguments(context, p.get_completion_node(), p.get_completion_line(), p.get_completion_argument_index(), options, r_forced, r_call_hint);
  1991. } break;
  1992. case GDScriptParser::COMPLETION_VIRTUAL_FUNC: {
  1993. GDScriptCompletionIdentifier cid = _get_native_class(context);
  1994. if (cid.obj_type != StringName()) {
  1995. List<MethodInfo> vm;
  1996. ClassDB::get_virtual_methods(cid.obj_type, &vm);
  1997. bool type_hint = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints").operator bool();
  1998. for (List<MethodInfo>::Element *E = vm.front(); E; E = E->next()) {
  1999. MethodInfo &mi = E->get();
  2000. String m = mi.name;
  2001. if (m.find(":") != -1)
  2002. m = m.substr(0, m.find(":"));
  2003. m += "(";
  2004. if (mi.arguments.size()) {
  2005. for (int i = 0; i < mi.arguments.size(); i++) {
  2006. if (i > 0)
  2007. m += ", ";
  2008. String n = mi.arguments[i].name;
  2009. if (n.find(":") != -1)
  2010. n = n.substr(0, n.find(":"));
  2011. m += n;
  2012. if (type_hint && mi.arguments[i].type != Variant::NIL) {
  2013. m += " : ";
  2014. if (mi.arguments[i].type == Variant::OBJECT && mi.arguments[i].class_name != StringName()) {
  2015. m += mi.arguments[i].class_name.operator String();
  2016. } else {
  2017. m += Variant::get_type_name(mi.arguments[i].type);
  2018. }
  2019. }
  2020. }
  2021. }
  2022. m += ")";
  2023. if (type_hint && (mi.return_val.type != Variant::NIL || !(mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
  2024. m += " -> ";
  2025. if (mi.return_val.type == Variant::NIL) {
  2026. m += "void";
  2027. } else if (mi.return_val.type == Variant::OBJECT && mi.return_val.class_name != StringName()) {
  2028. m += mi.return_val.class_name.operator String();
  2029. } else {
  2030. m += Variant::get_type_name(mi.return_val.type);
  2031. }
  2032. }
  2033. m += ":";
  2034. options.insert(m);
  2035. }
  2036. }
  2037. } break;
  2038. case GDScriptParser::COMPLETION_YIELD: {
  2039. const GDScriptParser::Node *node = p.get_completion_node();
  2040. GDScriptCompletionIdentifier t;
  2041. if (!_guess_expression_type(context, node, p.get_completion_line(), t))
  2042. break;
  2043. if (t.type == Variant::OBJECT && t.obj_type != StringName()) {
  2044. List<MethodInfo> sigs;
  2045. ClassDB::get_signal_list(t.obj_type, &sigs);
  2046. for (List<MethodInfo>::Element *E = sigs.front(); E; E = E->next()) {
  2047. options.insert("\"" + E->get().name + "\"");
  2048. r_forced = true;
  2049. }
  2050. }
  2051. } break;
  2052. case GDScriptParser::COMPLETION_RESOURCE_PATH: {
  2053. if (EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths")) {
  2054. get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
  2055. r_forced = true;
  2056. }
  2057. } break;
  2058. case GDScriptParser::COMPLETION_ASSIGN: {
  2059. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  2060. GDScriptCompletionIdentifier ci;
  2061. if (_guess_expression_type(context, p.get_completion_node(), p.get_completion_line(), ci)) {
  2062. String enumeration = ci.enumeration;
  2063. if (enumeration.find(".") != -1) {
  2064. //class constant
  2065. List<StringName> constants;
  2066. String cls = enumeration.get_slice(".", 0);
  2067. String enm = enumeration.get_slice(".", 1);
  2068. ClassDB::get_enum_constants(cls, enm, &constants);
  2069. //constants.sort_custom<StringName::AlphCompare>();
  2070. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  2071. String add = cls + "." + E->get();
  2072. r_options->push_back(add);
  2073. r_forced = true;
  2074. }
  2075. } else {
  2076. //global constant
  2077. StringName current_enum = enumeration;
  2078. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  2079. if (GlobalConstants::get_global_constant_enum(i) == current_enum) {
  2080. r_options->push_back(GlobalConstants::get_global_constant_name(i));
  2081. r_forced = true;
  2082. }
  2083. }
  2084. //global
  2085. }
  2086. }
  2087. #endif
  2088. } break;
  2089. case GDScriptParser::COMPLETION_TYPE_HINT: {
  2090. _find_types(context, options, p.get_completion_identifier_is_function());
  2091. } break;
  2092. case GDScriptParser::COMPLETION_TYPE_HINT_INDEX: {
  2093. options.insert("SomeIndexedTypeHere");
  2094. options.insert(p.get_completion_cursor().operator String());
  2095. } break;
  2096. }
  2097. for (Set<String>::Element *E = options.front(); E; E = E->next()) {
  2098. r_options->push_back(E->get());
  2099. }
  2100. return OK;
  2101. }
  2102. #else
  2103. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, bool &r_forced, String &r_call_hint) {
  2104. return OK;
  2105. }
  2106. #endif
  2107. String GDScriptLanguage::_get_indentation() const {
  2108. #ifdef TOOLS_ENABLED
  2109. if (Engine::get_singleton()->is_editor_hint()) {
  2110. bool use_space_indentation = EDITOR_DEF("text_editor/indent/type", 0);
  2111. if (use_space_indentation) {
  2112. int indent_size = EDITOR_DEF("text_editor/indent/size", 4);
  2113. String space_indent = "";
  2114. for (int i = 0; i < indent_size; i++) {
  2115. space_indent += " ";
  2116. }
  2117. return space_indent;
  2118. }
  2119. }
  2120. #endif
  2121. return "\t";
  2122. }
  2123. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  2124. String indent = _get_indentation();
  2125. Vector<String> lines = p_code.split("\n");
  2126. List<int> indent_stack;
  2127. for (int i = 0; i < lines.size(); i++) {
  2128. String l = lines[i];
  2129. int tc = 0;
  2130. for (int j = 0; j < l.length(); j++) {
  2131. if (l[j] == ' ' || l[j] == '\t') {
  2132. tc++;
  2133. } else {
  2134. break;
  2135. }
  2136. }
  2137. String st = l.substr(tc, l.length()).strip_edges();
  2138. if (st == "" || st.begins_with("#"))
  2139. continue; //ignore!
  2140. int ilevel = 0;
  2141. if (indent_stack.size()) {
  2142. ilevel = indent_stack.back()->get();
  2143. }
  2144. if (tc > ilevel) {
  2145. indent_stack.push_back(tc);
  2146. } else if (tc < ilevel) {
  2147. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  2148. indent_stack.pop_back();
  2149. }
  2150. if (indent_stack.size() && indent_stack.back()->get() != tc)
  2151. indent_stack.push_back(tc); //this is not right but gets the job done
  2152. }
  2153. if (i >= p_from_line) {
  2154. l = "";
  2155. for (int j = 0; j < indent_stack.size(); j++) {
  2156. l += indent;
  2157. }
  2158. l += st;
  2159. } else if (i > p_to_line) {
  2160. break;
  2161. }
  2162. //print_line(itos(indent_stack.size())+","+itos(tc)+": "+l);
  2163. lines[i] = l;
  2164. }
  2165. p_code = "";
  2166. for (int i = 0; i < lines.size(); i++) {
  2167. if (i > 0)
  2168. p_code += "\n";
  2169. p_code += lines[i];
  2170. }
  2171. }
  2172. #ifdef TOOLS_ENABLED
  2173. Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_base_path, Object *p_owner, LookupResult &r_result) {
  2174. //before parsing, try the usual stuff
  2175. if (ClassDB::class_exists(p_symbol)) {
  2176. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2177. r_result.class_name = p_symbol;
  2178. return OK;
  2179. }
  2180. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2181. Variant::Type t = Variant::Type(i);
  2182. if (Variant::get_type_name(t) == p_symbol) {
  2183. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2184. r_result.class_name = Variant::get_type_name(t);
  2185. return OK;
  2186. }
  2187. }
  2188. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  2189. if (GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i)) == p_symbol) {
  2190. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2191. r_result.class_name = "@GDScript";
  2192. r_result.class_member = p_symbol;
  2193. return OK;
  2194. }
  2195. }
  2196. if ("PI" == p_symbol || "TAU" == p_symbol || "INF" == p_symbol || "NAN" == p_symbol) {
  2197. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2198. r_result.class_name = "@GDScript";
  2199. r_result.class_member = p_symbol;
  2200. return OK;
  2201. }
  2202. GDScriptParser p;
  2203. p.parse(p_code, p_base_path, false, "", true);
  2204. if (p.get_completion_type() == GDScriptParser::COMPLETION_NONE)
  2205. return ERR_CANT_RESOLVE;
  2206. GDScriptCompletionContext context;
  2207. context._class = p.get_completion_class();
  2208. context.block = p.get_completion_block();
  2209. context.function = p.get_completion_function();
  2210. context.base = p_owner;
  2211. context.base_path = p_base_path;
  2212. if (context._class && context._class->extends_class.size() > 0) {
  2213. bool success = false;
  2214. ClassDB::get_integer_constant(context._class->extends_class[0], p_symbol, &success);
  2215. if (success) {
  2216. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2217. r_result.class_name = context._class->extends_class[0];
  2218. r_result.class_member = p_symbol;
  2219. return OK;
  2220. }
  2221. }
  2222. bool isfunction = false;
  2223. switch (p.get_completion_type()) {
  2224. case GDScriptParser::COMPLETION_GET_NODE:
  2225. case GDScriptParser::COMPLETION_NONE: {
  2226. } break;
  2227. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  2228. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2229. r_result.class_name = Variant::get_type_name(p.get_completion_built_in_constant());
  2230. r_result.class_member = p_symbol;
  2231. return OK;
  2232. } break;
  2233. case GDScriptParser::COMPLETION_FUNCTION: {
  2234. if (context._class && context._class->functions.size()) {
  2235. for (int i = 0; i < context._class->functions.size(); i++) {
  2236. if (context._class->functions[i]->name == p_symbol) {
  2237. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2238. r_result.location = context._class->functions[i]->line;
  2239. return OK;
  2240. }
  2241. }
  2242. }
  2243. Ref<GDScript> parent = _get_parent_class(context);
  2244. while (parent.is_valid()) {
  2245. int line = parent->get_member_line(p_symbol);
  2246. if (line >= 0) {
  2247. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2248. r_result.location = line;
  2249. r_result.script = parent;
  2250. return OK;
  2251. }
  2252. parent = parent->get_base();
  2253. }
  2254. GDScriptCompletionIdentifier identifier = _get_native_class(context);
  2255. print_line("identifier: " + String(identifier.obj_type));
  2256. if (ClassDB::has_method(identifier.obj_type, p_symbol)) {
  2257. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2258. r_result.class_name = identifier.obj_type;
  2259. r_result.class_member = p_symbol;
  2260. return OK;
  2261. }
  2262. } break;
  2263. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2264. //check if a function
  2265. if (p.get_completion_identifier_is_function()) {
  2266. if (context._class && context._class->functions.size()) {
  2267. for (int i = 0; i < context._class->functions.size(); i++) {
  2268. if (context._class->functions[i]->name == p_symbol) {
  2269. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2270. r_result.location = context._class->functions[i]->line;
  2271. return OK;
  2272. }
  2273. }
  2274. }
  2275. Ref<GDScript> parent = _get_parent_class(context);
  2276. while (parent.is_valid()) {
  2277. int line = parent->get_member_line(p_symbol);
  2278. if (line >= 0) {
  2279. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2280. r_result.location = line;
  2281. r_result.script = parent;
  2282. return OK;
  2283. }
  2284. parent = parent->get_base();
  2285. }
  2286. GDScriptCompletionIdentifier identifier = _get_native_class(context);
  2287. if (ClassDB::has_method(identifier.obj_type, p_symbol)) {
  2288. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2289. r_result.class_name = identifier.obj_type;
  2290. r_result.class_member = p_symbol;
  2291. return OK;
  2292. }
  2293. } else {
  2294. GDScriptCompletionIdentifier gdi = _get_native_class(context);
  2295. if (gdi.obj_type != StringName()) {
  2296. bool valid;
  2297. Variant::Type t = ClassDB::get_property_type(gdi.obj_type, p_symbol, &valid);
  2298. if (t != Variant::NIL && valid) {
  2299. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2300. r_result.class_name = gdi.obj_type;
  2301. r_result.class_member = p_symbol;
  2302. return OK;
  2303. }
  2304. }
  2305. const GDScriptParser::BlockNode *block = context.block;
  2306. //search in blocks going up (local var?)
  2307. while (block) {
  2308. for (int i = 0; i < block->statements.size(); i++) {
  2309. if (block->statements[i]->line > p.get_completion_line())
  2310. continue;
  2311. if (block->statements[i]->type == GDScriptParser::BlockNode::TYPE_LOCAL_VAR) {
  2312. const GDScriptParser::LocalVarNode *lv = static_cast<const GDScriptParser::LocalVarNode *>(block->statements[i]);
  2313. if (lv->assign && lv->name == p_symbol) {
  2314. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2315. r_result.location = block->statements[i]->line;
  2316. return OK;
  2317. }
  2318. }
  2319. }
  2320. block = block->parent_block;
  2321. }
  2322. //guess from function arguments
  2323. if (context.function && context.function->name != StringName()) {
  2324. for (int i = 0; i < context.function->arguments.size(); i++) {
  2325. if (context.function->arguments[i] == p_symbol) {
  2326. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2327. r_result.location = context.function->line;
  2328. return OK;
  2329. }
  2330. }
  2331. }
  2332. //guess in class constants
  2333. if (context._class->constant_expressions.has(p_symbol)) {
  2334. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2335. r_result.location = context._class->constant_expressions[p_symbol].expression->line;
  2336. return OK;
  2337. }
  2338. //guess in class variables
  2339. if (!(context.function && context.function->_static)) {
  2340. for (int i = 0; i < context._class->variables.size(); i++) {
  2341. if (context._class->variables[i].identifier == p_symbol) {
  2342. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2343. r_result.location = context._class->variables[i].line;
  2344. return OK;
  2345. }
  2346. }
  2347. }
  2348. //guess in autoloads as singletons
  2349. List<PropertyInfo> props;
  2350. ProjectSettings::get_singleton()->get_property_list(&props);
  2351. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2352. String s = E->get().name;
  2353. if (!s.begins_with("autoload/"))
  2354. continue;
  2355. String name = s.get_slice("/", 1);
  2356. if (name == String(p_symbol)) {
  2357. String path = ProjectSettings::get_singleton()->get(s);
  2358. if (path.begins_with("*")) {
  2359. String script = path.substr(1, path.length());
  2360. if (!script.ends_with(".gd")) {
  2361. //not a script, try find the script anyway,
  2362. //may have some success
  2363. script = script.get_basename() + ".gd";
  2364. }
  2365. if (FileAccess::exists(script)) {
  2366. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2367. r_result.location = 0;
  2368. r_result.script = ResourceLoader::load(script);
  2369. return OK;
  2370. }
  2371. }
  2372. }
  2373. }
  2374. //global
  2375. Map<StringName, int> classes = GDScriptLanguage::get_singleton()->get_global_map();
  2376. if (classes.has(p_symbol)) {
  2377. Variant value = GDScriptLanguage::get_singleton()->get_global_array()[classes[p_symbol]];
  2378. if (value.get_type() == Variant::OBJECT) {
  2379. Object *obj = value;
  2380. if (obj) {
  2381. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  2382. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2383. r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  2384. } else {
  2385. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2386. r_result.class_name = obj->get_class();
  2387. }
  2388. // proxy class remove the underscore.
  2389. if (r_result.class_name.begins_with("_")) {
  2390. r_result.class_name = r_result.class_name.right(1);
  2391. }
  2392. return OK;
  2393. }
  2394. } else {
  2395. /*
  2396. // Because get_integer_constant_enum and get_integer_constant dont work on @GlobalScope
  2397. // We cannot determine the exact nature of the identifier here
  2398. // Otherwise these codes would work
  2399. StringName enumName = ClassDB::get_integer_constant_enum("@GlobalScope", p_symbol, true);
  2400. if (enumName != NULL) {
  2401. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  2402. r_result.class_name = "@GlobalScope";
  2403. r_result.class_member = enumName;
  2404. return OK;
  2405. }
  2406. else {
  2407. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2408. r_result.class_name = "@GlobalScope";
  2409. r_result.class_member = p_symbol;
  2410. return OK;
  2411. }*/
  2412. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_TBD_GLOBALSCOPE;
  2413. r_result.class_name = "@GlobalScope";
  2414. r_result.class_member = p_symbol;
  2415. return OK;
  2416. }
  2417. }
  2418. }
  2419. } break;
  2420. case GDScriptParser::COMPLETION_PARENT_FUNCTION: {
  2421. } break;
  2422. case GDScriptParser::COMPLETION_METHOD:
  2423. isfunction = true;
  2424. case GDScriptParser::COMPLETION_INDEX: {
  2425. const GDScriptParser::Node *node = p.get_completion_node();
  2426. if (node->type != GDScriptParser::Node::TYPE_OPERATOR)
  2427. break;
  2428. GDScriptCompletionIdentifier t;
  2429. if (_guess_expression_type(context, static_cast<const GDScriptParser::OperatorNode *>(node)->arguments[0], p.get_completion_line(), t)) {
  2430. if (t.type == Variant::OBJECT && t.obj_type == "GDScriptNativeClass") {
  2431. //native enum
  2432. Ref<GDScriptNativeClass> gdn = t.value;
  2433. if (gdn.is_valid()) {
  2434. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2435. r_result.class_name = gdn->get_name();
  2436. r_result.class_member = p_symbol;
  2437. return OK;
  2438. }
  2439. } else if (t.type == Variant::OBJECT && t.obj_type != StringName()) {
  2440. Ref<GDScript> on_script;
  2441. if (t.value.get_type()) {
  2442. Object *obj = t.value;
  2443. if (obj) {
  2444. on_script = obj->get_script();
  2445. if (on_script.is_valid()) {
  2446. int loc = on_script->get_member_line(p_symbol);
  2447. if (loc >= 0) {
  2448. r_result.script = on_script;
  2449. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2450. r_result.location = loc;
  2451. return OK;
  2452. }
  2453. }
  2454. }
  2455. }
  2456. if (ClassDB::has_method(t.obj_type, p_symbol)) {
  2457. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2458. r_result.class_name = t.obj_type;
  2459. r_result.class_member = p_symbol;
  2460. return OK;
  2461. }
  2462. StringName enumName = ClassDB::get_integer_constant_enum(t.obj_type, p_symbol, true);
  2463. if (enumName != StringName()) {
  2464. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  2465. r_result.class_name = t.obj_type;
  2466. r_result.class_member = enumName;
  2467. return OK;
  2468. }
  2469. bool success;
  2470. ClassDB::get_integer_constant(t.obj_type, p_symbol, &success);
  2471. if (success) {
  2472. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2473. r_result.class_name = t.obj_type;
  2474. r_result.class_member = p_symbol;
  2475. return OK;
  2476. }
  2477. ClassDB::get_property_type(t.obj_type, p_symbol, &success);
  2478. if (success) {
  2479. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2480. r_result.class_name = t.obj_type;
  2481. r_result.class_member = p_symbol;
  2482. return OK;
  2483. }
  2484. } else {
  2485. Variant::CallError ce;
  2486. Variant v = Variant::construct(t.type, NULL, 0, ce);
  2487. bool valid;
  2488. v.get_numeric_constant_value(t.type, p_symbol, &valid);
  2489. if (valid) {
  2490. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2491. r_result.class_name = Variant::get_type_name(t.type);
  2492. r_result.class_member = p_symbol;
  2493. return OK;
  2494. }
  2495. //todo check all inputevent types for property
  2496. v.get(p_symbol, &valid);
  2497. if (valid) {
  2498. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2499. r_result.class_name = Variant::get_type_name(t.type);
  2500. r_result.class_member = p_symbol;
  2501. return OK;
  2502. }
  2503. if (v.has_method(p_symbol)) {
  2504. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2505. r_result.class_name = Variant::get_type_name(t.type);
  2506. r_result.class_member = p_symbol;
  2507. return OK;
  2508. }
  2509. }
  2510. }
  2511. } break;
  2512. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  2513. return ERR_CANT_RESOLVE;
  2514. } break;
  2515. case GDScriptParser::COMPLETION_VIRTUAL_FUNC: {
  2516. GDScriptCompletionIdentifier cid = _get_native_class(context);
  2517. if (cid.obj_type != StringName()) {
  2518. List<MethodInfo> vm;
  2519. ClassDB::get_virtual_methods(cid.obj_type, &vm);
  2520. for (List<MethodInfo>::Element *E = vm.front(); E; E = E->next()) {
  2521. if (p_symbol == E->get().name) {
  2522. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2523. r_result.class_name = cid.obj_type;
  2524. r_result.class_member = p_symbol;
  2525. return OK;
  2526. }
  2527. }
  2528. }
  2529. } break;
  2530. case GDScriptParser::COMPLETION_YIELD: {
  2531. return ERR_CANT_RESOLVE;
  2532. } break;
  2533. }
  2534. return ERR_CANT_RESOLVE;
  2535. }
  2536. #endif