Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go snippet evolution #448

Open
ccoVeille opened this issue Sep 1, 2024 · 3 comments
Open

Go snippet evolution #448

ccoVeille opened this issue Sep 1, 2024 · 3 comments

Comments

@ccoVeille
Copy link
Contributor

ccoVeille commented Sep 1, 2024

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

  • add the same header, as you already do now
  • test if it works, great
  • if fails, enclose the code with this
package main

func main() {

// a random string
<insert the code snippet here>
// the same random string
}
  • try to format
  • if it works, use code to extract everything between the random strings delimiter
  • trim the leading space on each line returned.
@hougesen
Copy link
Owner

hougesen commented Sep 3, 2024

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants