vfmt: do not generate a space if the comment is empty

pull/4293/head
Alexander Medvednikov 2020-04-08 14:19:13 +02:00
parent 2e78051933
commit 5ef5712e91
3 changed files with 44 additions and 32 deletions

View File

@ -16,7 +16,6 @@ import (
)
struct FormatOptions {
is_2 bool
is_l bool
is_c bool
is_w bool
@ -51,7 +50,6 @@ fn main() {
util.set_vroot_folder(os.dir(os.dir(os.dir(toolexe))))
args := join_flags_and_argument()
foptions := FormatOptions{
is_2: '-2' in args
is_c: '-c' in args
is_l: '-l' in args
is_w: '-w' in args
@ -84,19 +82,12 @@ fn main() {
}
mut files := []string
for file in possible_files {
if foptions.is_2 {
if !file.ends_with('.v') && !file.ends_with('.vv') {
verror('v fmt -2 can only be used on .v or .vv files.\nOffending file: "$file" .')
continue
}
} else {
if !file.ends_with('.v') {
verror('v fmt can only be used on .v files.\nOffending file: "$file" .')
continue
}
if !file.ends_with('.v') && !file.ends_with('.vv') {
verror('v fmt can only be used on .v files.\nOffending file: "$file"')
continue
}
if !os.exists(file) {
verror('"$file" does not exist.')
verror('"$file" does not exist')
continue
}
files << file
@ -107,7 +98,7 @@ fn main() {
}
mut cli_args_no_files := []string
for a in os.args {
if !a in files {
if !(a in files) {
cli_args_no_files << a
}
}
@ -251,7 +242,7 @@ fn find_working_diff_command() ?string {
}
pub fn (f FormatOptions) str() string {
return 'FormatOptions{ ' + ' is_2: $f.is_2' + ' is_l: $f.is_l' + ' is_w: $f.is_w' + ' is_diff: $f.is_diff' + ' is_verbose: $f.is_verbose' + ' is_all: $f.is_all' + ' is_worker: $f.is_worker' + ' is_debug: $f.is_debug' + ' }'
return 'FormatOptions{ is_l: $f.is_l' + ' is_w: $f.is_w' + ' is_diff: $f.is_diff' + ' is_verbose: $f.is_verbose' + ' is_all: $f.is_all' + ' is_worker: $f.is_worker' + ' is_debug: $f.is_debug' + ' }'
}
fn file_to_target_os(file string) string {

View File

@ -397,20 +397,21 @@ pub fn (c mut Checker) call_expr(call_expr mut ast.CallExpr) table.Type {
}
}
pub fn (c mut Checker) check_expr_opt_call(x ast.Expr, xtype table.Type, is_return_used bool){
pub fn (c mut Checker) check_expr_opt_call(x ast.Expr, xtype table.Type, is_return_used bool) {
match x {
ast.CallExpr {
if table.type_is(it.return_type, .optional) {
c.check_or_block(it, xtype, is_return_used)
}
}
else{}
else {}
}
}
pub fn (c mut Checker) check_or_block(call_expr mut ast.CallExpr, ret_type table.Type, is_ret_used bool) {
if !call_expr.or_block.is_used {
c.error('${call_expr.name}() returns an option, but you missed to add an `or {}` block to it', call_expr.pos)
c.error('${call_expr.name}() returns an option, but you missed to add an `or {}` block to it',
call_expr.pos)
return
}
stmts_len := call_expr.or_block.stmts.len
@ -427,7 +428,8 @@ pub fn (c mut Checker) check_or_block(call_expr mut ast.CallExpr, ret_type table
if is_ret_used {
if !c.is_last_or_block_stmt_valid(last_stmt) {
expected_type_name := c.table.get_type_symbol(ret_type).name
c.error('last statement in the `or {}` block should return $expected_type_name', call_expr.pos)
c.error('last statement in the `or {}` block should return $expected_type_name',
call_expr.pos)
return
}
match last_stmt {
@ -439,12 +441,14 @@ pub fn (c mut Checker) check_or_block(call_expr mut ast.CallExpr, ret_type table
}
type_name := c.table.get_type_symbol(c.expr(it.expr)).name
expected_type_name := c.table.get_type_symbol(ret_type).name
c.error('wrong return type `$type_name` in the `or {}` block, expected `$expected_type_name`', it.pos)
c.error('wrong return type `$type_name` in the `or {}` block, expected `$expected_type_name`',
it.pos)
return
}
ast.BranchStmt {
if !(it.tok.kind in [.key_continue, .key_break]) {
c.error('only break/continue is allowed as a branch statement in the end of an `or {}` block', it.tok.position())
c.error('only break/continue is allowed as a branch statement in the end of an `or {}` block',
it.tok.position())
return
}
}
@ -456,18 +460,30 @@ pub fn (c mut Checker) check_or_block(call_expr mut ast.CallExpr, ret_type table
fn is_expr_panic_or_exit(expr ast.Expr) bool {
match expr {
ast.CallExpr { return it.name in ['panic','exit'] }
else { return false }
ast.CallExpr {
return it.name in ['panic', 'exit']
}
else {
return false
}
}
}
// TODO: merge to check_or_block when v can handle it
pub fn (c mut Checker) is_last_or_block_stmt_valid(stmt ast.Stmt) bool {
return match stmt {
ast.Return { true }
ast.BranchStmt { true }
ast.ExprStmt { true }
else { false }
ast.Return {
true
}
ast.BranchStmt {
true
}
ast.ExprStmt {
true
}
else {
false
}
}
}
@ -679,7 +695,8 @@ fn (c mut Checker) stmt(node ast.Stmt) {
assert_type := c.expr(it.expr)
if assert_type != table.bool_type_idx {
atype_name := c.table.get_type_symbol(assert_type).name
c.error('assert can be used only with `bool` expressions, but found `${atype_name}` instead', it.pos)
c.error('assert can be used only with `bool` expressions, but found `${atype_name}` instead',
it.pos)
}
}
ast.AssignStmt {

View File

@ -688,12 +688,16 @@ fn (f mut Fmt) or_expr(or_block ast.OrExpr) {
fn (f mut Fmt) comment(node ast.Comment) {
if !node.text.contains('\n') {
is_separate_line := node.text.starts_with('|')
if is_separate_line {
f.writeln('// ${node.text[1..]}') // $node.pos.line_nr')
mut s := if is_separate_line { node.text[1..] } else { node.text }
if s == '' {
s = '//'
} else {
f.out.go_back(1)
f.writeln('// $node.text')
s = '// ' + s
}
if !is_separate_line {
f.out.go_back(1) // delete the generated \n
}
f.writeln(s)
return
}
lines := node.text.split_into_lines()