From d26ac0f6cbc689020779b32f0c509cf3fa00e45f Mon Sep 17 00:00:00 2001 From: Julia Bogdan Filipchuk Date: Sat, 30 Jan 2021 02:38:54 -0800 Subject: [PATCH] vfmt: add ability to use vfmt as a filter from stdin to stdout (#8432) --- cmd/tools/vfmt.v | 52 ++++++++++++++++++++++++---------------------- cmd/v/help/fmt.txt | 2 ++ vlib/os/os.v | 19 +++++++++++++++++ vlib/v/pref/pref.v | 10 +-------- 4 files changed, 49 insertions(+), 34 deletions(-) diff --git a/cmd/tools/vfmt.v b/cmd/tools/vfmt.v index cf9dbbfafc..90c5a9493b 100644 --- a/cmd/tools/vfmt.v +++ b/cmd/tools/vfmt.v @@ -28,19 +28,8 @@ struct FormatOptions { } const ( - formatted_file_token = '\@\@\@' + 'FORMATTED_FILE: ' - platform_and_file_extensions = [ - ['windows', '_windows.v'], - ['linux', '_lin.v', '_linux.v', '_nix.v'], - ['macos', '_mac.v', '_darwin.v'], - ['freebsd', '_bsd.v', '_freebsd.v'], - ['netbsd', '_bsd.v', '_netbsd.v'], - ['openbsd', '_bsd.v', '_openbsd.v'], - ['solaris', '_solaris.v'], - ['haiku', '_haiku.v'], - ['qnx', '_qnx.v'], - ] - vtmp_folder = util.get_vtmp_folder() + formatted_file_token = '\@\@\@' + 'FORMATTED_FILE: ' + vtmp_folder = util.get_vtmp_folder() ) fn main() { @@ -100,7 +89,11 @@ fn main() { } files << file } - if files.len == 0 { + if is_atty(0) == 0 && files.len == 0 { + foptions.format_pipe() + exit(0) + } + if files.len == 0 || '-help' in args || '--help' in args { vhelp.show_topic('fmt') exit(0) } @@ -183,6 +176,26 @@ fn (foptions &FormatOptions) format_file(file string) { eprintln('$formatted_file_token$vfmt_output_path') } +fn (foptions &FormatOptions) format_pipe() { + mut prefs := pref.new_preferences() + prefs.is_fmt = true + if foptions.is_verbose { + eprintln('vfmt2 running fmt.fmt over stdin') + } + input_text := os.get_raw_lines_joined() + table := table.new_table() + // checker := checker.new_checker(table, prefs) + file_ast := parser.parse_text(input_text, '', table, .parse_comments, prefs, &ast.Scope{ + parent: 0 + }) + // checker.check(file_ast) + formatted_content := fmt.fmt(file_ast, table, foptions.is_debug) + print(formatted_content) + if foptions.is_verbose { + eprintln('fmt.fmt worked and $formatted_content.len bytes were written to stdout.') + } +} + fn print_compiler_options(compiler_params &pref.Preferences) { eprintln(' os: ' + compiler_params.os.str()) eprintln(' ccompiler: $compiler_params.ccompiler') @@ -264,17 +277,6 @@ fn (f FormatOptions) str() string { ' is_verify: $f.is_verify" }' } -fn file_to_target_os(file string) string { - for extensions in platform_and_file_extensions { - for ext in extensions { - if file.ends_with(ext) { - return extensions[0] - } - } - } - return '' -} - fn file_to_mod_name_and_is_module_file(file string) (string, bool) { mut mod_name := 'main' mut is_module_file := false diff --git a/cmd/v/help/fmt.txt b/cmd/v/help/fmt.txt index 97111df772..cdfc8f4edd 100644 --- a/cmd/v/help/fmt.txt +++ b/cmd/v/help/fmt.txt @@ -1,6 +1,8 @@ Usage: v fmt [options] path_to_source.v [path_to_other_source.v] v fmt [options] path/to/dir [path/to/other_dir] + cat source.v | v fmt + Read source code from stdin, output formatted file to stdout. Formats the given V source files or recursively formats all files in the directory, then prints their formatted source to stdout. diff --git a/vlib/os/os.v b/vlib/os/os.v index f1d25d70af..8af66f2749 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -265,6 +265,25 @@ pub fn get_lines_joined() string { return inputstr } +// get_raw_lines_joined reads *all* input lines from stdin. +// It returns them as one large string. NB: unlike os.get_lines_joined, +// empty lines (that contain only `\r\n` or `\n`), will be present in +// the output. +// Reading is stopped, only on EOF of stdin. +pub fn get_raw_lines_joined() string { + mut line := '' + mut lines := []string{} + for { + line = get_raw_line() + if line.len <= 0 { + break + } + lines << line + } + res := lines.join('') + return res +} + // user_os returns current user operating system name. pub fn user_os() string { $if linux { diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index 99599fe88e..d6f5b927b0 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -457,15 +457,7 @@ pub fn parse_args(args []string) (&Preferences, string) { output_option = '-o "$tmp_exe_file_path"' } tmp_v_file_path := '${tmp_file_path}.v' - mut lines := []string{} - for { - iline := os.get_raw_line() - if iline.len == 0 { - break - } - lines << iline - } - contents := lines.join('') + contents := os.get_raw_lines_joined() os.write_file(tmp_v_file_path, contents) or { panic('Failed to create temporary file $tmp_v_file_path') }