Fix comment scope placement.

This commit is contained in:
Relintai 2022-06-06 00:55:17 +02:00
parent db534b724b
commit 10b8c64e76

View File

@ -29,6 +29,16 @@ class GDSScope:
var is_static : bool = false var is_static : bool = false
var scope_line_order : PoolIntArray var scope_line_order : PoolIntArray
var comment_accumulator : PoolStringArray = PoolStringArray()
func apply_comments() -> void:
for i in range(comment_accumulator.size()):
scope_lines.append(comment_accumulator[i])
scope_line_order.push_back(GDScopeLineType.GDSCOPE_TYPE_LINE)
scope_line_order.push_back(scope_lines.size() - 1)
comment_accumulator.resize(0)
func parse(contents : PoolStringArray, current_index : int = 0, current_indent : int = 0) -> int: func parse(contents : PoolStringArray, current_index : int = 0, current_indent : int = 0) -> int:
while current_index < contents.size(): while current_index < contents.size():
var cl : String = contents[current_index] var cl : String = contents[current_index]
@ -55,9 +65,7 @@ class GDSScope:
continue continue
if cl.begins_with("#"): if cl.begins_with("#"):
scope_lines.append(cl) comment_accumulator.append(cl)
scope_line_order.push_back(GDScopeLineType.GDSCOPE_TYPE_LINE)
scope_line_order.push_back(scope_lines.size() - 1)
current_index += 1 current_index += 1
continue continue
@ -67,15 +75,22 @@ class GDSScope:
if curr_line_indent < current_indent: if curr_line_indent < current_indent:
return current_index return current_index
apply_comments()
if cl.ends_with(":") || cl.begins_with("enum "): if cl.ends_with(":") || cl.begins_with("enum "):
var scope : GDSScope = GDSScope.new() var scope : GDSScope = GDSScope.new()
scope.parse_scope_data(clstripped) scope.parse_scope_data(clstripped)
current_index += 1 current_index += 1
current_index = scope.parse(contents, current_index, curr_line_indent + 1) current_index = scope.parse(contents, current_index, curr_line_indent + 1)
subscopes.append(scope) subscopes.append(scope)
scope_line_order.push_back(GDScopeLineType.GDSCOPE_TYPE_SCOPE) scope_line_order.push_back(GDScopeLineType.GDSCOPE_TYPE_SCOPE)
scope_line_order.push_back(subscopes.size() - 1) scope_line_order.push_back(subscopes.size() - 1)
comment_accumulator = scope.comment_accumulator
scope.comment_accumulator.resize(0)
apply_comments()
#don't #don't
#current_index += 1 #current_index += 1
continue continue
@ -372,42 +387,32 @@ class GDSScope:
s += "\n" s += "\n"
indents += " " indents += " "
if type == GDScopeType.GDSCOPE_TYPE_CLASS: for i in range(0, scope_line_order.size(), 2):
for subs in subscopes: if scope_line_order[i] == GDScopeLineType.GDSCOPE_TYPE_LINE:
var scstr : String = subs.get_cpp_impl_string(current_scope_level + 1, owner_class_name) var l : String = scope_lines[scope_line_order[i + 1]]
if scstr != "": if l.begins_with("#"):
s += scstr l = l.replace("#", "//")
s += "\n" s += indents + l + ";\n"
s += "\n" continue
else:
for i in range(0, scope_line_order.size(), 2):
if scope_line_order[i] == GDScopeLineType.GDSCOPE_TYPE_LINE:
var l : String = scope_lines[scope_line_order[i + 1]]
if l.begins_with("#"): if l.begins_with("var "):
l = l.replace("#", "//") if l.find("preload") != -1:
s += indents + l + ";\n" s += indents + "//" + l + ";\n"
continue continue
if l.begins_with("var "): s += indents + " " + transform_variable_to_cpp(l) + ";\n"
if l.find("preload") != -1:
s += indents + "//" + l + ";\n"
continue
s += indents + " " + transform_variable_to_cpp(l) + ";\n"
else:
s += indents + l + ";\n"
else: else:
s += indents + l + ";\n"
else:
var scstr : String = subscopes[scope_line_order[i + 1]].get_cpp_impl_string(current_scope_level + 1, owner_class_name)
var scstr : String = subscopes[scope_line_order[i + 1]].get_cpp_impl_string(current_scope_level + 1, owner_class_name) if scstr != "":
s += "\n"
s += scstr
s += "\n"
if scstr != "": s += "}\n"
s += "\n"
s += scstr
s += "\n"
s += "}\n"
if type == GDScopeType.GDSCOPE_TYPE_CLASS: if type == GDScopeType.GDSCOPE_TYPE_CLASS:
@ -874,6 +879,7 @@ class GDSParser:
root.camel_case_scope_data() root.camel_case_scope_data()
var c : PoolStringArray = split_preprocess_content(contents) var c : PoolStringArray = split_preprocess_content(contents)
root.parse(c) root.parse(c)
root.apply_comments()
func split_preprocess_content(contents : String) -> PoolStringArray: func split_preprocess_content(contents : String) -> PoolStringArray:
@ -912,7 +918,7 @@ class GDSParser:
var hash_symbol_index = l.find("#") var hash_symbol_index = l.find("#")
if hash_symbol_index != -1: if hash_symbol_index != -1:
var comment_str : String = l.substr(hash_symbol_index) var comment_str : String = l.substr(hash_symbol_index).strip_edges(true, false)
ret.append(comment_str) ret.append(comment_str)
l = l.substr(0, hash_symbol_index).strip_edges(false, true) l = l.substr(0, hash_symbol_index).strip_edges(false, true)