Skip to content

Commit 3989c3b

Browse files
aclementsgopherbot
authored andcommitted
_content/doc: expand documentation on directive comments
As part of golang/go#68021, we clarified the syntax of directive comments. Document these further details and point to the new function for parsing this syntax. Change-Id: I52dad06012c64539b5c85754d4a8a6df30e17265 Reviewed-on: https://go-review.googlesource.com/c/website/+/705675 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Austin Clements <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent df1bc8b commit 3989c3b

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

_content/doc/comment.md

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -498,25 +498,6 @@ are written in source code but will adjust presentation to make
498498
the semantic meaning of a particular comment clearer,
499499
analogous to reformatting `1+2 * 3` to `1 + 2*3` in ordinary source code.
500500

501-
Directive comments such as `//go:generate` are not
502-
considered part of a doc comment and are omitted from
503-
rendered documentation.
504-
Gofmt moves directive comments to the end of the doc comment,
505-
preceded by a blank line.
506-
For example:
507-
508-
package regexp
509-
510-
// An Op is a single regular expression operator.
511-
//
512-
//go:generate stringer -type Op -trimprefix Op
513-
type Op uint8
514-
515-
A directive comment is a line matching the regular expression
516-
`//(line |extern |export |[a-z0-9]+:[a-z0-9])`.
517-
Tools that define their own directives should use the form
518-
`//toolname:directive`.
519-
520501
Gofmt removes leading and trailing blank lines in doc comments.
521502
If all lines in a doc comment begin with the same sequence of
522503
spaces and tabs, gofmt removes that prefix.
@@ -871,6 +852,40 @@ replacing any other indentation the non-blank lines have in common.
871852
Gofmt also inserts a blank line before and after each code block,
872853
distinguishing the code block clearly from the surrounding paragraph text.
873854

855+
### Directives {#directives}
856+
857+
Directive comments such as `//go:generate` are not
858+
considered part of a doc comment and are omitted from
859+
rendered documentation.
860+
Gofmt moves directive comments to the end of the doc comment,
861+
preceded by a blank line.
862+
For example:
863+
864+
package regexp
865+
866+
// An Op is a single regular expression operator.
867+
//
868+
//go:generate stringer -type Op -trimprefix Op
869+
type Op uint8
870+
871+
A directive comment is a line starting with the regular expression
872+
`//(line |extern |export |[a-z0-9]+:[a-z0-9])`.
873+
874+
Tools may define their own directive comments using the form
875+
`//toolname:directive arguments`.
876+
Tool directives match the regular expression
877+
`//([a-z0-9]+):([a-z0-9]\PZ*)($|\pZ+)(.*)`, where the first group
878+
is the tool name and the second group is the directive name.
879+
Optional arguments are separated from the directive name by
880+
one or more Unicode whitespace characters.
881+
Each tool may define its own argument syntax, but a common convention is a
882+
sequence of space-separated arguments, where an argument may be
883+
a bare word, or a double-quoted or backtick-quoted Go string.
884+
The tool name `go` is reserved for use by the Go toolchain.
885+
886+
The [`go/ast.ParseDirective`](/pkg/go/ast#ParseDirective) function and its
887+
related types parse the tool directive syntax.
888+
874889
## Common mistakes and pitfalls {#mistakes}
875890

876891
The rule that any span of indented or blank lines

0 commit comments

Comments
 (0)