mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vfmt: add ability to use vfmt as a filter from stdin to stdout (#8432)
This commit is contained in:
parent
7f5d654c3a
commit
d26ac0f6cb
@ -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
|
||||
|
@ -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.
|
||||
|
19
vlib/os/os.v
19
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 {
|
||||
|
@ -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')
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user