@@ -266,7 +266,7 @@ abstract class ExtractWidgetRefactoring implements Refactoring {
266266 bool isAvailable ();
267267}
268268
269- /// [Refactoring] to inline a local [VariableElement] .
269+ /// [Refactoring] to inline a local variable .
270270abstract class InlineLocalRefactoring implements Refactoring {
271271 /// Returns a new [InlineLocalRefactoring] instance.
272272 factory InlineLocalRefactoring (
@@ -277,7 +277,7 @@ abstract class InlineLocalRefactoring implements Refactoring {
277277 return InlineLocalRefactoringImpl (searchEngine, resolveResult, offset);
278278 }
279279
280- /// Returns the number of references to the [VariableElement] .
280+ /// Returns the number of references to the variable .
281281 int get referenceCount;
282282
283283 /// Returns the name of the variable being inlined.
@@ -294,7 +294,7 @@ abstract class InlineLocalRefactoring implements Refactoring {
294294 bool isAvailable ();
295295}
296296
297- /// [Refactoring] to inline an [ExecutableElement] .
297+ /// [Refactoring] to inline an executable element .
298298abstract class InlineMethodRefactoring implements Refactoring {
299299 /// Returns a new [InlineMethodRefactoring] instance.
300300 factory InlineMethodRefactoring (
@@ -399,13 +399,11 @@ class RefactoringWorkspace {
399399
400400 RefactoringWorkspace (this .drivers, this .searchEngine);
401401
402- /// Whether the [element] is defined in a file that is in a context root.
403- bool containsElement (Element element) {
404- return containsFile (element.source! .fullName);
405- }
406-
407402 /// Whether the [element] is defined in a file that is in a context root.
408403 bool containsElement2 (Element2 element) {
404+ if (element is MockLibraryImportElement ) {
405+ return containsFile (element.libraryFragment.source.fullName);
406+ }
409407 return containsFile (element.firstFragment.libraryFragment! .source.fullName);
410408 }
411409
@@ -424,233 +422,111 @@ class RefactoringWorkspace {
424422 }
425423}
426424
427- /// Abstract [Refactoring] for renaming some [Element] .
425+ /// Abstract [Refactoring] for renaming some element .
428426abstract class RenameRefactoring implements Refactoring {
429427 /// Returns the human-readable description of the kind of element being
430428 /// renamed (such as “class” or “function type alias”).
431429 String get elementKindName;
432430
433- /// Sets the new name for the [Element] .
431+ /// Sets the new name for the element .
434432 set newName (String newName);
435433
436- /// Returns the old name of the [Element] being renamed.
434+ /// Returns the old name of the element being renamed.
437435 String get oldName;
438436
439437 /// Validates that the [newName] is a valid identifier and is appropriate for
440- /// the type of the [Element] being renamed.
438+ /// the type of the element being renamed.
441439 ///
442440 /// It does not perform all the checks (such as checking for conflicts with
443441 /// any existing names in any of the scopes containing the current name), as
444442 /// many of these checks require search engine. Use [checkFinalConditions] for
445443 /// this level of checking.
446444 RefactoringStatus checkNewName ();
447445
448- /// Returns a new [RenameRefactoring] instance for renaming [element] ,
449- /// maybe `null` if there is no support for renaming [Element] s of the given
450- /// type.
451- static RenameRefactoring ? create (
446+ /// Returns a new [RenameRefactoring] instance for renaming the [element] .
447+ ///
448+ /// Returns `null` if there is no support for renaming elements of the given
449+ /// kind.
450+ static RenameRefactoring ? create2 (
452451 RefactoringWorkspace workspace,
453452 ResolvedUnitResult resolvedUnit,
454- Element ? element,
453+ Element2 ? element,
455454 ) {
456455 if (element == null ) {
457456 return null ;
458457 }
459458 var session = resolvedUnit.session;
460459 var sessionHelper = AnalysisSessionHelper (session);
461- if (element is PropertyAccessorElement ) {
462- element = element.variable2 ;
460+ if (element is PropertyAccessorElement2 ) {
461+ element = element.variable3 ;
463462 if (element == null ) {
464463 return null ;
465464 }
466465 }
467- if (element is LibraryImportElement ) {
466+ if (element is MockLibraryImportElement ) {
468467 return RenameImportRefactoringImpl (
469468 workspace,
470469 sessionHelper,
471- element.asElement2 ,
470+ element.import ,
472471 );
473472 }
474- var enclosingElement = element.enclosingElement3;
475- if (enclosingElement is CompilationUnitElement ) {
473+ var enclosingElement = element.enclosingElement2;
474+ if (element is PrefixElement2 ) {
475+ enclosingElement = element.library2;
476+ }
477+ if (enclosingElement is LibraryElement2 ) {
476478 return RenameUnitMemberRefactoringImpl (
477479 workspace,
478480 sessionHelper,
479481 resolvedUnit,
480- element.asElement2 ! ,
482+ element,
481483 );
482484 }
483- if (element is ConstructorElement ) {
485+ if (element is ConstructorElement2 ) {
484486 return RenameConstructorRefactoringImpl (
485487 workspace,
486488 sessionHelper,
487- element.asElement2 ,
489+ element,
488490 );
489491 }
490- if (element is LabelElement ) {
491- return RenameLabelRefactoringImpl (
492- workspace,
493- sessionHelper,
494- element.asElement2 as LabelElement2 ,
495- );
492+ if (element is LabelElement2 ) {
493+ return RenameLabelRefactoringImpl (workspace, sessionHelper, element);
496494 }
497- if (element is LibraryElement ) {
498- return RenameLibraryRefactoringImpl (
499- workspace,
500- sessionHelper,
501- element.asElement2,
502- );
495+ if (element is LibraryElement2 ) {
496+ return RenameLibraryRefactoringImpl (workspace, sessionHelper, element);
503497 }
504- if (element is ParameterElement ) {
505- return RenameParameterRefactoringImpl (
506- workspace,
507- sessionHelper,
508- element.asElement2,
509- );
498+ if (element is FormalParameterElement ) {
499+ return RenameParameterRefactoringImpl (workspace, sessionHelper, element);
510500 }
511- if (element is LocalElement ) {
512- return RenameLocalRefactoringImpl (
513- workspace,
514- sessionHelper,
515- element.asElement2 as LocalElement2 ,
516- );
501+ if (element is LocalElement2 ) {
502+ return RenameLocalRefactoringImpl (workspace, sessionHelper, element);
517503 }
518- if (element is TypeParameterElement ) {
504+ if (element is TypeParameterElement2 ) {
519505 return RenameTypeParameterRefactoringImpl (
520506 workspace,
521507 sessionHelper,
522- element.asElement2 ,
508+ element,
523509 );
524510 }
525- if (enclosingElement is InterfaceElement ) {
511+ if (enclosingElement is InterfaceElement2 ) {
526512 return RenameClassMemberRefactoringImpl (
527513 workspace,
528514 sessionHelper,
529- enclosingElement.asElement2 ,
530- element.asElement2 ! ,
515+ enclosingElement,
516+ element,
531517 );
532518 }
533- if (enclosingElement is ExtensionElement ) {
519+ if (enclosingElement is ExtensionElement2 ) {
534520 return RenameExtensionMemberRefactoringImpl (
535521 workspace,
536522 sessionHelper,
537- enclosingElement.asElement2 ,
538- element.asElement2 ! ,
523+ enclosingElement,
524+ element,
539525 );
540526 }
541527 return null ;
542528 }
543529
544- /// Returns a new [RenameRefactoring] instance for renaming the [element] .
545- ///
546- /// Returns `null` if there is no support for renaming elements of the given
547- /// kind.
548- static RenameRefactoring ? create2 (
549- RefactoringWorkspace workspace,
550- ResolvedUnitResult resolvedUnit,
551- Element2 ? element,
552- ) {
553- return create (workspace, resolvedUnit, element.asElement);
554- }
555-
556- /// Given a node/element, finds the best element to rename (for example
557- /// the class when on the `new` keyword).
558- static RenameRefactoringElement ? getElementToRename (
559- AstNode node,
560- Element ? element,
561- ) {
562- // TODO(scheglov): This is bad code.
563- SyntacticEntity ? nameNode;
564- if (node is AssignedVariablePattern ) {
565- nameNode = node.name;
566- } else if (node is ConstructorDeclaration ) {
567- nameNode = node;
568- } else if (node is ConstructorSelector ) {
569- nameNode = node;
570- } else if (node is DeclaredIdentifier ) {
571- nameNode = node.name;
572- } else if (node is DeclaredVariablePattern ) {
573- nameNode = node.name;
574- } else if (node is EnumConstantDeclaration ) {
575- nameNode = node.name;
576- } else if (node is ExtensionDeclaration ) {
577- nameNode = node.name;
578- } else if (node is ExtensionOverride ) {
579- nameNode = node.name;
580- } else if (node is FieldFormalParameter ) {
581- nameNode = node.name;
582- } else if (node is ImportDirective ) {
583- nameNode = node;
584- } else if (node is ImportPrefixReference ) {
585- nameNode = node;
586- } else if (node is InstanceCreationExpression ) {
587- nameNode = node;
588- } else if (node is LibraryDirective ) {
589- nameNode = node;
590- } else if (node is MethodDeclaration ) {
591- nameNode = node.name;
592- } else if (node is NamedCompilationUnitMember ) {
593- nameNode = node.name;
594- } else if (node is NamedType ) {
595- nameNode = node.name2;
596- } else if (node is RepresentationConstructorName ) {
597- nameNode = node.name;
598- } else if (node is RepresentationDeclaration ) {
599- nameNode = node.fieldName;
600- } else if (node is SimpleFormalParameter ) {
601- nameNode = node.name;
602- } else if (node is SimpleIdentifier ) {
603- nameNode = node.token;
604- } else if (node is TypeParameter ) {
605- nameNode = node.name;
606- } else if (node is VariableDeclaration ) {
607- nameNode = node.name;
608- }
609- if (nameNode == null ) {
610- return null ;
611- }
612- var offset = nameNode.offset;
613- var length = nameNode.length;
614-
615- if (node is SimpleIdentifier && element is ParameterElement ) {
616- element = declaredParameterElement (node, element);
617- }
618-
619- Element2 ? element2;
620- // Use the prefix offset/length when renaming an import directive.
621- if (element is LibraryImportElement ) {
622- if (node is ImportDirective ) {
623- var prefix = node.prefix;
624- if (prefix != null ) {
625- offset = prefix.offset;
626- length = prefix.length;
627- } else {
628- // -1 means the name does not exist yet.
629- offset = - 1 ;
630- length = 0 ;
631- }
632- }
633- var importNode = node.thisOrAncestorOfType <ImportDirective >();
634- if (importNode != null ) {
635- element2 = MockLibraryImportElement (importNode.libraryImport! );
636- }
637- }
638-
639- // Rename the class when on `new` in an instance creation.
640- if (node is InstanceCreationExpression ) {
641- var namedType = node.constructorName.type;
642- element = namedType.element;
643- offset = namedType.name2.offset;
644- length = namedType.name2.length;
645- }
646-
647- if (element == null ) {
648- return null ;
649- }
650-
651- return RenameRefactoringElement (element, element2, offset, length);
652- }
653-
654530 /// Returns the best element to rename based on the [node] and [element] (for
655531 /// example, the class when on the `new` keyword).
656532 static RenameRefactoringElement ? getElementToRename2 (
@@ -711,7 +587,7 @@ abstract class RenameRefactoring implements Refactoring {
711587 var length = nameNode.length;
712588
713589 if (node is SimpleIdentifier && element is FormalParameterElement ) {
714- element = declaredParameterElement (node, element.asElement).asElement2 ;
590+ element = declaredParameterElement2 (node, element) ;
715591 }
716592
717593 // Use the prefix offset/length when renaming an import directive.
@@ -741,29 +617,20 @@ abstract class RenameRefactoring implements Refactoring {
741617 return null ;
742618 }
743619
744- return RenameRefactoringElement (
745- element.asElement! ,
746- element,
747- offset,
748- length,
749- );
620+ return RenameRefactoringElement (element, offset, length);
750621 }
751622}
752623
753624class RenameRefactoringElement {
754- final Element element;
755- final Element2 ? _element2;
625+ final Element2 _element2;
756626 final int offset;
757627 final int length;
758628
759- RenameRefactoringElement (
760- this .element,
761- this ._element2,
762- this .offset,
763- this .length,
764- );
629+ RenameRefactoringElement (this ._element2, this .offset, this .length);
630+
631+ Element get element => element2.asElement! ;
765632
766633 Element2 get element2 {
767- return _element2 ?? element.asElement2 ! ;
634+ return _element2;
768635 }
769636}
0 commit comments