From fa7035effaefee46d28b6cff8ab7164e9658b163 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Fri, 26 Jun 2020 12:34:15 +0100 Subject: [PATCH] cmd/shfmt: add -filename to name standard input Otherwise, EditorConfig support is pretty useless with stdin, as we would always use "" as the filename. This feature is particularly useful for editor integrations, which want to format a real file on disk but with the in-memory contents that the user is editing. Start writing the changelog for 3.2.0 too. Fixes #577. --- CHANGELOG.md | 5 ++++ cmd/shfmt/main.go | 26 ++++++++++++++------- cmd/shfmt/testdata/scripts/editorconfig.txt | 15 ++++++++++++ cmd/shfmt/testdata/scripts/flags.txt | 5 +++- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81d52c4c0..3af20d079 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +- **cmd/shfmt** + - Add `-filename` to give a name to standard input +- **syntax** + - Rewrite arithmetic parsing to fix operator precedence + ## [3.1.1] - 2020-05-04 - **cmd/shfmt** diff --git a/cmd/shfmt/main.go b/cmd/shfmt/main.go index 9ced7cbb5..a3164022d 100644 --- a/cmd/shfmt/main.go +++ b/cmd/shfmt/main.go @@ -38,8 +38,9 @@ var ( // useEditorConfig will be false if any parser or printer flags were used. useEditorConfig = true - langStr = flag.String("ln", "", "") - posix = flag.Bool("p", false, "") + langStr = flag.String("ln", "", "") + posix = flag.Bool("p", false, "") + filename = flag.String("filename", "", "") indent = flag.Uint("i", 0, "") binNext = flag.Bool("bn", false, "") @@ -85,8 +86,9 @@ for shell files - both by filename extension and by shebang. Parser options: - -ln str language variant to parse (bash/posix/mksh, default "bash") - -p shorthand for -ln=posix + -ln str language variant to parse (bash/posix/mksh, default "bash") + -p shorthand for -ln=posix + -filename str provide a name for the standard input file Printer options: @@ -170,7 +172,11 @@ Utilities: color = true } if flag.NArg() == 0 || (flag.NArg() == 1 && flag.Arg(0) == "-") { - if err := formatStdin(); err != nil { + name := "" + if *filename != "" { + name = *filename + } + if err := formatStdin(name); err != nil { if err != errChangedWithDiff { fmt.Fprintln(os.Stderr, err) } @@ -178,8 +184,12 @@ Utilities: } return 0 } + if *filename != "" { + fmt.Fprintln(os.Stderr, "-filename can only be used with stdin") + return 1 + } if *toJSON { - fmt.Fprintln(os.Stderr, "-tojson can only be used with stdin/out") + fmt.Fprintln(os.Stderr, "-tojson can only be used with stdin") return 1 } status := 0 @@ -196,7 +206,7 @@ Utilities: var errChangedWithDiff = fmt.Errorf("") -func formatStdin() error { +func formatStdin(name string) error { if *write { return fmt.Errorf("-w cannot be used on standard input") } @@ -204,7 +214,7 @@ func formatStdin() error { if err != nil { return err } - return formatBytes(src, "") + return formatBytes(src, name) } var vcsDir = regexp.MustCompile(`^\.(git|svn|hg)$`) diff --git a/cmd/shfmt/testdata/scripts/editorconfig.txt b/cmd/shfmt/testdata/scripts/editorconfig.txt index 0070bdc4e..17370ad0f 100644 --- a/cmd/shfmt/testdata/scripts/editorconfig.txt +++ b/cmd/shfmt/testdata/scripts/editorconfig.txt @@ -6,6 +6,14 @@ shfmt cmp stdout input.sh.golden ! stderr . +# Verify that -filename works well with EditorConfig. +stdin stdin-filename-bash +shfmt + +stdin stdin-filename-bash +! shfmt -filename=foo-posix.sh +stderr '^foo-posix.sh:.* arrays are a bash' + # Using a file path should use EditorConfig, including with the use of flags # like -l. shfmt input.sh @@ -51,6 +59,9 @@ root = true [*] indent_style = space indent_size = 3 + +[*-posix.sh] +shell_variant = posix -- input.sh -- { indented @@ -59,6 +70,10 @@ indent_size = 3 { indented } +-- stdin-filename-bash -- +array=( + element +) -- morespaces/.editorconfig -- [*.sh] indent_size = 6 diff --git a/cmd/shfmt/testdata/scripts/flags.txt b/cmd/shfmt/testdata/scripts/flags.txt index d675d00e4..c70572b0f 100644 --- a/cmd/shfmt/testdata/scripts/flags.txt +++ b/cmd/shfmt/testdata/scripts/flags.txt @@ -16,7 +16,10 @@ stderr 'cannot coexist' stderr 'unknown shell language' ! shfmt -tojson file -stderr 'can only be used with stdin' +stderr '-tojson can only be used with stdin' + +! shfmt -filename=foo file +stderr '-filename can only be used with stdin' # Check all the -ln variations. stdin notbash.sh