From a3bc800455d52a1945fbaf123e8a0f9d337c235b Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Wed, 16 Oct 2019 17:02:03 -0400 Subject: [PATCH] go/expect: support markers in comments in go/packagestest This is specifically necessary to test CL 197879. Change-Id: I2b4bbdd322d52097fc1444242d3e26a3d8ea75e7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/201520 Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot Reviewed-by: Ian Cottrell --- go/expect/expect_test.go | 7 ++++++- go/expect/extract.go | 18 +++++++++++++++++- go/expect/testdata/test.go | 2 ++ internal/lsp/testdata/bad/bad1.go | 4 ---- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/go/expect/expect_test.go b/go/expect/expect_test.go index 0d1a8fb33c..b09737efdc 100644 --- a/go/expect/expect_test.go +++ b/go/expect/expect_test.go @@ -20,7 +20,7 @@ func TestMarker(t *testing.T) { t.Fatal(err) } - const expectNotes = 11 + const expectNotes = 12 expectMarkers := map[string]string{ "αSimpleMarker": "α", "OffsetMarker": "β", @@ -30,6 +30,7 @@ func TestMarker(t *testing.T) { "ηBlockMarker": "η", "Declared": "η", "Comment": "ι", + "LineComment": "someFunc", "NonIdentifier": "+", } expectChecks := map[string][]interface{}{ @@ -129,6 +130,10 @@ func checkMarker(t *testing.T, fset *token.FileSet, readFile expect.ReadFile, ma if start != expectStart { t.Errorf("%v: Expected %v got %v", fset.Position(pos), fset.Position(expectStart), fset.Position(start)) } + // Don't check the end for the LineComment test. + if name == "LineComment" { + return + } if expectEnd, ok := markers[name+"@"]; ok && end != expectEnd { t.Errorf("%v: Expected end %v got %v", fset.Position(pos), fset.Position(expectEnd), fset.Position(end)) } diff --git a/go/expect/extract.go b/go/expect/extract.go index a3400d9856..249369f87a 100644 --- a/go/expect/extract.go +++ b/go/expect/extract.go @@ -59,11 +59,27 @@ func Extract(fset *token.FileSet, file *ast.File) ([]*Note, error) { text = strings.TrimSuffix(text, "*/") } text = text[2:] // remove "//" or "/*" prefix + + // Allow notes to appear within comments. + // For example: + // "// //@mark()" is valid. + // "// @mark()" is not valid. + // "// /*@mark()*/" is not valid. + var adjust int + if i := strings.Index(text, commentStart); i > 2 { + // Get the text before the commentStart. + pre := text[i-2 : i] + if pre != "//" { + continue + } + text = text[i:] + adjust = i + } if !strings.HasPrefix(text, commentStart) { continue } text = text[len(commentStart):] - parsed, err := parse(fset, c.Pos()+4, text) + parsed, err := parse(fset, token.Pos(int(c.Pos())+4+adjust), text) if err != nil { return nil, err } diff --git a/go/expect/testdata/test.go b/go/expect/testdata/test.go index da7ee82011..221cd15af3 100644 --- a/go/expect/testdata/test.go +++ b/go/expect/testdata/test.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// Package fake1 is used to test the expect package. package fake1 // The greek letters in this file mark points we use for marker tests. @@ -19,6 +20,7 @@ const ( /*Marker ι inside ι a comment*/ //@mark(Comment,"ι inside ") +// someFunc is a function. //@mark(LineComment, "someFunc") func someFunc(a, b int) int { // The line below must be the first occurrence of the plus operator return a + b + 1 //@mark(NonIdentifier, re`\+[^\+]*`) diff --git a/internal/lsp/testdata/bad/bad1.go b/internal/lsp/testdata/bad/bad1.go index 1a9988c41c..16e6c599ed 100644 --- a/internal/lsp/testdata/bad/bad1.go +++ b/internal/lsp/testdata/bad/bad1.go @@ -2,10 +2,6 @@ package bad -// import ( -// "github.com/bob/pkg" //@diag("\"github.com/bob/pkg\"", "LSP", "unable to import "\"github.com/bob/pkg\"") -// ) - var a unknown //@item(global_a, "a", "unknown", "var"),diag("unknown", "LSP", "undeclared name: unknown") func random() int { //@item(random, "random", "func() int", "func")