Add support for type hints in non-default script editor templates

This also refactors template processing to avoid repetition.

This closes #27074.
This commit is contained in:
Hugo Locurcio 2019-04-05 23:15:12 +02:00
parent cc349336e7
commit 00799fc8c2
No known key found for this signature in database
GPG key ID: 39E8F8BE30B0A49C
3 changed files with 38 additions and 28 deletions

View file

@ -675,14 +675,14 @@ bool EditorSettings::_save_text_editor_theme(String p_file) {
static Dictionary _get_builtin_script_templates() { static Dictionary _get_builtin_script_templates() {
Dictionary templates; Dictionary templates;
//No Comments // No Comments
templates["no_comments.gd"] = templates["no_comments.gd"] =
"extends %BASE%\n" "extends %BASE%\n"
"\n" "\n"
"func _ready():\n" "func _ready()%VOID_RETURN%:\n"
"%TS%pass\n"; "%TS%pass\n";
//Empty // Empty
templates["empty.gd"] = templates["empty.gd"] =
"extends %BASE%" "extends %BASE%"
"\n" "\n"

View file

@ -444,6 +444,7 @@ public:
virtual void get_reserved_words(List<String> *p_words) const; virtual void get_reserved_words(List<String> *p_words) const;
virtual void get_comment_delimiters(List<String> *p_delimiters) const; virtual void get_comment_delimiters(List<String> *p_delimiters) const;
virtual void get_string_delimiters(List<String> *p_delimiters) const; virtual void get_string_delimiters(List<String> *p_delimiters) const;
virtual String _get_processed_template(const String &p_template, const String &p_base_class_name) const;
virtual Ref<Script> get_template(const String &p_class_name, const String &p_base_class_name) const; virtual Ref<Script> get_template(const String &p_class_name, const String &p_base_class_name) const;
virtual bool is_using_templates(); virtual bool is_using_templates();
virtual void make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script); virtual void make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script);

View file

@ -44,12 +44,43 @@ void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const
p_delimiters->push_back("#"); p_delimiters->push_back("#");
} }
void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const { void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
p_delimiters->push_back("\" \""); p_delimiters->push_back("\" \"");
p_delimiters->push_back("' '"); p_delimiters->push_back("' '");
p_delimiters->push_back("\"\"\" \"\"\""); p_delimiters->push_back("\"\"\" \"\"\"");
} }
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;
}
Ref<Script> GDScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const { Ref<Script> GDScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
String _template = "extends %BASE%\n" String _template = "extends %BASE%\n"
"\n" "\n"
@ -65,27 +96,7 @@ Ref<Script> GDScriptLanguage::get_template(const String &p_class_name, const Str
"#func _process(delta%FLOAT_TYPE%)%VOID_RETURN%:\n" "#func _process(delta%FLOAT_TYPE%)%VOID_RETURN%:\n"
"#%TS%pass\n"; "#%TS%pass\n";
#ifdef TOOLS_ENABLED _template = _get_processed_template(_template, p_base_class_name);
if (EDITOR_DEF("text_editor/completion/add_type_hints", false)) {
_template = _template.replace("%INT_TYPE%", ": int");
_template = _template.replace("%STRING_TYPE%", ": String");
_template = _template.replace("%FLOAT_TYPE%", ": float");
_template = _template.replace("%VOID_RETURN%", " -> void");
} else {
_template = _template.replace("%INT_TYPE%", "");
_template = _template.replace("%STRING_TYPE%", "");
_template = _template.replace("%FLOAT_TYPE%", "");
_template = _template.replace("%VOID_RETURN%", "");
}
#else
_template = _template.replace("%INT_TYPE%", "");
_template = _template.replace("%STRING_TYPE%", "");
_template = _template.replace("%FLOAT_TYPE%", "");
_template = _template.replace("%VOID_RETURN%", "");
#endif
_template = _template.replace("%BASE%", p_base_class_name);
_template = _template.replace("%TS%", _get_indentation());
Ref<GDScript> script; Ref<GDScript> script;
script.instance(); script.instance();
@ -101,10 +112,8 @@ bool GDScriptLanguage::is_using_templates() {
void GDScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) { void GDScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
String src = p_script->get_source_code(); String _template = _get_processed_template(p_script->get_source_code(), p_base_class_name);
src = src.replace("%BASE%", p_base_class_name); p_script->set_source_code(_template);
src = src.replace("%TS%", _get_indentation());
p_script->set_source_code(src);
} }
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 { 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 {