Skip to content

Commit

Permalink
Version 3.7.0-271.0.dev
Browse files Browse the repository at this point in the history
Merge 922ad1b into dev
  • Loading branch information
Dart CI committed Dec 19, 2024
2 parents 43723f2 + 922ad1b commit 7241137
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 212 deletions.
1 change: 0 additions & 1 deletion pkg/analysis_server/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ lib/src/handler/legacy/edit_get_available_refactorings.dart
lib/src/handler/legacy/search_find_element_references.dart
lib/src/lsp/handlers/handler_completion.dart
lib/src/lsp/handlers/handler_completion_resolve.dart
lib/src/lsp/handlers/handler_definition.dart
lib/src/lsp/handlers/handler_references.dart
lib/src/lsp/handlers/handler_rename.dart
lib/src/protocol_server.dart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'package:analysis_server/src/request_handler_mixin.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart';
import 'package:analyzer_plugin/protocol/protocol_generated.dart' as plugin;
import 'package:analyzer_plugin/src/utilities/navigation/navigation.dart';
import 'package:analyzer_plugin/utilities/navigation/navigation_dart.dart';
import 'package:analyzer_plugin/src/utilities/navigation/navigation_dart.dart';

/// The handler for the `analysis.getNavigation` request.
class AnalysisGetNavigationHandler extends LegacyHandler
Expand Down
2 changes: 1 addition & 1 deletion pkg/analysis_server/lib/src/legacy_analysis_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ import 'package:analyzer/src/utilities/cancellation.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart' hide Element;
import 'package:analyzer_plugin/src/utilities/client_uri_converter.dart';
import 'package:analyzer_plugin/src/utilities/navigation/navigation.dart';
import 'package:analyzer_plugin/utilities/navigation/navigation_dart.dart';
import 'package:analyzer_plugin/src/utilities/navigation/navigation_dart.dart';
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';
import 'package:telemetry/crash_reporting.dart';
Expand Down
53 changes: 26 additions & 27 deletions pkg/analysis_server/lib/src/lsp/handlers/handler_definition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ import 'package:analysis_server/src/plugin/result_merger.dart';
import 'package:analysis_server/src/protocol_server.dart' show NavigationTarget;
import 'package:analyzer/dart/analysis/results.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/source/line_info.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart' as protocol;
import 'package:analyzer_plugin/protocol/protocol_generated.dart' as plugin;
import 'package:analyzer_plugin/src/utilities/navigation/navigation.dart';
import 'package:analyzer_plugin/utilities/analyzer_converter.dart';
import 'package:analyzer_plugin/utilities/navigation/navigation_dart.dart';
import 'package:analyzer_plugin/src/utilities/navigation/navigation_dart.dart';

typedef StaticOptions = Either2<bool, DefinitionOptions>;

Expand Down Expand Up @@ -211,38 +209,42 @@ class DefinitionHandler
return otherResults.isNotEmpty ? otherResults : results;
}

