From 5cc85fb119f21f74de406c6b84950813be53898e Mon Sep 17 00:00:00 2001 From: Ninni Pipping Date: Sun, 25 Jun 2023 18:18:58 +0200 Subject: [PATCH] Fix indentation in script templates (cherry picked from commit 39353462acfd5c32d6ad02076dd7568ec0ab06e4) --- editor/script_create_dialog.cpp | 51 ++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/editor/script_create_dialog.cpp b/editor/script_create_dialog.cpp index 23272ab2842..a4eabf409ac 100644 --- a/editor/script_create_dialog.cpp +++ b/editor/script_create_dialog.cpp @@ -863,7 +863,7 @@ ScriptLanguage::ScriptTemplate ScriptCreateDialog::_parse_template(const ScriptL ScriptLanguage::ScriptTemplate script_template = ScriptLanguage::ScriptTemplate(); script_template.origin = p_origin; script_template.inherit = p_inherits; - String space_indent = " "; + int space_indent_size = 4; // Get meta delimiter String meta_delimiter; List comment_delimiters; @@ -884,30 +884,49 @@ ScriptLanguage::ScriptTemplate ScriptCreateDialog::_parse_template(const ScriptL String line = file->get_line(); if (line.begins_with(meta_prefix)) { // Store meta information - line = line.substr(meta_prefix.length(), -1); - if (line.begins_with("name")) { - script_template.name = line.substr(5, -1).strip_edges(); - } - if (line.begins_with("description")) { - script_template.description = line.substr(12, -1).strip_edges(); - } - if (line.begins_with("space-indent")) { - String indent_value = line.substr(17, -1).strip_edges(); + line = line.substr(meta_prefix.length()); + if (line.begins_with("name:")) { + script_template.name = line.substr(5).strip_edges(); + } else if (line.begins_with("description:")) { + script_template.description = line.substr(12).strip_edges(); + } else if (line.begins_with("space-indent:")) { + String indent_value = line.substr(13).strip_edges(); if (indent_value.is_valid_int()) { int indent_size = indent_value.to_int(); if (indent_size >= 0) { - space_indent = String(" ").repeat(indent_size); + space_indent_size = indent_size; + } else { + WARN_PRINT(vformat("Template meta-space-indent need to be a non-negative integer value. Found %s.", indent_value)); } } else { - WARN_PRINT(vformat("Template meta-use_space_indent need to be a valid integer value. Found %s.", indent_value)); + WARN_PRINT(vformat("Template meta-space-indent need to be a valid integer value. Found %s.", indent_value)); } } } else { - // Store script - if (space_indent != "") { - line = line.replace(space_indent, "_TS_"); + // Replace indentation. + int i = 0; + int space_count = 0; + for (; i < line.length(); i++) { + if (line[i] == '\t') { + if (space_count) { + script_template.content += String(" ").repeat(space_count); + space_count = 0; + } + script_template.content += "_TS_"; + } else if (line[i] == ' ') { + space_count++; + if (space_count == space_indent_size) { + script_template.content += "_TS_"; + space_count = 0; + } + } else { + break; + } } - script_template.content += line.replace("\t", "_TS_") + "\n"; + if (space_count) { + script_template.content += String(" ").repeat(space_count); + } + script_template.content += line.substr(i) + "\n"; } } }