@@ -25,6 +25,7 @@ import 'package:analyzer/dart/element/type.dart';
25
25
import 'package:analyzer/dart/element/visitor.dart' ;
26
26
import 'package:analyzer/file_system/file_system.dart' as file_system;
27
27
import 'package:analyzer/file_system/physical_file_system.dart' ;
28
+ import 'package:analyzer/source/line_info.dart' ;
28
29
import 'package:analyzer/src/context/builder.dart' ;
29
30
import 'package:analyzer/src/dart/analysis/byte_store.dart' ;
30
31
import 'package:analyzer/src/dart/analysis/driver.dart' ;
@@ -50,7 +51,6 @@ import 'package:crypto/crypto.dart';
50
51
import 'package:dartdoc/src/dartdoc_options.dart' ;
51
52
import 'package:dartdoc/src/element_type.dart' ;
52
53
import 'package:dartdoc/src/io_utils.dart' ;
53
- import 'package:dartdoc/src/line_number_cache.dart' ;
54
54
import 'package:dartdoc/src/logging.dart' ;
55
55
import 'package:dartdoc/src/markdown_processor.dart' show Documentation;
56
56
import 'package:dartdoc/src/model_utils.dart' as utils;
@@ -1390,6 +1390,17 @@ class Constructor extends ModelElement
1390
1390
ConstructorElement element, Library library, PackageGraph packageGraph)
1391
1391
: super (element, library, packageGraph, null );
1392
1392
1393
+ @override
1394
+ CharacterLocation get characterLocation {
1395
+ if (element.isSynthetic) {
1396
+ // Make warnings for a synthetic constructor refer to somewhere reasonable
1397
+ // since a synthetic constructor has no definition independent of the
1398
+ // parent class.
1399
+ return enclosingElement.characterLocation;
1400
+ }
1401
+ return super .characterLocation;
1402
+ }
1403
+
1393
1404
@override
1394
1405
// TODO(jcollins-g): Revisit this when dart-lang/sdk#31517 is implemented.
1395
1406
List <TypeParameter > get typeParameters =>
@@ -2057,7 +2068,7 @@ class Field extends ModelElement
2057
2068
}
2058
2069
2059
2070
/// Mixin for top-level variables and fields (aka properties)
2060
- abstract class GetterSetterCombo implements ModelElement {
2071
+ mixin GetterSetterCombo on ModelElement {
2061
2072
Accessor get getter;
2062
2073
2063
2074
Iterable <Accessor > get allAccessors sync * {
@@ -2114,6 +2125,20 @@ abstract class GetterSetterCombo implements ModelElement {
2114
2125
return const HtmlEscape (HtmlEscapeMode .unknown).convert (result);
2115
2126
}
2116
2127
2128
+ @override
2129
+ CharacterLocation get characterLocation {
2130
+ // Handle all synthetic possibilities. Ordinarily, warnings for
2131
+ // explicit setters/getters will be handled by those objects, but
2132
+ // if a warning comes up for an enclosing synthetic field we have to
2133
+ // put it somewhere. So pick an accessor.
2134
+ if (element.isSynthetic) {
2135
+ if (hasExplicitGetter) return getter.characterLocation;
2136
+ if (hasExplicitSetter) return setter.characterLocation;
2137
+ assert (false , 'Field and accessors can not all be synthetic' );
2138
+ }
2139
+ return super .characterLocation;
2140
+ }
2141
+
2117
2142
String get constantValue => linkifyConstantValue (constantValueBase);
2118
2143
2119
2144
String get constantValueTruncated =>
@@ -2367,6 +2392,18 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
2367
2392
2368
2393
List <Class > get allClasses => _allClasses;
2369
2394
2395
+ @override
2396
+ CharacterLocation get characterLocation {
2397
+ if (element.nameOffset == - 1 ) {
2398
+ assert (isAnonymous, 'Only anonymous libraries are allowed to have no declared location' );
2399
+ return CharacterLocation (1 , 1 );
2400
+ }
2401
+ return super .characterLocation;
2402
+ }
2403
+
2404
+ @override
2405
+ CompilationUnitElement get compilationUnitElement => (element as LibraryElement ).definingCompilationUnit;
2406
+
2370
2407
@override
2371
2408
Iterable <Class > get classes {
2372
2409
return _allClasses
@@ -2917,6 +2954,19 @@ class Method extends ModelElement
2917
2954
}).toList ();
2918
2955
}
2919
2956
2957
+ @override
2958
+ CharacterLocation get characterLocation {
2959
+ if (enclosingElement is Enum && name == 'toString' ) {
2960
+ // The toString() method on Enums is special, treated as not having
2961
+ // a definition location by the analyzer yet not being inherited, either.
2962
+ // Just directly override our location with the Enum definition --
2963
+ // this is OK because Enums can not inherit from each other nor
2964
+ // have their definitions split between files.
2965
+ return enclosingElement.characterLocation;
2966
+ }
2967
+ return super .characterLocation;
2968
+ }
2969
+
2920
2970
@override
2921
2971
ModelElement get enclosingElement {
2922
2972
if (_enclosingClass == null ) {
@@ -3078,7 +3128,7 @@ abstract class Privacy {
3078
3128
/// ModelElement will reference itself as part of the "wrong" [Library]
3079
3129
/// from the public interface perspective.
3080
3130
abstract class ModelElement extends Canonicalization
3081
- with Privacy , Warnable , Nameable , SourceCodeMixin , Indexable
3131
+ with Privacy , Warnable , Locatable , Nameable , SourceCodeMixin , Indexable
3082
3132
implements Comparable , Documentable {
3083
3133
final Element _element;
3084
3134
@@ -3715,8 +3765,8 @@ abstract class ModelElement extends Canonicalization
3715
3765
@override
3716
3766
String get location {
3717
3767
// Call nothing from here that can emit warnings or you'll cause stack overflows.
3718
- if (lineAndColumn != null ) {
3719
- return "(${path .toUri (sourceFileName )}:${lineAndColumn . item1 }:${ lineAndColumn . item2 })" ;
3768
+ if (characterLocation != null ) {
3769
+ return "(${path .toUri (sourceFileName )}:${characterLocation . toString () })" ;
3720
3770
}
3721
3771
return "(${path .toUri (sourceFileName )})" ;
3722
3772
}
@@ -3751,19 +3801,25 @@ abstract class ModelElement extends Canonicalization
3751
3801
3752
3802
String get sourceFileName => element.source.fullName;
3753
3803
3754
- Tuple2 < int , int > _lineAndColumn ;
3755
- bool _isLineNumberComputed = false ;
3804
+ CharacterLocation _characterLocation ;
3805
+ bool _characterLocationIsSet = false ;
3756
3806
3757
3807
@override
3758
- Tuple2 <int , int > get lineAndColumn {
3759
- // TODO(jcollins-g): implement lineAndColumn for explicit fields
3760
- if (! _isLineNumberComputed) {
3761
- _lineAndColumn = lineNumberCache.lineAndColumn (
3762
- element.source.fullName, element.nameOffset);
3808
+ CharacterLocation get characterLocation {
3809
+ if (! _characterLocationIsSet) {
3810
+ LineInfo lineInfo = compilationUnitElement.lineInfo;
3811
+ _characterLocationIsSet = true ;
3812
+ assert (element.nameOffset >= 0 , 'Invalid location data for element: $fullyQualifiedName ' );
3813
+ assert (lineInfo != null , 'No lineInfo data available for element: $fullyQualifiedName ' );
3814
+ if (element.nameOffset >= 0 ) {
3815
+ _characterLocation = lineInfo? .getLocation (element.nameOffset);
3816
+ }
3763
3817
}
3764
- return _lineAndColumn ;
3818
+ return _characterLocation ;
3765
3819
}
3766
3820
3821
+ CompilationUnitElement get compilationUnitElement => element.getAncestor ((e) => e is CompilationUnitElement );
3822
+
3767
3823
bool get hasAnnotations => annotations.isNotEmpty;
3768
3824
3769
3825
@override
@@ -4191,8 +4247,8 @@ abstract class ModelElement extends Canonicalization
4191
4247
warn (PackageWarning .toolError, message: message),
4192
4248
content: basicMatch[2 ],
4193
4249
environment: {
4194
- 'SOURCE_LINE' : lineAndColumn ? .item1 ? .toString (),
4195
- 'SOURCE_COLUMN' : lineAndColumn ? .item2 ? .toString (),
4250
+ 'SOURCE_LINE' : characterLocation ? .lineNumber .toString (),
4251
+ 'SOURCE_COLUMN' : characterLocation ? .columnNumber .toString (),
4196
4252
'SOURCE_PATH' : (sourceFileName == null ||
4197
4253
package? .packagePath == null )
4198
4254
? null
@@ -5153,13 +5209,6 @@ class PackageGraph {
5153
5209
warnable = warnable.enclosingElement;
5154
5210
}
5155
5211
}
5156
- if (warnable is Accessor ) {
5157
- // This might be part of a Field, if so, assign this warning to the field
5158
- // rather than the Accessor.
5159
- if ((warnable as Accessor ).enclosingCombo != null ) {
5160
- warnable = (warnable as Accessor ).enclosingCombo;
5161
- }
5162
- }
5163
5212
} else {
5164
5213
// If we don't have an element, we need a message to disambiguate.
5165
5214
assert (message != null );
@@ -5224,9 +5273,6 @@ class PackageGraph {
5224
5273
break ;
5225
5274
case PackageWarning .unresolvedDocReference:
5226
5275
warningMessage = "unresolved doc reference [${message }]" ;
5227
- if (referredFrom == null ) {
5228
- referredFrom = warnable.documentationFrom;
5229
- }
5230
5276
referredFromPrefix = 'in documentation inherited from' ;
5231
5277
break ;
5232
5278
case PackageWarning .unknownMacro:
@@ -5273,14 +5319,14 @@ class PackageGraph {
5273
5319
List <String > messageParts = [warningMessage];
5274
5320
if (warnable != null ) {
5275
5321
messageParts
5276
- .add ("${ warnablePrefix } ${ warnableName } : ${ warnable .location ?? '' } " );
5322
+ .add ("$warnablePrefix $ warnableName : $warnable .location" );
5277
5323
}
5278
5324
if (referredFrom != null ) {
5279
5325
for (Locatable referral in referredFrom) {
5280
5326
if (referral != warnable) {
5281
5327
var referredFromStrings = _safeWarnableName (referral);
5282
5328
messageParts.add (
5283
- "${ referredFromPrefix } ${ referredFromStrings } : ${referral .location ?? '' }" );
5329
+ "$referredFromPrefix $ referredFromStrings : ${referral .location }" );
5284
5330
}
5285
5331
}
5286
5332
}
@@ -5957,6 +6003,7 @@ abstract class MarkdownFileDocumentation
5957
6003
class Category extends Nameable
5958
6004
with
5959
6005
Warnable ,
6006
+ Locatable ,
5960
6007
Canonicalization ,
5961
6008
MarkdownFileDocumentation ,
5962
6009
LibraryContainer ,
@@ -6510,7 +6557,7 @@ class Parameter extends ModelElement implements EnclosedElement {
6510
6557
abstract class SourceCodeMixin implements Documentable {
6511
6558
ModelNode get modelNode;
6512
6559
6513
- Tuple2 < int , int > get lineAndColumn ;
6560
+ CharacterLocation get characterLocation ;
6514
6561
6515
6562
Element get element;
6516
6563
0 commit comments