Skip to content

Commit 12adc15

Browse files
authored
Add PrefixElement linkage to analyzer scope lookups (#2672)
* Start of prefix implementation... * dartfmt
1 parent a916e60 commit 12adc15

File tree

6 files changed

+64
-11
lines changed

6 files changed

+64
-11
lines changed

lib/src/markdown_processor.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ MatchingLinkResult _getMatchingLinkElementCommentReferable(
309309
var lookupResult =
310310
warnable.referenceBy(commentReference.referenceBy, filter: filter);
311311

312+
// TODO(jcollins-g): Referring to packages or other non-[ModelElement]s
313+
// might be needed here. Determine if that's the case.
314+
if (!(lookupResult is ModelElement)) {
315+
lookupResult = null;
316+
}
317+
312318
// TODO(jcollins-g): Consider prioritizing analyzer resolution before custom.
313319
return MatchingLinkResult(lookupResult);
314320
}

lib/src/model/comment_referable.dart

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,12 @@ mixin CommentReferable implements Nameable {
7979
/// Looks up references by [scope], skipping over results that do not match
8080
/// the given filter.
8181
///
82-
/// Override if [Scope.lookup] may return a [PrefixElement] or other elements
83-
/// not corresponding to a [CommentReferable], but you still want to have
84-
/// an implementation of [scope].
82+
/// Override if [Scope.lookup] may return elements not corresponding to a
83+
/// [CommentReferable], but you still want to have an implementation of
84+
/// [scope].
8585
CommentReferable lookupViaScope(ReferenceChildrenLookup referenceLookup,
8686
bool Function(CommentReferable) filter) {
8787
var resultElement = scope.lookupPreferGetter(referenceLookup.lookup);
88-
if (resultElement is PrefixElement) {
89-
assert(false,
90-
'PrefixElement detected, override [lookupViaScope] in subclass');
91-
return null;
92-
}
9388
if (resultElement == null) return null;
9489
var result = ModelElement.fromElement(resultElement, packageGraph);
9590
if (result is Accessor) {

lib/src/model/model_element.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import 'package:dartdoc/src/model/comment_referable.dart';
2222
import 'package:dartdoc/src/model/feature.dart';
2323
import 'package:dartdoc/src/model/feature_set.dart';
2424
import 'package:dartdoc/src/model/model.dart';
25+
import 'package:dartdoc/src/model/prefix.dart';
2526
import 'package:dartdoc/src/model_utils.dart' as utils;
2627
import 'package:dartdoc/src/render/model_element_renderer.dart';
2728
import 'package:dartdoc/src/render/parameter_renderer.dart';
@@ -286,6 +287,9 @@ abstract class ModelElement extends Canonicalization
286287
if (e is LibraryElement) {
287288
return Library(e, packageGraph);
288289
}
290+
if (e is PrefixElement) {
291+
return Prefix(e, library, packageGraph);
292+
}
289293
if (e is ClassElement) {
290294
if (e.isMixin) {
291295
return Mixin(e, library, packageGraph);

lib/src/model/parameter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Parameter extends ModelElement implements EnclosedElement {
3131

3232
@override
3333
String get filePath {
34-
throw StateError('filePath not implemented for parameters');
34+
throw UnimplementedError('Parameters have no generated files in dartdoc');
3535
}
3636

3737
@override

lib/src/model/prefix.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/dart/element/element.dart';
6+
import 'package:analyzer/dart/element/scope.dart';
7+
import 'package:dartdoc/src/model/comment_referable.dart';
8+
import 'package:dartdoc/src/model/library.dart';
9+
10+
import '../../dartdoc.dart';
11+
12+
/// Represents a [PrefixElement] for dartdoc.
13+
///
14+
/// Like [Parameter], it doesn't have doc pages, but participates in lookups.
15+
class Prefix extends ModelElement implements EnclosedElement {
16+
/// [library] is the library the prefix is defined in, not the [Library]
17+
/// referred to by the [PrefixElement].
18+
Prefix(PrefixElement element, Library library, PackageGraph packageGraph)
19+
: super(element, library, packageGraph);
20+
21+
@override
22+
// TODO(jcollins-g): consider allowing bare prefixes to link to a library doc?
23+
bool get isCanonical => false;
24+
25+
@override
26+
Scope get scope => element.scope;
27+
28+
@override
29+
PrefixElement get element => super.element;
30+
31+
@override
32+
ModelElement get enclosingElement => library;
33+
34+
@override
35+
String get filePath =>
36+
throw UnimplementedError('prefixes have no generated files in dartdoc');
37+
38+
@override
39+
String get href => null;
40+
41+
@override
42+
String get kind => 'prefix';
43+
44+
@override
45+
Map<String, CommentReferable> get referenceChildren => {};
46+
47+
@override
48+
Iterable<CommentReferable> get referenceParents => [library];
49+
}

test/end2end/model_test.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,8 +2264,7 @@ void main() {
22642264
equals(MatchingLinkResult(incorrectDocReferenceFromEx)));
22652265

22662266
// A prefixed constant in another library.
2267-
// TODO(jcollins-g): prefixed namespace lookups are not yet implemented with new lookup code.
2268-
expect(originalLookup(doAwesomeStuff, 'css.theOnlyThingInTheLibrary'),
2267+
expect(bothLookup(doAwesomeStuff, 'css.theOnlyThingInTheLibrary'),
22692268
equals(MatchingLinkResult(theOnlyThingInTheLibrary)));
22702269

22712270
// A name that exists in this package but is not imported.

0 commit comments

Comments
 (0)