| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package golang |
| 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 |
import ( |
| 45 |
"bytes" |
| 46 |
"context" |
| 47 |
"errors" |
| 48 |
"fmt" |
| 49 |
"go/ast" |
| 50 |
"go/parser" |
| 51 |
"go/printer" |
| 52 |
"go/token" |
| 53 |
"go/types" |
| 54 |
"maps" |
| 55 |
"os" |
| 56 |
"path/filepath" |
| 57 |
"regexp" |
| 58 |
"slices" |
| 59 |
"sort" |
| 60 |
"strconv" |
| 61 |
"strings" |
| 62 |
|
| 63 |
"golang.org/x/mod/modfile" |
| 64 |
goastutil "golang.org/x/tools/go/ast/astutil" |
| 65 |
"golang.org/x/tools/go/ast/inspector" |
| 66 |
"golang.org/x/tools/go/types/objectpath" |
| 67 |
"golang.org/x/tools/go/types/typeutil" |
| 68 |
"golang.org/x/tools/gopls/internal/cache" |
| 69 |
"golang.org/x/tools/gopls/internal/cache/metadata" |
| 70 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 71 |
"golang.org/x/tools/gopls/internal/file" |
| 72 |
"golang.org/x/tools/gopls/internal/protocol" |
| 73 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 74 |
"golang.org/x/tools/gopls/internal/util/cursorutil" |
| 75 |
"golang.org/x/tools/gopls/internal/util/pathutil" |
| 76 |
"golang.org/x/tools/gopls/internal/util/safetoken" |
| 77 |
"golang.org/x/tools/internal/astutil" |
| 78 |
"golang.org/x/tools/internal/diff" |
| 79 |
"golang.org/x/tools/internal/event" |
| 80 |
"golang.org/x/tools/internal/typesinternal" |
| 81 |
"golang.org/x/tools/refactor/satisfy" |
| 82 |
) |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
type renamer struct { |
| 88 |
pkg *cache.Package |
| 89 |
objsToUpdate map[types.Object]bool |
| 90 |
conflicts []string |
| 91 |
from, to string |
| 92 |
satisfyConstraints map[satisfy.Constraint]bool |
| 93 |
msets typeutil.MethodSetCache |
| 94 |
changeMethods bool |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
type PrepareItem struct { |
| 100 |
Range protocol.Range |
| 101 |
Text string |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
func PrepareRename(ctx context.Context, snapshot *cache.Snapshot, f file.Handle, rng protocol.Range) (_ *PrepareItem, usererr, err error) { |
| 110 |
ctx, done := event.Start(ctx, "golang.PrepareRename") |
| 111 |
defer done() |
| 112 |
|
| 113 |
|
| 114 |
if pgf, inPackageName, err := parsePackageNameDecl(ctx, snapshot, f, rng); err != nil { |
| 115 |
return nil, err, err |
| 116 |
} else if inPackageName { |
| 117 |
item, err := prepareRenamePackageName(ctx, snapshot, pgf) |
| 118 |
return item, err, err |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, f.URI()) |
| 131 |
if err != nil { |
| 132 |
return nil, nil, err |
| 133 |
} |
| 134 |
start, end, err := pgf.RangePos(rng) |
| 135 |
if err != nil { |
| 136 |
return nil, nil, err |
| 137 |
} |
| 138 |
cur, _ := pgf.Cursor().FindByPos(start, end) |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
if item, err := prepareRenameFuncSignature(pgf, start, end, cur); err != nil { |
| 143 |
return nil, nil, err |
| 144 |
} else if item != nil { |
| 145 |
return item, nil, nil |
| 146 |
} |
| 147 |
|
| 148 |
targets, err := objectsAt(pkg.TypesInfo(), cur) |
| 149 |
if err != nil { |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
var ok bool |
| 154 |
cur, ok = docCommentPosToIdent(pgf, start, end, cur) |
| 155 |
if !ok { |
| 156 |
return nil, nil, err |
| 157 |
} |
| 158 |
id := cur.Node().(*ast.Ident) |
| 159 |
obj := pkg.TypesInfo().Defs[id] |
| 160 |
if obj == nil { |
| 161 |
return nil, nil, fmt.Errorf("error fetching Object for ident %q", id.Name) |
| 162 |
} |
| 163 |
|
| 164 |
targets = []objectAt{{obj, cur}} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
obj, node := targets[0].obj, targets[0].cur.Node() |
| 170 |
if err := checkRenamable(obj, node); err != nil { |
| 171 |
return nil, nil, err |
| 172 |
} |
| 173 |
nodeRng, err := pgf.NodeRange(node) |
| 174 |
if err != nil { |
| 175 |
return nil, nil, err |
| 176 |
} |
| 177 |
if _, isImport := node.(*ast.ImportSpec); isImport { |
| 178 |
|
| 179 |
nodeRng.End = nodeRng.Start |
| 180 |
} |
| 181 |
return &PrepareItem{ |
| 182 |
Range: nodeRng, |
| 183 |
Text: obj.Name(), |
| 184 |
}, nil, nil |
| 185 |
} |
| 186 |
|
| 187 |
func prepareRenamePackageName(ctx context.Context, snapshot *cache.Snapshot, pgf *parsego.File) (*PrepareItem, error) { |
| 188 |
|
| 189 |
if !slices.Contains(snapshot.Options().SupportedResourceOperations, protocol.Rename) { |
| 190 |
return nil, errors.New("can't rename package: LSP client does not support file renaming") |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
meta, err := snapshot.NarrowestMetadataForFile(ctx, pgf.URI) |
| 195 |
if err != nil { |
| 196 |
return nil, err |
| 197 |
} |
| 198 |
if meta.Name == "main" { |
| 199 |
return nil, fmt.Errorf("can't rename package \"main\"") |
| 200 |
} |
| 201 |
|
| 202 |
if strings.HasSuffix(string(meta.Name), "_test") { |
| 203 |
return nil, fmt.Errorf("can't rename x_test packages") |
| 204 |
} |
| 205 |
if meta.Module == nil { |
| 206 |
return nil, fmt.Errorf("can't rename package: missing module information for package %q", meta.PkgPath) |
| 207 |
} |
| 208 |
|
| 209 |
if meta.Module.Path == string(meta.PkgPath) { |
| 210 |
return nil, fmt.Errorf("can't rename package: package path %q is the same as module path %q", meta.PkgPath, meta.Module.Path) |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
rng, err := pgf.NodeRange(pgf.File.Name) |
| 215 |
if err != nil { |
| 216 |
return nil, err |
| 217 |
} |
| 218 |
|
| 219 |
return &PrepareItem{ |
| 220 |
Range: rng, |
| 221 |
Text: string(meta.PkgPath), |
| 222 |
}, nil |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
func prepareRenameFuncSignature(pgf *parsego.File, start, end token.Pos, cursor inspector.Cursor) (*PrepareItem, error) { |
| 232 |
fdecl := funcKeywordDecl(start, end, cursor) |
| 233 |
if fdecl == nil { |
| 234 |
return nil, nil |
| 235 |
} |
| 236 |
ftyp := nameBlankParams(fdecl.Type) |
| 237 |
var buf bytes.Buffer |
| 238 |
if err := printer.Fprint(&buf, token.NewFileSet(), ftyp); err != nil { |
| 239 |
return nil, err |
| 240 |
} |
| 241 |
rng, err := pgf.PosRange(ftyp.Func, ftyp.Func+token.Pos(len("func"))) |
| 242 |
if err != nil { |
| 243 |
return nil, err |
| 244 |
} |
| 245 |
text := buf.String() |
| 246 |
return &PrepareItem{ |
| 247 |
Range: rng, |
| 248 |
Text: text, |
| 249 |
}, nil |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
func nameBlankParams(ftype *ast.FuncType) *ast.FuncType { |
| 255 |
ftype = astutil.CloneNode(ftype) |
| 256 |
|
| 257 |
|
| 258 |
scope := make(map[string]bool) |
| 259 |
for name := range astutil.FlatFields(ftype.Params) { |
| 260 |
if name != nil { |
| 261 |
scope[name.Name] = true |
| 262 |
} |
| 263 |
} |
| 264 |
blanks := 0 |
| 265 |
for name, field := range astutil.FlatFields(ftype.Params) { |
| 266 |
if name == nil { |
| 267 |
name = ast.NewIdent("_") |
| 268 |
field.Names = append(field.Names, name) |
| 269 |
} |
| 270 |
if name.Name == "" || name.Name == "_" { |
| 271 |
for { |
| 272 |
newName := fmt.Sprintf("_%d", blanks) |
| 273 |
blanks++ |
| 274 |
if !scope[newName] { |
| 275 |
name.Name = newName |
| 276 |
break |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
return ftype |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
func renameFuncSignature(ctx context.Context, pkg *cache.Package, pgf *parsego.File, start, end token.Pos, snapshot *cache.Snapshot, cursor inspector.Cursor, newName string) (map[protocol.DocumentURI][]protocol.TextEdit, error) { |
| 287 |
fdecl := funcKeywordDecl(start, end, cursor) |
| 288 |
if fdecl == nil { |
| 289 |
return nil, nil |
| 290 |
} |
| 291 |
ftyp := nameBlankParams(fdecl.Type) |
| 292 |
|
| 293 |
|
| 294 |
parsed, err := parser.ParseExpr(newName) |
| 295 |
if err != nil { |
| 296 |
return nil, err |
| 297 |
} |
| 298 |
newType, _ := parsed.(*ast.FuncType) |
| 299 |
if newType == nil { |
| 300 |
return nil, fmt.Errorf("parsed signature is %T, not a function type", parsed) |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
if got, want := newType.Results.NumFields(), ftyp.Results.NumFields(); got != want { |
| 305 |
return nil, fmt.Errorf("changing results not yet supported (got %d results, want %d)", got, want) |
| 306 |
} |
| 307 |
var resultTypes []string |
| 308 |
for _, field := range astutil.FlatFields(ftyp.Results) { |
| 309 |
resultTypes = append(resultTypes, FormatNode(token.NewFileSet(), field.Type)) |
| 310 |
} |
| 311 |
resultIndex := 0 |
| 312 |
for _, field := range astutil.FlatFields(newType.Results) { |
| 313 |
if FormatNode(token.NewFileSet(), field.Type) != resultTypes[resultIndex] { |
| 314 |
return nil, fmt.Errorf("changing results not yet supported") |
| 315 |
} |
| 316 |
resultIndex++ |
| 317 |
} |
| 318 |
|
| 319 |
type paramInfo struct { |
| 320 |
idx int |
| 321 |
typ string |
| 322 |
} |
| 323 |
oldParams := make(map[string]paramInfo) |
| 324 |
for name, field := range astutil.FlatFields(ftyp.Params) { |
| 325 |
oldParams[name.Name] = paramInfo{ |
| 326 |
idx: len(oldParams), |
| 327 |
typ: types.ExprString(field.Type), |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
var newParams []int |
| 332 |
for name, field := range astutil.FlatFields(newType.Params) { |
| 333 |
if name == nil { |
| 334 |
return nil, fmt.Errorf("need named fields") |
| 335 |
} |
| 336 |
info, ok := oldParams[name.Name] |
| 337 |
if !ok { |
| 338 |
return nil, fmt.Errorf("couldn't find name %s: adding parameters not yet supported", name) |
| 339 |
} |
| 340 |
if newType := types.ExprString(field.Type); newType != info.typ { |
| 341 |
return nil, fmt.Errorf("changing types (%s to %s) not yet supported", info.typ, newType) |
| 342 |
} |
| 343 |
newParams = append(newParams, info.idx) |
| 344 |
} |
| 345 |
|
| 346 |
funcRng, err := pgf.PosRange(ftyp.Func, ftyp.Func) |
| 347 |
if err != nil { |
| 348 |
return nil, err |
| 349 |
} |
| 350 |
changes, err := ChangeSignature(ctx, snapshot, pkg, pgf, funcRng, newParams) |
| 351 |
if err != nil { |
| 352 |
return nil, err |
| 353 |
} |
| 354 |
transposed := make(map[protocol.DocumentURI][]protocol.TextEdit) |
| 355 |
for _, change := range changes { |
| 356 |
transposed[change.TextDocumentEdit.TextDocument.URI] = protocol.AsTextEdits(change.TextDocumentEdit.Edits) |
| 357 |
} |
| 358 |
return transposed, nil |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
func funcKeywordDecl(start, end token.Pos, cursor inspector.Cursor) *ast.FuncDecl { |
| 364 |
fdecl, _ := cursorutil.FirstEnclosing[*ast.FuncDecl](cursor) |
| 365 |
if fdecl == nil { |
| 366 |
return nil |
| 367 |
} |
| 368 |
ftyp := fdecl.Type |
| 369 |
if start < ftyp.Func || end > ftyp.Func+token.Pos(len("func")) { |
| 370 |
return nil |
| 371 |
} |
| 372 |
return fdecl |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
func checkRenamable(obj types.Object, node ast.Node) error { |
| 378 |
switch obj := obj.(type) { |
| 379 |
case *types.Var: |
| 380 |
|
| 381 |
if obj.Embedded() && node.Pos() != obj.Pos() { |
| 382 |
return errors.New("an embedded field must be renamed at its declaration (since it renames the type too)") |
| 383 |
|
| 384 |
} |
| 385 |
case *types.Builtin, *types.Nil: |
| 386 |
return fmt.Errorf("%s is built in and cannot be renamed", obj.Name()) |
| 387 |
} |
| 388 |
if obj.Pkg() == nil || obj.Pkg().Path() == "unsafe" { |
| 389 |
|
| 390 |
return fmt.Errorf("%s is built in and cannot be renamed", obj.Name()) |
| 391 |
} |
| 392 |
if obj.Name() == "_" { |
| 393 |
return errors.New("can't rename \"_\"") |
| 394 |
} |
| 395 |
return nil |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
func editsToDocChanges(ctx context.Context, snapshot *cache.Snapshot, edits map[protocol.DocumentURI][]protocol.TextEdit) ([]protocol.DocumentChange, error) { |
| 400 |
var changes []protocol.DocumentChange |
| 401 |
for uri, e := range edits { |
| 402 |
fh, err := snapshot.ReadFile(ctx, uri) |
| 403 |
if err != nil { |
| 404 |
return nil, err |
| 405 |
} |
| 406 |
changes = append(changes, protocol.DocumentChangeEdit(fh, e)) |
| 407 |
} |
| 408 |
return changes, nil |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
func Rename(ctx context.Context, snapshot *cache.Snapshot, f file.Handle, rng protocol.Range, newName string) ([]protocol.DocumentChange, error) { |
| 414 |
ctx, done := event.Start(ctx, "golang.Rename") |
| 415 |
defer done() |
| 416 |
|
| 417 |
pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, f.URI()) |
| 418 |
if err != nil { |
| 419 |
return nil, err |
| 420 |
} |
| 421 |
start, end, err := pgf.RangePos(rng) |
| 422 |
if err != nil { |
| 423 |
return nil, err |
| 424 |
} |
| 425 |
|
| 426 |
cur, ok := pgf.Cursor().FindByPos(start, end) |
| 427 |
if !ok { |
| 428 |
return nil, fmt.Errorf("can't find cursor for selection") |
| 429 |
} |
| 430 |
|
| 431 |
if edits, err := renameFuncSignature(ctx, pkg, pgf, start, end, snapshot, cur, newName); err != nil { |
| 432 |
return nil, err |
| 433 |
} else if edits != nil { |
| 434 |
return editsToDocChanges(ctx, snapshot, edits) |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
_, inPackageName, err := parsePackageNameDecl(ctx, snapshot, f, rng) |
| 439 |
if err != nil { |
| 440 |
return nil, err |
| 441 |
} |
| 442 |
|
| 443 |
var ( |
| 444 |
editMap map[protocol.DocumentURI][]diff.Edit |
| 445 |
newPkgDir string |
| 446 |
) |
| 447 |
if inPackageName { |
| 448 |
countRenamePackage.Inc() |
| 449 |
var ( |
| 450 |
newPkgName PackageName |
| 451 |
newPkgPath PackagePath |
| 452 |
err error |
| 453 |
) |
| 454 |
moveSubpackages := snapshot.Options().RenameMovesSubpackages |
| 455 |
if newPkgDir, newPkgName, newPkgPath, err = checkPackageRename(pkg, f, newName, moveSubpackages); err != nil { |
| 456 |
return nil, err |
| 457 |
} |
| 458 |
editMap, err = renamePackage(ctx, snapshot, f, newPkgName, newPkgPath, newPkgDir, moveSubpackages) |
| 459 |
if err != nil { |
| 460 |
return nil, err |
| 461 |
} |
| 462 |
} else { |
| 463 |
if !isValidIdentifier(newName) { |
| 464 |
return nil, fmt.Errorf("invalid identifier to rename: %q", newName) |
| 465 |
} |
| 466 |
editMap, err = renameOrdinary(ctx, snapshot, f.URI(), rng, newName) |
| 467 |
if err != nil { |
| 468 |
return nil, err |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
result := make(map[protocol.DocumentURI][]protocol.TextEdit) |
| 474 |
for uri, edits := range editMap { |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
diff.SortEdits(edits) |
| 486 |
edits = slices.Compact(edits) |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
fh, err := snapshot.ReadFile(ctx, uri) |
| 497 |
if err != nil { |
| 498 |
return nil, err |
| 499 |
} |
| 500 |
data, err := fh.Content() |
| 501 |
if err != nil { |
| 502 |
return nil, err |
| 503 |
} |
| 504 |
m := protocol.NewMapper(uri, data) |
| 505 |
textedits, err := protocol.EditsFromDiffEdits(m, edits) |
| 506 |
if err != nil { |
| 507 |
return nil, err |
| 508 |
} |
| 509 |
result[uri] = textedits |
| 510 |
} |
| 511 |
|
| 512 |
changes, err := editsToDocChanges(ctx, snapshot, result) |
| 513 |
if err != nil { |
| 514 |
return nil, err |
| 515 |
} |
| 516 |
if inPackageName { |
| 517 |
oldDir := f.URI().DirPath() |
| 518 |
if snapshot.Options().RenameMovesSubpackages { |
| 519 |
|
| 520 |
changes = append(changes, protocol.DocumentChangeRename( |
| 521 |
protocol.URIFromPath(oldDir), |
| 522 |
protocol.URIFromPath(newPkgDir))) |
| 523 |
} else { |
| 524 |
|
| 525 |
|
| 526 |
files, err := os.ReadDir(oldDir) |
| 527 |
if err != nil { |
| 528 |
return nil, err |
| 529 |
} |
| 530 |
hasNestedDir := false |
| 531 |
for _, f := range files { |
| 532 |
if !f.IsDir() { |
| 533 |
changes = append(changes, |
| 534 |
protocol.DocumentChangeRename( |
| 535 |
protocol.URIFromPath(filepath.Join(oldDir, f.Name())), |
| 536 |
protocol.URIFromPath(filepath.Join(newPkgDir, f.Name()))), |
| 537 |
) |
| 538 |
} else { |
| 539 |
hasNestedDir = true |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
oldPkgDir := f.URI().DirPath() |
| 554 |
if !pathutil.InDir(oldPkgDir, newPkgDir) && !hasNestedDir { |
| 555 |
changes = append(changes, |
| 556 |
protocol.DocumentChangeDelete(protocol.URIFromPath(oldPkgDir))) |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
return changes, nil |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
func renameOrdinary(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI, rng protocol.Range, newName string) (map[protocol.DocumentURI][]diff.Edit, error) { |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
mps, err := snapshot.MetadataForFile(ctx, uri, true) |
| 573 |
if err != nil { |
| 574 |
return nil, err |
| 575 |
} |
| 576 |
if len(mps) == 0 { |
| 577 |
return nil, fmt.Errorf("no package metadata for file %s", uri) |
| 578 |
} |
| 579 |
widest := mps[len(mps)-1] |
| 580 |
pkgs, err := snapshot.TypeCheck(ctx, widest.ID) |
| 581 |
if err != nil { |
| 582 |
return nil, err |
| 583 |
} |
| 584 |
pkg := pkgs[0] |
| 585 |
pgf, err := pkg.File(uri) |
| 586 |
if err != nil { |
| 587 |
return nil, err |
| 588 |
} |
| 589 |
start, end, err := pgf.RangePos(rng) |
| 590 |
if err != nil { |
| 591 |
return nil, err |
| 592 |
} |
| 593 |
cur, _ := pgf.Cursor().FindByPos(start, end) |
| 594 |
|
| 595 |
targets, err := objectsAt(pkg.TypesInfo(), cur) |
| 596 |
if err != nil { |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
var ok bool |
| 601 |
cur, ok = docCommentPosToIdent(pgf, start, end, cur) |
| 602 |
if !ok { |
| 603 |
return nil, err |
| 604 |
} |
| 605 |
id := cur.Node().(*ast.Ident) |
| 606 |
obj := pkg.TypesInfo().Defs[id] |
| 607 |
if obj == nil { |
| 608 |
return nil, fmt.Errorf("error fetching types.Object for ident %q", id.Name) |
| 609 |
} |
| 610 |
|
| 611 |
targets = []objectAt{{obj, cur}} |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
obj, node := targets[0].obj, targets[0].cur.Node() |
| 617 |
if obj.Name() == newName { |
| 618 |
return nil, fmt.Errorf("old and new names are the same: %s", newName) |
| 619 |
} |
| 620 |
if err := checkRenamable(obj, node); err != nil { |
| 621 |
return nil, err |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
if is[*types.Var](obj) && obj.(*types.Var).Embedded() { |
| 627 |
if id, ok := node.(*ast.Ident); ok { |
| 628 |
|
| 629 |
if typeName := pkg.TypesInfo().Uses[id]; typeName != nil { |
| 630 |
loc, err := ObjectLocation(ctx, pkg.FileSet(), snapshot, typeName) |
| 631 |
if err != nil { |
| 632 |
return nil, err |
| 633 |
} |
| 634 |
return renameOrdinary(ctx, snapshot, loc.URI, loc.Range, newName) |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
var declObjPath objectpath.Path |
| 641 |
if obj.Exported() { |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
switch obj0 := obj.(type) { |
| 648 |
case *types.TypeName: |
| 649 |
if _, ok := types.Unalias(obj.Type()).(*types.TypeParam); ok { |
| 650 |
|
| 651 |
|
| 652 |
goto skipObjectPath |
| 653 |
} |
| 654 |
case *types.Func: |
| 655 |
obj = obj0.Origin() |
| 656 |
case *types.Var: |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
if !obj0.IsField() && !typesinternal.IsPackageLevel(obj) { |
| 670 |
goto skipObjectPath |
| 671 |
} |
| 672 |
} |
| 673 |
if path, err := objectpath.For(obj); err == nil { |
| 674 |
declObjPath = path |
| 675 |
} |
| 676 |
skipObjectPath: |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
if declObjPath == "" { |
| 681 |
var objects []types.Object |
| 682 |
for _, o := range targets { |
| 683 |
objects = append(objects, o.obj) |
| 684 |
} |
| 685 |
|
| 686 |
editMap, _, err := renameObjects(newName, pkg, objects...) |
| 687 |
if err != nil { |
| 688 |
return nil, err |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
if id, ok := cur.Node().(*ast.Ident); ok && id.Pos() == obj.Pos() { |
| 700 |
|
| 701 |
if decl, _ := cursorutil.FirstEnclosing[*ast.FuncDecl](cur); decl != nil { |
| 702 |
if decl.Recv != nil && |
| 703 |
len(decl.Recv.List) > 0 && |
| 704 |
len(decl.Recv.List[0].Names) > 0 { |
| 705 |
recv := pkg.TypesInfo().Defs[decl.Recv.List[0].Names[0]] |
| 706 |
if recv == obj { |
| 707 |
|
| 708 |
|
| 709 |
renameReceivers(pkg, recv.(*types.Var), newName, editMap) |
| 710 |
} |
| 711 |
} |
| 712 |
} |
| 713 |
} |
| 714 |
return editMap, nil |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
transitive := false |
| 726 |
switch obj := obj.(type) { |
| 727 |
case *types.TypeName: |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
if obj.Parent() == obj.Pkg().Scope() { |
| 738 |
transitive = true |
| 739 |
} |
| 740 |
|
| 741 |
case *types.Var: |
| 742 |
if obj.IsField() { |
| 743 |
transitive = true |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
case *types.Func: |
| 750 |
if obj.Signature().Recv() != nil { |
| 751 |
transitive = true |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
declURI := protocol.URIFromPath(pkg.FileSet().File(obj.Pos()).Name()) |
| 764 |
pkgs, err = typeCheckReverseDependencies(ctx, snapshot, declURI, transitive) |
| 765 |
if err != nil { |
| 766 |
return nil, err |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
declPkgPath := PackagePath(obj.Pkg().Path()) |
| 771 |
return renameExported(pkgs, declPkgPath, declObjPath, newName) |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
func renameReceivers(pkg *cache.Package, recv *types.Var, newName string, editMap map[protocol.DocumentURI][]diff.Edit) { |
| 778 |
_, named := typesinternal.ReceiverNamed(recv) |
| 779 |
if named == nil { |
| 780 |
return |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
for m := range named.Origin().Methods() { |
| 785 |
recv2 := m.Signature().Recv() |
| 786 |
if recv2 == recv { |
| 787 |
continue |
| 788 |
} |
| 789 |
if recv2.Name() == newName { |
| 790 |
continue |
| 791 |
} |
| 792 |
editMap2, _, err := renameObjects(newName, pkg, recv2) |
| 793 |
if err != nil { |
| 794 |
continue |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
for uri, edits := range editMap2 { |
| 802 |
editMap[uri] = append(editMap[uri], edits...) |
| 803 |
} |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
func typeCheckReverseDependencies(ctx context.Context, snapshot *cache.Snapshot, declURI protocol.DocumentURI, transitive bool) ([]*cache.Package, error) { |
| 819 |
variants, err := snapshot.MetadataForFile(ctx, declURI, false) |
| 820 |
if err != nil { |
| 821 |
return nil, err |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
allRdeps := make(map[PackageID]*metadata.Package) |
| 826 |
for _, variant := range variants { |
| 827 |
rdeps, err := snapshot.ReverseDependencies(ctx, variant.ID, transitive) |
| 828 |
if err != nil { |
| 829 |
return nil, err |
| 830 |
} |
| 831 |
allRdeps[variant.ID] = variant |
| 832 |
maps.Copy(allRdeps, rdeps) |
| 833 |
} |
| 834 |
var ids []PackageID |
| 835 |
for id, meta := range allRdeps { |
| 836 |
if meta.IsIntermediateTestVariant() { |
| 837 |
continue |
| 838 |
} |
| 839 |
ids = append(ids, id) |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
metadata.SortPostOrder(snapshot, ids) |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
return snapshot.TypeCheck(ctx, ids...) |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
func renameExported(pkgs []*cache.Package, declPkgPath PackagePath, declObjPath objectpath.Path, newName string) (map[protocol.DocumentURI][]diff.Edit, error) { |
| 865 |
|
| 866 |
|
| 867 |
type target struct { |
| 868 |
pkg PackagePath |
| 869 |
obj objectpath.Path |
| 870 |
} |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
targets := map[target]bool{{declPkgPath, declObjPath}: true} |
| 888 |
|
| 889 |
|
| 890 |
allEdits := make(map[protocol.DocumentURI][]diff.Edit) |
| 891 |
for _, pkg := range pkgs { |
| 892 |
|
| 893 |
|
| 894 |
var objects []types.Object |
| 895 |
for t := range targets { |
| 896 |
p := pkg.DependencyTypes(t.pkg) |
| 897 |
if p == nil { |
| 898 |
continue |
| 899 |
} |
| 900 |
obj, err := objectpath.Object(p, t.obj) |
| 901 |
if err != nil { |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
continue |
| 912 |
} |
| 913 |
objects = append(objects, obj) |
| 914 |
} |
| 915 |
if len(objects) == 0 { |
| 916 |
continue |
| 917 |
} |
| 918 |
|
| 919 |
|
| 920 |
editMap, moreObjects, err := renameObjects(newName, pkg, objects...) |
| 921 |
if err != nil { |
| 922 |
return nil, err |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
for uri, edits := range editMap { |
| 928 |
allEdits[uri] = append(allEdits[uri], edits...) |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
for obj := range moreObjects { |
| 933 |
objpath, err := objectpath.For(obj) |
| 934 |
if err != nil { |
| 935 |
continue |
| 936 |
} |
| 937 |
target := target{PackagePath(obj.Pkg().Path()), objpath} |
| 938 |
targets[target] = true |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
return allEdits, nil |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
func renamePackage(ctx context.Context, s *cache.Snapshot, f file.Handle, newName PackageName, newPath PackagePath, newDir string, moveSubpackages bool) (map[protocol.DocumentURI][]diff.Edit, error) { |
| 957 |
|
| 958 |
renamingEdits := make(map[protocol.DocumentURI][]diff.Edit) |
| 959 |
err := updatePackageDeclsAndImports(ctx, s, f, newName, newPath, renamingEdits, moveSubpackages) |
| 960 |
if err != nil { |
| 961 |
return nil, err |
| 962 |
} |
| 963 |
|
| 964 |
err = updateModFiles(ctx, s, f.URI().DirPath(), newDir, renamingEdits, moveSubpackages) |
| 965 |
if err != nil { |
| 966 |
return nil, err |
| 967 |
} |
| 968 |
|
| 969 |
return renamingEdits, nil |
| 970 |
} |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
func updateModFiles(ctx context.Context, s *cache.Snapshot, oldDir string, newPkgDir string, renamingEdits map[protocol.DocumentURI][]diff.Edit, moveSubpackages bool) error { |
| 976 |
modFiles := s.View().ModFiles() |
| 977 |
for _, m := range modFiles { |
| 978 |
fh, err := s.ReadFile(ctx, m) |
| 979 |
if err != nil { |
| 980 |
return err |
| 981 |
} |
| 982 |
pm, err := s.ParseMod(ctx, fh) |
| 983 |
if err != nil { |
| 984 |
return err |
| 985 |
} |
| 986 |
|
| 987 |
modFileDir := pm.URI.DirPath() |
| 988 |
affectedReplaces := []*modfile.Replace{} |
| 989 |
|
| 990 |
|
| 991 |
for _, r := range pm.File.Replace { |
| 992 |
if !strings.HasPrefix(r.New.Path, "/") && !strings.HasPrefix(r.New.Path, "./") && !strings.HasPrefix(r.New.Path, "../") { |
| 993 |
continue |
| 994 |
} |
| 995 |
replacedDir := r.New.Path |
| 996 |
if strings.HasPrefix(r.New.Path, "./") || strings.HasPrefix(r.New.Path, "../") { |
| 997 |
replacedDir = filepath.Join(modFileDir, r.New.Path) |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
if moveSubpackages && !strings.HasPrefix(filepath.ToSlash(replacedDir)+"/", filepath.ToSlash(oldDir)+"/") || |
| 1003 |
!moveSubpackages && !(filepath.ToSlash(replacedDir) == filepath.ToSlash(oldDir)) { |
| 1004 |
continue |
| 1005 |
} |
| 1006 |
|
| 1007 |
affectedReplaces = append(affectedReplaces, r) |
| 1008 |
} |
| 1009 |
|
| 1010 |
if len(affectedReplaces) == 0 { |
| 1011 |
continue |
| 1012 |
} |
| 1013 |
copied, err := modfile.Parse("", pm.Mapper.Content, nil) |
| 1014 |
if err != nil { |
| 1015 |
return err |
| 1016 |
} |
| 1017 |
|
| 1018 |
for _, r := range affectedReplaces { |
| 1019 |
replacedDir := r.New.Path |
| 1020 |
if strings.HasPrefix(r.New.Path, "./") || strings.HasPrefix(r.New.Path, "../") { |
| 1021 |
replacedDir = filepath.Join(modFileDir, r.New.Path) |
| 1022 |
} |
| 1023 |
|
| 1024 |
suffix := strings.TrimPrefix(replacedDir, oldDir) |
| 1025 |
|
| 1026 |
newReplacedPath, err := filepath.Rel(modFileDir, newPkgDir+suffix) |
| 1027 |
if err != nil { |
| 1028 |
return err |
| 1029 |
} |
| 1030 |
|
| 1031 |
newReplacedPath = filepath.ToSlash(newReplacedPath) |
| 1032 |
|
| 1033 |
if !strings.HasPrefix(newReplacedPath, "/") && !strings.HasPrefix(newReplacedPath, "../") { |
| 1034 |
newReplacedPath = "./" + newReplacedPath |
| 1035 |
} |
| 1036 |
|
| 1037 |
if err := copied.AddReplace(r.Old.Path, "", newReplacedPath, ""); err != nil { |
| 1038 |
return err |
| 1039 |
} |
| 1040 |
} |
| 1041 |
|
| 1042 |
copied.Cleanup() |
| 1043 |
newContent, err := copied.Format() |
| 1044 |
if err != nil { |
| 1045 |
return err |
| 1046 |
} |
| 1047 |
|
| 1048 |
|
| 1049 |
edits := diff.Bytes(pm.Mapper.Content, newContent) |
| 1050 |
renamingEdits[pm.URI] = append(renamingEdits[pm.URI], edits...) |
| 1051 |
} |
| 1052 |
return nil |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
func updatePackageDeclsAndImports(ctx context.Context, s *cache.Snapshot, f file.Handle, newName PackageName, newPkgPath PackagePath, renamingEdits map[protocol.DocumentURI][]diff.Edit, moveSubpackages bool) error { |
| 1063 |
if strings.HasSuffix(string(newName), "_test") { |
| 1064 |
return fmt.Errorf("cannot rename to _test package") |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
meta, err := s.NarrowestMetadataForFile(ctx, f.URI()) |
| 1070 |
if err != nil { |
| 1071 |
return err |
| 1072 |
} |
| 1073 |
|
| 1074 |
oldPkgPath := meta.PkgPath |
| 1075 |
if meta.Module == nil { |
| 1076 |
return fmt.Errorf("cannot rename package: missing module information for package %q", meta.PkgPath) |
| 1077 |
} |
| 1078 |
modulePath := PackagePath(meta.Module.Path) |
| 1079 |
if modulePath == oldPkgPath { |
| 1080 |
return fmt.Errorf("cannot rename package: module path %q is the same as the package path, so renaming the package directory would have no effect", modulePath) |
| 1081 |
} |
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
allMetadata, err := s.AllMetadata(ctx) |
| 1088 |
if err != nil { |
| 1089 |
return err |
| 1090 |
} |
| 1091 |
|
| 1092 |
for _, mp := range allMetadata { |
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
if mp.PkgPath == oldPkgPath+"_test" { |
| 1097 |
if err := renamePackageClause(ctx, mp, s, newName+"_test", renamingEdits); err != nil { |
| 1098 |
return err |
| 1099 |
} |
| 1100 |
continue |
| 1101 |
} |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
if moveSubpackages && !pathutil.InDir(filepath.FromSlash(string(oldPkgPath)), filepath.FromSlash(string(mp.PkgPath))) || |
| 1107 |
!moveSubpackages && mp.PkgPath != oldPkgPath { |
| 1108 |
continue |
| 1109 |
} |
| 1110 |
|
| 1111 |
if mp.Module == nil { |
| 1112 |
|
| 1113 |
return fmt.Errorf("cannot rename package: missing module information for package %q", mp.PkgPath) |
| 1114 |
} |
| 1115 |
|
| 1116 |
if modulePath != PackagePath(mp.Module.Path) { |
| 1117 |
continue |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
suffix := strings.TrimPrefix(string(mp.PkgPath), string(oldPkgPath)) |
| 1122 |
newImportPath := string(newPkgPath) + suffix |
| 1123 |
|
| 1124 |
pkgName := mp.Name |
| 1125 |
if mp.PkgPath == oldPkgPath { |
| 1126 |
pkgName = newName |
| 1127 |
|
| 1128 |
if err := renamePackageClause(ctx, mp, s, newName, renamingEdits); err != nil { |
| 1129 |
return err |
| 1130 |
} |
| 1131 |
} |
| 1132 |
imp := ImportPath(newImportPath) |
| 1133 |
if err := renameImports(ctx, s, mp, imp, pkgName, renamingEdits); err != nil { |
| 1134 |
return err |
| 1135 |
} |
| 1136 |
} |
| 1137 |
|
| 1138 |
return nil |
| 1139 |
} |
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
func renamePackageClause(ctx context.Context, mp *metadata.Package, snapshot *cache.Snapshot, newName PackageName, edits map[protocol.DocumentURI][]diff.Edit) error { |
| 1146 |
|
| 1147 |
for _, uri := range mp.CompiledGoFiles { |
| 1148 |
fh, err := snapshot.ReadFile(ctx, uri) |
| 1149 |
if err != nil { |
| 1150 |
return err |
| 1151 |
} |
| 1152 |
f, err := snapshot.ParseGo(ctx, fh, parsego.Header) |
| 1153 |
if err != nil { |
| 1154 |
return err |
| 1155 |
} |
| 1156 |
if f.File.Name == nil { |
| 1157 |
continue |
| 1158 |
} |
| 1159 |
|
| 1160 |
edit, err := posEdit(f.Tok, f.File.Name.Pos(), f.File.Name.End(), string(newName)) |
| 1161 |
if err != nil { |
| 1162 |
return err |
| 1163 |
} |
| 1164 |
edits[f.URI] = append(edits[f.URI], edit) |
| 1165 |
} |
| 1166 |
|
| 1167 |
return nil |
| 1168 |
} |
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
func renameImports(ctx context.Context, snapshot *cache.Snapshot, mp *metadata.Package, newPath ImportPath, newName PackageName, allEdits map[protocol.DocumentURI][]diff.Edit) error { |
| 1176 |
rdeps, err := snapshot.ReverseDependencies(ctx, mp.ID, false) |
| 1177 |
if err != nil { |
| 1178 |
return err |
| 1179 |
} |
| 1180 |
|
| 1181 |
|
| 1182 |
needsTypeCheck := make(map[PackageID][]protocol.DocumentURI) |
| 1183 |
for _, rdep := range rdeps { |
| 1184 |
if rdep.IsIntermediateTestVariant() { |
| 1185 |
continue |
| 1186 |
} |
| 1187 |
|
| 1188 |
for _, uri := range rdep.CompiledGoFiles { |
| 1189 |
fh, err := snapshot.ReadFile(ctx, uri) |
| 1190 |
if err != nil { |
| 1191 |
return err |
| 1192 |
} |
| 1193 |
f, err := snapshot.ParseGo(ctx, fh, parsego.Header) |
| 1194 |
if err != nil { |
| 1195 |
return err |
| 1196 |
} |
| 1197 |
if f.File.Name == nil { |
| 1198 |
continue |
| 1199 |
} |
| 1200 |
for _, imp := range f.File.Imports { |
| 1201 |
if rdep.DepsByImpPath[metadata.UnquoteImportPath(imp)] != mp.ID { |
| 1202 |
continue |
| 1203 |
} |
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
if !metadata.IsValidImport(rdep.PkgPath, metadata.PackagePath(newPath), snapshot.View().Type() != cache.GoPackagesDriverView) { |
| 1212 |
return fmt.Errorf("invalid: package move would result in illegal internal import") |
| 1213 |
} |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
if imp.Name == nil { |
| 1224 |
needsTypeCheck[rdep.ID] = append(needsTypeCheck[rdep.ID], uri) |
| 1225 |
} |
| 1226 |
|
| 1227 |
|
| 1228 |
edit, err := posEdit(f.Tok, imp.Path.Pos(), imp.Path.End(), strconv.Quote(string(newPath))) |
| 1229 |
if err != nil { |
| 1230 |
return err |
| 1231 |
} |
| 1232 |
allEdits[uri] = append(allEdits[uri], edit) |
| 1233 |
} |
| 1234 |
} |
| 1235 |
} |
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
if newName == mp.Name { |
| 1240 |
return nil |
| 1241 |
} |
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
ids := make([]PackageID, 0, len(needsTypeCheck)) |
| 1246 |
for id := range needsTypeCheck { |
| 1247 |
ids = append(ids, id) |
| 1248 |
} |
| 1249 |
pkgs, err := snapshot.TypeCheck(ctx, ids...) |
| 1250 |
if err != nil { |
| 1251 |
return err |
| 1252 |
} |
| 1253 |
for i, id := range ids { |
| 1254 |
pkg := pkgs[i] |
| 1255 |
for _, uri := range needsTypeCheck[id] { |
| 1256 |
f, err := pkg.File(uri) |
| 1257 |
if err != nil { |
| 1258 |
return err |
| 1259 |
} |
| 1260 |
for _, imp := range f.File.Imports { |
| 1261 |
if imp.Name != nil { |
| 1262 |
continue |
| 1263 |
} |
| 1264 |
if rdeps[id].DepsByImpPath[metadata.UnquoteImportPath(imp)] != mp.ID { |
| 1265 |
continue |
| 1266 |
} |
| 1267 |
|
| 1268 |
pkgname, ok := pkg.TypesInfo().Implicits[imp].(*types.PkgName) |
| 1269 |
if !ok { |
| 1270 |
|
| 1271 |
return fmt.Errorf("internal error: missing type information for %s import at %s", |
| 1272 |
imp.Path.Value, safetoken.StartPosition(pkg.FileSet(), imp.Pos())) |
| 1273 |
} |
| 1274 |
|
| 1275 |
pkgScope := pkg.Types().Scope() |
| 1276 |
fileScope := pkg.TypesInfo().Scopes[f.File] |
| 1277 |
|
| 1278 |
localName := string(newName) |
| 1279 |
try := 0 |
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
for fileScope.Lookup(localName) != nil || pkgScope.Lookup(localName) != nil { |
| 1288 |
try++ |
| 1289 |
localName = fmt.Sprintf("%s%d", newName, try) |
| 1290 |
} |
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
editMap, _, err := renameObjects(localName, pkg, pkgname) |
| 1303 |
if err != nil { |
| 1304 |
return err |
| 1305 |
} |
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
if localName == string(newName) { |
| 1312 |
edits, ok := editMap[uri] |
| 1313 |
if !ok { |
| 1314 |
return fmt.Errorf("internal error: no changes for %s", uri) |
| 1315 |
} |
| 1316 |
diff.SortEdits(edits) |
| 1317 |
editMap[uri] = edits[1:] |
| 1318 |
} |
| 1319 |
for uri, edits := range editMap { |
| 1320 |
allEdits[uri] = append(allEdits[uri], edits...) |
| 1321 |
} |
| 1322 |
} |
| 1323 |
} |
| 1324 |
} |
| 1325 |
return nil |
| 1326 |
} |
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
func renameObjects(newName string, pkg *cache.Package, targets ...types.Object) (map[protocol.DocumentURI][]diff.Edit, map[types.Object]bool, error) { |
| 1337 |
r := renamer{ |
| 1338 |
pkg: pkg, |
| 1339 |
objsToUpdate: make(map[types.Object]bool), |
| 1340 |
from: targets[0].Name(), |
| 1341 |
to: newName, |
| 1342 |
} |
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
for _, obj := range targets { |
| 1349 |
if obj, ok := obj.(*types.Func); ok { |
| 1350 |
recv := obj.Signature().Recv() |
| 1351 |
if recv != nil && types.IsInterface(recv.Type().Underlying()) { |
| 1352 |
r.changeMethods = true |
| 1353 |
break |
| 1354 |
} |
| 1355 |
} |
| 1356 |
} |
| 1357 |
|
| 1358 |
|
| 1359 |
for _, obj := range targets { |
| 1360 |
r.check(obj) |
| 1361 |
if len(r.conflicts) > 0 { |
| 1362 |
|
| 1363 |
return nil, nil, fmt.Errorf("%s", strings.Join(r.conflicts, "\n")) |
| 1364 |
} |
| 1365 |
} |
| 1366 |
|
| 1367 |
editMap, err := r.update() |
| 1368 |
if err != nil { |
| 1369 |
return nil, nil, err |
| 1370 |
} |
| 1371 |
|
| 1372 |
|
| 1373 |
for _, obj := range targets { |
| 1374 |
delete(r.objsToUpdate, obj) |
| 1375 |
} |
| 1376 |
return editMap, r.objsToUpdate, nil |
| 1377 |
} |
| 1378 |
|
| 1379 |
|
| 1380 |
func (r *renamer) update() (map[protocol.DocumentURI][]diff.Edit, error) { |
| 1381 |
result := make(map[protocol.DocumentURI][]diff.Edit) |
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
shouldUpdate := func(obj types.Object) bool { |
| 1386 |
return containsOrigin(r.objsToUpdate, obj) |
| 1387 |
} |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
type item struct { |
| 1393 |
node ast.Node |
| 1394 |
obj types.Object |
| 1395 |
isDef bool |
| 1396 |
} |
| 1397 |
var items []item |
| 1398 |
info := r.pkg.TypesInfo() |
| 1399 |
for id, obj := range info.Uses { |
| 1400 |
if shouldUpdate(obj) { |
| 1401 |
items = append(items, item{id, obj, false}) |
| 1402 |
} |
| 1403 |
} |
| 1404 |
for id, obj := range info.Defs { |
| 1405 |
if shouldUpdate(obj) { |
| 1406 |
items = append(items, item{id, obj, true}) |
| 1407 |
} |
| 1408 |
} |
| 1409 |
for node, obj := range info.Implicits { |
| 1410 |
if shouldUpdate(obj) { |
| 1411 |
switch node.(type) { |
| 1412 |
case *ast.ImportSpec, *ast.CaseClause: |
| 1413 |
items = append(items, item{node, obj, true}) |
| 1414 |
} |
| 1415 |
} |
| 1416 |
} |
| 1417 |
sort.Slice(items, func(i, j int) bool { |
| 1418 |
return items[i].node.Pos() < items[j].node.Pos() |
| 1419 |
}) |
| 1420 |
|
| 1421 |
|
| 1422 |
for _, item := range items { |
| 1423 |
pgf, err := r.pkg.FileEnclosing(item.node.Pos()) |
| 1424 |
if err != nil { |
| 1425 |
bug.Reportf("edit does not belong to syntax of package %q: %v", r.pkg, err) |
| 1426 |
continue |
| 1427 |
} |
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
if pkgName, ok := item.obj.(*types.PkgName); ok && item.isDef { |
| 1432 |
edit, err := r.updatePkgName(pgf, pkgName) |
| 1433 |
if err != nil { |
| 1434 |
return nil, err |
| 1435 |
} |
| 1436 |
result[pgf.URI] = append(result[pgf.URI], edit) |
| 1437 |
continue |
| 1438 |
} |
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
if _, ok := item.node.(*ast.CaseClause); ok { |
| 1445 |
path, _ := goastutil.PathEnclosingInterval(pgf.File, item.obj.Pos(), item.obj.Pos()) |
| 1446 |
item.node = path[0].(*ast.Ident) |
| 1447 |
} |
| 1448 |
|
| 1449 |
|
| 1450 |
edit, err := posEdit(pgf.Tok, item.node.Pos(), item.node.End(), r.to) |
| 1451 |
if err != nil { |
| 1452 |
return nil, err |
| 1453 |
} |
| 1454 |
|
| 1455 |
result[pgf.URI] = append(result[pgf.URI], edit) |
| 1456 |
|
| 1457 |
if !item.isDef { |
| 1458 |
continue |
| 1459 |
} |
| 1460 |
|
| 1461 |
cur, _ := pgf.Cursor().FindNode(item.node) |
| 1462 |
doc := docComment(pgf, cur) |
| 1463 |
if doc == nil { |
| 1464 |
continue |
| 1465 |
} |
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
docRegexp := regexp.MustCompile(`\b` + r.from + `\b`) |
| 1471 |
for _, comment := range doc.List { |
| 1472 |
if isDirective(comment.Text) { |
| 1473 |
continue |
| 1474 |
} |
| 1475 |
|
| 1476 |
|
| 1477 |
lines := strings.Split(comment.Text, "\n") |
| 1478 |
tokFile := pgf.Tok |
| 1479 |
commentLine := safetoken.Line(tokFile, comment.Pos()) |
| 1480 |
uri := protocol.URIFromPath(tokFile.Name()) |
| 1481 |
for i, line := range lines { |
| 1482 |
lineStart := comment.Pos() |
| 1483 |
if i > 0 { |
| 1484 |
lineStart = tokFile.LineStart(commentLine + i) |
| 1485 |
} |
| 1486 |
for _, locs := range docRegexp.FindAllIndex([]byte(line), -1) { |
| 1487 |
edit, err := posEdit(tokFile, lineStart+token.Pos(locs[0]), lineStart+token.Pos(locs[1]), r.to) |
| 1488 |
if err != nil { |
| 1489 |
return nil, err |
| 1490 |
} |
| 1491 |
result[uri] = append(result[uri], edit) |
| 1492 |
} |
| 1493 |
} |
| 1494 |
} |
| 1495 |
} |
| 1496 |
|
| 1497 |
docLinkEdits, err := r.updateCommentDocLinks() |
| 1498 |
if err != nil { |
| 1499 |
return nil, err |
| 1500 |
} |
| 1501 |
for uri, edits := range docLinkEdits { |
| 1502 |
result[uri] = append(result[uri], edits...) |
| 1503 |
} |
| 1504 |
|
| 1505 |
return result, nil |
| 1506 |
} |
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
func (r *renamer) updateCommentDocLinks() (map[protocol.DocumentURI][]diff.Edit, error) { |
| 1512 |
result := make(map[protocol.DocumentURI][]diff.Edit) |
| 1513 |
var docRenamers []*docLinkRenamer |
| 1514 |
for obj := range r.objsToUpdate { |
| 1515 |
if _, ok := obj.(*types.PkgName); ok { |
| 1516 |
|
| 1517 |
if obj.Name() == "." { |
| 1518 |
continue |
| 1519 |
} |
| 1520 |
|
| 1521 |
docRenamers = append(docRenamers, &docLinkRenamer{ |
| 1522 |
isDep: false, |
| 1523 |
isPkgOrType: true, |
| 1524 |
file: r.pkg.FileSet().File(obj.Pos()), |
| 1525 |
regexp: docLinkPattern("", "", obj.Name(), true), |
| 1526 |
to: r.to, |
| 1527 |
}) |
| 1528 |
continue |
| 1529 |
} |
| 1530 |
if !obj.Exported() { |
| 1531 |
continue |
| 1532 |
} |
| 1533 |
recvName := "" |
| 1534 |
|
| 1535 |
|
| 1536 |
if !typesinternal.IsPackageLevel(obj) { |
| 1537 |
obj, isFunc := obj.(*types.Func) |
| 1538 |
if !isFunc { |
| 1539 |
continue |
| 1540 |
} |
| 1541 |
recv := obj.Signature().Recv() |
| 1542 |
if recv == nil { |
| 1543 |
continue |
| 1544 |
} |
| 1545 |
_, named := typesinternal.ReceiverNamed(recv) |
| 1546 |
if named == nil { |
| 1547 |
continue |
| 1548 |
} |
| 1549 |
|
| 1550 |
if types.IsInterface(named.Underlying()) { |
| 1551 |
continue |
| 1552 |
} |
| 1553 |
name := named.Origin().Obj() |
| 1554 |
if !name.Exported() || !typesinternal.IsPackageLevel(name) { |
| 1555 |
continue |
| 1556 |
} |
| 1557 |
recvName = name.Name() |
| 1558 |
} |
| 1559 |
|
| 1560 |
|
| 1561 |
pkgName := "" |
| 1562 |
if r.pkg.Types() != obj.Pkg() { |
| 1563 |
pkgName = obj.Pkg().Name() |
| 1564 |
} |
| 1565 |
_, isTypeName := obj.(*types.TypeName) |
| 1566 |
docRenamers = append(docRenamers, &docLinkRenamer{ |
| 1567 |
isDep: r.pkg.Types() != obj.Pkg(), |
| 1568 |
isPkgOrType: isTypeName, |
| 1569 |
packagePath: obj.Pkg().Path(), |
| 1570 |
packageName: pkgName, |
| 1571 |
recvName: recvName, |
| 1572 |
objName: obj.Name(), |
| 1573 |
regexp: docLinkPattern(pkgName, recvName, obj.Name(), isTypeName), |
| 1574 |
to: r.to, |
| 1575 |
}) |
| 1576 |
} |
| 1577 |
for _, pgf := range r.pkg.CompiledGoFiles() { |
| 1578 |
for _, d := range docRenamers { |
| 1579 |
edits, err := d.update(pgf) |
| 1580 |
if err != nil { |
| 1581 |
return nil, err |
| 1582 |
} |
| 1583 |
if len(edits) > 0 { |
| 1584 |
result[pgf.URI] = append(result[pgf.URI], edits...) |
| 1585 |
} |
| 1586 |
} |
| 1587 |
} |
| 1588 |
return result, nil |
| 1589 |
} |
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
func docLinkPattern(pkgName, recvName, objName string, isPkgOrType bool) *regexp.Regexp { |
| 1594 |
|
| 1595 |
pattern := `\[\*?` |
| 1596 |
if pkgName != "" { |
| 1597 |
pattern += pkgName + `\.` |
| 1598 |
} |
| 1599 |
if recvName != "" { |
| 1600 |
pattern += recvName + `\.` |
| 1601 |
} |
| 1602 |
|
| 1603 |
pattern += `(` + objName + `)` |
| 1604 |
|
| 1605 |
|
| 1606 |
if isPkgOrType { |
| 1607 |
pattern += `(?:\.\w+)*` |
| 1608 |
} |
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
pattern += `\](?:[^:]|$)` |
| 1614 |
|
| 1615 |
return regexp.MustCompile(pattern) |
| 1616 |
} |
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
type docLinkRenamer struct { |
| 1627 |
isDep bool |
| 1628 |
isPkgOrType bool |
| 1629 |
packagePath string |
| 1630 |
packageName string |
| 1631 |
recvName string |
| 1632 |
objName string |
| 1633 |
to string |
| 1634 |
regexp *regexp.Regexp |
| 1635 |
|
| 1636 |
file *token.File |
| 1637 |
} |
| 1638 |
|
| 1639 |
|
| 1640 |
func (r *docLinkRenamer) update(pgf *parsego.File) (result []diff.Edit, err error) { |
| 1641 |
if r.file != nil && r.file != pgf.Tok { |
| 1642 |
return nil, nil |
| 1643 |
} |
| 1644 |
pattern := r.regexp |
| 1645 |
|
| 1646 |
|
| 1647 |
if r.isDep { |
| 1648 |
for _, spec := range pgf.File.Imports { |
| 1649 |
importPath, _ := strconv.Unquote(spec.Path.Value) |
| 1650 |
if importPath == r.packagePath { |
| 1651 |
|
| 1652 |
if spec.Name == nil || spec.Name.Name == "_" || spec.Name.Name == "." { |
| 1653 |
continue |
| 1654 |
} |
| 1655 |
if spec.Name.Name != r.packageName { |
| 1656 |
pattern = docLinkPattern(spec.Name.Name, r.recvName, r.objName, r.isPkgOrType) |
| 1657 |
} |
| 1658 |
break |
| 1659 |
} |
| 1660 |
} |
| 1661 |
} |
| 1662 |
|
| 1663 |
var edits []diff.Edit |
| 1664 |
updateDocLinks := func(doc *ast.CommentGroup) error { |
| 1665 |
if doc != nil { |
| 1666 |
for _, c := range doc.List { |
| 1667 |
for _, locs := range pattern.FindAllStringSubmatchIndex(c.Text, -1) { |
| 1668 |
|
| 1669 |
edit, err := posEdit(pgf.Tok, c.Pos()+token.Pos(locs[2]), c.Pos()+token.Pos(locs[3]), r.to) |
| 1670 |
if err != nil { |
| 1671 |
return err |
| 1672 |
} |
| 1673 |
edits = append(edits, edit) |
| 1674 |
} |
| 1675 |
} |
| 1676 |
} |
| 1677 |
return nil |
| 1678 |
} |
| 1679 |
|
| 1680 |
|
| 1681 |
err = updateDocLinks(pgf.File.Doc) |
| 1682 |
if err != nil { |
| 1683 |
return nil, err |
| 1684 |
} |
| 1685 |
for _, decl := range pgf.File.Decls { |
| 1686 |
var doc *ast.CommentGroup |
| 1687 |
switch decl := decl.(type) { |
| 1688 |
case *ast.GenDecl: |
| 1689 |
doc = decl.Doc |
| 1690 |
case *ast.FuncDecl: |
| 1691 |
doc = decl.Doc |
| 1692 |
} |
| 1693 |
err = updateDocLinks(doc) |
| 1694 |
if err != nil { |
| 1695 |
return nil, err |
| 1696 |
} |
| 1697 |
} |
| 1698 |
return edits, nil |
| 1699 |
} |
| 1700 |
|
| 1701 |
|
| 1702 |
func docComment(pgf *parsego.File, curId inspector.Cursor) *ast.CommentGroup { |
| 1703 |
|
| 1704 |
id := curId.Node().(*ast.Ident) |
| 1705 |
for cur := range curId.Enclosing() { |
| 1706 |
switch decl := cur.Node().(type) { |
| 1707 |
case *ast.FuncDecl: |
| 1708 |
return decl.Doc |
| 1709 |
case *ast.Field: |
| 1710 |
return decl.Doc |
| 1711 |
case *ast.GenDecl: |
| 1712 |
return decl.Doc |
| 1713 |
|
| 1714 |
|
| 1715 |
case *ast.TypeSpec: |
| 1716 |
if decl.Doc != nil { |
| 1717 |
return decl.Doc |
| 1718 |
} |
| 1719 |
case *ast.ValueSpec: |
| 1720 |
if decl.Doc != nil { |
| 1721 |
return decl.Doc |
| 1722 |
} |
| 1723 |
case *ast.Ident: |
| 1724 |
case *ast.AssignStmt: |
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
if decl.Tok != token.DEFINE { |
| 1730 |
return nil |
| 1731 |
} |
| 1732 |
|
| 1733 |
identLine := safetoken.Line(pgf.Tok, id.Pos()) |
| 1734 |
for _, comment := range pgf.File.Comments { |
| 1735 |
if comment.Pos() > id.Pos() { |
| 1736 |
|
| 1737 |
continue |
| 1738 |
} |
| 1739 |
|
| 1740 |
lastCommentLine := safetoken.Line(pgf.Tok, comment.End()) |
| 1741 |
if lastCommentLine+1 == identLine { |
| 1742 |
return comment |
| 1743 |
} |
| 1744 |
} |
| 1745 |
default: |
| 1746 |
return nil |
| 1747 |
} |
| 1748 |
} |
| 1749 |
return nil |
| 1750 |
} |
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
func docCommentPosToIdent(pgf *parsego.File, start, end token.Pos, cur inspector.Cursor) (inspector.Cursor, bool) { |
| 1756 |
for curId := range cur.Preorder((*ast.Ident)(nil)) { |
| 1757 |
id := curId.Node().(*ast.Ident) |
| 1758 |
if start > id.Pos() { |
| 1759 |
continue |
| 1760 |
} |
| 1761 |
doc := docComment(pgf, curId) |
| 1762 |
if doc == nil || !(doc.Pos() <= start && end < doc.End()) { |
| 1763 |
continue |
| 1764 |
} |
| 1765 |
|
| 1766 |
docRegexp := regexp.MustCompile(`\b` + id.Name + `\b`) |
| 1767 |
for _, comment := range doc.List { |
| 1768 |
if isDirective(comment.Text) || !(comment.Pos() <= start && end < comment.End()) { |
| 1769 |
continue |
| 1770 |
} |
| 1771 |
commentStart := comment.Pos() |
| 1772 |
text, err := pgf.NodeText(comment) |
| 1773 |
if err != nil { |
| 1774 |
return inspector.Cursor{}, false |
| 1775 |
} |
| 1776 |
for _, locs := range docRegexp.FindAllIndex(text, -1) { |
| 1777 |
matchStart := commentStart + token.Pos(locs[0]) |
| 1778 |
matchEnd := commentStart + token.Pos(locs[1]) |
| 1779 |
if matchStart <= start && end <= matchEnd { |
| 1780 |
return curId, true |
| 1781 |
} |
| 1782 |
} |
| 1783 |
} |
| 1784 |
} |
| 1785 |
return inspector.Cursor{}, false |
| 1786 |
} |
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
func (r *renamer) updatePkgName(pgf *parsego.File, pkgName *types.PkgName) (diff.Edit, error) { |
| 1791 |
|
| 1792 |
path, _ := goastutil.PathEnclosingInterval(pgf.File, pkgName.Pos(), pkgName.Pos()) |
| 1793 |
if len(path) < 2 { |
| 1794 |
return diff.Edit{}, fmt.Errorf("no path enclosing interval for %s", pkgName.Name()) |
| 1795 |
} |
| 1796 |
spec, ok := path[1].(*ast.ImportSpec) |
| 1797 |
if !ok { |
| 1798 |
return diff.Edit{}, fmt.Errorf("failed to update PkgName for %s", pkgName.Name()) |
| 1799 |
} |
| 1800 |
|
| 1801 |
newText := "" |
| 1802 |
if pkgName.Imported().Name() != r.to { |
| 1803 |
newText = r.to + " " |
| 1804 |
} |
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
return posEdit(pgf.Tok, spec.Pos(), spec.Path.Pos(), newText) |
| 1810 |
} |
| 1811 |
|
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
func parsePackageNameDecl(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, rng protocol.Range) (*parsego.File, bool, error) { |
| 1818 |
pgf, err := snapshot.ParseGo(ctx, fh, parsego.Header) |
| 1819 |
if err != nil { |
| 1820 |
return nil, false, err |
| 1821 |
} |
| 1822 |
|
| 1823 |
|
| 1824 |
start, end, _ := pgf.RangePos(rng) |
| 1825 |
return pgf, astutil.NodeContains(pgf.File.Name, astutil.RangeOf(start, end)), nil |
| 1826 |
} |
| 1827 |
|
| 1828 |
|
| 1829 |
func posEdit(tf *token.File, start, end token.Pos, new string) (diff.Edit, error) { |
| 1830 |
startOffset, endOffset, err := safetoken.Offsets(tf, start, end) |
| 1831 |
if err != nil { |
| 1832 |
return diff.Edit{}, err |
| 1833 |
} |
| 1834 |
return diff.Edit{Start: startOffset, End: endOffset, New: new}, nil |
| 1835 |
} |