@@ -160,7 +160,9 @@ class DartScopeBuilder extends VisitorDefault<void> with VisitorVoidMixin {
160160 //
161161 // A null name signals that the variable was synthetically introduced by the
162162 // compiler so they are skipped.
163- if ((decl.fileOffset < 0 || decl.fileOffset < _offset) && name != null ) {
163+ if ((decl.fileOffset < 0 || decl.fileOffset < _offset) &&
164+ ! decl.isWildcard &&
165+ name != null ) {
164166 _definitions[name] = decl.type;
165167 }
166168 super .visitVariableDeclaration (decl);
@@ -262,7 +264,7 @@ class DartScopeBuilder2 extends VisitorDefault<void> with VisitorVoidMixin {
262264 final Uri _scriptUri;
263265 final int _offset;
264266 final List <DartScope2 > findScopes = [];
265- final Set < int > foundOffsets = {} ;
267+ int ? closestLessOrEqualFoundOffset ;
266268
267269 final Set <VariableDeclaration > hoistedUnwritten = {};
268270 final List <List <VariableDeclaration >> scopes = [];
@@ -289,7 +291,8 @@ class DartScopeBuilder2 extends VisitorDefault<void> with VisitorVoidMixin {
289291 String ? name = decl.name;
290292 if (name != null &&
291293 ! decl.isSynthesized &&
292- ! hoistedUnwritten.contains (decl)) {
294+ ! hoistedUnwritten.contains (decl) &&
295+ ! decl.isWildcard) {
293296 definitions[name] = decl;
294297 }
295298 }
@@ -529,20 +532,32 @@ class DartScopeBuilder2 extends VisitorDefault<void> with VisitorVoidMixin {
529532 scopes.last.add (node);
530533 }
531534
535+ void _updateClosestFoundOffset (int offset) {
536+ if (offset > _offset) {
537+ // This offset is larger that the one we're looking for so we'll skip.
538+ return ;
539+ }
540+ if (closestLessOrEqualFoundOffset == null ||
541+ offset > closestLessOrEqualFoundOffset! ) {
542+ closestLessOrEqualFoundOffset = offset;
543+ return ;
544+ }
545+ }
546+
532547 void _checkOffset (TreeNode node) {
533- if (_currentUri == _scriptUri) {
534- foundOffsets. add (node.fileOffset);
535- if (node.fileOffset == _offset) {
536- addFound (node);
537- } else {
538- List < int > ? allOffsets = node.fileOffsetsIfMultiple;
539- if ( allOffsets != null ) {
540- for ( final int offset in allOffsets ) {
541- foundOffsets. add ( offset);
542- if (offset == _offset) {
543- addFound (node);
544- break ;
545- }
548+ if (_currentUri != _scriptUri) return ;
549+
550+ _updateClosestFoundOffset (node.fileOffset);
551+ if (node.fileOffset == _offset) {
552+ addFound (node);
553+ } else {
554+ List < int > ? allOffsets = node.fileOffsetsIfMultiple;
555+ if (allOffsets != null ) {
556+ for ( final int offset in allOffsets) {
557+ _updateClosestFoundOffset (offset);
558+ if (offset == _offset) {
559+ addFound (node) ;
560+ break ;
546561 }
547562 }
548563 }
@@ -553,36 +568,15 @@ class DartScopeBuilder2 extends VisitorDefault<void> with VisitorVoidMixin {
553568 Library library, Uri scriptUri, Class ? cls, int offset) {
554569 DartScopeBuilder2 data = _raw (library, scriptUri, cls, offset);
555570 if (data.findScopes.isEmpty) {
556- int ? closestMatchingOrSmallerOffset =
557- _findClosestMatchingOrSmallerOffset (data, offset);
558- if (closestMatchingOrSmallerOffset != null ) {
559- offset = closestMatchingOrSmallerOffset;
571+ int ? closestLessOrEqualFoundOffset = data.closestLessOrEqualFoundOffset;
572+ if (closestLessOrEqualFoundOffset != null ) {
573+ offset = closestLessOrEqualFoundOffset;
560574 data = _raw (library, scriptUri, cls, offset);
561575 }
562576 }
563577 return _findScopePick (data.findScopes, library, cls, offset);
564578 }
565579
566- static int ? _findClosestMatchingOrSmallerOffset (
567- DartScopeBuilder2 data, int offset) {
568- List <int > foundOffsets = data.foundOffsets.toList ()..sort ();
569- if (foundOffsets.isEmpty) return null ;
570- int low = 0 ;
571- int high = foundOffsets.length - 1 ;
572- while (low < high) {
573- int mid = high - ((high - low) >> 1 ); // Get middle, rounding up.
574- int pivot = foundOffsets[mid];
575- if (pivot <= offset) {
576- low = mid;
577- } else {
578- high = mid - 1 ;
579- }
580- }
581- int result = foundOffsets[low];
582- if (result < 0 ) return null ;
583- return result;
584- }
585-
586580 static DartScope _findScopePick (
587581 List <DartScope2 > scopes, Library library, Class ? cls, int offset) {
588582 DartScope2 scope;
@@ -619,10 +613,9 @@ class DartScopeBuilder2 extends VisitorDefault<void> with VisitorVoidMixin {
619613 Library library, Uri scriptUri, int offset) {
620614 DartScopeBuilder2 data = _rawNoClass (library, scriptUri, offset);
621615 if (data.findScopes.isEmpty) {
622- int ? closestMatchingOrSmallerOffset =
623- _findClosestMatchingOrSmallerOffset (data, offset);
624- if (closestMatchingOrSmallerOffset != null ) {
625- offset = closestMatchingOrSmallerOffset;
616+ int ? closestLessOrEqualFoundOffset = data.closestLessOrEqualFoundOffset;
617+ if (closestLessOrEqualFoundOffset != null ) {
618+ offset = closestLessOrEqualFoundOffset;
626619 data = _rawNoClass (library, scriptUri, offset);
627620 }
628621 }
0 commit comments