vfmt: fix struct init indent and wrapped lines

pull/4289/head^2
Alexander Medvednikov 2020-04-08 00:59:28 +02:00
parent 2fbed2f880
commit d54150cd22
3 changed files with 55 additions and 50 deletions

View File

@ -235,7 +235,7 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
f.expr(it.cond)
f.write('; ')
f.expr(it.inc)
f.writeln('{ ')
f.writeln(' {')
f.stmts(it.stmts)
f.writeln('}')
}
@ -439,7 +439,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.writeln('\t$it.var_name |')
// TODO StructInit copy pasta
for i, field in it.fields {
f.write('\t$field: ')
f.write('$field: ')
f.expr(it.exprs[i])
f.writeln('')
}
@ -625,11 +625,13 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.write('$name{}')
} else {
f.writeln('$name{')
f.indent++
for i, field in it.fields {
f.write('\t$field: ')
f.write('$field: ')
f.expr(it.exprs[i])
f.writeln('')
}
f.indent--
f.write('}')
}
}
@ -652,6 +654,9 @@ fn (f mut Fmt) expr(node ast.Expr) {
fn (f mut Fmt) wrap_long_line() {
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.line_len = 0
}
@ -713,8 +718,8 @@ fn short_module(name string) string {
}
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 ==
1 && (it.is_expr || f.is_assign)
single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len == 1 &&
it.branches[1].stmts.len == 1 && (it.is_expr || f.is_assign)
f.single_line_if = single_line
for i, branch in it.branches {
if branch.comment.text != '' {

View File

@ -15,9 +15,9 @@ import (
)
const (
c_reserved = ['delete', 'exit', 'unix', 'error', 'calloc', 'malloc', 'free', 'panic', 'auto',
'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long', 'register',
'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned',
c_reserved = ['delete', 'exit', 'unix', 'error', 'calloc', 'malloc', 'free', 'panic', 'auto',
'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long', 'register',
'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned',
'void', 'volatile', 'while']
)
@ -58,7 +58,7 @@ mut:
}
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']
)
@ -86,7 +86,7 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string
indent: -1
}
g.init()
//
//
mut autofree_used := false
for file in files {
g.file = file
@ -115,9 +115,9 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string
if g.is_test {
g.write_tests_main()
}
//
//
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()
}
@ -141,7 +141,7 @@ pub fn (g mut Gen) init() {
g.write_sorted_types()
g.write_multi_return_types()
g.definitions.writeln('// end of definitions #endif')
//
//
g.stringliterals.writeln('')
g.stringliterals.writeln('// >> string literal consts')
g.stringliterals.writeln('void vinit_string_literals(){')
@ -205,7 +205,7 @@ pub fn (g mut Gen) typ(t table.Type) string {
return styp
}
//
//
pub fn (g mut Gen) write_typedef_types() {
for typ in g.table.types {
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)
if it.no_body {
// Just a function header.
@ -1452,7 +1452,7 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
// sum_type_str
} else if type_sym.kind == .string {
g.write('string_eq(')
//
//
g.expr(node.cond)
g.write(', ')
// 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) {
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)
mut arg_no := 0
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('} $name;\n')
//
//
g.definitions.writeln('};\n')
}
table.Alias {
@ -2199,8 +2199,8 @@ fn (g Gen) sort_structs(typesa []table.TypeSymbol) []table.TypeSymbol {
// sort graph
dep_graph_sorted := dep_graph.resolve()
if !dep_graph_sorted.acyclic {
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' +
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' +
'\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
@ -2342,7 +2342,7 @@ fn (g mut Gen) method_call(node ast.CallExpr) {
return
}
// 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'] {
// && rec_sym.name == '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.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)
if node.args.len > 0 || is_variadic {
g.write(', ')

View File

@ -46,9 +46,9 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
pref: &pref.Preferences{}
scope: scope
global_scope: &ast.Scope{
start_pos: 0
parent: 0
}
start_pos: 0
parent: 0
}
}
p.init_parse_fns()
p.read_first_token()
@ -67,9 +67,9 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
file_name: path
pref: pref
scope: &ast.Scope{
start_pos: 0
parent: 0
}
start_pos: 0
parent: 0
}
global_scope: global_scope
}
// 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
node = x
}
} else if p.peek_tok.kind == .lcbr && !p.inside_match_case &&
( is_c || p.tok.lit[0].is_capital() || (p.builtin_mod && p.tok.lit in table.builtin_type_names) ) &&
( 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) ) {
} else if p.peek_tok.kind == .lcbr && !p.inside_match_case && (is_c || p.tok.lit[0].is_capital() ||
(p.builtin_mod && p.tok.lit in table.builtin_type_names)) && (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)) {
// short_syntax: false
return p.struct_init(false)
} 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
pos: p.tok.position()
index: ast.RangeExpr{
low: ast.Expr{}
high: high
has_high: true
}
low: ast.Expr{}
high: high
has_high: true
}
}
}
expr := p.expr(0) // `[expr]` or `[expr..]`
@ -906,11 +906,11 @@ fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
left: left
pos: p.tok.position()
index: ast.RangeExpr{
low: expr
high: high
has_high: has_high
has_low: has_low
}
low: expr
high: high
has_high: has_high
has_low: has_low
}
}
}
// [expr]
@ -1287,7 +1287,7 @@ fn (p mut Parser) array_init() ast.ArrayInit {
}
} else {
// [1,2,3]
for i := 0; p.tok.kind != .rsbr; i++{
for i := 0; p.tok.kind != .rsbr; i++ {
expr := p.expr(0)
exprs << expr
if p.tok.kind == .comma {
@ -1533,9 +1533,9 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
kind: .struct_
name: name
info: table.Struct{
fields: fields
is_typedef: is_typedef
}
fields: fields
is_typedef: is_typedef
}
}
mut ret := 0
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_
name: name
info: table.Enum{
vals: vals
}
vals: vals
}
})
return ast.EnumDecl{
name: name
@ -1902,8 +1902,8 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
kind: .sum_type
name: p.prepend_mod(name)
info: table.SumType{
variants: sum_variants
}
variants: sum_variants
}
})
return ast.SumTypeDecl{
name: name
@ -1929,8 +1929,8 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
name: p.prepend_mod(name)
parent_idx: pid
info: table.Alias{
foo: ''
}
foo: ''
}
})
return ast.AliasTypeDecl{
name: name