Skip to content

Commit

Permalink
cue/...: remove remnants of /*block*/ comment support
Browse files Browse the repository at this point in the history
https://cue-review.googlesource.com/c/cue/+/3983 and later
https//cuelang.org/cl/547012 meant to fully remove support for
block comments as explained in https://cuelang.org/issue/87,
but the documentation and bits of code and tests were left behind.

Note that tools/fix rewriting block comments as line comments
has been untested and broken since the two CLs above in 2019.
Given the amount of time that has passed, remove the fix code as well.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I8ade2b2461c665de1fd54d09e11cbfbf4a1d03a4
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1196133
Reviewed-by: Aram Hăvărneanu <aram@cue.works>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
mvdan committed Jun 14, 2024
1 parent 1d70939 commit 0cf2060
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 86 deletions.
21 changes: 7 additions & 14 deletions cue/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ func (c *comments) SetComments(cgs []*CommentGroup) {
*c.groups = cgs
}

// A Comment node represents a single //-style or /*-style comment.
// A Comment node represents a single //-style comment.
type Comment struct {
Slash token.Pos // position of "/" starting the comment
Text string // comment text (excluding '\n' for //-style comments)
Text string // comment text excluding '\n'
}

func (c *Comment) Comments() []*CommentGroup { return nil }
Expand Down Expand Up @@ -226,7 +226,7 @@ func stripTrailingWhitespace(s string) string {
}

// Text returns the text of the comment.
// Comment markers (//, /*, and */), the first space of a line comment, and
// Comment markers ("//"), the first space of a line comment, and
// leading and trailing empty lines are removed. Multiple empty lines are
// reduced to one, and trailing space on lines is trimmed. Unless the result
// is empty, it is newline-terminated.
Expand All @@ -243,17 +243,10 @@ func (g *CommentGroup) Text() string {
for _, c := range comments {
// Remove comment markers.
// The parser has given us exactly the comment text.
switch c[1] {
case '/':
//-style comment (no newline at the end)
c = c[2:]
// strip first space - required for Example tests
if len(c) > 0 && c[0] == ' ' {
c = c[1:]
}
case '*':
/*-style comment */
c = c[2 : len(c)-2]
c = c[2:]
// strip first space - required for Example tests
if len(c) > 0 && c[0] == ' ' {
c = c[1:]
}

// Split on newlines.
Expand Down
12 changes: 0 additions & 12 deletions cue/ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,7 @@ func TestCommentText(t *testing.T) {
{[]string{"// foo bar "}, "foo bar\n"},
{[]string{"// foo", "// bar"}, "foo\nbar\n"},
{[]string{"// foo", "//", "//", "//", "// bar"}, "foo\n\nbar\n"},
{[]string{"// foo", "/* bar */"}, "foo\n bar\n"},
{[]string{"//", "//", "//", "// foo", "//", "//", "//"}, "foo\n"},

{[]string{"/**/"}, ""},
{[]string{"/* */"}, ""},
{[]string{"/**/", "/**/", "/* */"}, ""},
{[]string{"/* Foo */"}, " Foo\n"},
{[]string{"/* Foo Bar */"}, " Foo Bar\n"},
{[]string{"/* Foo*/", "/* Bar*/"}, " Foo\n Bar\n"},
{[]string{"/* Foo*/", "/**/", "/**/", "/**/", "// Bar"}, " Foo\n\nBar\n"},
{[]string{"/* Foo*/", "/*\n*/", "//", "/*\n*/", "// Bar"}, " Foo\n\nBar\n"},
{[]string{"/* Foo*/", "// Bar"}, " Foo\nBar\n"},
{[]string{"/* Foo\n Bar*/"}, " Foo\n Bar\n"},
}

for i, c := range testCases {
Expand Down
18 changes: 5 additions & 13 deletions cue/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package format
import (
"bytes"
"fmt"
"strings"
"text/tabwriter"

"cuelang.org/go/cue/ast"
Expand Down Expand Up @@ -320,22 +319,15 @@ func (f *formatter) printComment(cg *ast.CommentGroup) {
printBlank = true
}
for _, c := range cg.List {
isEnd := strings.HasPrefix(c.Text, "//")
if !printBlank {
if isEnd {
f.Print(vtab)
} else {
f.Print(blank)
}
f.Print(vtab)
}
f.Print(c.Slash)
f.Print(c)
if isEnd {
f.printingComment = true
f.Print(newline)
if cg.Doc {
f.Print(nooverride)
}
f.printingComment = true
f.Print(newline)
if cg.Doc {
f.Print(nooverride)
}
}
}
12 changes: 0 additions & 12 deletions cue/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,19 +274,7 @@ func (p *parser) next0() {

// Consume a comment and return it and the line on which it ends.
func (p *parser) consumeComment() (comment *ast.Comment, endline int) {
// /*-style comments may end on a different line than where they start.
// Scan the comment for '\n' chars and adjust endline accordingly.
endline = p.file.Line(p.pos)
if p.lit[1] == '*' {
p.assertV0(p.pos, 0, 10, "block quotes")

// don't use range here - no need to decode Unicode code points
for i := 0; i < len(p.lit); i++ {
if p.lit[i] == '\n' {
endline++
}
}
}

comment = &ast.Comment{Slash: p.pos, Text: p.lit}
p.next0()
Expand Down
35 changes: 0 additions & 35 deletions tools/fix/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
package fix

import (
"strings"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/ast/astutil"
"cuelang.org/go/cue/token"
Expand Down Expand Up @@ -64,39 +62,6 @@ func File(f *ast.File, o ...Option) *ast.File {
return true
}, nil).(*ast.File)

// Rewrite block comments to regular comments.
ast.Walk(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.CommentGroup:
comments := []*ast.Comment{}
for _, c := range x.List {
s := c.Text
if !strings.HasPrefix(s, "/*") || !strings.HasSuffix(s, "*/") {
comments = append(comments, c)
continue
}
if x.Position > 0 {
// Moving to the end doesn't work, as it still
// may inject at a false line break position.
x.Position = 0
x.Doc = true
}
s = strings.TrimSpace(s[2 : len(s)-2])
for _, s := range strings.Split(s, "\n") {
for i := 0; i < 3; i++ {
if strings.HasPrefix(s, " ") || strings.HasPrefix(s, "*") {
s = s[1:]
}
}
comments = append(comments, &ast.Comment{Text: "// " + s})
}
}
x.List = comments
return false
}
return true
}, nil)

// TODO: we are probably reintroducing slices. Disable for now.
//
// Rewrite slice expression.
Expand Down

0 comments on commit 0cf2060

Please sign in to comment.