microsoft / vscode-go

An extension for VS Code which provides support for the Go language. We have moved to https://github.com/golang/vscode-go
Other
5.93k stars 648 forks source link

go.formatFlags not working with gofmt -s (simplify) #3230

Closed Graham42 closed 4 years ago

Graham42 commented 4 years ago

What version of Go, VS Code & VS Code Go extension are you using?

Share the Go related settings you have added/edited

{
  "[go]": {
    "editor.formatOnSave": true
  },
  "go.formatTool": "gofmt",
  "go.formatFlags": ["-s"],
  "go.lintTool": "golangci-lint",
  "go.useLanguageServer": true
}

Describe the bug

Setting "go.formatFlags": ["-s"], doesn't work. My code is formatted on save, but the -s flag is not applied.

type foo struct{}

func Do() {
    x := []foo{
        foo{},
        //^^^ `gofmt -s` should remove the type annotation here
    }
    println(x)
}

Steps to reproduce the behavior:

  1. Use the above code snippet and vscode settings.
  2. Delete some whitespace.
  3. Save the file.

Screenshots or recordings

If applicable, add screenshots or recordings to help explain your problem.

formatting-on-save

stamblerre commented 4 years ago

Thanks for the report. The language server now does this though a source.FixAll code action, so you will need to add the following to the your "[go]" settings block:

"[go]": {
    "editor.codeActionsOnSave": {
        "source.fixAll": true,
    },
 },
Graham42 commented 4 years ago

Thanks! That snippet solves the "on save" use case for me. However, if I use the editor "Format Document" command the -s flag is still not applied.

stamblerre commented 4 years ago

You can run the simplifications manually by opening the Command Palette (Ctrl + Shift + P) and selecting "Source Action". I believe this will be added to the right click menu in the next VS Code release.

Graham42 commented 4 years ago

This issue is effectively solved for me with the fixAll action on save, I just wanted to share that finding. If you don't consider it a bug that the formatFlags option doesn't apply to the "Format Document" command then feel free to close this issue :slightly_smiling_face:

stamblerre commented 4 years ago

Ok - thanks! The reason it no longer works as part of formatting is that it's now been split out as a code action, as defined in the LSP specification, which is intentional. Closing as result.