-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathinlay_hint.go
340 lines (315 loc) · 9.33 KB
/
inlay_hint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package golang
import (
"context"
"fmt"
"go/ast"
"go/constant"
"go/token"
"go/types"
"strings"
"golang.org/x/tools/gopls/internal/cache"
"golang.org/x/tools/gopls/internal/cache/parsego"
"golang.org/x/tools/gopls/internal/file"
"golang.org/x/tools/gopls/internal/protocol"
"golang.org/x/tools/gopls/internal/settings"
"golang.org/x/tools/internal/astutil/cursor"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/typesinternal"
)
func InlayHint(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pRng protocol.Range) ([]protocol.InlayHint, error) {
ctx, done := event.Start(ctx, "golang.InlayHint")
defer done()
pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, fh.URI())
if err != nil {
return nil, fmt.Errorf("getting file for InlayHint: %w", err)
}
// Collect a list of the inlay hints that are enabled.
inlayHintOptions := snapshot.Options().InlayHintOptions
var enabledHints []inlayHintFunc
for hint, enabled := range inlayHintOptions.Hints {
if !enabled {
continue
}
if fn, ok := allInlayHints[hint]; ok {
enabledHints = append(enabledHints, fn)
}
}
if len(enabledHints) == 0 {
return nil, nil
}
info := pkg.TypesInfo()
qual := typesinternal.FileQualifier(pgf.File, pkg.Types())
// Set the range to the full file if the range is not valid.
start, end := pgf.File.FileStart, pgf.File.FileEnd
// TODO(adonovan): this condition looks completely wrong!
if pRng.Start.Line < pRng.End.Line || pRng.Start.Character < pRng.End.Character {
// Adjust start and end for the specified range.
var err error
start, end, err = pgf.RangePos(pRng)
if err != nil {
return nil, err
}
}
var hints []protocol.InlayHint
if curSubrange, ok := pgf.Cursor.FindByPos(start, end); ok {
add := func(hint protocol.InlayHint) { hints = append(hints, hint) }
for _, fn := range enabledHints {
fn(info, pgf, qual, curSubrange, add)
}
}
return hints, nil
}
type inlayHintFunc func(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint))
var allInlayHints = map[settings.InlayHint]inlayHintFunc{
settings.AssignVariableTypes: assignVariableTypes,
settings.ConstantValues: constantValues,
settings.ParameterNames: parameterNames,
settings.RangeVariableTypes: rangeVariableTypes,
settings.CompositeLiteralTypes: compositeLiteralTypes,
settings.CompositeLiteralFieldNames: compositeLiteralFields,
settings.FunctionTypeParameters: funcTypeParams,
}
func parameterNames(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) {
for curCall := range cur.Preorder((*ast.CallExpr)(nil)) {
callExpr := curCall.Node().(*ast.CallExpr)
t := info.TypeOf(callExpr.Fun)
if t == nil {
continue
}
signature, ok := typeparams.CoreType(t).(*types.Signature)
if !ok {
continue
}
for i, v := range callExpr.Args {
start, err := pgf.PosPosition(v.Pos())
if err != nil {
continue
}
params := signature.Params()
// When a function has variadic params, we skip args after
// params.Len().
if i > params.Len()-1 {
break
}
param := params.At(i)
// param.Name is empty for built-ins like append
if param.Name() == "" {
continue
}
// Skip the parameter name hint if the arg matches
// the parameter name.
if i, ok := v.(*ast.Ident); ok && i.Name == param.Name() {
continue
}
label := param.Name()
if signature.Variadic() && i == params.Len()-1 {
label = label + "..."
}
add(protocol.InlayHint{
Position: start,
Label: buildLabel(label + ":"),
Kind: protocol.Parameter,
PaddingRight: true,
})
}
}
}
func funcTypeParams(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) {
for curCall := range cur.Preorder((*ast.CallExpr)(nil)) {
call := curCall.Node().(*ast.CallExpr)
id, ok := call.Fun.(*ast.Ident)
if !ok {
continue
}
inst := info.Instances[id]
if inst.TypeArgs == nil {
continue
}
start, err := pgf.PosPosition(id.End())
if err != nil {
continue
}
var args []string
for i := 0; i < inst.TypeArgs.Len(); i++ {
args = append(args, inst.TypeArgs.At(i).String())
}
if len(args) == 0 {
continue
}
add(protocol.InlayHint{
Position: start,
Label: buildLabel("[" + strings.Join(args, ", ") + "]"),
Kind: protocol.Type,
})
}
}
func assignVariableTypes(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) {
for curAssign := range cur.Preorder((*ast.AssignStmt)(nil)) {
stmt := curAssign.Node().(*ast.AssignStmt)
if stmt.Tok != token.DEFINE {
continue
}
for _, v := range stmt.Lhs {
variableType(info, pgf, qual, v, add)
}
}
}
func rangeVariableTypes(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) {
for curRange := range cur.Preorder((*ast.RangeStmt)(nil)) {
rStmt := curRange.Node().(*ast.RangeStmt)
variableType(info, pgf, qual, rStmt.Key, add)
variableType(info, pgf, qual, rStmt.Value, add)
}
}
func variableType(info *types.Info, pgf *parsego.File, qual types.Qualifier, e ast.Expr, add func(protocol.InlayHint)) {
typ := info.TypeOf(e)
if typ == nil {
return
}
end, err := pgf.PosPosition(e.End())
if err != nil {
return
}
add(protocol.InlayHint{
Position: end,
Label: buildLabel(types.TypeString(typ, qual)),
Kind: protocol.Type,
PaddingLeft: true,
})
}
func constantValues(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) {
for curDecl := range cur.Preorder((*ast.GenDecl)(nil)) {
genDecl := curDecl.Node().(*ast.GenDecl)
if genDecl.Tok != token.CONST {
continue
}
for _, v := range genDecl.Specs {
spec, ok := v.(*ast.ValueSpec)
if !ok {
continue
}
end, err := pgf.PosPosition(v.End())
if err != nil {
continue
}
// Show hints when values are missing or at least one value is not
// a basic literal.
showHints := len(spec.Values) == 0
checkValues := len(spec.Names) == len(spec.Values)
var values []string
for i, w := range spec.Names {
obj, ok := info.ObjectOf(w).(*types.Const)
if !ok || obj.Val().Kind() == constant.Unknown {
continue
}
if checkValues {
switch spec.Values[i].(type) {
case *ast.BadExpr:
continue
case *ast.BasicLit:
default:
if obj.Val().Kind() != constant.Bool {
showHints = true
}
}
}
values = append(values, fmt.Sprintf("%v", obj.Val()))
}
if !showHints || len(values) == 0 {
continue
}
add(protocol.InlayHint{
Position: end,
Label: buildLabel("= " + strings.Join(values, ", ")),
PaddingLeft: true,
})
}
}
}
func compositeLiteralFields(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) {
for curCompLit := range cur.Preorder((*ast.CompositeLit)(nil)) {
compLit, ok := curCompLit.Node().(*ast.CompositeLit)
if !ok {
continue
}
typ := info.TypeOf(compLit)
if typ == nil {
continue
}
typ = typesinternal.Unpointer(typ)
strct, ok := typeparams.CoreType(typ).(*types.Struct)
if !ok {
continue
}
var hints []protocol.InlayHint
var allEdits []protocol.TextEdit
for i, v := range compLit.Elts {
if _, ok := v.(*ast.KeyValueExpr); !ok {
start, err := pgf.PosPosition(v.Pos())
if err != nil {
continue
}
if i > strct.NumFields()-1 {
break
}
hints = append(hints, protocol.InlayHint{
Position: start,
Label: buildLabel(strct.Field(i).Name() + ":"),
Kind: protocol.Parameter,
PaddingRight: true,
})
allEdits = append(allEdits, protocol.TextEdit{
Range: protocol.Range{Start: start, End: start},
NewText: strct.Field(i).Name() + ": ",
})
}
}
// It is not allowed to have a mix of keyed and unkeyed fields, so
// have the text edits add keys to all fields.
for i := range hints {
hints[i].TextEdits = allEdits
add(hints[i])
}
}
}
func compositeLiteralTypes(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) {
for curCompLit := range cur.Preorder((*ast.CompositeLit)(nil)) {
compLit := curCompLit.Node().(*ast.CompositeLit)
typ := info.TypeOf(compLit)
if typ == nil {
continue
}
if compLit.Type != nil {
continue
}
prefix := ""
if t, ok := typeparams.CoreType(typ).(*types.Pointer); ok {
typ = t.Elem()
prefix = "&"
}
// The type for this composite literal is implicit, add an inlay hint.
start, err := pgf.PosPosition(compLit.Lbrace)
if err != nil {
continue
}
add(protocol.InlayHint{
Position: start,
Label: buildLabel(fmt.Sprintf("%s%s", prefix, types.TypeString(typ, qual))),
Kind: protocol.Type,
})
}
}
func buildLabel(s string) []protocol.InlayHintLabelPart {
const maxLabelLength = 28
label := protocol.InlayHintLabelPart{
Value: s,
}
if len(s) > maxLabelLength+len("...") {
label.Value = s[:maxLabelLength] + "..."
}
return []protocol.InlayHintLabelPart{label}
}