Skip to content

Commit f157b0c

Browse files
committed
handle doc references to top-level constants
Closes #762 order imports Squashed commit of the following: commit 4e528dc Author: Seth Ladd <[email protected]> Date: Wed Aug 5 08:56:09 2015 -0700 order imports commit 05f7941 Author: Seth Ladd <[email protected]> Date: Wed Aug 5 08:54:12 2015 -0700 disable formatting checks, because of version diffs commit f36c0fd Author: Seth Ladd <[email protected]> Date: Tue Aug 4 19:16:39 2015 -0700 upgrade analyzer, add more tests, skip tests we don't have a solution for right now commit d8a730d Author: Seth Ladd <[email protected]> Date: Tue Aug 4 17:52:12 2015 -0700 add a skip to a test, we'll take care of later commit f614435 Merge: 802c3ef 6b1d987 Author: Seth Ladd <[email protected]> Date: Tue Aug 4 17:37:20 2015 -0700 Merge remote-tracking branch 'origin/master' into sl-underscores commit 802c3ef Author: Seth Ladd <[email protected]> Date: Tue Aug 4 16:07:40 2015 -0700 WIP commit 1250b18 Author: Seth Ladd <[email protected]> Date: Tue Aug 4 12:50:22 2015 -0700 lots more tests commit dea14d4 Author: Seth Ladd <[email protected]> Date: Mon Aug 3 22:40:31 2015 -0700 handle doc references to top-level constants commit a66b2db Author: Seth Ladd <[email protected]> Date: Mon Aug 3 22:40:09 2015 -0700 handle doc references to top-level constants
1 parent 6b1d987 commit f157b0c

File tree

9 files changed

+202
-67
lines changed

9 files changed

+202
-67
lines changed

lib/markdown_processor.dart

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import 'package:analyzer/src/generated/element.dart'
1111
LibraryElement,
1212
Element,
1313
ConstructorElement,
14+
CompilationUnitElement,
1415
ClassMemberElement,
16+
TopLevelVariableElement,
1517
PropertyAccessorElement;
1618
import 'package:html/dom.dart' show Document;
1719
import 'package:html/parser.dart' show parse;
@@ -20,9 +22,6 @@ import 'package:markdown/markdown.dart' as md;
2022
import 'src/html_utils.dart' show htmlEscape;
2123
import 'src/model.dart';
2224

23-
const _leftChar = '[';
24-
const _rightChar = ']';
25-
2625
final List<md.InlineSyntax> _markdown_syntaxes = [new _InlineCodeSyntax()];
2726

