| 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 astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 9 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 10 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 11 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 12 | "unicode/utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/" |
| 13 | ) |
| 14 | |
| 15 | // RangeInStringLiteral calculates the positional range within a string literal |
| 16 | // corresponding to the specified start and end byte offsets within the logical string. |
| 17 | func RangeInStringLiteralscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/RangeInStringLiteral().(lit *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BasicLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#, start, end int) (Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#, error) { |
| 18 | startPos, err := PosInStringLiteralscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/PosInStringLiteral().(lit, start) |
| 19 | if err != nil { |
| 20 | return Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#{}, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("start: %v", err) |
| 21 | } |
| 22 | endPos, err := PosInStringLiteralscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/PosInStringLiteral().(lit, end) |
| 23 | if err != nil { |
| 24 | return Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#{}, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("end: %v", err) |
| 25 | } |
| 26 | return Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#{startPos, endPos}, nil |
| 27 | } |
| 28 | |
| 29 | // PosInStringLiteral returns the position within a string literal |
| 30 | // corresponding to the specified byte offset within the logical |
| 31 | // string that it denotes. |
| 32 | func PosInStringLiteralscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/PosInStringLiteral().(lit *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BasicLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#, offset int) (tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#, error) { |
| 33 | raw := lit.Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value. |
| 34 | |
| 35 | value, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Unquotescip-go gomod github.com/golang/go/src go1.25.0 strconv/Unquote().(raw) |
| 36 | if err != nil { |
| 37 | return 0, err |
| 38 | } |
| 39 | if !(0 <= offset && offset <= len(value)) { |
| 40 | return 0, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("invalid offset") |
| 41 | } |
| 42 | |
| 43 | pos, _ := walkStringLiteralscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/walkStringLiteral().(lit, lit.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#End().(), offset) |
| 44 | return pos, nil |
| 45 | } |
| 46 | |
| 47 | // OffsetInStringLiteral returns the byte offset within the logical (unquoted) |
| 48 | // string corresponding to the specified source position. |
| 49 | func OffsetInStringLiteralscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/OffsetInStringLiteral().(lit *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BasicLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#, pos tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#) (int, error) { |
| 50 | if !NodeContainsPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/NodeContainsPos().(lit, pos) { |
| 51 | return 0, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("invalid position") |
| 52 | } |
| 53 | |
| 54 | raw := lit.Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value. |
| 55 | |
| 56 | value, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Unquotescip-go gomod github.com/golang/go/src go1.25.0 strconv/Unquote().(raw) |
| 57 | if err != nil { |
| 58 | return 0, err |
| 59 | } |
| 60 | |
| 61 | _, offset := walkStringLiteralscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/walkStringLiteral().(lit, pos, len(value)) |
| 62 | return offset, nil |
| 63 | } |
| 64 | |
| 65 | // walkStringLiteral iterates through the raw string literal to map between |
| 66 | // a file position and a logical byte offset. It stops when it reaches |
| 67 | // either the targetPos or the targetOffset. |
| 68 | // |
| 69 | // TODO(hxjiang): consider making an iterator. |
| 70 | func walkStringLiteralscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/walkStringLiteral().(lit *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BasicLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#, targetPos tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#, targetOffset int) (tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#, int) { |
| 71 | raw := lit.Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value. |
| 72 | norm := int(lit.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#End().()-lit.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Pos().()) > len(lit.Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value.) |
| 73 | |
| 74 | // remove quotes |
| 75 | quote := raw[0] // '"' or '`' |
| 76 | raw = raw[1 : len(raw)-1] |
| 77 | |
| 78 | var ( |
| 79 | i = 0 // byte index within logical value |
| 80 | pos = lit.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Pos().() + 1 // position within literal |
| 81 | ) |
| 82 | |
| 83 | for raw != "" { |
| 84 | r, _, rest, _ := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.UnquoteCharscip-go gomod github.com/golang/go/src go1.25.0 strconv/UnquoteChar().(raw, quote) // can't fail |
| 85 | sz := len(raw) - len(rest) // length of literal char in raw bytes |
| 86 | |
| 87 | nextPos := pos + tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#(sz) |
| 88 | if norm && r == '\n' { |
| 89 | nextPos++ |
| 90 | } |
| 91 | nextI := i + utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/.RuneLenscip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/RuneLen().(r) // length of logical char in "cooked" bytes |
| 92 | |
| 93 | if nextPos > targetPos || nextI > targetOffset { |
| 94 | break |
| 95 | } |
| 96 | |
| 97 | raw = raw[sz:] |
| 98 | i = nextI |
| 99 | pos = nextPos |
| 100 | } |
| 101 | |
| 102 | return pos, i |
| 103 | } |