janpfeifer / gonb

GoNB, a Go Notebook Kernel for Jupyter
https://github.com/janpfeifer/gonb
MIT License
631 stars 35 forks source link

Tabs are added to raw strings #19

Closed AnotherCodeArtist closed 1 year ago

AnotherCodeArtist commented 1 year ago

Hi Jan!

Looks like your are using some code beautifier behind the scenes? This has some unwanted aspects on raw strings. Here's an example:

result := 
`This is some text!
It is stored in a file!
Every file needs to have an end.
`
fmt.Println(result)

This produces:

This is some text!
    It is stored in a file!
    Every file needs to have an end.

Any idea how to fix this?

janpfeifer commented 1 year ago

You are right! Everything after %% is moved inside a func main() { ... } with each line indented ...

So short-term solutions:

  1. Remove the %% and create your own func main() {...} for your particular cell.
  2. Or move the statement result := ... to a global variable, if you don't mind it being around for the other cells.

Longer-term solution:

  1. Should we remove the indenting when converting stuff after %% ? It should be simple enough ... but I wonder ...
  2. Actually parsing and understanding the Go code, and when the line is inside a string, not indenting it: this would be the optimal solution, but it's complicated, since we would have to actually parse the code ourselves and understand much more about it, which I wanted to avoid, to keep it simple.

Any thoughts ?

AnotherCodeArtist commented 1 year ago

Hi Jan!

Since the solution is just about using func main() explicitly that's absolutely fine with me.

janpfeifer commented 1 year ago

I'll leave it as is for now then.