2827
// We don't emit warnings currently: #572.
@@ -164,16 +163,15 @@ String _getMatchingLink(
164163

165164
if (refElement == null) return null;
166165

167-
Library refLibrary;
168-
var e = refElement is ClassMemberElement ||
169-
refElement is PropertyAccessorElement
170-
? refElement.enclosingElement
171-
: refElement;
166+
if (refElement is PropertyAccessorElement &&
167+
refElement.enclosingElement is CompilationUnitElement) {
168+
// yay we found an accessor that wraps a const, but we really
169+
// want the top-level field itself
170+
refElement = (refElement as PropertyAccessorElement).variable;
171+
}
172+
173+
Library refLibrary = element.package.findLibraryFor(refElement);
172174

173-
// If e is a ParameterElement, it's
174-
// never going to be in a library. So refLibrary is going to be null.
175-
refLibrary = element.package.libraries.firstWhere(
176-
(lib) => lib.hasInNamespace(e), orElse: () => null);
177175
if (refLibrary != null) {
178176
// Is there a way to pull this from a registry of known elements?
179177
// Seems like we're creating too many objects this way.

lib/src/debug.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
library debugger_helper;
2+
3+
import "dart:developer" as dev;
4+
5+
get debugger =>
6+
const String.fromEnvironment('DEBUG') == null ? _nodebugger : dev.debugger;
7+
_nodebugger({when, message}) {}

lib/src/html_utils.dart

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,6 @@ import 'dart:convert';
88

99
String htmlEscape(String text) => HTML_ESCAPE.convert(text);
1010

11-
String escapeBrackets(String text) {
12-
return text.replaceAll('>', '_').replaceAll('<', '_');
13-
}
14-
15-
String stringEscape(String text, String quoteType) {
16-
return text
17-
.replaceAll('\\', r'\\')
18-
.replaceAll(quoteType, "\\${quoteType}")
19-
.replaceAllMapped(_escapeRegExp, (m) {
20-
return _escapeMap[m.input];
21-
});
22-
}
23-
2411
String stripComments(String str) {
2512
if (str == null) return null;
2613

lib/src/model.dart

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import 'package:analyzer/src/generated/resolver.dart';
1111
import 'package:analyzer/src/generated/utilities_dart.dart' show ParameterKind;
1212
import 'package:quiver/core.dart';
1313

14+
import 'debug.dart';
1415
import 'html_utils.dart';
1516
import 'model_utils.dart';
1617
import 'package_meta.dart';
18+
1719
import '../markdown_processor.dart';
1820

1921
int byName(a, b) => a.name.toUpperCase().compareTo(b.name.toUpperCase());
@@ -84,12 +86,12 @@ abstract class ModelElement implements Comparable {
8486
if (e is MethodElement && !e.isOperator) {
8587
return new Method(e, library);
8688
}
87-
if (e is PropertyAccessorElement) {
88-
return new Accessor(e, library);
89-
}
9089
if (e is TopLevelVariableElement) {
9190
return new TopLevelVariable(e, library);
9291
}
92+
if (e is PropertyAccessorElement) {
93+
return new Accessor(e, library);
94+
}
9395
if (e is TypeParameterElement) {
9496
return new TypeParameter(e, library);
9597
}
@@ -239,13 +241,13 @@ abstract class ModelElement implements Comparable {
239241

240242
ElementType get modelType => _modelType;
241243

242-
/// Returns the [ModelElement] that encloses this.
243-
ModelElement get enclosingElement {
244+
/// Returns the [ClassElement] that encloses this.
245+
Class get enclosingClass {
244246
// A class's enclosing element is a library, and there isn't a
245247
// modelelement for a library.
246248
if (element.enclosingElement != null &&
247249
element.enclosingElement is ClassElement) {
248-
return new ModelElement.from(element.enclosingElement, library);
250+
return new ModelElement.from(element.enclosingElement, library) as Class;
249251
} else {
250252
return null;
251253
}
@@ -258,10 +260,13 @@ abstract class ModelElement implements Comparable {
258260
if (!package.isDocumented(this.element)) {
259261
return htmlEscape(name);
260262
}
263+
261264
if (name.startsWith('_')) {
262265
return htmlEscape(name);
263266
}
264-
Class c = enclosingElement;
267+
268+
// this smells like it's in the wrong place
269+
Class c = enclosingClass;
265270
if (c != null && c.name.startsWith('_')) {
266271
return '${c.name}.${htmlEscape(name)}';
267272
}
@@ -292,8 +297,8 @@ abstract class ModelElement implements Comparable {
292297
buf.write(' <span class="parameter-name">${p.name}</span>');
293298
}
294299
buf.write('(');
295-
buf.write(p.modelType.element
296-
.linkedParams(showNames: showNames, showMetadata: showMetadata));
300+
buf.write(p.modelType.element.linkedParams(
301+
showNames: showNames, showMetadata: showMetadata));
297302
buf.write(')');
298303
} else if (p.modelType != null && p.modelType.element != null) {
299304
var mt = p.modelType;
@@ -414,29 +419,42 @@ class Package {
414419

415420
String toString() => isSdk ? 'SDK' : 'Package $name';
416421

417-
bool isDocumented(Element element) {
422+
Library findLibraryFor(final Element element) {
418423
if (element is LibraryElement) {
419-
return _libraries.any((lib) => lib.element == element);
424+
// will equality work here? or should we check names?
425+
return _libraries.firstWhere((lib) => lib.element == element,
426+
orElse: () => null);
420427
}
421428

422429
Element el;
423430
if (element is ClassMemberElement || element is PropertyAccessorElement) {
424-
el = element.enclosingElement;
431+
if (element.enclosingElement is! CompilationUnitElement) {
432+
el = element.enclosingElement;
433+
} else {
434+
// in this case, element is an accessor for a library-level variable,
435+
// likely a const. We, in this case, actually don't want the enclosing
436+
// element because it's a compilation unit, whatever that is.
437+
el = element;
438+
}
425439
} else if (element is TopLevelVariableElement) {
426-
TopLevelVariableElement variable = element;
427-
if (variable.getter != null) {
428-
el = variable.getter;
429-
} else if (variable.setter != null) {
430-
el = variable.setter;
440+
final TopLevelVariableElement variableElement = element;
441+
if (variableElement.getter != null) {
442+
el = variableElement.getter;
443+
} else if (variableElement.setter != null) {
444+
el = variableElement.setter;
431445
} else {
432-
el = variable;
446+
el = variableElement;
433447
}
434448
} else {
435449
el = element;
436450
}
437-
return _libraries.any((lib) => lib.hasInNamespace(el));
451+
//debugger(when: element.name == 'NAME_WITH_TWO_UNDERSCORES');
452+
return _libraries.firstWhere((lib) => lib.hasInNamespace(el),
453+
orElse: () => null);
438454
}
439455

456+
bool isDocumented(Element element) => findLibraryFor(element) != null;
457+
440458
String get href => 'index.html';
441459

442460
Library _getLibraryFor(Element e) {
@@ -682,9 +700,8 @@ class Library extends ModelElement {
682700
}
683701

684702
List<Class> get classes {
685-
return _allClasses
686-
.where((c) => !c.isErrorOrException)
687-
.toList(growable: false);
703+
return _allClasses.where((c) => !c.isErrorOrException).toList(
704+
growable: false);
688705
}
689706

690707
List<Class> get allClasses => _allClasses;
@@ -781,9 +798,9 @@ class Class extends ModelElement {
781798
}
782799

783800
List<TypeParameter> get _typeParameters => _cls.typeParameters.map((f) {
784-
var lib = new Library(f.enclosingElement.library, package);
785-
return new TypeParameter(f, lib);
786-
}).toList();
801+
var lib = new Library(f.enclosingElement.library, package);
802+
return new TypeParameter(f, lib);
803+
}).toList();
787804

788805
String get kind => 'class';
789806

@@ -1212,8 +1229,7 @@ class Enum extends Class {
12121229

12131230
@override
12141231
List<EnumField> get instanceProperties {
1215-
return super
1216-
.instanceProperties
1232+
return super.instanceProperties
12171233
.map((Field p) => new EnumField(p.element, p.library))
12181234
.toList(growable: false);
12191235
}
@@ -1437,7 +1453,7 @@ class Constructor extends ModelElement {
14371453
@override
14381454
String get name {
14391455
String constructorName = element.name;
1440-
Class c = enclosingElement;
1456+
Class c = enclosingClass;
14411457
if (constructorName.isEmpty) {
14421458
return c.name;
14431459
} else {
@@ -1706,10 +1722,8 @@ class ElementType {
17061722

17071723
ElementType get _returnType {
17081724
var rt = (_type as FunctionType).returnType;
1709-
return new ElementType(
1710-
rt,
1711-
new ModelElement.from(rt.element,
1712-
new Library(_element.library.element, _element.package)));
1725+
return new ElementType(rt, new ModelElement.from(
1726+
rt.element, new Library(_element.library.element, _element.package)));
17131727
}
17141728

17151729
ModelElement get returnElement {
@@ -1723,9 +1737,9 @@ class ElementType {
17231737

17241738
List<ElementType> get typeArguments =>
17251739
(_type as ParameterizedType).typeArguments.map((f) {
1726-
var lib = new Library(f.element.library, _element.package);
1727-
return new ElementType(f, new ModelElement.from(f.element, lib));
1728-
}).toList();
1740+
var lib = new Library(f.element.library, _element.package);
1741+
return new ElementType(f, new ModelElement.from(f.element, lib));
1742+
}).toList();
17291743

17301744
String get linkedName {
17311745
if (_linkedName != null) return _linkedName;

pubspec.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ packages:
44
analyzer:
55
description: analyzer
66
source: hosted
7-
version: "0.25.2"
7+
version: "0.26.0"
88
ansicolor:
99
description: ansicolor
1010
source: hosted
@@ -88,7 +88,7 @@ packages:
8888
markdown:
8989
description: markdown
9090
source: hosted
91-
version: "0.7.1+2"
91+
version: "0.7.1+3"
9292
matcher:
9393
description: matcher
9494
source: hosted
@@ -148,7 +148,7 @@ packages:
148148
shelf_static:
149149
description: shelf_static
150150
source: hosted
151-
version: "0.2.2"
151+
version: "0.2.3"
152152
shelf_web_socket:
153153
description: shelf_web_socket
154154
source: hosted
@@ -180,7 +180,7 @@ packages:
180180
test:
181181
description: test
182182
source: hosted
183-
version: "0.12.3+8"
183+
version: "0.12.3+9"
184184
unscripted:
185185
description: unscripted
186186
source: hosted

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ homepage: https://github.com/dart-lang/dartdoc
77
environment:
88
sdk: '>=1.9.0 <2.0.0' # when we go to 1.12, bump analyzer version
99
dependencies:
10-
analyzer: ^0.25.0
10+
analyzer: '>=0.25.0 <0.27.0'
1111
args: ^0.13.0
1212
cli_util: ^0.0.1
1313
html: ^0.12.1

0 commit comments

Comments
 (0)