Skip to content

Commit

Permalink
Version 3.4.0-258.0.dev
Browse files Browse the repository at this point in the history
Merge 87df8c4 into dev
  • Loading branch information
Dart CI committed Mar 21, 2024
2 parents b89d2de + 87df8c4 commit 1989304
Show file tree
Hide file tree
Showing 34 changed files with 515 additions and 450 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ vars = {
# EOL comment after a dependency to disable this and pin it at its current
# revision.

"args_rev": "7dc7fe0430709229ccc87c3eaae729aeffe10c56",
"args_rev": "788d93541a578e49f066699e1584bc3ce591c376",
"async_rev": "1556660ca5159d84bf28239825a25dca58f1fde3",
"bazel_worker_rev": "8619b92baa9959e55b9fc49d2afcd6dda2ec1c10",
"benchmark_harness_rev": "c8a0c8b1883dc16c7558a43cee1c3f4f9a163418",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,11 @@ class DeclarationHelper {
case MixinElement():
_suggestMixin(element, importData);
case PropertyAccessorElement():
// Do not add synthetic setters, as these may prevent adding getters,
// they are both tracked with the same name in the [VisibilityTracker].
if (element.isSynthetic && element.isSetter) {
break;
}
_suggestTopLevelProperty(element, importData);
case TopLevelVariableElement():
_suggestTopLevelVariable(element, importData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class VisibilityTracker {
/// whether the name of the element has already been added, in order to
/// prevent suggesting elements that are shadowed.
bool isVisible(Element? element) {
var name = element?.name;
var name = element?.displayName;
return name != null && declaredNames.add(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ class AddTypeAnnotation extends ResolvedCorrectionProducer {

Future<void> _applyChange(
ChangeBuilder builder, Token? keyword, Token name, DartType type) async {
_configureTargetLocation(node);

await builder.addDartFileEdit(file, (builder) {
if (builder.canWriteType(type)) {
if (keyword != null && keyword.keyword == Keyword.VAR) {
Expand All @@ -107,18 +105,6 @@ class AddTypeAnnotation extends ResolvedCorrectionProducer {
});
}

/// Configure the [utils] using the given [target].
void _configureTargetLocation(Object target) {
utils.targetClassElement = null;
if (target is AstNode) {
var targetClassDeclaration =
target.thisOrAncestorOfType<ClassDeclaration>();
if (targetClassDeclaration != null) {
utils.targetClassElement = targetClassDeclaration.declaredElement;
}
}
}

Future<void> _forDeclaredIdentifier(
ChangeBuilder builder, DeclaredIdentifier declaredIdentifier) async {
// Ensure that there isn't already a type annotation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ class CreateField extends CreateFieldOrGetter {
if (targetElement.library.isInSdk) {
return;
}
utils.targetClassElement = targetElement;
// prepare target ClassDeclaration
// Prepare target ClassDeclaration.
var targetDeclarationResult =
await sessionHelper.getElementDeclaration(targetElement);
if (targetDeclarationResult == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ class CreateFunction extends ResolvedCorrectionProducer {
}
insertOffset = enclosingMember.end;
sourcePrefix = '$eol$eol';
utils.targetClassElement = null;
// build method source
// Build method source.
await builder.addDartFileEdit(file, (builder) {
builder.addInsertion(insertOffset, (builder) {
builder.write(sourcePrefix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class CreateMissingOverrides extends ResolvedCorrectionProducer {
if (targetClass is! ClassDeclaration) {
return;
}
utils.targetClassElement = targetClass.declaredElement;
var signatures = [
...InheritanceOverrideVerifier.missingOverrides(targetClass),
...InheritanceOverrideVerifier.missingMustBeOverridden(targetClass)
Expand Down
216 changes: 0 additions & 216 deletions pkg/analysis_server/lib/src/services/correction/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import 'package:analyzer/dart/analysis/session.dart';
import 'package:analyzer/dart/ast/precedence.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/error/listener.dart';
import 'package:analyzer/file_system/file_system.dart';
Expand Down Expand Up @@ -546,21 +545,12 @@ final class CorrectionUtils {

final CompilationUnit unit;

final LibraryElement? _library;

final String _buffer;

/// The [ClassElement] the generated code is inserted to, so we can decide if
/// a type parameter may or may not be used.
InterfaceElement? targetClassElement;

ExecutableElement? targetExecutableElement;

String? _endOfLine;

CorrectionUtils(ParsedUnitResult result)
: unit = result.unit,
_library = result is ResolvedUnitResult ? result.libraryElement : null,
_buffer = result.content;

/// Returns the EOL to use for this [CompilationUnit].
Expand Down Expand Up @@ -809,84 +799,6 @@ final class CorrectionUtils {
return _buffer.substring(offset, offset + length);
}

/// Returns the source to reference [type] in this [CompilationUnit].
///
/// Fills [librariesToImport] with [LibraryElement]s whose elements are
/// used by the generated source, but not imported.
String? getTypeSource(DartType type, Set<Source> librariesToImport,
{StringBuffer? parametersBuffer}) {
var alias = type.alias;
if (alias != null) {
return _getTypeCodeElementArguments(
librariesToImport: librariesToImport,
element: alias.element,
isNullable: type.nullabilitySuffix == NullabilitySuffix.question,
typeArguments: alias.typeArguments,
);
}

if (type is DynamicType) {
return 'dynamic';
}

if (type is FunctionType) {
if (parametersBuffer == null) {
return 'Function';
}
parametersBuffer.write('(');
for (var parameter in type.parameters) {
var parameterType = getTypeSource(parameter.type, librariesToImport);
if (parametersBuffer.length != 1) {
parametersBuffer.write(', ');
}
parametersBuffer.write(parameterType);
parametersBuffer.write(' ');
parametersBuffer.write(parameter.name);
}
parametersBuffer.write(')');
return getTypeSource(type.returnType, librariesToImport);
}

if (type is InterfaceType) {
return _getTypeCodeElementArguments(
librariesToImport: librariesToImport,
element: type.element,
isNullable: type.nullabilitySuffix == NullabilitySuffix.question,
typeArguments: type.typeArguments,
);
}

if (type is InvalidType) {
return 'dynamic';
}

if (type is NeverType) {
return 'Never';
}

if (type is RecordType) {
return _getTypeCodeRecord(
librariesToImport: librariesToImport,
type: type,
);
}

if (type is TypeParameterType) {
var element = type.element;
if (_isTypeParameterVisible(element)) {
return element.name;
} else {
return 'dynamic';
}
}

if (type is VoidType) {
return 'void';
}

throw UnimplementedError('(${type.runtimeType}) $type');
}

/// Splits [text] into lines, and removes one level of indent from each line.
/// Lines that don't start with indentation are left as is.
String indentLeft(String text) {
Expand Down Expand Up @@ -1201,22 +1113,6 @@ final class CorrectionUtils {
selection, range.node(node));
}

/// Return the import element used to import given [element] into the library.
/// May be `null` if was not imported, i.e. declared in the same library.
LibraryImportElement? _getImportElement(Element element) {
var library = _library;
if (library == null) {
return null;
}
for (var imp in library.libraryImports) {
var definedNames = getImportNamespace(imp);
if (definedNames.containsValue(element)) {
return imp;
}
}
return null;
}

/// Returns a description of the place in which to insert a new directive or a
/// top-level declaration at the top of the file.
InsertionLocation _getInsertionLocationTop() {
Expand Down Expand Up @@ -1310,110 +1206,6 @@ final class CorrectionUtils {
return null;
}

String? _getTypeCodeElementArguments({
required Set<Source> librariesToImport,
required Element element,
required bool isNullable,
required List<DartType> typeArguments,
}) {
var sb = StringBuffer();

// check if imported
var library = element.library;
if (library != null && library != _library) {
// no source, if private
if (element.isPrivate) {
return null;
}
// ensure import
var importElement = _getImportElement(element);
if (importElement != null) {
var prefix = importElement.prefix?.element;
if (prefix != null) {
sb.write(prefix.displayName);
sb.write('.');
}
} else {
librariesToImport.add(library.source);
}
}

// append simple name
var name = element.displayName;
sb.write(name);

// append type arguments
if (typeArguments.isNotEmpty) {
sb.write('<');
for (var i = 0; i < typeArguments.length; i++) {
var argument = typeArguments[i];
if (i != 0) {
sb.write(', ');
}
var argumentSrc = getTypeSource(argument, librariesToImport);
if (argumentSrc != null) {
sb.write(argumentSrc);
} else {
return null;
}
}
sb.write('>');
}

// append nullability
if (isNullable) {
sb.write('?');
}

// done
return sb.toString();
}

String _getTypeCodeRecord({
required Set<Source> librariesToImport,
required RecordType type,
}) {
final buffer = StringBuffer();

final positionalFields = type.positionalFields;
final namedFields = type.namedFields;
final fieldCount = positionalFields.length + namedFields.length;
buffer.write('(');

var index = 0;
for (final field in positionalFields) {
buffer.write(
getTypeSource(field.type, librariesToImport),
);
if (index++ < fieldCount - 1) {
buffer.write(', ');
}
}

if (namedFields.isNotEmpty) {
buffer.write('{');
for (final field in namedFields) {
buffer.write(
getTypeSource(field.type, librariesToImport),
);
buffer.write(' ');
buffer.write(field.name);
if (index++ < fieldCount - 1) {
buffer.write(', ');
}
}
buffer.write('}');
}

buffer.write(')');

if (type.nullabilitySuffix == NullabilitySuffix.question) {
buffer.write('?');
}

return buffer.toString();
}

/// @return the [InvertedCondition] for the given logical expression.
_InvertedCondition _invertCondition0(Expression expression) {
if (expression is BooleanLiteral) {
Expand Down Expand Up @@ -1500,14 +1292,6 @@ final class CorrectionUtils {
return TokenUtils.getTokens(trimmedText, unit.featureSet).isEmpty;
}

/// Checks if [element] is visible in [targetExecutableElement] or
/// [targetClassElement].
bool _isTypeParameterVisible(TypeParameterElement element) {
var enclosing = element.enclosingElement;
return identical(enclosing, targetExecutableElement) ||
identical(enclosing, targetClassElement);
}

/// Return `true` if [selection] covers [range] and there are any
/// non-whitespace tokens between [selection] and [range] start/end.
bool _selectionIncludesNonWhitespaceOutsideRange(
Expand Down
Loading

0 comments on commit 1989304

Please sign in to comment.