Przeglądaj źródła

protoc-gen-go: move package comments to before the package decl (#715)

We intentionally don't attach comments from the .proto source file to
the Go package declaration (since this produces strange output when
multiple .proto files in the same Go package all have comments), but
putting the comment above the package decl makes it easier to find and
keeps it from being obscured by the imports.

Also change the package comment to be generated with PrintComments
(producing // comments rather than /* */ comments) to save a few lines
of code and be internally consistent.
Damien Neil 7 lat temu
rodzic
commit
32c79afd48

+ 2 - 15
protoc-gen-go/generator/generator.go

@@ -1211,23 +1211,10 @@ func (g *Generator) generateHeader() {
 		g.P("// source: ", g.file.Name)
 	}
 	g.P()
-
+	g.PrintComments(strconv.Itoa(packagePath))
+	g.P()
 	g.P("package ", g.file.packageName)
 	g.P()
-
-	if loc, ok := g.file.comments[strconv.Itoa(packagePath)]; ok {
-		g.P("/*")
-		// not using g.PrintComments because this is a /* */ comment block.
-		text := strings.TrimSuffix(loc.GetLeadingComments(), "\n")
-		for _, line := range strings.Split(text, "\n") {
-			line = strings.TrimPrefix(line, " ")
-			// ensure we don't escape from the block comment
-			line = strings.Replace(line, "*/", "* /", -1)
-			g.P(line)
-		}
-		g.P("*/")
-		g.P()
-	}
 }
 
 // deprecationComment is the standard comment added to deprecated

+ 2 - 4
protoc-gen-go/testdata/deprecated/deprecated.pb.go

@@ -1,11 +1,9 @@
 // Code generated by protoc-gen-go. DO NOT EDIT.
 // deprecated/deprecated.proto is a deprecated file.
 
-package deprecated
+// package deprecated contains only deprecated messages and services.
 
-/*
-package deprecated contains only deprecated messages and services.
-*/
+package deprecated
 
 import (
 	fmt "fmt"

+ 2 - 4
protoc-gen-go/testdata/my_test/test.pb.go

@@ -1,11 +1,9 @@
 // Code generated by protoc-gen-go. DO NOT EDIT.
 // source: my_test/test.proto
 
-package test
+// This package holds interesting messages.
 
-/*
-This package holds interesting messages.
-*/
+package test
 
 import (
 	fmt "fmt"