vfmt: do not generate a space if the comment is empty
parent
2e78051933
commit
5ef5712e91
|
@ -16,7 +16,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
struct FormatOptions {
|
struct FormatOptions {
|
||||||
is_2 bool
|
|
||||||
is_l bool
|
is_l bool
|
||||||
is_c bool
|
is_c bool
|
||||||
is_w bool
|
is_w bool
|
||||||
|
@ -51,7 +50,6 @@ fn main() {
|
||||||
util.set_vroot_folder(os.dir(os.dir(os.dir(toolexe))))
|
util.set_vroot_folder(os.dir(os.dir(os.dir(toolexe))))
|
||||||
args := join_flags_and_argument()
|
args := join_flags_and_argument()
|
||||||
foptions := FormatOptions{
|
foptions := FormatOptions{
|
||||||
is_2: '-2' in args
|
|
||||||
is_c: '-c' in args
|
is_c: '-c' in args
|
||||||
is_l: '-l' in args
|
is_l: '-l' in args
|
||||||
is_w: '-w' in args
|
is_w: '-w' in args
|
||||||
|
@ -84,19 +82,12 @@ fn main() {
|
||||||
}
|
}
|
||||||
mut files := []string
|
mut files := []string
|
||||||
for file in possible_files {
|
for file in possible_files {
|
||||||
if foptions.is_2 {
|
if !file.ends_with('.v') && !file.ends_with('.vv') {
|
||||||
if !file.ends_with('.v') && !file.ends_with('.vv') {
|
verror('v fmt can only be used on .v files.\nOffending file: "$file"')
|
||||||
verror('v fmt -2 can only be used on .v or .vv files.\nOffending file: "$file" .')
|
continue
|
||||||
continue
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if !file.ends_with('.v') {
|
|
||||||
verror('v fmt can only be used on .v files.\nOffending file: "$file" .')
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if !os.exists(file) {
|
if !os.exists(file) {
|
||||||
verror('"$file" does not exist.')
|
verror('"$file" does not exist')
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
files << file
|
files << file
|
||||||
|
@ -107,7 +98,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
mut cli_args_no_files := []string
|
mut cli_args_no_files := []string
|
||||||
for a in os.args {
|
for a in os.args {
|
||||||
if !a in files {
|
if !(a in files) {
|
||||||
cli_args_no_files << a
|
cli_args_no_files << a
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -251,7 +242,7 @@ fn find_working_diff_command() ?string {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (f FormatOptions) str() 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 {
|
fn file_to_target_os(file string) string {
|
||||||
|
|
|
@ -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 {
|
match x {
|
||||||
ast.CallExpr {
|
ast.CallExpr {
|
||||||
if table.type_is(it.return_type, .optional) {
|
if table.type_is(it.return_type, .optional) {
|
||||||
c.check_or_block(it, xtype, is_return_used)
|
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) {
|
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 {
|
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
|
return
|
||||||
}
|
}
|
||||||
stmts_len := call_expr.or_block.stmts.len
|
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 is_ret_used {
|
||||||
if !c.is_last_or_block_stmt_valid(last_stmt) {
|
if !c.is_last_or_block_stmt_valid(last_stmt) {
|
||||||
expected_type_name := c.table.get_type_symbol(ret_type).name
|
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
|
return
|
||||||
}
|
}
|
||||||
match last_stmt {
|
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
|
type_name := c.table.get_type_symbol(c.expr(it.expr)).name
|
||||||
expected_type_name := c.table.get_type_symbol(ret_type).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
|
return
|
||||||
}
|
}
|
||||||
ast.BranchStmt {
|
ast.BranchStmt {
|
||||||
if !(it.tok.kind in [.key_continue, .key_break]) {
|
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
|
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 {
|
fn is_expr_panic_or_exit(expr ast.Expr) bool {
|
||||||
match expr {
|
match expr {
|
||||||
ast.CallExpr { return it.name in ['panic','exit'] }
|
ast.CallExpr {
|
||||||
else { return false }
|
return it.name in ['panic', 'exit']
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: merge to check_or_block when v can handle it
|
// 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 {
|
pub fn (c mut Checker) is_last_or_block_stmt_valid(stmt ast.Stmt) bool {
|
||||||
return match stmt {
|
return match stmt {
|
||||||
ast.Return { true }
|
ast.Return {
|
||||||
ast.BranchStmt { true }
|
true
|
||||||
ast.ExprStmt { true }
|
}
|
||||||
else { false }
|
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)
|
assert_type := c.expr(it.expr)
|
||||||
if assert_type != table.bool_type_idx {
|
if assert_type != table.bool_type_idx {
|
||||||
atype_name := c.table.get_type_symbol(assert_type).name
|
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 {
|
ast.AssignStmt {
|
||||||
|
|
|
@ -688,12 +688,16 @@ fn (f mut Fmt) or_expr(or_block ast.OrExpr) {
|
||||||
fn (f mut Fmt) comment(node ast.Comment) {
|
fn (f mut Fmt) comment(node ast.Comment) {
|
||||||
if !node.text.contains('\n') {
|
if !node.text.contains('\n') {
|
||||||
is_separate_line := node.text.starts_with('|')
|
is_separate_line := node.text.starts_with('|')
|
||||||
if is_separate_line {
|
mut s := if is_separate_line { node.text[1..] } else { node.text }
|
||||||
f.writeln('// ${node.text[1..]}') // $node.pos.line_nr')
|
if s == '' {
|
||||||
|
s = '//'
|
||||||
} else {
|
} else {
|
||||||
f.out.go_back(1)
|
s = '// ' + s
|
||||||
f.writeln('// $node.text')
|
|
||||||
}
|
}
|
||||||
|
if !is_separate_line {
|
||||||
|
f.out.go_back(1) // delete the generated \n
|
||||||
|
}
|
||||||
|
f.writeln(s)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
lines := node.text.split_into_lines()
|
lines := node.text.split_into_lines()
|
||||||
|
|
Loading…
Reference in New Issue