/// Get the location of the code (excluding leading doc comments) for this element.
Future<protocol.Location?> _getCodeLocation(Element element) async {
Element? codeElement = element;
/// Get the location of the code (excluding doc comments) for this fragment.
Future<({int offset, int length})?> _getCodeLocation(
Fragment fragment,
) async {
Fragment? codeFragment = fragment;
// For synthetic getters created for fields, we need to access the associated
// variable to get the codeOffset/codeLength.
if (codeElement is PropertyAccessorElementImpl && codeElement.isSynthetic) {
codeElement = codeElement.variable2!;
if (codeFragment is PropertyAccessorFragment &&
codeFragment.element.isSynthetic) {
codeFragment = codeFragment.element.nonSynthetic2.firstFragment;
}

// For extension types, the primary constructor has a range that covers only
// the parameters / representation type but we want the whole declaration
// for the code range because otherwise previews will just show `(int a)`
// which is not what the user expects.
if (codeElement.enclosingElement3 case ExtensionTypeElement enclosingElement
when enclosingElement.primaryConstructor == codeElement) {
codeElement = enclosingElement;
if (codeFragment.element.enclosingElement2
case ExtensionTypeElement2 enclosingElement
when enclosingElement.primaryConstructor2 == codeFragment.element) {
codeFragment = codeFragment.enclosingFragment;
}

// Read the main codeOffset from the element. This may include doc comments
// but will give the correct end position.
int? codeOffset, codeLength;
if (codeElement is ElementImpl) {
codeOffset = codeElement.codeOffset;
codeLength = codeElement.codeLength;
if (codeFragment case ElementImpl codeFragment) {
codeOffset = codeFragment.codeOffset;
codeLength = codeFragment.codeLength;
}

if (codeOffset == null || codeLength == null) {
if (codeFragment == null || codeOffset == null || codeLength == null) {
return null;
}

// Read the declaration so we can get the offset after the doc comments.
var declaration = await _parsedDeclaration(codeElement);
var declaration = await _parsedDeclaration(codeFragment);
var node = declaration?.node;

if (node is VariableDeclaration) {
Expand All @@ -264,11 +266,7 @@ class DefinitionHandler
codeOffset = offsetAfterDocs;
}

return AnalyzerConverter().locationFromElement(
element,
offset: codeOffset,
length: codeLength,
);
return (offset: codeOffset, length: codeLength);
}

Location? _toLocation(
Expand Down Expand Up @@ -308,7 +306,7 @@ class DefinitionHandler
NavigationCollectorImpl collector,
) async {
for (var targetToUpdate in collector.targetsToUpdate) {
var codeLocation = await _getCodeLocation(targetToUpdate.element);
var codeLocation = await _getCodeLocation(targetToUpdate.fragment);
if (codeLocation != null) {
targetToUpdate.target
..codeOffset = codeLocation.offset
Expand All @@ -318,14 +316,15 @@ class DefinitionHandler
}

static Future<ElementDeclarationResult?> _parsedDeclaration(
Element element,
Fragment fragment,
) async {
var session = element.session;
var session = fragment.element.session;
if (session == null) {
return null;
}

var libraryPath = element.library?.source.fullName;
var libraryPath =
fragment.libraryFragment?.element.firstFragment.source.fullName;
if (libraryPath == null) {
return null;
}
Expand All @@ -335,7 +334,7 @@ class DefinitionHandler
return null;
}

return parsedLibrary.getElementDeclaration(element);
return parsedLibrary.getElementDeclaration2(fragment);
}
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/analysis_server/test/analysis/get_navigation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ final b = new Foo.named(); // 0
expect(target.length, 5);
}

Future<void> test_constructorInvocation_insideNullAwareElement_inList() async {
Future<void>
test_constructorInvocation_insideNullAwareElement_inList() async {
addTestFile('''
class Foo {
Foo() {}
Expand Down Expand Up @@ -472,7 +473,7 @@ part of foo;
assertHasRegionString('foo');
expect(testTargets, hasLength(1));
expect(testTargets[0].kind, ElementKind.LIBRARY);
assertHasFileTarget(partOfFile.path, 8, 3); // library [[foo]]
assertHasFileTarget(partOfFile.path, 0, 0);
}

Future<void> test_partOfDirective_uri() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,6 @@ class AbstractNavigationTest extends PubPackageAnalysisServerTest {
assertHasFileTarget(dartCoreFile.path, offset, length);
}

/// Validates that there is a target in [testTargets] with [testFile], at the
/// offset of [str] in [testFile], and with the length of [str].
void assertHasTargetString(String str) {
assertHasTarget(str, length: str.length);
}

/// Validates that there is not a region at [search] and with the given
/// [length].
void assertNoRegion(String search, int length) {
Expand Down Expand Up @@ -1476,7 +1470,7 @@ library my.lib;
''');
await prepareNavigation();
assertHasRegionString('my.lib');
assertHasTargetString('my.lib');
assertHasTarget('library', length: 0); // line 0, col 0 for unit targets.
}

Future<void>
Expand Down Expand Up @@ -1705,7 +1699,7 @@ void f() {
addTestFile('part of lib;');
await prepareNavigation();
assertHasRegionString('lib');
assertHasFileTarget(libFile, libCode.indexOf('lib;'), 'lib'.length);
assertHasFileTarget(libFile, 0, 0);
}

Future<void> test_propertyAccess_propertyName_read() async {
Expand Down Expand Up @@ -1788,7 +1782,7 @@ class A {
addTestFile('export "lib.dart";');
await prepareNavigation();
assertHasRegionString('"lib.dart"');
assertHasFileTarget(libFile, libCode.indexOf('lib;'), 'lib'.length);
assertHasFileTarget(libFile, 0, 0);
}

Future<void> test_string_export_unresolvedUri() async {
Expand All @@ -1803,7 +1797,7 @@ class A {
addTestFile('import "lib.dart";');
await prepareNavigation();
assertHasRegionString('"lib.dart"');
assertHasFileTarget(libFile, libCode.indexOf('lib;'), 'lib'.length);
assertHasFileTarget(libFile, 0, 0);
}

Future<void> test_string_import_noUri() async {
Expand Down
4 changes: 4 additions & 0 deletions pkg/analyzer/lib/src/dart/ast/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ extension IndexExpressionExtension on IndexExpression {
Element? get writeOrReadElement {
return _writeElement(this) ?? staticElement;
}

Element2? get writeOrReadElement2 {
return _writeElement2(this) ?? element;
}
}

extension ListOfFormalParameterExtension on List<FormalParameter> {
Expand Down
24 changes: 24 additions & 0 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,15 @@ class ConstructorElementImpl2 extends ExecutableElementImpl2
@override
ElementKind get kind => ElementKind.CONSTRUCTOR;

@override
Element2 get nonSynthetic2 {
if (isSynthetic) {
return enclosingElement2;
} else {
return this;
}
}

@override
ConstructorElement2? get redirectedConstructor2 =>
(firstFragment.redirectedConstructor?.declaration
Expand Down Expand Up @@ -10120,6 +10129,21 @@ abstract class PropertyInducingElementImpl2 extends VariableElementImpl2
return _fragments.any((f) => f.hasInitializer);
}

@override
Element2 get nonSynthetic2 {
if (isSynthetic) {
if (enclosingElement2 case EnumElementImpl2 enclosingElement2) {
// TODO(scheglov): remove 'index'?
if (name3 == 'index' || name3 == 'values') {
return enclosingElement2;
}
}
return (getter2 ?? setter2)!;
} else {
return this;
}
}

List<PropertyInducingElementImpl> get _fragments;
}

Expand Down
4 changes: 1 addition & 3 deletions pkg/analyzer_plugin/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ lib/src/utilities/completion/completion_target.dart
lib/src/utilities/completion/element_suggestion_builder.dart
lib/src/utilities/completion/optype.dart
lib/src/utilities/completion/suggestion_builder.dart
lib/src/utilities/navigation/navigation.dart
lib/src/utilities/navigation/navigation_dart.dart
lib/src/utilities/visitors/local_declaration_visitor.dart
lib/utilities/analyzer_converter.dart
lib/utilities/change_builder/change_builder_dart.dart
lib/utilities/completion/inherited_reference_contributor.dart
lib/utilities/completion/suggestion_builder.dart
lib/utilities/completion/type_member_contributor.dart
lib/utilities/navigation/navigation.dart
lib/utilities/navigation/navigation_dart.dart
lib/utilities/range_factory.dart
test/src/utilities/change_builder/change_builder_dart_test.dart
test/src/utilities/change_builder/dart/import_library_element_test.dart
Expand Down
20 changes: 10 additions & 10 deletions pkg/analyzer_plugin/lib/src/utilities/navigation/navigation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/element/element.dart' as analyzer;
import 'package:analyzer/dart/element/element2.dart' as analyzer;
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/source/source_range.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart';
Expand Down Expand Up @@ -58,19 +58,19 @@ class NavigationCollectorImpl implements NavigationCollector {
@override
void addRange(
SourceRange range, ElementKind targetKind, Location targetLocation,
{analyzer.Element? targetElement}) {
{analyzer.Fragment? targetFragment}) {
addRegion(range.offset, range.length, targetKind, targetLocation,
targetElement: targetElement);
targetFragment: targetFragment);
}

@override
void addRegion(
int offset, int length, ElementKind targetKind, Location targetLocation,
{analyzer.Element? targetElement}) {
{analyzer.Fragment? targetFragment}) {
var range = SourceRange(offset, length);
// add new target
var targets = regionMap.putIfAbsent(range, () => <int>[]);
var targetIndex = _addTarget(targetKind, targetLocation, targetElement);
var targetIndex = _addTarget(targetKind, targetLocation, targetFragment);
targets.add(targetIndex);
}

Expand All @@ -95,7 +95,7 @@ class NavigationCollectorImpl implements NavigationCollector {
}

int _addTarget(
ElementKind kind, Location location, analyzer.Element? element) {
ElementKind kind, Location location, analyzer.Fragment? fragment) {
var pair = Pair<ElementKind, Location>(kind, location);
var index = targetMap[pair];
if (index == null) {
Expand All @@ -106,8 +106,8 @@ class NavigationCollectorImpl implements NavigationCollector {
location.length, location.startLine, location.startColumn);
targets.add(target);
targetMap[pair] = index;
if (element != null) {
targetsToUpdate.add(TargetToUpdate(element, target));
if (fragment != null) {
targetsToUpdate.add(TargetToUpdate(fragment, target));
}
}
return index;
Expand All @@ -118,8 +118,8 @@ class NavigationCollectorImpl implements NavigationCollector {
///
/// If code location feature is enabled, we update [target] using [element].
class TargetToUpdate {
final analyzer.Element element;
final analyzer.Fragment fragment;
final NavigationTarget target;

TargetToUpdate(this.element, this.target);
TargetToUpdate(this.fragment, this.target);
}
Loading

0 comments on commit 7241137

Please sign in to comment.