2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2017-11-16 17:38:18 +00:00
|
|
|
/* gdscript_editor.cpp */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2020-01-01 10:16:22 +00:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
#include "gdscript.h"
|
2017-08-27 19:07:15 +00:00
|
|
|
|
2017-11-25 03:07:54 +00:00
|
|
|
#include "core/engine.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/global_constants.h"
|
|
|
|
#include "core/os/file_access.h"
|
2017-11-16 17:38:18 +00:00
|
|
|
#include "gdscript_compiler.h"
|
2020-05-01 22:14:56 +00:00
|
|
|
#include "gdscript_parser.h"
|
|
|
|
#include "gdscript_tokenizer.h"
|
2017-08-27 19:07:15 +00:00
|
|
|
|
2017-04-16 19:09:17 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
#include "editor/editor_file_system.h"
|
|
|
|
#include "editor/editor_settings.h"
|
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
|
|
|
|
p_delimiters->push_back("#");
|
|
|
|
}
|
2019-04-05 21:15:12 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
|
|
|
|
p_delimiters->push_back("\" \"");
|
|
|
|
p_delimiters->push_back("' '");
|
2018-08-19 11:43:58 +00:00
|
|
|
p_delimiters->push_back("\"\"\" \"\"\"");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2019-04-05 21:15:12 +00:00
|
|
|
|
|
|
|
String GDScriptLanguage::_get_processed_template(const String &p_template, const String &p_base_class_name) const {
|
|
|
|
String processed_template = p_template;
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (EDITOR_DEF("text_editor/completion/add_type_hints", false)) {
|
|
|
|
processed_template = processed_template.replace("%INT_TYPE%", ": int");
|
|
|
|
processed_template = processed_template.replace("%STRING_TYPE%", ": String");
|
|
|
|
processed_template = processed_template.replace("%FLOAT_TYPE%", ": float");
|
|
|
|
processed_template = processed_template.replace("%VOID_RETURN%", " -> void");
|
|
|
|
} else {
|
|
|
|
processed_template = processed_template.replace("%INT_TYPE%", "");
|
|
|
|
processed_template = processed_template.replace("%STRING_TYPE%", "");
|
|
|
|
processed_template = processed_template.replace("%FLOAT_TYPE%", "");
|
|
|
|
processed_template = processed_template.replace("%VOID_RETURN%", "");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
processed_template = processed_template.replace("%INT_TYPE%", "");
|
|
|
|
processed_template = processed_template.replace("%STRING_TYPE%", "");
|
|
|
|
processed_template = processed_template.replace("%FLOAT_TYPE%", "");
|
|
|
|
processed_template = processed_template.replace("%VOID_RETURN%", "");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
processed_template = processed_template.replace("%BASE%", p_base_class_name);
|
|
|
|
processed_template = processed_template.replace("%TS%", _get_indentation());
|
|
|
|
|
|
|
|
return processed_template;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Ref<Script> GDScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
|
2018-05-28 17:34:41 +00:00
|
|
|
String _template = "extends %BASE%\n"
|
2019-11-17 11:54:43 +00:00
|
|
|
"\n"
|
2018-05-28 17:34:41 +00:00
|
|
|
"\n"
|
|
|
|
"# Declare member variables here. Examples:\n"
|
2018-08-26 16:43:13 +00:00
|
|
|
"# var a%INT_TYPE% = 2\n"
|
|
|
|
"# var b%STRING_TYPE% = \"text\"\n"
|
2018-05-28 17:34:41 +00:00
|
|
|
"\n"
|
2019-11-17 11:54:43 +00:00
|
|
|
"\n"
|
2018-05-28 17:34:41 +00:00
|
|
|
"# Called when the node enters the scene tree for the first time.\n"
|
2018-05-30 02:16:59 +00:00
|
|
|
"func _ready()%VOID_RETURN%:\n"
|
2018-05-28 17:34:41 +00:00
|
|
|
"%TS%pass # Replace with function body.\n"
|
|
|
|
"\n"
|
2019-11-17 11:54:43 +00:00
|
|
|
"\n"
|
2018-05-28 17:34:41 +00:00
|
|
|
"# Called every frame. 'delta' is the elapsed time since the previous frame.\n"
|
2018-05-30 02:16:59 +00:00
|
|
|
"#func _process(delta%FLOAT_TYPE%)%VOID_RETURN%:\n"
|
2017-10-22 19:07:34 +00:00
|
|
|
"#%TS%pass\n";
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-04-05 21:15:12 +00:00
|
|
|
_template = _get_processed_template(_template, p_base_class_name);
|
2016-08-06 01:46:45 +00:00
|
|
|
|
|
|
|
Ref<GDScript> script;
|
|
|
|
script.instance();
|
|
|
|
script->set_source_code(_template);
|
|
|
|
|
|
|
|
return script;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 20:03:08 +00:00
|
|
|
bool GDScriptLanguage::is_using_templates() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
|
2019-04-05 21:15:12 +00:00
|
|
|
String _template = _get_processed_template(p_script->get_source_code(), p_base_class_name);
|
|
|
|
p_script->set_source_code(_template);
|
2017-06-13 20:03:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
static void get_function_names_recursively(const GDScriptParser::ClassNode *p_class, const String &p_prefix, Map<int, String> &r_funcs) {
|
|
|
|
for (int i = 0; i < p_class->members.size(); i++) {
|
|
|
|
if (p_class->members[i].type == GDScriptParser::ClassNode::Member::FUNCTION) {
|
|
|
|
const GDScriptParser::FunctionNode *function = p_class->members[i].function;
|
|
|
|
r_funcs[function->start_line] = p_prefix.empty() ? String(function->identifier->name) : p_prefix + "." + String(function->identifier->name);
|
|
|
|
} else if (p_class->members[i].type == GDScriptParser::ClassNode::Member::CLASS) {
|
|
|
|
String new_prefix = p_class->members[i].m_class->identifier->name;
|
|
|
|
get_function_names_recursively(p_class->members[i].m_class, p_prefix.empty() ? new_prefix : p_prefix + "." + new_prefix, r_funcs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-01 16:17:40 +00:00
|
|
|
bool GDScriptLanguage::validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions, List<ScriptLanguage::Warning> *r_warnings, Set<int> *r_safe_lines) const {
|
2017-11-16 17:38:18 +00:00
|
|
|
GDScriptParser parser;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
Error err = parser.parse(p_script, p_path, false);
|
2018-07-01 16:17:40 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
2020-05-01 22:14:56 +00:00
|
|
|
// FIXME: Warnings.
|
|
|
|
// if (r_warnings) {
|
|
|
|
// for (const List<GDScriptWarning>::Element *E = parser.get_warnings().front(); E; E = E->next()) {
|
|
|
|
// const GDScriptWarning &warn = E->get();
|
|
|
|
// ScriptLanguage::Warning w;
|
|
|
|
// w.line = warn.line;
|
|
|
|
// w.code = (int)warn.code;
|
|
|
|
// w.string_code = GDScriptWarning::get_name_from_code(warn.code);
|
|
|
|
// w.message = warn.get_message();
|
|
|
|
// r_warnings->push_back(w);
|
|
|
|
// }
|
|
|
|
// }
|
2018-07-01 16:17:40 +00:00
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
if (err) {
|
2020-05-01 22:14:56 +00:00
|
|
|
GDScriptParser::ParserError parse_error = parser.get_errors().front()->get();
|
|
|
|
r_line_error = parse_error.line;
|
|
|
|
r_col_error = parse_error.column;
|
|
|
|
r_test_error = parse_error.message;
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2020-05-01 22:14:56 +00:00
|
|
|
const GDScriptParser::ClassNode *cl = parser.get_tree();
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<int, String> funcs;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
get_function_names_recursively(cl, "", funcs);
|
2018-06-11 19:39:55 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Map<int, String>::Element *E = funcs.front(); E; E = E->next()) {
|
|
|
|
r_functions->push_back(E->get() + ":" + itos(E->key()));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDScriptLanguage::has_named_classes() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-23 23:54:47 +00:00
|
|
|
bool GDScriptLanguage::supports_builtin_mode() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
|
2020-05-01 22:14:56 +00:00
|
|
|
GDScriptTokenizer tokenizer;
|
|
|
|
tokenizer.set_source_code(p_code);
|
2017-03-05 15:44:50 +00:00
|
|
|
int indent = 0;
|
2020-05-01 22:14:56 +00:00
|
|
|
GDScriptTokenizer::Token current = tokenizer.scan();
|
|
|
|
while (current.type != GDScriptTokenizer::Token::TK_EOF && current.type != GDScriptTokenizer::Token::ERROR) {
|
|
|
|
if (current.type == GDScriptTokenizer::Token::INDENT) {
|
|
|
|
indent++;
|
|
|
|
} else if (current.type == GDScriptTokenizer::Token::DEDENT) {
|
|
|
|
indent--;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-01 22:14:56 +00:00
|
|
|
if (indent == 0 && current.type == GDScriptTokenizer::Token::FUNC) {
|
|
|
|
current = tokenizer.scan();
|
|
|
|
if (current.is_identifier()) {
|
|
|
|
String identifier = current.get_identifier();
|
|
|
|
if (identifier == p_function) {
|
|
|
|
return current.start_line;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-01 22:14:56 +00:00
|
|
|
current = tokenizer.scan();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Script *GDScriptLanguage::create_script() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
return memnew(GDScript);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* DEBUGGER FUNCTIONS */
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
|
2014-02-10 01:10:30 +00:00
|
|
|
//break because of parse error
|
|
|
|
|
2020-02-27 02:30:20 +00:00
|
|
|
if (EngineDebugger::is_active() && Thread::get_caller_id() == Thread::get_main_id()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
_debug_parse_err_line = p_line;
|
|
|
|
_debug_parse_err_file = p_file;
|
|
|
|
_debug_error = p_error;
|
2020-02-27 02:30:20 +00:00
|
|
|
EngineDebugger::get_script_debugger()->debug(this, false, true);
|
2017-03-05 15:44:50 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
|
2020-02-27 02:30:20 +00:00
|
|
|
if (EngineDebugger::is_active() && Thread::get_caller_id() == Thread::get_main_id()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
_debug_parse_err_line = -1;
|
|
|
|
_debug_parse_err_file = "";
|
|
|
|
_debug_error = p_error;
|
2019-07-29 18:09:22 +00:00
|
|
|
bool is_error_breakpoint = p_error != "Breakpoint";
|
2020-02-27 02:30:20 +00:00
|
|
|
EngineDebugger::get_script_debugger()->debug(this, p_allow_continue, is_error_breakpoint);
|
2017-03-05 15:44:50 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String GDScriptLanguage::debug_get_error() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
return _debug_error;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int GDScriptLanguage::debug_get_stack_level_count() const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (_debug_parse_err_line >= 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return 1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return _debug_call_stack_pos;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (_debug_parse_err_line >= 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return _debug_parse_err_line;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, -1);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int l = _debug_call_stack_pos - p_level - 1;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return *(_call_stack[l].line);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (_debug_parse_err_line >= 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return "";
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
|
|
|
|
int l = _debug_call_stack_pos - p_level - 1;
|
|
|
|
return _call_stack[l].function->get_name();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (_debug_parse_err_line >= 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return _debug_parse_err_file;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
|
|
|
|
int l = _debug_call_stack_pos - p_level - 1;
|
|
|
|
return _call_stack[l].function->get_source();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
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) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (_debug_parse_err_line >= 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
|
|
|
|
int l = _debug_call_stack_pos - p_level - 1;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
GDScriptFunction *f = _call_stack[l].function;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-03-17 06:33:00 +00:00
|
|
|
List<Pair<StringName, int>> locals;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
f->debug_get_stack_member_state(*_call_stack[l].line, &locals);
|
2020-03-17 06:33:00 +00:00
|
|
|
for (List<Pair<StringName, int>>::Element *E = locals.front(); E; E = E->next()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
p_locals->push_back(E->get().first);
|
|
|
|
p_values->push_back(_call_stack[l].stack[E->get().second]);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
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) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (_debug_parse_err_line >= 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
|
|
|
|
int l = _debug_call_stack_pos - p_level - 1;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
GDScriptInstance *instance = _call_stack[l].instance;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!instance) {
|
2015-06-27 18:52:39 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-06-27 18:52:39 +00:00
|
|
|
Ref<GDScript> script = instance->get_script();
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(script.is_null());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const Map<StringName, GDScript::MemberInfo> &mi = script->debug_get_member_indices();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (const Map<StringName, GDScript::MemberInfo>::Element *E = mi.front(); E; E = E->next()) {
|
2015-06-27 18:52:39 +00:00
|
|
|
p_members->push_back(E->key());
|
2017-03-05 15:44:50 +00:00
|
|
|
p_values->push_back(instance->debug_get_member_by_index(E->get().index));
|
2015-06-27 18:52:39 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 14:35:49 +00:00
|
|
|
ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (_debug_parse_err_line >= 0) {
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-01-16 20:12:24 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, nullptr);
|
2017-10-17 14:35:49 +00:00
|
|
|
|
|
|
|
int l = _debug_call_stack_pos - p_level - 1;
|
2017-11-16 09:18:36 +00:00
|
|
|
ScriptInstance *instance = _call_stack[l].instance;
|
2017-10-17 14:35:49 +00:00
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDScriptLanguage::debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
|
|
|
|
const Map<StringName, int> &name_idx = GDScriptLanguage::get_singleton()->get_global_map();
|
|
|
|
const Variant *globals = GDScriptLanguage::get_singleton()->get_global_array();
|
|
|
|
|
2020-03-17 06:33:00 +00:00
|
|
|
List<Pair<String, Variant>> cinfo;
|
2017-11-16 09:18:36 +00:00
|
|
|
get_public_constants(&cinfo);
|
|
|
|
|
2017-10-17 14:35:49 +00:00
|
|
|
for (const Map<StringName, int>::Element *E = name_idx.front(); E; E = E->next()) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (ClassDB::class_exists(E->key()) || Engine::get_singleton()->has_singleton(E->key())) {
|
2017-11-16 09:18:36 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-11-16 09:18:36 +00:00
|
|
|
|
|
|
|
bool is_script_constant = false;
|
2020-03-17 06:33:00 +00:00
|
|
|
for (List<Pair<String, Variant>>::Element *CE = cinfo.front(); CE; CE = CE->next()) {
|
2017-11-16 09:18:36 +00:00
|
|
|
if (CE->get().first == E->key()) {
|
|
|
|
is_script_constant = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 14:41:43 +00:00
|
|
|
if (is_script_constant) {
|
2017-10-17 14:35:49 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-10-17 14:35:49 +00:00
|
|
|
|
|
|
|
const Variant &var = globals[E->value()];
|
|
|
|
if (Object *obj = var) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (Object::cast_to<GDScriptNativeClass>(obj)) {
|
2017-10-17 14:35:49 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-10-17 14:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool skip = false;
|
|
|
|
for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
|
|
|
|
if (E->key() == GlobalConstants::get_global_constant_name(i)) {
|
|
|
|
skip = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 14:41:43 +00:00
|
|
|
if (skip) {
|
2017-10-17 14:35:49 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-10-17 14:35:49 +00:00
|
|
|
|
|
|
|
p_globals->push_back(E->key());
|
|
|
|
p_values->push_back(var);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-10-17 14:35:49 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
|
|
|
|
p_extensions->push_back("gd");
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
|
2017-11-16 17:38:18 +00:00
|
|
|
for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
|
|
|
|
p_functions->push_back(GDScriptFunctions::get_info(GDScriptFunctions::Function(i)));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-02-09 19:58:03 +00:00
|
|
|
|
|
|
|
//not really "functions", but..
|
|
|
|
{
|
|
|
|
MethodInfo mi;
|
2017-08-29 05:15:46 +00:00
|
|
|
mi.name = "preload";
|
2017-03-05 15:44:50 +00:00
|
|
|
mi.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
|
|
|
|
mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
|
2016-02-09 19:58:03 +00:00
|
|
|
p_functions->push_back(mi);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
MethodInfo mi;
|
2017-03-05 15:44:50 +00:00
|
|
|
mi.name = "assert";
|
2017-08-29 05:15:46 +00:00
|
|
|
mi.return_val.type = Variant::NIL;
|
2017-03-05 15:44:50 +00:00
|
|
|
mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition"));
|
2020-03-31 08:39:23 +00:00
|
|
|
mi.arguments.push_back(PropertyInfo(Variant::STRING, "message"));
|
|
|
|
mi.default_arguments.push_back(String());
|
2016-02-09 19:58:03 +00:00
|
|
|
p_functions->push_back(mi);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 06:33:00 +00:00
|
|
|
void GDScriptLanguage::get_public_constants(List<Pair<String, Variant>> *p_constants) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
Pair<String, Variant> pi;
|
|
|
|
pi.first = "PI";
|
|
|
|
pi.second = Math_PI;
|
2014-02-22 23:28:19 +00:00
|
|
|
p_constants->push_back(pi);
|
2017-02-06 22:44:22 +00:00
|
|
|
|
2017-11-04 09:34:27 +00:00
|
|
|
Pair<String, Variant> tau;
|
|
|
|
tau.first = "TAU";
|
|
|
|
tau.second = Math_TAU;
|
|
|
|
p_constants->push_back(tau);
|
|
|
|
|
2017-02-06 22:44:22 +00:00
|
|
|
Pair<String, Variant> infinity;
|
|
|
|
infinity.first = "INF";
|
|
|
|
infinity.second = Math_INF;
|
|
|
|
p_constants->push_back(infinity);
|
|
|
|
|
|
|
|
Pair<String, Variant> nan;
|
|
|
|
nan.first = "NAN";
|
|
|
|
nan.second = Math_NAN;
|
|
|
|
p_constants->push_back(nan);
|
2014-02-22 23:28:19 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const {
|
2018-05-30 02:16:59 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
bool th = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
|
|
|
|
#else
|
|
|
|
bool th = false;
|
|
|
|
#endif
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String s = "func " + p_name + "(";
|
2014-02-10 01:10:30 +00:00
|
|
|
if (p_args.size()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < p_args.size(); i++) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (i > 0) {
|
2017-03-05 15:44:50 +00:00
|
|
|
s += ", ";
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
s += p_args[i].get_slice(":", 0);
|
2018-05-30 02:16:59 +00:00
|
|
|
if (th) {
|
|
|
|
String type = p_args[i].get_slice(":", 1);
|
|
|
|
if (!type.empty() && type != "var") {
|
2018-08-26 16:43:13 +00:00
|
|
|
s += ": " + type;
|
2018-05-30 02:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-30 02:16:59 +00:00
|
|
|
s += String(")") + (th ? " -> void" : "") + ":\n" + _get_indentation() + "pass # Replace with function body.\n";
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-06-21 01:41:26 +00:00
|
|
|
//////// COMPLETION //////////
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
// FIXME: Readd completion
|
|
|
|
Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptCodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) {
|
|
|
|
return OK;
|
2018-06-21 01:41:26 +00:00
|
|
|
}
|
2014-12-17 01:31:57 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
//////// END COMPLETION //////////
|
2018-06-21 01:41:26 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
String GDScriptLanguage::_get_indentation() const {
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
bool use_space_indentation = EDITOR_DEF("text_editor/indent/type", false);
|
2019-02-08 21:39:08 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
if (use_space_indentation) {
|
|
|
|
int indent_size = EDITOR_DEF("text_editor/indent/size", 4);
|
2019-02-08 21:39:08 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
String space_indent = "";
|
|
|
|
for (int i = 0; i < indent_size; i++) {
|
|
|
|
space_indent += " ";
|
2018-06-21 01:41:26 +00:00
|
|
|
}
|
2020-05-01 22:14:56 +00:00
|
|
|
return space_indent;
|
2018-06-21 01:41:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-01 22:14:56 +00:00
|
|
|
#endif
|
|
|
|
return "\t";
|
2018-06-21 01:41:26 +00:00
|
|
|
}
|
2017-08-27 22:03:19 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
|
|
|
|
String indent = _get_indentation();
|
2014-12-08 18:47:45 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
Vector<String> lines = p_code.split("\n");
|
|
|
|
List<int> indent_stack;
|
2014-12-08 18:47:45 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
for (int i = 0; i < lines.size(); i++) {
|
|
|
|
String l = lines[i];
|
|
|
|
int tc = 0;
|
|
|
|
for (int j = 0; j < l.length(); j++) {
|
|
|
|
if (l[j] == ' ' || l[j] == '\t') {
|
|
|
|
tc++;
|
|
|
|
} else {
|
2018-06-21 01:41:26 +00:00
|
|
|
break;
|
2017-08-25 03:34:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
String st = l.substr(tc, l.length()).strip_edges();
|
|
|
|
if (st == "" || st.begins_with("#")) {
|
|
|
|
continue; //ignore!
|
2014-12-08 18:47:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
int ilevel = 0;
|
|
|
|
if (indent_stack.size()) {
|
|
|
|
ilevel = indent_stack.back()->get();
|
2014-12-08 18:47:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
if (tc > ilevel) {
|
|
|
|
indent_stack.push_back(tc);
|
|
|
|
} else if (tc < ilevel) {
|
|
|
|
while (indent_stack.size() && indent_stack.back()->get() > tc) {
|
|
|
|
indent_stack.pop_back();
|
2018-10-27 23:53:48 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
if (indent_stack.size() && indent_stack.back()->get() != tc) {
|
|
|
|
indent_stack.push_back(tc); //this is not right but gets the job done
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-28 19:36:29 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
if (i >= p_from_line) {
|
|
|
|
l = "";
|
|
|
|
for (int j = 0; j < indent_stack.size(); j++) {
|
|
|
|
l += indent;
|
2015-12-28 19:36:29 +00:00
|
|
|
}
|
2020-05-01 22:14:56 +00:00
|
|
|
l += st;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
} else if (i > p_to_line) {
|
|
|
|
break;
|
2018-06-21 01:41:26 +00:00
|
|
|
}
|
2018-02-11 09:29:30 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
lines.write[i] = l;
|
2018-06-21 01:41:26 +00:00
|
|
|
}
|
2018-02-11 09:29:30 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
p_code = "";
|
|
|
|
for (int i = 0; i < lines.size(); i++) {
|
|
|
|
if (i > 0) {
|
|
|
|
p_code += "\n";
|
2018-06-21 01:41:26 +00:00
|
|
|
}
|
2020-05-01 22:14:56 +00:00
|
|
|
p_code += lines[i];
|
2018-06-21 01:41:26 +00:00
|
|
|
}
|
2014-12-17 01:31:57 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2016-09-12 13:52:29 +00:00
|
|
|
|
2019-04-17 15:06:21 +00:00
|
|
|
Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) {
|
2020-05-01 22:14:56 +00:00
|
|
|
// FIXME: Implement lookup.
|
2016-09-12 13:52:29 +00:00
|
|
|
return ERR_CANT_RESOLVE;
|
|
|
|
}
|
2016-09-13 01:40:46 +00:00
|
|
|
|
|
|
|
#endif
|