hougesen / mdsf

Format markdown code blocks using your favorite code formatters.
MIT License
12 stars 0 forks source link

Go snippet evolution #448

Open ccoVeille opened 1 month ago

ccoVeille commented 1 month ago

You already fixed issues with Go code with my previous reported issue

I found a new pattern, but I have a new solution.

The example I used where struct, so a type definition,something that works simply by adding a package.

Unfortunately, there are piece of code, that are not formatted, and lead to errors today

Here are example

f, err := strconv.Atoi("42")
if err != nil {

    panic(err) }

fmt.Println(f +3,
)

Here the code is badly formatted on purpose.

This snippet cannot work with the following solution.

Here is what could be done

package main

func main() {

// a random string
<insert the code snippet here>
// the same random string
}
hougesen commented 1 month ago

So if I understand it correctly the issue is that code must be inside a function definition?

The only "issue" I can see from this is that mdsf would also have to handle removing the extra indentation, which varies depending on formatter and configuration.

ccoVeille commented 1 month ago

Yes, exactly. I knew my suggestion is a bit messy, but I'm afraid I don't see another solution right now.

And the issue will also be that the formatting will import the path.

So this

f, err := strconv.Atoi("42")
if err != nil {

    panic(err) }

fmt.Println(f +3
)

will have to be written like this before being sent to formatter

package main

func main() {
    f, err := strconv.Atoi("42")
if err != nil {

    panic(err) }

fmt.Println(f +3,
)
}

https://go.dev/play/p/kMQRuIxEo-Z

would be formatted like this (please note the imports were added)

https://go.dev/play/p/xIBzjl-lUCh

package main

import (
    "fmt"
    "strconv"
)

func main() {
    f, err := strconv.Atoi("42")
    if err != nil {

        panic(err)
    }

    fmt.Println(f + 3)
}

So here you would have to extract the content of main

    f, err := strconv.Atoi("42")
    if err != nil {

        panic(err)
    }

    fmt.Println(f + 3)

and trim the leading spaces

f, err := strconv.Atoi("42")
if err != nil {
        panic(err)
}

fmt.Println(f + 3)
ccoVeille commented 1 month ago

That's why I suggested adding comment block to split the text

https://go.dev/play/p/PMDiIRjpvcV

format then split the content between

https://go.dev/play/p/4t5Ltud5EL-