gop icon indicating copy to clipboard operation
gop copied to clipboard

Proposal: preserve the original source's comments in the generated gop_autogen.go file as much as possible

Open CarlJi opened this issue 10 months ago • 0 comments

Proposal

Currently, when converting Go+ code or Go code to gop_autogen.go, we only preserve the associated documentation of the Node, but the comments are ignored.

For example:

const Pi = 3.14

var version = "1.0"

// comment one
func MulInt(a, b int) int {
	// comment two
	return a * b
}

// comment three
func MulFloat(a, b float64) float64 {
	return a * b
}

// comment four
func Add = (
	// comment five
	func(a int, b int) int {
		// comment six
		return a+b
	}
	func(a float64, b float64) float64 {
		return a+b
	}
)

func Mul = (
	// comment seven
	MulInt
	// comment eight
	MulFloat
)

type M struct {
	Info string
}

type T struct {
	X int
	Y int
}

func (t *T) Info() string {
	return sprintf("%v-%v", t.X, t.Y)
}

func onStart(fn func(int) int) {
	// comment nine
	n := fn(100)
	println n
}

println(version)

// comment ten
onStart x => x*x
onStart x => {
	// comment eleven
	return x*x
}

we got:

// Code generated by gop (Go+); DO NOT EDIT.

package main

import "fmt"

const _ = true
const Pi = 3.14
const Gopo_Mul = "MulInt,MulFloat"

type M struct {
	Info string
}
type T struct {
	X int
	Y int
}
//line examples/002/002.gop:16:1
// comment four
func Add__0(a int, b int) int {
//line examples/002/002.gop:21:1
	return a + b
}
//line examples/002/002.gop:16:1
// comment four
func Add__1(a float64, b float64) float64 {
//line examples/002/002.gop:24:1
	return a + b
}
//line examples/002/002.gop:5:1
// comment one
func MulInt(a int, b int) int {
//line examples/002/002.gop:8:1
	return a * b
}
//line examples/002/002.gop:11:1
// comment three
func MulFloat(a float64, b float64) float64 {
//line examples/002/002.gop:13:1
	return a * b
}

var version = "1.0"
//line examples/002/002.gop:44:1
func (t *T) Info() string {
//line examples/002/002.gop:45:1
	return fmt.Sprintf("%v-%v", t.X, t.Y)
}
//line examples/002/002.gop:48:1
func onStart(fn func(int) int) {
//line examples/002/002.gop:50:1
	n := fn(100)
//line examples/002/002.gop:51:1
	fmt.Println(n)
}
//line examples/002/002.gop:54
func main() {
//line examples/002/002.gop:54:1
	fmt.Println(version)
//line examples/002/002.gop:57:1
	onStart(func(x int) int {
//line examples/002/002.gop:57:1
		return x * x
	})
//line examples/002/002.gop:58:1
	onStart(func(x int) int {
//line examples/002/002.gop:60:1
		return x * x
	})
}

From a completeness perspective, I am thinking it would be better to keep all original comments as much as possible.

Background

Considering that the gop_autogen.go file is intended to be pushed into the code repository, I am currently exploring ways to ensure it adheres to the standard Go coding style.

Workarounds

node

CarlJi avatar Apr 21 '24 10:04 CarlJi