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

@ -679,7 +679,7 @@ static Dictionary _get_builtin_script_templates() {
templates["no_comments.gd"] =
"extends %BASE%\n"
"\n"
"func _ready():\n"
"func _ready()%VOID_RETURN%:\n"
"%TS%pass\n";
// Empty

View File

@ -444,6 +444,7 @@ public:
virtual void get_reserved_words(List<String> *p_words) const;
virtual void get_comment_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 bool is_using_templates();
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("#");
}
void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
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 {
String _template = "extends %BASE%\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"
"#%TS%pass\n";
#ifdef TOOLS_ENABLED
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());
_template = _get_processed_template(_template, p_base_class_name);
Ref<GDScript> script;
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) {
String src = p_script->get_source_code();
src = src.replace("%BASE%", p_base_class_name);
src = src.replace("%TS%", _get_indentation());
p_script->set_source_code(src);
String _template = _get_processed_template(p_script->get_source_code(), p_base_class_name);
p_script->set_source_code(_template);
}
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 {