From 68af46402e2661210decc984552436b0697b8b6f Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 4 Jul 2020 15:29:00 +0300 Subject: [PATCH] vvet: allow passing many files, improve specifity for emacs goto error --- cmd/tools/vdoc.v | 6 +++--- cmd/tools/vvet.v | 45 ++++++++++++++++++++++++++++-------------- vlib/v/parser/parser.v | 10 +++++++--- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/cmd/tools/vdoc.v b/cmd/tools/vdoc.v index 51f4ae1f1b..205033ac9c 100644 --- a/cmd/tools/vdoc.v +++ b/cmd/tools/vdoc.v @@ -632,9 +632,9 @@ fn (mut cfg DocConfig) generate_docs_from_file() { } if cfg.include_readme { readme_contents := cfg.get_readme(dir_path) - if cfg.output_type == .stdout { + if cfg.output_type == .stdout { println(markdown.to_plain(readme_contents)) - } else if cfg.output_type == .html && cfg.is_multi { + } else if cfg.output_type == .html && cfg.is_multi { cfg.docs << doc.Doc{ head: doc.DocNode{ name: 'README', @@ -642,7 +642,7 @@ fn (mut cfg DocConfig) generate_docs_from_file() { } time_generated: time.now() } - } + } } dirs := if cfg.is_multi { get_modules_list(cfg.input_path, []string{}) } else { [cfg.input_path] } for dirpath in dirs { diff --git a/cmd/tools/vvet.v b/cmd/tools/vvet.v index 2b03318e9f..6a6dbfdd7e 100644 --- a/cmd/tools/vvet.v +++ b/cmd/tools/vvet.v @@ -9,33 +9,48 @@ import v.parser import v.util import v.table import os +import os.cmdline -fn main() { - mut prefs := pref.new_preferences() - prefs.is_vet = true - table := table.new_table() - args := util.join_env_vflags_and_os_args() - if args.len < 3 { +struct VetOptions { + is_verbose bool +} + +fn (vet_options &VetOptions) vprintln(s string) { + if !vet_options.is_verbose { return } - path := args[2] - if path.ends_with('.v') { - vet_file(path, table, prefs) - } else if os.is_dir(path) { - println("vet'ing directory '$path'...") - files := os.walk_ext(path, '.v') - for file in files { - vet_file(file, table, prefs) + println(s) +} + +fn main() { + args := util.join_env_vflags_and_os_args() + paths := cmdline.only_non_options(cmdline.options_after(args, ['vet'])) + vet_options := VetOptions{ + is_verbose: '-verbose' in args || '-v' in args + } + for path in paths { + if path.ends_with('.v') { + vet_options.vet_file(path) + } else if os.is_dir(path) { + vet_options.vprintln("vetting folder '$path'...") + files := os.walk_ext(path, '.v') + for file in files { + vet_options.vet_file(file) + } } } } -fn vet_file(path string, table &table.Table, prefs &pref.Preferences) { +fn (vet_options &VetOptions) vet_file(path string) { + mut prefs := pref.new_preferences() + prefs.is_vet = true + table := table.new_table() if path.contains('/tests') { return } file_ast := parser.parse_file(path, table, .parse_comments, prefs, &ast.Scope{ parent: 0 }) + vet_options.vprintln("vetting file '$path'...") vet.vet(file_ast, table, true) } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 4abfa5b605..4397c0bbc6 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -115,9 +115,13 @@ pub fn parse_file(path string, b_table &table.Table, comments_mode scanner.Comme global_scope: global_scope } if pref.is_vet && p.scanner.text.contains('\n ') { - // TODO make this smarter - println(p.scanner.file_path) - println('Looks like you are using spaces for indentation.\n' + 'You can run `v fmt -w file.v` to fix that automatically') + source_lines := os.read_lines(path) or { []string{} } + for lnumber, line in source_lines { + if line.starts_with(' ') { + eprintln('${p.scanner.file_path}:${lnumber+1}: Looks like you are using spaces for indentation.') + } + } + eprintln('NB: You can run `v fmt -w file.v` to fix these automatically') exit(1) } return p.parse()