vfmt: fix struct init indent and wrapped lines
parent
2fbed2f880
commit
d54150cd22
|
@ -235,7 +235,7 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
|
||||||
f.expr(it.cond)
|
f.expr(it.cond)
|
||||||
f.write('; ')
|
f.write('; ')
|
||||||
f.expr(it.inc)
|
f.expr(it.inc)
|
||||||
f.writeln('{ ')
|
f.writeln(' {')
|
||||||
f.stmts(it.stmts)
|
f.stmts(it.stmts)
|
||||||
f.writeln('}')
|
f.writeln('}')
|
||||||
}
|
}
|
||||||
|
@ -439,7 +439,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
f.writeln('\t$it.var_name |')
|
f.writeln('\t$it.var_name |')
|
||||||
// TODO StructInit copy pasta
|
// TODO StructInit copy pasta
|
||||||
for i, field in it.fields {
|
for i, field in it.fields {
|
||||||
f.write('\t$field: ')
|
f.write('$field: ')
|
||||||
f.expr(it.exprs[i])
|
f.expr(it.exprs[i])
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
}
|
}
|
||||||
|
@ -625,11 +625,13 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
f.write('$name{}')
|
f.write('$name{}')
|
||||||
} else {
|
} else {
|
||||||
f.writeln('$name{')
|
f.writeln('$name{')
|
||||||
|
f.indent++
|
||||||
for i, field in it.fields {
|
for i, field in it.fields {
|
||||||
f.write('\t$field: ')
|
f.write('$field: ')
|
||||||
f.expr(it.exprs[i])
|
f.expr(it.exprs[i])
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
}
|
}
|
||||||
|
f.indent--
|
||||||
f.write('}')
|
f.write('}')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -652,6 +654,9 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
|
|
||||||
fn (f mut Fmt) wrap_long_line() {
|
fn (f mut Fmt) wrap_long_line() {
|
||||||
if f.line_len > max_len {
|
if f.line_len > max_len {
|
||||||
|
if f.out.buf[f.out.buf.len - 1] == ' ' {
|
||||||
|
f.out.go_back(1)
|
||||||
|
}
|
||||||
f.write('\n' + tabs[f.indent + 1])
|
f.write('\n' + tabs[f.indent + 1])
|
||||||
f.line_len = 0
|
f.line_len = 0
|
||||||
}
|
}
|
||||||
|
@ -713,8 +718,8 @@ fn short_module(name string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (f mut Fmt) if_expr(it ast.IfExpr) {
|
fn (f mut Fmt) if_expr(it ast.IfExpr) {
|
||||||
single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len == 1 && it.branches[1].stmts.len ==
|
single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len == 1 &&
|
||||||
1 && (it.is_expr || f.is_assign)
|
it.branches[1].stmts.len == 1 && (it.is_expr || f.is_assign)
|
||||||
f.single_line_if = single_line
|
f.single_line_if = single_line
|
||||||
for i, branch in it.branches {
|
for i, branch in it.branches {
|
||||||
if branch.comment.text != '' {
|
if branch.comment.text != '' {
|
||||||
|
|
|
@ -15,9 +15,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
c_reserved = ['delete', 'exit', 'unix', 'error', 'calloc', 'malloc', 'free', 'panic', 'auto',
|
c_reserved = ['delete', 'exit', 'unix', 'error', 'calloc', 'malloc', 'free', 'panic', 'auto',
|
||||||
'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long', 'register',
|
'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long', 'register',
|
||||||
'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned',
|
'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned',
|
||||||
'void', 'volatile', 'while']
|
'void', 'volatile', 'while']
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ mut:
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t',
|
tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t',
|
||||||
'\t\t\t\t\t\t\t\t']
|
'\t\t\t\t\t\t\t\t']
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string
|
||||||
indent: -1
|
indent: -1
|
||||||
}
|
}
|
||||||
g.init()
|
g.init()
|
||||||
//
|
//
|
||||||
mut autofree_used := false
|
mut autofree_used := false
|
||||||
for file in files {
|
for file in files {
|
||||||
g.file = file
|
g.file = file
|
||||||
|
@ -115,9 +115,9 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string
|
||||||
if g.is_test {
|
if g.is_test {
|
||||||
g.write_tests_main()
|
g.write_tests_main()
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
g.finish()
|
g.finish()
|
||||||
return g.hashes() + g.includes.str() + g.typedefs.str() + g.typedefs2.str() + g.definitions.str() +
|
return g.hashes() + g.includes.str() + g.typedefs.str() + g.typedefs2.str() + g.definitions.str() +
|
||||||
g.gowrappers.str() + g.stringliterals.str() + g.out.str()
|
g.gowrappers.str() + g.stringliterals.str() + g.out.str()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,7 +141,7 @@ pub fn (g mut Gen) init() {
|
||||||
g.write_sorted_types()
|
g.write_sorted_types()
|
||||||
g.write_multi_return_types()
|
g.write_multi_return_types()
|
||||||
g.definitions.writeln('// end of definitions #endif')
|
g.definitions.writeln('// end of definitions #endif')
|
||||||
//
|
//
|
||||||
g.stringliterals.writeln('')
|
g.stringliterals.writeln('')
|
||||||
g.stringliterals.writeln('// >> string literal consts')
|
g.stringliterals.writeln('// >> string literal consts')
|
||||||
g.stringliterals.writeln('void vinit_string_literals(){')
|
g.stringliterals.writeln('void vinit_string_literals(){')
|
||||||
|
@ -205,7 +205,7 @@ pub fn (g mut Gen) typ(t table.Type) string {
|
||||||
return styp
|
return styp
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
pub fn (g mut Gen) write_typedef_types() {
|
pub fn (g mut Gen) write_typedef_types() {
|
||||||
for typ in g.table.types {
|
for typ in g.table.types {
|
||||||
match typ.kind {
|
match typ.kind {
|
||||||
|
@ -817,7 +817,7 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
//
|
//
|
||||||
g.fn_args(it.args, it.is_variadic)
|
g.fn_args(it.args, it.is_variadic)
|
||||||
if it.no_body {
|
if it.no_body {
|
||||||
// Just a function header.
|
// Just a function header.
|
||||||
|
@ -1452,7 +1452,7 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
|
||||||
// sum_type_str
|
// sum_type_str
|
||||||
} else if type_sym.kind == .string {
|
} else if type_sym.kind == .string {
|
||||||
g.write('string_eq(')
|
g.write('string_eq(')
|
||||||
//
|
//
|
||||||
g.expr(node.cond)
|
g.expr(node.cond)
|
||||||
g.write(', ')
|
g.write(', ')
|
||||||
// g.write('string_eq($tmp, ')
|
// g.write('string_eq($tmp, ')
|
||||||
|
@ -1951,7 +1951,7 @@ fn (g mut Gen) assoc(node ast.Assoc) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (g mut Gen) call_args(args []ast.CallArg, expected_types []table.Type) {
|
fn (g mut Gen) call_args(args []ast.CallArg, expected_types []table.Type) {
|
||||||
is_variadic := expected_types.len > 0 && table.type_is(expected_types[expected_types.len -
|
is_variadic := expected_types.len > 0 && table.type_is(expected_types[expected_types.len -
|
||||||
1], .variadic)
|
1], .variadic)
|
||||||
mut arg_no := 0
|
mut arg_no := 0
|
||||||
for arg in args {
|
for arg in args {
|
||||||
|
@ -2132,7 +2132,7 @@ fn (g mut Gen) write_types(types []table.TypeSymbol) {
|
||||||
g.definitions.writeln('EMPTY_STRUCT_DECLARATION;')
|
g.definitions.writeln('EMPTY_STRUCT_DECLARATION;')
|
||||||
}
|
}
|
||||||
// g.definitions.writeln('} $name;\n')
|
// g.definitions.writeln('} $name;\n')
|
||||||
//
|
//
|
||||||
g.definitions.writeln('};\n')
|
g.definitions.writeln('};\n')
|
||||||
}
|
}
|
||||||
table.Alias {
|
table.Alias {
|
||||||
|
@ -2199,8 +2199,8 @@ fn (g Gen) sort_structs(typesa []table.TypeSymbol) []table.TypeSymbol {
|
||||||
// sort graph
|
// sort graph
|
||||||
dep_graph_sorted := dep_graph.resolve()
|
dep_graph_sorted := dep_graph.resolve()
|
||||||
if !dep_graph_sorted.acyclic {
|
if !dep_graph_sorted.acyclic {
|
||||||
verror('cgen.sort_structs(): the following structs form a dependency cycle:\n' + dep_graph_sorted.display_cycles() +
|
verror('cgen.sort_structs(): the following structs form a dependency cycle:\n' + dep_graph_sorted.display_cycles() +
|
||||||
'\nyou can solve this by making one or both of the dependant struct fields references, eg: field &MyStruct' +
|
'\nyou can solve this by making one or both of the dependant struct fields references, eg: field &MyStruct' +
|
||||||
'\nif you feel this is an error, please create a new issue here: https://github.com/vlang/v/issues and tag @joe-conigliaro')
|
'\nif you feel this is an error, please create a new issue here: https://github.com/vlang/v/issues and tag @joe-conigliaro')
|
||||||
}
|
}
|
||||||
// sort types
|
// sort types
|
||||||
|
@ -2342,7 +2342,7 @@ fn (g mut Gen) method_call(node ast.CallExpr) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// TODO performance, detect `array` method differently
|
// TODO performance, detect `array` method differently
|
||||||
if typ_sym.kind == .array && node.name in ['repeat', 'sort_with_compare', 'free', 'push_many',
|
if typ_sym.kind == .array && node.name in ['repeat', 'sort_with_compare', 'free', 'push_many',
|
||||||
'trim', 'first', 'last', 'clone', 'reverse', 'slice'] {
|
'trim', 'first', 'last', 'clone', 'reverse', 'slice'] {
|
||||||
// && rec_sym.name == 'array' {
|
// && rec_sym.name == 'array' {
|
||||||
// && rec_sym.name == 'array' && receiver_name.starts_with('array') {
|
// && rec_sym.name == 'array' && receiver_name.starts_with('array') {
|
||||||
|
@ -2368,7 +2368,7 @@ fn (g mut Gen) method_call(node ast.CallExpr) {
|
||||||
g.write('/*rec*/*')
|
g.write('/*rec*/*')
|
||||||
}
|
}
|
||||||
g.expr(node.left)
|
g.expr(node.left)
|
||||||
is_variadic := node.expected_arg_types.len > 0 && table.type_is(node.expected_arg_types[node.expected_arg_types.len -
|
is_variadic := node.expected_arg_types.len > 0 && table.type_is(node.expected_arg_types[node.expected_arg_types.len -
|
||||||
1], .variadic)
|
1], .variadic)
|
||||||
if node.args.len > 0 || is_variadic {
|
if node.args.len > 0 || is_variadic {
|
||||||
g.write(', ')
|
g.write(', ')
|
||||||
|
|
|
@ -46,9 +46,9 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
|
||||||
pref: &pref.Preferences{}
|
pref: &pref.Preferences{}
|
||||||
scope: scope
|
scope: scope
|
||||||
global_scope: &ast.Scope{
|
global_scope: &ast.Scope{
|
||||||
start_pos: 0
|
start_pos: 0
|
||||||
parent: 0
|
parent: 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.init_parse_fns()
|
p.init_parse_fns()
|
||||||
p.read_first_token()
|
p.read_first_token()
|
||||||
|
@ -67,9 +67,9 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
|
||||||
file_name: path
|
file_name: path
|
||||||
pref: pref
|
pref: pref
|
||||||
scope: &ast.Scope{
|
scope: &ast.Scope{
|
||||||
start_pos: 0
|
start_pos: 0
|
||||||
parent: 0
|
parent: 0
|
||||||
}
|
}
|
||||||
global_scope: global_scope
|
global_scope: global_scope
|
||||||
}
|
}
|
||||||
// comments_mode: comments_mode
|
// comments_mode: comments_mode
|
||||||
|
@ -661,9 +661,9 @@ pub fn (p mut Parser) name_expr() ast.Expr {
|
||||||
x := p.call_expr(is_c, mod) // TODO `node,typ :=` should work
|
x := p.call_expr(is_c, mod) // TODO `node,typ :=` should work
|
||||||
node = x
|
node = x
|
||||||
}
|
}
|
||||||
} else if p.peek_tok.kind == .lcbr && !p.inside_match_case &&
|
} else if p.peek_tok.kind == .lcbr && !p.inside_match_case && (is_c || p.tok.lit[0].is_capital() ||
|
||||||
( is_c || p.tok.lit[0].is_capital() || (p.builtin_mod && p.tok.lit in table.builtin_type_names) ) &&
|
(p.builtin_mod && p.tok.lit in table.builtin_type_names)) && (p.tok.lit.len in [1, 2, 3] ||
|
||||||
( p.tok.lit.len in [1, 2, 3] || !p.tok.lit[p.tok.lit.len - 1].is_capital() || p.table.known_type(p.tok.lit) ) {
|
!p.tok.lit[p.tok.lit.len - 1].is_capital() || p.table.known_type(p.tok.lit)) {
|
||||||
// short_syntax: false
|
// short_syntax: false
|
||||||
return p.struct_init(false)
|
return p.struct_init(false)
|
||||||
} else if p.peek_tok.kind == .dot && (p.tok.lit[0].is_capital() && !known_var) {
|
} else if p.peek_tok.kind == .dot && (p.tok.lit[0].is_capital() && !known_var) {
|
||||||
|
@ -885,10 +885,10 @@ fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
|
||||||
left: left
|
left: left
|
||||||
pos: p.tok.position()
|
pos: p.tok.position()
|
||||||
index: ast.RangeExpr{
|
index: ast.RangeExpr{
|
||||||
low: ast.Expr{}
|
low: ast.Expr{}
|
||||||
high: high
|
high: high
|
||||||
has_high: true
|
has_high: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
expr := p.expr(0) // `[expr]` or `[expr..]`
|
expr := p.expr(0) // `[expr]` or `[expr..]`
|
||||||
|
@ -906,11 +906,11 @@ fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
|
||||||
left: left
|
left: left
|
||||||
pos: p.tok.position()
|
pos: p.tok.position()
|
||||||
index: ast.RangeExpr{
|
index: ast.RangeExpr{
|
||||||
low: expr
|
low: expr
|
||||||
high: high
|
high: high
|
||||||
has_high: has_high
|
has_high: has_high
|
||||||
has_low: has_low
|
has_low: has_low
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// [expr]
|
// [expr]
|
||||||
|
@ -1287,7 +1287,7 @@ fn (p mut Parser) array_init() ast.ArrayInit {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// [1,2,3]
|
// [1,2,3]
|
||||||
for i := 0; p.tok.kind != .rsbr; i++{
|
for i := 0; p.tok.kind != .rsbr; i++ {
|
||||||
expr := p.expr(0)
|
expr := p.expr(0)
|
||||||
exprs << expr
|
exprs << expr
|
||||||
if p.tok.kind == .comma {
|
if p.tok.kind == .comma {
|
||||||
|
@ -1533,9 +1533,9 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
|
||||||
kind: .struct_
|
kind: .struct_
|
||||||
name: name
|
name: name
|
||||||
info: table.Struct{
|
info: table.Struct{
|
||||||
fields: fields
|
fields: fields
|
||||||
is_typedef: is_typedef
|
is_typedef: is_typedef
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mut ret := 0
|
mut ret := 0
|
||||||
if p.builtin_mod && t.name in table.builtin_type_names {
|
if p.builtin_mod && t.name in table.builtin_type_names {
|
||||||
|
@ -1868,8 +1868,8 @@ fn (p mut Parser) enum_decl() ast.EnumDecl {
|
||||||
kind: .enum_
|
kind: .enum_
|
||||||
name: name
|
name: name
|
||||||
info: table.Enum{
|
info: table.Enum{
|
||||||
vals: vals
|
vals: vals
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return ast.EnumDecl{
|
return ast.EnumDecl{
|
||||||
name: name
|
name: name
|
||||||
|
@ -1902,8 +1902,8 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
|
||||||
kind: .sum_type
|
kind: .sum_type
|
||||||
name: p.prepend_mod(name)
|
name: p.prepend_mod(name)
|
||||||
info: table.SumType{
|
info: table.SumType{
|
||||||
variants: sum_variants
|
variants: sum_variants
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return ast.SumTypeDecl{
|
return ast.SumTypeDecl{
|
||||||
name: name
|
name: name
|
||||||
|
@ -1929,8 +1929,8 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
|
||||||
name: p.prepend_mod(name)
|
name: p.prepend_mod(name)
|
||||||
parent_idx: pid
|
parent_idx: pid
|
||||||
info: table.Alias{
|
info: table.Alias{
|
||||||
foo: ''
|
foo: ''
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return ast.AliasTypeDecl{
|
return ast.AliasTypeDecl{
|
||||||
name: name
|
name: name
|
||||||
|
|
Loading…
Reference in New Issue