From 6d480e29d422334387fdad82b5030d97448f4637 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Fri, 15 Nov 2019 13:03:47 -0500 Subject: [PATCH] internal/lsp/cmd: don't create .orig files for rename by default Fixes golang/go#35551 Change-Id: I78086e0288da84a4c0b008d05e376f47b30d978a Reviewed-on: https://go-review.googlesource.com/c/tools/+/207347 Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot Reviewed-by: Ian Cottrell --- internal/lsp/cmd/rename.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/internal/lsp/cmd/rename.go b/internal/lsp/cmd/rename.go index 98f7d69c1df..42eeeaa12c3 100644 --- a/internal/lsp/cmd/rename.go +++ b/internal/lsp/cmd/rename.go @@ -23,8 +23,9 @@ import ( // rename implements the rename verb for gopls. type rename struct { - Diff bool `flag:"d" help:"display diffs instead of rewriting files"` - Write bool `flag:"w" help:"write result to (source) file instead of stdout"` + Diff bool `flag:"d" help:"display diffs instead of rewriting files"` + Write bool `flag:"w" help:"write result to (source) file instead of stdout"` + Preserve bool `flag:"preserve" help:"preserve original files"` app *Application } @@ -102,9 +103,10 @@ func (r *rename) Run(ctx context.Context, args ...string) error { switch { case r.Write: fmt.Fprintln(os.Stderr, filename) - err := os.Rename(filename, filename+".orig") - if err != nil { - return errors.Errorf("%v: %v", edits, err) + if r.Preserve { + if err := os.Rename(filename, filename+".orig"); err != nil { + return errors.Errorf("%v: %v", edits, err) + } } ioutil.WriteFile(filename, []byte(newContent), 0644) case r.Diff: