fmt: preserve method receiver mutability in interface decls (#11222)

pull/11226/head
spaceface 2021-08-17 18:39:53 +02:00 committed by GitHub
parent f96b81b53a
commit 7c9a1defa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 5 deletions

View File

@ -1049,16 +1049,26 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
f.comments(iface.comments, inline: true, has_nl: false, level: .indent) f.comments(iface.comments, inline: true, has_nl: false, level: .indent)
f.writeln('') f.writeln('')
} }
for i, field in node.fields { immut_fields := if node.mut_pos < 0 { node.fields } else { node.fields[..node.mut_pos] }
if i == node.mut_pos { mut_fields := if node.mut_pos < 0 { []ast.StructField{} } else { node.fields[node.mut_pos..] }
f.writeln('mut:')
mut immut_methods := node.methods
mut mut_methods := []ast.FnDecl{}
for i, method in node.methods {
if method.params[0].is_mut {
immut_methods = node.methods[..i]
mut_methods = node.methods[i..]
break
} }
}
// TODO: alignment, comments, etc. // TODO: alignment, comments, etc.
for field in immut_fields {
mut ft := f.no_cur_mod(f.table.type_to_str_using_aliases(field.typ, f.mod2alias)) mut ft := f.no_cur_mod(f.table.type_to_str_using_aliases(field.typ, f.mod2alias))
f.writeln('\t$field.name $ft') f.writeln('\t$field.name $ft')
f.mark_types_import_as_used(field.typ) f.mark_types_import_as_used(field.typ)
} }
for method in node.methods { for method in immut_methods {
f.write('\t') f.write('\t')
f.write(method.stringify(f.table, f.cur_mod, f.mod2alias).after('fn ')) f.write(method.stringify(f.table, f.cur_mod, f.mod2alias).after('fn '))
f.comments(method.comments, inline: true, has_nl: false, level: .indent) f.comments(method.comments, inline: true, has_nl: false, level: .indent)
@ -1069,6 +1079,25 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
} }
f.mark_types_import_as_used(method.return_type) f.mark_types_import_as_used(method.return_type)
} }
if mut_fields.len + mut_methods.len > 0 {
f.writeln('mut:')
for field in mut_fields {
mut ft := f.no_cur_mod(f.table.type_to_str_using_aliases(field.typ, f.mod2alias))
f.writeln('\t$field.name $ft')
f.mark_types_import_as_used(field.typ)
}
for method in mut_methods {
f.write('\t')
f.write(method.stringify(f.table, f.cur_mod, f.mod2alias).after('fn '))
f.comments(method.comments, inline: true, has_nl: false, level: .indent)
f.writeln('')
f.comments(method.next_comments, inline: false, has_nl: true, level: .indent)
for param in method.params {
f.mark_types_import_as_used(param.typ)
}
f.mark_types_import_as_used(method.return_type)
}
}
f.writeln('}\n') f.writeln('}\n')
} }

View File

@ -1,5 +1,6 @@
interface Toto { interface Toto {
a int a int
d()
mut: mut:
b int b int
f() f()