Skip to content

Commit

Permalink
Elements. Migrate SurroundWith.
Browse files Browse the repository at this point in the history
Change-Id: Ia603ebff824012b8992bdcc5e93bf5a98f1daa05
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/387424
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Sep 30, 2024
1 parent 2008dd6 commit 6fdb0fb
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/analysis_server/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ lib/src/services/correction/dart/sort_unnamed_constructor_first.dart
lib/src/services/correction/dart/split_and_condition.dart
lib/src/services/correction/dart/split_multiple_declarations.dart
lib/src/services/correction/dart/split_variable_declaration.dart
lib/src/services/correction/dart/surround_with.dart
lib/src/services/correction/dart/surround_with_parentheses.dart
lib/src/services/correction/dart/update_sdk_constraints.dart
lib/src/services/correction/dart/use_curly_braces.dart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,10 @@ class _SurroundWithSetState extends _SurroundWith {

@override
Future<void> compute(ChangeBuilder builder) async {
var classElement =
node.parent?.thisOrAncestorOfType<ClassDeclaration>()?.declaredElement;
var classElement = node.parent
?.thisOrAncestorOfType<ClassDeclaration>()
?.declaredFragment
?.element;
if (classElement != null && classElement.isState) {
await builder.addDartFileEdit(file, (builder) {
builder.addReplacement(statementsRange, (builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import 'package:analysis_server/src/services/correction/fix/data_driven/element_kind.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/element2.dart' hide ElementKind;
import 'package:analyzer/dart/element/type.dart';

/// A description of an element.
Expand Down
69 changes: 69 additions & 0 deletions pkg/analysis_server/lib/src/utilities/extensions/flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:analysis_server/src/utilities/strings.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:collection/collection.dart';

Expand Down Expand Up @@ -177,6 +178,18 @@ extension ClassElementExtension on ClassElement {
bool get isStatefulWidgetDeclaration => supertype.isExactlyStatefulWidgetType;
}

extension ClassElementExtension2 on ClassElement2 {
/// Whether this is the Flutter class `State`.
bool get isExactState => _isExactly(_nameState, _uriFramework);

/// Whether this has the Flutter class `State` as a superclass.
bool get isState => _hasSupertype(_uriFramework, _nameState);

/// Whether this is a [ClassElement2] that extends the Flutter class
/// `StatefulWidget`.
bool get isStatefulWidgetDeclaration => supertype.isExactlyStatefulWidgetType;
}

extension DartTypeExtension on DartType? {
/// Whether this is the 'dart.ui' class `Color`, or a subtype.
bool get isColor {
Expand Down Expand Up @@ -459,3 +472,59 @@ extension InterfaceElementExtension on InterfaceElement? {
return self is ClassElement && self.name == type && self.source.uri == uri;
}
}

extension InterfaceElementExtension2 on InterfaceElement2? {
/// Whether this is the Flutter class `Alignment`.
bool get isExactAlignment {
return _isExactly('Alignment', _uriAlignment);
}

/// Whether this is the Flutter class `AlignmentDirectional`.
bool get isExactAlignmentDirectional {
return _isExactly('AlignmentDirectional', _uriAlignment);
}

/// Whether this is the Flutter class `AlignmentGeometry`.
bool get isExactAlignmentGeometry {
return _isExactly('AlignmentGeometry', _uriAlignment);
}

/// Whether this is the Flutter class `Widget`, or a subtype.
bool get isWidget {
var self = this;
if (self is! ClassElement2) {
return false;
}
if (_isExactly(_nameWidget, _uriFramework)) {
return true;
}
return self.allSupertypes
.any((type) => type.element._isExactly(_nameWidget, _uriFramework));
}

/// Whether this has a supertype with the [requiredName] defined in the file
/// with the [requiredUri].
bool _hasSupertype(Uri requiredUri, String requiredName) {
var self = this;
if (self == null) {
return false;
}
for (var type in self.allSupertypes) {
if (type.element.name == requiredName) {
var uri = type.element.source.uri;
if (uri == requiredUri) {
return true;
}
}
}
return false;
}

/// Whether this is the exact [type] defined in the file with the given [uri].
bool _isExactly(String type, Uri uri) {
var self = this;
return self is ClassElement2 &&
self.name == type &&
self.firstFragment.libraryFragment.source.uri == uri;
}
}

0 comments on commit 6fdb0fb

Please sign in to comment.