Skip to content

Commit

Permalink
Merge pull request #225 from keertip/annotate
Browse files Browse the repository at this point in the history
resolve references in annotations
  • Loading branch information
keertip committed Feb 21, 2015
2 parents bf66a34 + 296f867 commit dfea2e3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 75 deletions.
30 changes: 0 additions & 30 deletions lib/src/html_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,36 +72,6 @@ String ltrim(String str) {
return str;
}

String replaceAll(String str, List<String> matchChars,
{String htmlEntity, var replaceFunction}) {
int lastWritten = 0;
int index = str.indexOf(matchChars[0]);
StringBuffer buf = new StringBuffer();

while (index != -1) {
int end = str.indexOf(matchChars[1], index + 1);
if (end != -1) {
if (index - lastWritten > 0) {
buf.write(str.substring(lastWritten, index));
}
String codeRef = str.substring(index + matchChars[0].length, end);
if (htmlEntity != null) {
buf.write('<$htmlEntity>$codeRef</$htmlEntity>');
} else {
buf.write(replaceFunction(codeRef));
}
lastWritten = end + matchChars[1].length;
} else {
break;
}
index = str.indexOf(matchChars[0], end + 1);
}
if (lastWritten < str.length) {
buf.write(str.substring(lastWritten, str.length));
}
return buf.toString();
}

const _escapMap = const {
'\n': r'\n',
'\r': r'\r',
Expand Down
48 changes: 16 additions & 32 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,38 +134,14 @@ abstract class ModelElement {
return docs;
}

var matchChars = ['[', ']'];
int lastWritten = 0;
int index = docs.indexOf(matchChars[0]);
StringBuffer buf = new StringBuffer();

while (index != -1) {
int end = docs.indexOf(matchChars[1], index + 1);
if (end != -1) {
if (index - lastWritten > 0) {
buf.write(docs.substring(lastWritten, index));
}
String codeRef = docs.substring(index + matchChars[0].length, end);
buf.write('[$codeRef]');
var refElement = commentRefs.firstWhere(
(ref) => ref.identifier.name == codeRef).identifier.staticElement;
var refLibrary = new Library(refElement.library, package);
var e = new ModelElement.from(refElement, refLibrary);
var link = e.href;
if (link != null) {
buf.write('(${e.href})');
}
lastWritten = end + matchChars[1].length;
} else {
break;
}
index = docs.indexOf(matchChars[0], end + 1);
}
if (lastWritten < docs.length) {
buf.write(docs.substring(lastWritten, docs.length));
String _getMatchingLink(String codeRef) {
var refElement = commentRefs.firstWhere(
(ref) => ref.identifier.name == codeRef).identifier.staticElement;
var refLibrary = new Library(refElement.library, package);
var e = new ModelElement.from(refElement, refLibrary);
return e.href;
}
print(buf.toString());
return buf.toString();
return replaceAllLinks(docs, replaceFunction: _getMatchingLink);
}

String get htmlId => name;
Expand All @@ -178,7 +154,15 @@ abstract class ModelElement {
List<Annotation> annotations = node.metadata;
if (annotations.isNotEmpty) {
return annotations.map((f) {
return f.toSource().substring(1);
var annotationString = f.toSource().substring(1);
var e = f.element;
if (e != null && (e is ConstructorElement)) {
var me = new ModelElement.from(
e.enclosingElement, new Library(e.library, package));
return annotationString.replaceAll(
me.name, '[${me.name}](${me.href})');
}
return annotationString;
}).toList(growable: false);
}
}
Expand Down
31 changes: 31 additions & 0 deletions lib/src/model_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,35 @@ List<InterfaceType> getAllSupertypes(ClassElement c) {
return c.allSupertypes;
}

String replaceAllLinks(String str, {var replaceFunction}) {
var matchChars = ['[', ']'];
int lastWritten = 0;
int index = str.indexOf(matchChars[0]);
StringBuffer buf = new StringBuffer();

while (index != -1) {
int end = str.indexOf(matchChars[1], index + 1);
if (end != -1) {
if (index - lastWritten > 0) {
buf.write(str.substring(lastWritten, index));
}
String codeRef = str.substring(index + matchChars[0].length, end);
if (codeRef != null) {
buf.write('[$codeRef]');
var link = replaceFunction(codeRef);
if (link != null) {
buf.write('($link)');
}
}
lastWritten = end + matchChars[1].length;
} else {
break;
}
index = str.indexOf(matchChars[0], end + 1);
}
if (lastWritten < str.length) {
buf.write(str.substring(lastWritten, str.length));
}
return buf.toString();
}

8 changes: 5 additions & 3 deletions test/fake_package/lib/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ abstract class Cat {
/// implements [Cat], [E]
class Dog implements Cat, E {

@deprecated
List<Apple> getClassA() {
return [new Apple()];
}

@override
bool get isImplemented => true;

List<Apple> getClassA() {
return [new Apple()];
}
}

abstract class E {
Expand Down
31 changes: 21 additions & 10 deletions test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ void main() {
});

test('docs ', () {
expect(Apple.resolveReferences(Apple.documentation), 'Sample class [String]');
expect(Apple.resolveReferences(Apple.documentation),
'Sample class [String]');
});

test('docs refs', () {
Expand Down Expand Up @@ -162,7 +163,6 @@ void main() {
expect(B.inheritedMethods[0].name, 'printMsg');
expect(B.inheritedMethods[1].name, 'isGreaterThan');
});

});

group('Function', () {
Expand Down Expand Up @@ -209,7 +209,7 @@ void main() {
'@override\n void m1() {\n var a = 6;\n var b = a * 9;\n }');
});

test('method documentation',(){
test('method documentation', () {
expect(m2.documentation, 'this is a method');
});

Expand Down Expand Up @@ -356,7 +356,6 @@ void main() {
});

group('Implementors', () {

Class apple;
Class b;
List<Class> implA, implC;
Expand Down Expand Up @@ -393,28 +392,40 @@ void main() {
});

group('Errors and exceptions', () {
final List<String> expectedNames =
['MyError', 'MyException', 'MyErrorImplements', 'MyExceptionImplements'];
final List<String> expectedNames = [
'MyError',
'MyException',
'MyErrorImplements',
'MyExceptionImplements'
];
test('library has the exact errors/exceptions we expect', () {
expect(library.getExceptions().map((e) => e.name),
unorderedEquals(expectedNames));
});
});

group('Annotations', () {
Class forAnnotation;
Class forAnnotation, dog;
setUp(() {
forAnnotation = library.getClasses().firstWhere((c) => c.name == 'HasAnnotation');
forAnnotation =
library.getClasses().firstWhere((c) => c.name == 'HasAnnotation');
dog = library.getClasses().firstWhere((c) => c.name == 'Dog');
});

test('is not null', () => expect(forAnnotation, isNotNull));

test('has annotations', () => expect(forAnnotation.hasAnnotations, true));

test('has one annotation', () => expect(forAnnotation.annotations, hasLength(1)));
test('has one annotation',
() => expect(forAnnotation.annotations, hasLength(1)));

test('has the right annotation', () {
expect(forAnnotation.annotations.first, equals('ForAnnotation(\'my value\')'));
expect(forAnnotation.annotations.first,
equals('[ForAnnotation](ex/ForAnnotation.html)(\'my value\')'));
});

test('methods has the right annotation', () {
expect(dog.instanceMethods.first.annotations.first, equals('deprecated'));
});
});
}
Expand Down

0 comments on commit dfea2e3

Please sign in to comment.