| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package parsego |
| 6 |
|
| 7 |
import ( |
| 8 |
"go/ast" |
| 9 |
"go/parser" |
| 10 |
"go/scanner" |
| 11 |
"go/token" |
| 12 |
"sync" |
| 13 |
"unicode" |
| 14 |
|
| 15 |
"golang.org/x/tools/go/ast/inspector" |
| 16 |
"golang.org/x/tools/gopls/internal/protocol" |
| 17 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 18 |
"golang.org/x/tools/gopls/internal/util/safetoken" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
type File struct { |
| 23 |
URI protocol.DocumentURI |
| 24 |
Mode parser.Mode |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
File *ast.File |
| 32 |
Tok *token.File |
| 33 |
|
| 34 |
|
| 35 |
Src []byte |
| 36 |
|
| 37 |
cursor inspector.Cursor |
| 38 |
cursorOnce sync.Once |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
fixedSrc bool |
| 52 |
fixedAST bool |
| 53 |
Mapper *protocol.Mapper |
| 54 |
ParseErr scanner.ErrorList |
| 55 |
|
| 56 |
|
| 57 |
resolveOnce sync.Once |
| 58 |
} |
| 59 |
|
| 60 |
func (pgf *File) String() string { return string(pgf.URI) } |
| 61 |
|
| 62 |
|
| 63 |
func (pgf *File) Cursor() inspector.Cursor { |
| 64 |
pgf.cursorOnce.Do(func() { |
| 65 |
inspect := inspector.New([]*ast.File{pgf.File}) |
| 66 |
pgf.cursor, _ = inspect.Root().FirstChild() |
| 67 |
_ = pgf.cursor.Node().(*ast.File) |
| 68 |
}) |
| 69 |
return pgf.cursor |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
func (pgf *File) Fixed() bool { |
| 75 |
return pgf.fixedSrc || pgf.fixedAST |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
func (pgf *File) PositionPos(p protocol.Position) (token.Pos, error) { |
| 82 |
offset, err := pgf.Mapper.PositionOffset(p) |
| 83 |
if err != nil { |
| 84 |
return token.NoPos, err |
| 85 |
} |
| 86 |
return safetoken.Pos(pgf.Tok, offset) |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
func (pgf *File) PosPosition(pos token.Pos) (protocol.Position, error) { |
| 91 |
return pgf.Mapper.PosPosition(pgf.Tok, pos) |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
func (pgf *File) PosRange(start, end token.Pos) (protocol.Range, error) { |
| 96 |
return pgf.Mapper.PosRange(pgf.Tok, start, end) |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
func (pgf *File) PosLocation(start, end token.Pos) (protocol.Location, error) { |
| 101 |
return pgf.Mapper.PosLocation(pgf.Tok, start, end) |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
func (pgf *File) PosText(start, end token.Pos) ([]byte, error) { |
| 106 |
return pgf.Mapper.PosText(pgf.Tok, start, end) |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
func (pgf *File) NodeRange(node ast.Node) (protocol.Range, error) { |
| 111 |
return pgf.Mapper.NodeRange(pgf.Tok, node) |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
func (pgf *File) NodeOffsets(node ast.Node) (start int, end int, _ error) { |
| 116 |
return safetoken.Offsets(pgf.Tok, node.Pos(), node.End()) |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
func (pgf *File) NodeLocation(node ast.Node) (protocol.Location, error) { |
| 121 |
return pgf.Mapper.PosLocation(pgf.Tok, node.Pos(), node.End()) |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
func (pgf *File) NodeText(node ast.Node) ([]byte, error) { |
| 126 |
return pgf.Mapper.NodeText(pgf.Tok, node) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
func (pgf *File) RangePos(r protocol.Range) (token.Pos, token.Pos, error) { |
| 131 |
start, end, err := pgf.Mapper.RangeOffsets(r) |
| 132 |
if err != nil { |
| 133 |
return token.NoPos, token.NoPos, err |
| 134 |
} |
| 135 |
return pgf.Tok.Pos(start), pgf.Tok.Pos(end), nil |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
func (pgf *File) CheckNode(node ast.Node) { |
| 140 |
|
| 141 |
pgf.CheckPos(node.Pos()) |
| 142 |
pgf.CheckPos(node.End()) |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
func (pgf *File) CheckPos(pos token.Pos) { |
| 147 |
if !pos.IsValid() { |
| 148 |
bug.Report("invalid token.Pos") |
| 149 |
} else if _, err := safetoken.Offset(pgf.Tok, pos); err != nil { |
| 150 |
bug.Report("token.Pos out of range") |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
func (pgf *File) Resolve() { |
| 161 |
pgf.resolveOnce.Do(func() { |
| 162 |
if pgf.File.Scope != nil { |
| 163 |
return |
| 164 |
} |
| 165 |
defer func() { |
| 166 |
|
| 167 |
if e := recover(); e != nil { |
| 168 |
|
| 169 |
if _, ok := e.(bailout); !ok { |
| 170 |
panic(e) |
| 171 |
} |
| 172 |
} |
| 173 |
}() |
| 174 |
declErr := func(token.Pos, string) {} |
| 175 |
resolveFile(pgf.File, pgf.Tok, declErr) |
| 176 |
}) |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
func (pgf *File) Indentation(pos token.Pos) (string, error) { |
| 184 |
line := safetoken.Line(pgf.Tok, pos) |
| 185 |
start, end, err := safetoken.Offsets(pgf.Tok, pgf.Tok.LineStart(line), pos) |
| 186 |
if err != nil { |
| 187 |
return "", err |
| 188 |
} |
| 189 |
|
| 190 |
s := string(pgf.Src[start:end]) |
| 191 |
for i, r := range s { |
| 192 |
if !unicode.IsSpace(r) { |
| 193 |
return s[:i], nil |
| 194 |
} |
| 195 |
} |
| 196 |
return s, nil |
| 197 |
} |