Skip to content

Commit 96a07bb

Browse files
h9jianggopherbot
authored andcommitted
gopls/internal/settings: include deprecation message in api-json
- gopls api-json will return deprecation message as additional property of a configuration. - go generate ./... will parse the comment of a given field as doc(entire doc comment) and deprecation message(introduced by prefix "Deprecated: "). Follow pattern defined in https://go.dev/wiki/Deprecated. VSCode-Go side CL 643056. For golang/vscode-go#3632 Change-Id: Ia6a67948c75dd51b5cb76bbd6d7385b95ea979e4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642998 Auto-Submit: Hongxiang Jiang <hxjiang@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com>
1 parent df4e4ef commit 96a07bb

File tree

9 files changed

+160
-91
lines changed

9 files changed

+160
-91
lines changed

gopls/doc/generate/generate.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
"golang.org/x/tools/gopls/internal/mod"
4545
"golang.org/x/tools/gopls/internal/settings"
4646
"golang.org/x/tools/gopls/internal/util/safetoken"
47+
internalastutil "golang.org/x/tools/internal/astutil"
4748
)
4849

4950
func main() {
@@ -221,11 +222,13 @@ func loadOptions(category reflect.Value, optsType types.Object, pkg *packages.Pa
221222
if len(path) < 2 {
222223
return nil, fmt.Errorf("could not find AST node for field %v", typesField)
223224
}
225+
224226
// The AST field gives us the doc.
225227
astField, ok := path[1].(*ast.Field)
226228
if !ok {
227229
return nil, fmt.Errorf("unexpected AST path %v", path)
228230
}
231+
description, deprecation := astField.Doc.Text(), internalastutil.Deprecation(astField.Doc)
229232

230233
// The reflect field gives us the default value.
231234
reflectField := category.FieldByName(typesField.Name())
@@ -285,14 +288,15 @@ func loadOptions(category reflect.Value, optsType types.Object, pkg *packages.Pa
285288
status := reflectStructField.Tag.Get("status")
286289

287290
opts = append(opts, &doc.Option{
288-
Name: name,
289-
Type: typ,
290-
Doc: lowerFirst(astField.Doc.Text()),
291-
Default: def,
292-
EnumKeys: enumKeys,
293-
EnumValues: enums[typesField.Type()],
294-
Status: status,
295-
Hierarchy: hierarchy,
291+
Name: name,
292+
Type: typ,
293+
Doc: lowerFirst(description),
294+
Default: def,
295+
EnumKeys: enumKeys,
296+
EnumValues: enums[typesField.Type()],
297+
Status: status,
298+
Hierarchy: hierarchy,
299+
DeprecationMessage: lowerFirst(strings.TrimPrefix(deprecation, "Deprecated: ")),
296300
})
297301
}
298302
return opts, nil

gopls/doc/settings.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ Default: `false`.
208208

209209
noSemanticString turns off the sending of the semantic token 'string'
210210

211+
Deprecated: Use SemanticTokenTypes["string"] = false instead. See
212+
golang/vscode-go#3632
213+
211214
Default: `false`.
212215

213216
<a id='noSemanticNumber'></a>
@@ -217,6 +220,9 @@ Default: `false`.
217220

218221
noSemanticNumber turns off the sending of the semantic token 'number'
219222

223+
Deprecated: Use SemanticTokenTypes["number"] = false instead. See
224+
golang/vscode-go#3632.
225+
220226
Default: `false`.
221227

222228
<a id='semanticTokenTypes'></a>

gopls/internal/analysis/deprecated/deprecated.go

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"golang.org/x/tools/go/analysis/passes/inspect"
2020
"golang.org/x/tools/go/ast/inspector"
2121
"golang.org/x/tools/internal/analysisinternal"
22+
internalastutil "golang.org/x/tools/internal/astutil"
2223
)
2324

2425
//go:embed doc.go
@@ -155,26 +156,8 @@ type deprecatedNames struct {
155156
// them both as Facts and the return value. This is a simplified copy
156157
// of staticcheck's fact_deprecated analyzer.
157158
func collectDeprecatedNames(pass *analysis.Pass, ins *inspector.Inspector) (deprecatedNames, error) {
158-
extractDeprecatedMessage := func(docs []*ast.CommentGroup) string {
159-
for _, doc := range docs {
160-
if doc == nil {
161-
continue
162-
}
163-
parts := strings.Split(doc.Text(), "\n\n")
164-
for _, part := range parts {
165-
if !strings.HasPrefix(part, "Deprecated: ") {
166-
continue
167-
}
168-
alt := part[len("Deprecated: "):]
169-
alt = strings.Replace(alt, "\n", " ", -1)
170-
return strings.TrimSpace(alt)
171-
}
172-
}
173-
return ""
174-
}
175-
176159
doDocs := func(names []*ast.Ident, docs *ast.CommentGroup) {
177-
alt := extractDeprecatedMessage([]*ast.CommentGroup{docs})
160+
alt := strings.TrimPrefix(internalastutil.Deprecation(docs), "Deprecated: ")
178161
if alt == "" {
179162
return
180163
}
@@ -185,19 +168,21 @@ func collectDeprecatedNames(pass *analysis.Pass, ins *inspector.Inspector) (depr
185168
}
186169
}
187170

188-
var docs []*ast.CommentGroup
189-
for _, f := range pass.Files {
190-
docs = append(docs, f.Doc)
191-
}
192-
if alt := extractDeprecatedMessage(docs); alt != "" {
193-
// Don't mark package syscall as deprecated, even though
194-
// it is. A lot of people still use it for simple
195-
// constants like SIGKILL, and I am not comfortable
196-
// telling them to use x/sys for that.
197-
if pass.Pkg.Path() != "syscall" {
198-
pass.ExportPackageFact(&deprecationFact{alt})
171+
// Is package deprecated?
172+
//
173+
// Don't mark package syscall as deprecated, even though
174+
// it is. A lot of people still use it for simple
175+
// constants like SIGKILL, and I am not comfortable
176+
// telling them to use x/sys for that.
177+
if pass.Pkg.Path() != "syscall" {
178+
for _, f := range pass.Files {
179+
if depr := internalastutil.Deprecation(f.Doc); depr != "" {
180+
pass.ExportPackageFact(&deprecationFact{depr})
181+
break
182+
}
199183
}
200184
}
185+
201186
nodeFilter := []ast.Node{
202187
(*ast.GenDecl)(nil),
203188
(*ast.FuncDecl)(nil),

gopls/internal/doc/api.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ type API struct {
2727
}
2828

2929
type Option struct {
30-
Name string
31-
Type string // T = bool | string | int | enum | any | []T | map[T]T | time.Duration
32-
Doc string
33-
EnumKeys EnumKeys
34-
EnumValues []EnumValue
35-
Default string
36-
Status string
37-
Hierarchy string
30+
Name string
31+
Type string // T = bool | string | int | enum | any | []T | map[T]T | time.Duration
32+
Doc string
33+
EnumKeys EnumKeys
34+
EnumValues []EnumValue
35+
Default string
36+
Status string
37+
Hierarchy string
38+
DeprecationMessage string
3839
}
3940

4041
type EnumKeys struct {

0 commit comments

Comments
 (0)