Code Monkey home page Code Monkey logo

Comments (16)

Gys avatar Gys commented on August 23, 2024

Did you enable File -> Auto save ?
Because formatting this way works for me (using the default goreturns).

But formatting at save does not work for me at all. See other issue.

from vscode-go.

mholt avatar mholt commented on August 23, 2024

I did enable it, but I can't remember if I enabled it before or after this. I know in this particular instance, I right-clicked, then clicked "Format Code" when it happened. (formatting on save didn't do anything)

from vscode-go.

Gys avatar Gys commented on August 23, 2024

Actually I just now realize that formatting on save is not in the feature list....

from vscode-go.

lukehoban avatar lukehoban commented on August 23, 2024

Format on save is not implemented yet - tracking that in #14.

@mholt Could you share the full file where you see this problem? And could you confirm whether you see this even when you save the file manually before invoking fmt? I wasn't able to repro this myself so far.

from vscode-go.

grabbou avatar grabbou commented on August 23, 2024

I am also experiencing some issues when imports are added/removed, e.g. this:

import (
    "net/http"

    "github.com/jinzhu/gorm"
)

gets formatted to this:

import (
    "net/http"
gin-gonic/gin"jinzhu/gorm"
)

or this:

import (
      "net/http"
      "github.com/xxx/yyy/utils"
)

to this:

import (
      "net/http
      "github.com/gin-gonic/gin"
      "github.com/xxx/yyy/utils"
)

from vscode-go.

lukehoban avatar lukehoban commented on August 23, 2024

Could anyone share a full code sample where they are seeing this? I can't reproduce with the snippets shared so far, but I expect that is because I don't have the rest of the file contents.

from vscode-go.

mholt avatar mholt commented on August 23, 2024

I got my error in this file: https://github.com/mholt/caddy/blob/e17d43b58a3b31f16e716791e16b69752f36a5ae/caddy/sigtrap_posix.go

by adding a fmt.Println in one of the cases of the switch.

from vscode-go.

sanderhahn avatar sanderhahn commented on August 23, 2024

One snippet that goes wrong with goreturns and also goimports is (where | is the cursor):

package main

func main() {
    fmt.Println("Hello World! :)")
}
|

This results into:

package mainimport "fmt"func main() {
    fmt.Println("Hello World! :)")
}

Tried to debug the goFormat.ts, but can't pinpoint the exact problem. If you enter another space on the last line without saving the file, the format works correctly. Copy and paste also sometimes works correctly, enter the code manually and it seems to go wrong (line end settings: LF).

from vscode-go.

dmitshur avatar dmitshur commented on August 23, 2024

I think I am seeing this issue too.

Here's a very simple reproduce case:

package main


func main() {
}

Note the 2 blank lines. Formatting this (with gofmt as the format tool) results in correctly formatting .go code.

However, consider this version with 3 blank lines:

package main



func main() {
}

Formatting this leaves 3 blank lines, which is incorrect. Running gofmt -d main.go shows:

diff main.go gofmt/main.go
--- /var/folders/_w/3kl_g4rn5sv1nyjblk7tf4h00000gn/T/gofmt780202922 2015-11-18 15:08:19.000000000 -0800
+++ /var/folders/_w/3kl_g4rn5sv1nyjblk7tf4h00000gn/T/gofmt316322049 2015-11-18 15:08:19.000000000 -0800
@@ -1,6 +1,4 @@
 package main

-
-
 func main() {
 }

from vscode-go.

lukehoban avatar lukehoban commented on August 23, 2024

Thanks for the repro cases @shurcooL and @sanderhahn. As far as I can tell the Go extension is requesting the right edits here, but Code isn't applying them correctly. Following up on that in microsoft/vscode#144.

from vscode-go.

dmitshur avatar dmitshur commented on August 23, 2024

I've looked over goFormat.ts and I agree with your assessment @lukehoban. At least I didn't spot anything obviously wrong in there.

from vscode-go.

nictuku avatar nictuku commented on August 23, 2024

Another repro case (vertical spaces omitted to save space here):

package main
import (
    "log"
    "net/http"
)
func main() {
    log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))
}

Now add a spurious "fmt" import:

package main
import (
    "fmt"
    "log"
    "net/http"
)
func main() {
    log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))
}

When I save, it shows the red lines below "fmt", as expected. But then after Format Code, the import block becomes (note the sole double quotes)


import (
    "
    "log"
    "net/http"
)

btw: Nice work overall, Luke and team. This is shaping up really really nicely.

from vscode-go.

freeformz avatar freeformz commented on August 23, 2024

@nictuku FWIW: I copy-n-pasted that code, saved it as mytest.go, right clicked and chose "Format Code" and everything is fine.

I tested this with "goreturns" / "goimports" (when I specify gofmt I get a missing tool prompt for some reason).

from vscode-go.

dmitshur avatar dmitshur commented on August 23, 2024

when I specify gofmt I get a missing tool prompt for some reason

I think the problem is that "do you have this tool installed" check only looks in GOPATH/bin folders (for each workspace), but not GOROOT/bin where gofmt is located.

from vscode-go.

tpng avatar tpng commented on August 23, 2024

vscode 0.10.2 should have fixed this issue.

from vscode-go.

lukehoban avatar lukehoban commented on August 23, 2024

All - I believe this is now fixed if you update to VS Code 0.10.2 with the fix for #70.

If anyone still sees corruption after formatting, please reopen.

from vscode-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.