|
| 1 | +// Copyright 2019 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package source |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "go/types" |
| 11 | + |
| 12 | + "golang.org/x/tools/go/ast/astutil" |
| 13 | + "golang.org/x/tools/internal/lsp/protocol" |
| 14 | + "golang.org/x/tools/internal/lsp/snippet" |
| 15 | +) |
| 16 | + |
| 17 | +func typeDefaultValues(field *types.Var, fieldType types.Type, qff types.Qualifier) string { |
| 18 | + var edit string |
| 19 | + switch typ := fieldType.(type) { |
| 20 | + case *types.Basic: |
| 21 | + switch typ.Info() { |
| 22 | + case types.IsInteger, types.IsUnsigned: |
| 23 | + edit = "0" |
| 24 | + case types.IsBoolean: |
| 25 | + edit = "false" |
| 26 | + case types.IsFloat: |
| 27 | + edit = "0.0" |
| 28 | + case types.IsString: |
| 29 | + edit = `""` |
| 30 | + } |
| 31 | + case *types.Named: |
| 32 | + switch underlying := typ.Underlying().(type) { |
| 33 | + |
| 34 | + // To prevent such cases where |
| 35 | + // type NameA struct{} |
| 36 | + // type NameB NameA |
| 37 | + // type NameC NameB |
| 38 | + // we just return the field's typename. |
| 39 | + case *types.Struct: |
| 40 | + edit = types.TypeString(field.Type(), qff) + "{}" |
| 41 | + default: |
| 42 | + // get the base type of a named type recursively. |
| 43 | + edit = typeDefaultValues(field, underlying, qff) |
| 44 | + } |
| 45 | + default: |
| 46 | + edit = "nil" |
| 47 | + } |
| 48 | + return edit |
| 49 | +} |
| 50 | + |
| 51 | +func StructFill(ctx context.Context, params *protocol.CodeActionParams, snapshot Snapshot, fh FileHandle, codeActions []protocol.CodeAction) ([]protocol.CodeAction, error) { |
| 52 | + rangeOfTarget := params.Range |
| 53 | + |
| 54 | + pkg, pgh, err := getParsedFile(ctx, snapshot, fh, NarrowestPackageHandle) |
| 55 | + if err != nil { |
| 56 | + return codeActions, fmt.Errorf("getting file for Completion: %v", err) |
| 57 | + } |
| 58 | + file, _, m, _, err := pgh.Cached() |
| 59 | + if err != nil { |
| 60 | + return codeActions, err |
| 61 | + } |
| 62 | + spn, err := m.PointSpan(rangeOfTarget.Start) |
| 63 | + if err != nil { |
| 64 | + return codeActions, err |
| 65 | + } |
| 66 | + rng, err := spn.Range(m.Converter) |
| 67 | + if err != nil { |
| 68 | + return codeActions, err |
| 69 | + } |
| 70 | + path, _ := astutil.PathEnclosingInterval(file, rng.Start, rng.End) |
| 71 | + if path == nil { |
| 72 | + return codeActions, nil |
| 73 | + } |
| 74 | + |
| 75 | + ecl := enclosingCompositeLiteral(path, rng.Start, pkg.GetTypesInfo()) |
| 76 | + if ecl == nil || !ecl.isStruct() { |
| 77 | + return codeActions, nil |
| 78 | + } |
| 79 | + |
| 80 | + // If in F{ Bar<> : V} or anywhere in F{Bar : V, ...} |
| 81 | + // we should not fill the struct. |
| 82 | + if ecl.inKey || len(ecl.cl.Elts) != 0 { |
| 83 | + return codeActions, nil |
| 84 | + } |
| 85 | + |
| 86 | + // the range to replace all text between enclosing braces {<> ... <>} |
| 87 | + newMappedRange := newMappedRange(snapshot.View().Session().Cache().FileSet(), m, ecl.cl.Lbrace+1, ecl.cl.Rbrace) |
| 88 | + newProtoRange, err := newMappedRange.Range() |
| 89 | + if err != nil { |
| 90 | + return codeActions, err |
| 91 | + } |
| 92 | + qfFunc := qualifier(file, pkg.GetTypes(), pkg.GetTypesInfo()) |
| 93 | + switch obj := ecl.clType.(type) { |
| 94 | + case *types.Struct: |
| 95 | + |
| 96 | + fieldCount := obj.NumFields() |
| 97 | + if fieldCount == 0 { |
| 98 | + return codeActions, nil |
| 99 | + } |
| 100 | + var edit, insert string |
| 101 | + for i := 0; i < fieldCount; i++ { |
| 102 | + field := obj.Field(i) |
| 103 | + if !IsObjectExported(field, pkg) { |
| 104 | + continue |
| 105 | + } |
| 106 | + |
| 107 | + // Code action does not support placeholders (?) yet |
| 108 | + _, insert = generateCompletionForStructFields(&snippet.Builder{}, field, qfFunc, false) |
| 109 | + edit += insert |
| 110 | + } |
| 111 | + edit += "\n" |
| 112 | + codeActions = append(codeActions, protocol.CodeAction{ |
| 113 | + Title: "Fill struct", |
| 114 | + Kind: protocol.RefactorRewrite, |
| 115 | + Edit: protocol.WorkspaceEdit{ |
| 116 | + DocumentChanges: []protocol.TextDocumentEdit{ |
| 117 | + { |
| 118 | + TextDocument: protocol.VersionedTextDocumentIdentifier{ |
| 119 | + Version: fh.Identity().Version, |
| 120 | + TextDocumentIdentifier: protocol.TextDocumentIdentifier{ |
| 121 | + URI: protocol.URIFromSpanURI(fh.Identity().URI), |
| 122 | + }, |
| 123 | + }, |
| 124 | + Edits: []protocol.TextEdit{ |
| 125 | + { |
| 126 | + Range: newProtoRange, |
| 127 | + NewText: edit, |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }) |
| 134 | + } |
| 135 | + |
| 136 | + return codeActions, nil |
| 137 | +} |
0 commit comments