|
| 1 | +// Copyright 2025 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 goasm |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "go/token" |
| 12 | + "strings" |
| 13 | + "unicode" |
| 14 | + |
| 15 | + "golang.org/x/tools/gopls/internal/cache" |
| 16 | + "golang.org/x/tools/gopls/internal/cache/metadata" |
| 17 | + "golang.org/x/tools/gopls/internal/file" |
| 18 | + "golang.org/x/tools/gopls/internal/protocol" |
| 19 | + "golang.org/x/tools/gopls/internal/util/morestrings" |
| 20 | + "golang.org/x/tools/internal/event" |
| 21 | +) |
| 22 | + |
| 23 | +// Definition handles the textDocument/definition request for Go assembly files. |
| 24 | +func Definition(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, position protocol.Position) ([]protocol.Location, error) { |
| 25 | + ctx, done := event.Start(ctx, "goasm.Definition") |
| 26 | + defer done() |
| 27 | + |
| 28 | + mp, err := snapshot.NarrowestMetadataForFile(ctx, fh.URI()) |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + |
| 33 | + // Read the file. |
| 34 | + content, err := fh.Content() |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + mapper := protocol.NewMapper(fh.URI(), content) |
| 39 | + offset, err := mapper.PositionOffset(position) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + |
| 44 | + // Figure out the selected symbol. |
| 45 | + // For now, just find the identifier around the cursor. |
| 46 | + // |
| 47 | + // TODO(adonovan): use a real asm parser; see cmd/asm/internal/asm/parse.go. |
| 48 | + // Ideally this would just be just another attribute of the |
| 49 | + // type-checked cache.Package. |
| 50 | + nonIdentRune := func(r rune) bool { return !isIdentRune(r) } |
| 51 | + i := bytes.LastIndexFunc(content[:offset], nonIdentRune) |
| 52 | + j := bytes.IndexFunc(content[offset:], nonIdentRune) |
| 53 | + if j < 0 || j == 0 { |
| 54 | + return nil, nil // identifier runs to EOF, or not an identifier |
| 55 | + } |
| 56 | + sym := string(content[i+1 : offset+j]) |
| 57 | + sym = strings.ReplaceAll(sym, "·", ".") // (U+00B7 MIDDLE DOT) |
| 58 | + sym = strings.ReplaceAll(sym, "∕", "/") // (U+2215 DIVISION SLASH) |
| 59 | + if sym != "" && sym[0] == '.' { |
| 60 | + sym = string(mp.PkgPath) + sym |
| 61 | + } |
| 62 | + |
| 63 | + // package-qualified symbol? |
| 64 | + if pkgpath, name, ok := morestrings.CutLast(sym, "."); ok { |
| 65 | + // Find declaring package among dependencies. |
| 66 | + // |
| 67 | + // TODO(adonovan): assembly may legally reference |
| 68 | + // non-dependencies. For example, sync/atomic calls |
| 69 | + // internal/runtime/atomic. Perhaps we should search |
| 70 | + // the entire metadata graph, but that's path-dependent. |
| 71 | + var declaring *metadata.Package |
| 72 | + for pkg := range snapshot.MetadataGraph().ForwardReflexiveTransitiveClosure(mp.ID) { |
| 73 | + if pkg.PkgPath == metadata.PackagePath(pkgpath) { |
| 74 | + declaring = pkg |
| 75 | + break |
| 76 | + } |
| 77 | + } |
| 78 | + if declaring == nil { |
| 79 | + return nil, fmt.Errorf("package %q is not a dependency", pkgpath) |
| 80 | + } |
| 81 | + |
| 82 | + pkgs, err := snapshot.TypeCheck(ctx, declaring.ID) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + pkg := pkgs[0] |
| 87 | + def := pkg.Types().Scope().Lookup(name) |
| 88 | + if def == nil { |
| 89 | + return nil, fmt.Errorf("no symbol %q in package %q", name, pkgpath) |
| 90 | + } |
| 91 | + loc, err := mapPosition(ctx, pkg.FileSet(), snapshot, def.Pos(), def.Pos()) |
| 92 | + if err == nil { |
| 93 | + return []protocol.Location{loc}, nil |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // TODO(adonovan): support jump to var, block label, and other |
| 98 | + // TEXT, DATA, and GLOBAL symbols in the same file. Needs asm parser. |
| 99 | + |
| 100 | + return nil, nil |
| 101 | +} |
| 102 | + |
| 103 | +// The assembler allows center dot (· U+00B7) and |
| 104 | +// division slash (∕ U+2215) to work as identifier characters. |
| 105 | +func isIdentRune(r rune) bool { |
| 106 | + return unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' || r == '·' || r == '∕' |
| 107 | +} |
| 108 | + |
| 109 | +// TODO(rfindley): avoid the duplicate column mapping here, by associating a |
| 110 | +// column mapper with each file handle. |
| 111 | +// TODO(adonovan): plundered from ../golang; factor. |
| 112 | +func mapPosition(ctx context.Context, fset *token.FileSet, s file.Source, start, end token.Pos) (protocol.Location, error) { |
| 113 | + file := fset.File(start) |
| 114 | + uri := protocol.URIFromPath(file.Name()) |
| 115 | + fh, err := s.ReadFile(ctx, uri) |
| 116 | + if err != nil { |
| 117 | + return protocol.Location{}, err |
| 118 | + } |
| 119 | + content, err := fh.Content() |
| 120 | + if err != nil { |
| 121 | + return protocol.Location{}, err |
| 122 | + } |
| 123 | + m := protocol.NewMapper(fh.URI(), content) |
| 124 | + return m.PosLocation(file, start, end) |
| 125 | +} |
0 commit comments