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
|
// Copyright 2020 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package export
import (
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
)
// ExtractDoc collects documentation strings for a field.
//
// Comments are attached to a field with a field shorthand belong to the
// child node. So in the following the comment is attached to field bar.
//
// // comment
// foo: bar: 2
func ExtractDoc(v *adt.Vertex) (docs []*ast.CommentGroup) {
return extractDocs(v)
}
func extractDocs(v *adt.Vertex) (docs []*ast.CommentGroup) {
fields := []*ast.Field{}
// Collect docs directly related to this Vertex.
v.VisitLeafConjuncts(func(x adt.Conjunct) bool {
// TODO: Is this still being used?
if v, ok := x.Elem().(*adt.Vertex); ok {
docs = append(docs, extractDocs(v)...)
return true
}
switch f := x.Field().Source().(type) {
case *ast.Field:
if hasShorthandValue(f) {
return true
}
fields = append(fields, f)
for _, cg := range f.Comments() {
if !containsDoc(docs, cg) && cg.Doc {
docs = append(docs, cg)
}
}
case *ast.File:
fdocs, _ := internal.FileComments(f)
docs = append(docs, fdocs...)
}
return true
})
// Collect docs from parent scopes in collapsed fields.
for p := v.Parent; p != nil; p = p.Parent {
newFields := []*ast.Field{}
p.VisitLeafConjuncts(func(x adt.Conjunct) bool {
f, ok := x.Source().(*ast.Field)
if !ok || !hasShorthandValue(f) {
return true
}
nested := nestedField(f)
for _, child := range fields {
if nested == child {
newFields = append(newFields, f)
for _, cg := range f.Comments() {
if !containsDoc(docs, cg) && cg.Doc {
docs = append(docs, cg)
}
}
}
}
return true
})
fields = newFields
}
return docs
}
// hasShorthandValue reports whether this field has a struct value that will
// be rendered as a shorthand, for instance:
//
// f: g: 2
func hasShorthandValue(f *ast.Field) bool {
if f = nestedField(f); f == nil {
return false
}
// Not a regular field, but shorthand field.
// TODO: Should we return here? For now mimic old implementation.
if _, _, err := ast.LabelName(f.Label); err != nil {
return false
}
return true
}
// nestedField returns the child field of a field shorthand.
func nestedField(f *ast.Field) *ast.Field {
s, _ := f.Value.(*ast.StructLit)
if s == nil ||
len(s.Elts) != 1 ||
s.Lbrace != token.NoPos ||
s.Rbrace != token.NoPos {
return nil
}
f, _ = s.Elts[0].(*ast.Field)
return f
}
func containsDoc(a []*ast.CommentGroup, cg *ast.CommentGroup) bool {
for _, c := range a {
if c == cg {
return true
}
}
for _, c := range a {
if c.Text() == cg.Text() {
return true
}
}
return false
}
func ExtractFieldAttrs(v *adt.Vertex) (attrs []*ast.Attribute) {
v.VisitLeafConjuncts(func(x adt.Conjunct) bool {
attrs = extractFieldAttrs(attrs, x.Field())
return true
})
return attrs
}
// extractFieldAttrs extracts the fields from n and appends unique entries to
// attrs.
//
// The value of n should be obtained from the Conjunct.Field method if the
// source for n is a Conjunct so that Comprehensions are properly unwrapped.
func extractFieldAttrs(attrs []*ast.Attribute, n adt.Node) []*ast.Attribute {
if f, ok := n.Source().(*ast.Field); ok {
for _, a := range f.Attrs {
if !containsAttr(attrs, a) {
attrs = append(attrs, a)
}
}
}
return attrs
}
func ExtractDeclAttrs(v *adt.Vertex) (attrs []*ast.Attribute) {
for _, st := range v.Structs {
if src := st.StructLit; src != nil {
attrs = extractDeclAttrs(attrs, src.Src)
}
}
return attrs
}
func extractDeclAttrs(attrs []*ast.Attribute, n ast.Node) []*ast.Attribute {
switch x := n.(type) {
case nil:
case *ast.File:
attrs = appendDeclAttrs(attrs, x.Decls[len(x.Preamble()):])
case *ast.StructLit:
attrs = appendDeclAttrs(attrs, x.Elts)
}
return attrs
}
func appendDeclAttrs(a []*ast.Attribute, decls []ast.Decl) []*ast.Attribute {
for _, d := range decls {
if attr, ok := d.(*ast.Attribute); ok && !containsAttr(a, attr) {
a = append(a, attr)
}
}
return a
}
func containsAttr(a []*ast.Attribute, x *ast.Attribute) bool {
for _, e := range a {
if e.Text == x.Text {
return true
}
}
return false
}
|