| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package golang |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"go/ast" |
| 12 |
"go/doc/comment" |
| 13 |
"go/token" |
| 14 |
"go/types" |
| 15 |
"iter" |
| 16 |
pathpkg "path" |
| 17 |
"strings" |
| 18 |
|
| 19 |
"golang.org/x/tools/gopls/internal/cache" |
| 20 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 21 |
"golang.org/x/tools/gopls/internal/protocol" |
| 22 |
"golang.org/x/tools/gopls/internal/settings" |
| 23 |
"golang.org/x/tools/internal/astutil" |
| 24 |
) |
| 25 |
|
| 26 |
var errNoCommentReference = errors.New("no comment reference found") |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
func DocCommentToMarkdown(text string, options *settings.Options) string { |
| 35 |
var parser comment.Parser |
| 36 |
doc := parser.Parse(text) |
| 37 |
|
| 38 |
var printer comment.Printer |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
printer.HeadingID = func(*comment.Heading) string { return "" } |
| 44 |
printer.DocLinkURL = func(link *comment.DocLink) string { |
| 45 |
msg := fmt.Sprintf("https://%s/%s", options.LinkTarget, link.ImportPath) |
| 46 |
if link.Name != "" { |
| 47 |
msg += "#" |
| 48 |
if link.Recv != "" { |
| 49 |
msg += link.Recv + "." |
| 50 |
} |
| 51 |
msg += link.Name |
| 52 |
} |
| 53 |
return msg |
| 54 |
} |
| 55 |
|
| 56 |
return string(printer.Markdown(doc)) |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
func docLinkDefinition(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, pgf *parsego.File, start, end token.Pos) ([]protocol.Location, error) { |
| 64 |
obj, _, err := resolveDocLink(pkg, pgf, astutil.RangeOf(start, end)) |
| 65 |
if err != nil { |
| 66 |
return nil, err |
| 67 |
} |
| 68 |
loc, err := ObjectLocation(ctx, pkg.FileSet(), snapshot, obj) |
| 69 |
if err != nil { |
| 70 |
return nil, err |
| 71 |
} |
| 72 |
return []protocol.Location{loc}, nil |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
func resolveDocLink(pkg *cache.Package, pgf *parsego.File, rng astutil.Range) (types.Object, protocol.Range, error) { |
| 78 |
var comment *ast.CommentGroup |
| 79 |
for _, c := range pgf.File.Comments { |
| 80 |
if astutil.NodeContains(c, rng) { |
| 81 |
comment = c |
| 82 |
break |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if comment == nil { |
| 87 |
return nil, protocol.Range{}, errNoCommentReference |
| 88 |
} |
| 89 |
|
| 90 |
for docLink := range commentDocLinks(comment) { |
| 91 |
if astutil.NodeContains(docLink.partRange, rng) { |
| 92 |
if obj := lookupDocLinkSymbol(pkg, pgf, docLink.nameText); obj != nil { |
| 93 |
rng, err := pgf.NodeRange(docLink.partRange) |
| 94 |
if err != nil { |
| 95 |
return nil, protocol.Range{}, err |
| 96 |
} |
| 97 |
return obj, rng, nil |
| 98 |
} |
| 99 |
break |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return nil, protocol.Range{}, errNoCommentReference |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
type docLink struct { |
| 111 |
|
| 112 |
|
| 113 |
partText string |
| 114 |
partRange astutil.Range |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
nameText string |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
bracketText string |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
func commentDocLinks(cg *ast.CommentGroup) iter.Seq[docLink] { |
| 128 |
return func(yield func(docLink) bool) { |
| 129 |
for _, comment := range cg.List { |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
for _, idx := range findDocLinkIndices(comment.Text) { |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
mstart, mend := idx[2], idx[3] |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
bracketText := comment.Text[idx[0] : mend+1] |
| 146 |
|
| 147 |
match := comment.Text[mstart:mend] |
| 148 |
|
| 149 |
if strings.Contains(match, "\n") { |
| 150 |
continue |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
var name string |
| 156 |
|
| 157 |
namePos := astutil.RangeOf(comment.Pos()+token.Pos(mstart), comment.Pos()+token.Pos(mstart-1)) |
| 158 |
|
| 159 |
|
| 160 |
partPos := namePos |
| 161 |
for part := range strings.SplitSeq(match, ".") { |
| 162 |
if name != "" { |
| 163 |
name += "." |
| 164 |
} |
| 165 |
name += part |
| 166 |
|
| 167 |
partPos.Start = partPos.EndPos + token.Pos(len(".")) |
| 168 |
partPos.EndPos = partPos.Start + token.Pos(len(part)) |
| 169 |
|
| 170 |
namePos.EndPos = partPos.EndPos |
| 171 |
|
| 172 |
if !yield(docLink{ |
| 173 |
partRange: partPos, |
| 174 |
partText: part, |
| 175 |
nameText: name, |
| 176 |
bracketText: bracketText, |
| 177 |
}) { |
| 178 |
return |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
func lookupDocLinkSymbol(pkg *cache.Package, pgf *parsego.File, name string) types.Object { |
| 189 |
scope := pkg.Types().Scope() |
| 190 |
|
| 191 |
prefix, suffix, _ := strings.Cut(name, ".") |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
fileScope := pkg.TypesInfo().Scopes[pgf.File] |
| 196 |
if fileScope == nil { |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
return nil |
| 202 |
} |
| 203 |
pkgname, ok := fileScope.Lookup(prefix).(*types.PkgName) |
| 204 |
if !ok { |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
for _, imp := range pgf.File.Imports { |
| 209 |
pkgname2 := pkg.TypesInfo().PkgNameOf(imp) |
| 210 |
if pkgname2 != nil && pkgname2.Imported().Name() == prefix { |
| 211 |
pkgname = pkgname2 |
| 212 |
break |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
if pkgname != nil { |
| 217 |
scope = pkgname.Imported().Scope() |
| 218 |
if suffix == "" { |
| 219 |
return pkgname |
| 220 |
} |
| 221 |
name = suffix |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
recv, sel, ok := strings.Cut(name, ".") |
| 230 |
if ok { |
| 231 |
obj := scope.Lookup(recv) |
| 232 |
if obj == nil { |
| 233 |
obj = types.Universe.Lookup(recv) |
| 234 |
} |
| 235 |
obj, ok := obj.(*types.TypeName) |
| 236 |
if !ok { |
| 237 |
return nil |
| 238 |
} |
| 239 |
m, _, _ := types.LookupFieldOrMethod(obj.Type(), true, obj.Pkg(), sel) |
| 240 |
return m |
| 241 |
} |
| 242 |
|
| 243 |
if obj := scope.Lookup(name); obj != nil { |
| 244 |
return obj |
| 245 |
} |
| 246 |
return types.Universe.Lookup(name) |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
func newDocCommentParser(pkg *cache.Package) func(fileNode ast.Node, text string) *comment.Doc { |
| 262 |
var currentFilePos token.Pos |
| 263 |
parser := &comment.Parser{ |
| 264 |
LookupPackage: func(name string) (importPath string, ok bool) { |
| 265 |
for _, f := range pkg.Syntax() { |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
if astutil.NodeContainsPos(f, currentFilePos) { |
| 270 |
|
| 271 |
for _, imp := range f.Imports { |
| 272 |
pkgName := pkg.TypesInfo().PkgNameOf(imp) |
| 273 |
if pkgName != nil && pkgName.Name() == name { |
| 274 |
return pkgName.Imported().Path(), true |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
for _, imp := range f.Imports { |
| 284 |
pkgName := pkg.TypesInfo().PkgNameOf(imp) |
| 285 |
if pkgName != nil && pkgName.Imported().Name() == name { |
| 286 |
return pkgName.Imported().Path(), true |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
for path := range pkg.Metadata().DepsByPkgPath { |
| 300 |
if pathpkg.Base(trimVersionSuffix(string(path))) == name { |
| 301 |
return string(path), true |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
break |
| 306 |
} |
| 307 |
} |
| 308 |
return "", false |
| 309 |
}, |
| 310 |
LookupSym: func(recv, name string) (ok bool) { |
| 311 |
|
| 312 |
if recv == "" { |
| 313 |
return pkg.Types().Scope().Lookup(name) != nil |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
tname, ok := pkg.Types().Scope().Lookup(recv).(*types.TypeName) |
| 318 |
if !ok { |
| 319 |
return false |
| 320 |
} |
| 321 |
m, _, _ := types.LookupFieldOrMethod(tname.Type(), true, pkg.Types(), name) |
| 322 |
return is[*types.Func](m) |
| 323 |
}, |
| 324 |
} |
| 325 |
return func(fileNode ast.Node, text string) *comment.Doc { |
| 326 |
currentFilePos = fileNode.Pos() |
| 327 |
return parser.Parse(text) |
| 328 |
} |
| 329 |
} |