OLD | NEW |
---|---|
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * To use it, from this directory, run: | 6 * To use it, from this directory, run: |
7 * | 7 * |
8 * $ ./dartdoc <path to .dart file> | 8 * $ ./dartdoc <path to .dart file> |
9 * | 9 * |
10 * This will create a "docs" directory with the docs for your libraries. To | 10 * This will create a "docs" directory with the docs for your libraries. To |
(...skipping 773 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
784 */ | 784 */ |
785 md.Node resolveNameReference(String name, [Member member = null, | 785 md.Node resolveNameReference(String name, [Member member = null, |
786 Type type = null, Library library = null]) { | 786 Type type = null, Library library = null]) { |
787 makeLink(String href) { | 787 makeLink(String href) { |
788 final anchor = new md.Element.text('a', name); | 788 final anchor = new md.Element.text('a', name); |
789 anchor.attributes['href'] = relativePath(href); | 789 anchor.attributes['href'] = relativePath(href); |
790 anchor.attributes['class'] = 'crossref'; | 790 anchor.attributes['class'] = 'crossref'; |
791 return anchor; | 791 return anchor; |
792 } | 792 } |
793 | 793 |
794 findMember(Type type) { | 794 findMember(Type type, String name) { |
Bob Nystrom
2012/01/10 00:40:41
Shadowing name here, makes this already-confusing
nweiz
2012/01/10 01:51:55
Done.
| |
795 final member = type.members[name]; | 795 final member = type.members[name]; |
796 if (member == null) return null; | 796 if (member == null) return null; |
797 | 797 |
798 // Special case: if the member we've resolved is a property (i.e. it wraps | 798 // Special case: if the member we've resolved is a property (i.e. it wraps |
799 // a getter and/or setter then *that* member itself won't be on the docs, | 799 // a getter and/or setter then *that* member itself won't be on the docs, |
800 // just the getter or setter will be. So pick one of those to link to. | 800 // just the getter or setter will be. So pick one of those to link to. |
801 if (member.isProperty) { | 801 if (member.isProperty) { |
802 return member.canGet ? member.getter : member.setter; | 802 return member.canGet ? member.getter : member.setter; |
803 } | 803 } |
804 | 804 |
805 return member; | 805 return member; |
806 } | 806 } |
807 | 807 |
808 // See if it's a parameter of the current method. | 808 // See if it's a parameter of the current method. |
809 if (member != null) { | 809 if (member != null) { |
810 for (final parameter in member.parameters) { | 810 for (final parameter in member.parameters) { |
811 if (parameter.name == name) { | 811 if (parameter.name == name) { |
812 final element = new md.Element.text('span', name); | 812 final element = new md.Element.text('span', name); |
813 element.attributes['class'] = 'param'; | 813 element.attributes['class'] = 'param'; |
814 return element; | 814 return element; |
815 } | 815 } |
816 } | 816 } |
817 } | 817 } |
818 | 818 |
819 // See if it's another member of the current type. | 819 // See if it's another member of the current type. |
820 if (type != null) { | 820 if (type != null) { |
821 final member = findMember(type); | 821 final member = findMember(type, name); |
822 if (member != null) { | 822 if (member != null) { |
823 return makeLink(memberUrl(member)); | 823 return makeLink(memberUrl(member)); |
824 } | 824 } |
825 } | 825 } |
826 | 826 |
827 // See if it's another type in the current library. | 827 // See if it's another type or a member of another type in the current |
828 // library. | |
828 if (library != null) { | 829 if (library != null) { |
830 // See if it's a constructor | |
831 final constructorLink = (() { | |
832 if (!name.startsWith('new ')) return; | |
833 final classAndName = name.substring(4).split('.'); | |
Bob Nystrom
2012/01/10 00:40:41
I think a regex would be simpler here: 'new (\w+).
nweiz
2012/01/10 01:51:55
Done.
| |
834 if (classAndName.length > 2) return; | |
835 final type = library.types[classAndName[0]]; | |
836 if (type == null) return; | |
837 final constructor = type.getConstructor( | |
838 classAndName.length == 2 ? classAndName[1] : ''); | |
839 if (constructor == null) return; | |
840 return makeLink(memberUrl(constructor)); | |
841 })(); | |
Bob Nystrom
2012/01/10 00:40:41
This is clever, but perverse. :) If using a regex
nweiz
2012/01/10 01:51:55
I had this as a local named function before this,
| |
842 if (constructorLink != null) return constructorLink; | |
843 | |
844 // See if it's a member of another type | |
845 final foreignMemberLink = (() { | |
846 final nameAndMember = name.split('.'); | |
847 if (nameAndMember.length != 2) return; | |
848 final type = library.types[nameAndMember[0]]; | |
849 if (type == null) return; | |
850 final member = findMember(type, nameAndMember[1]); | |
851 if (member == null) return; | |
852 return makeLink(memberUrl(member)); | |
853 })(); | |
854 if (foreignMemberLink != null) return foreignMemberLink; | |
855 | |
829 final type = library.types[name]; | 856 final type = library.types[name]; |
830 if (type != null) { | 857 if (type != null) { |
831 return makeLink(typeUrl(type)); | 858 return makeLink(typeUrl(type)); |
832 } | 859 } |
833 | 860 |
834 // See if it's a top-level member in the current library. | 861 // See if it's a top-level member in the current library. |
835 final member = findMember(library.topType); | 862 final member = findMember(library.topType, name); |
836 if (member != null) { | 863 if (member != null) { |
837 return makeLink(memberUrl(member)); | 864 return makeLink(memberUrl(member)); |
838 } | 865 } |
839 } | 866 } |
840 | 867 |
841 // TODO(rnystrom): Should also consider: | 868 // TODO(rnystrom): Should also consider: |
842 // * Names imported by libraries this library imports. | 869 // * Names imported by libraries this library imports. |
843 // * Type parameters of the enclosing type. | 870 // * Type parameters of the enclosing type. |
844 | 871 |
845 return new md.Element.text('code', name); | 872 return new md.Element.text('code', name); |
846 } | 873 } |
847 | 874 |
848 // TODO(rnystrom): Move into SourceSpan? | 875 // TODO(rnystrom): Move into SourceSpan? |
849 int getSpanColumn(SourceSpan span) { | 876 int getSpanColumn(SourceSpan span) { |
850 final line = span.file.getLine(span.start); | 877 final line = span.file.getLine(span.start); |
851 return span.file.getColumn(line, span.start); | 878 return span.file.getColumn(line, span.start); |
852 } | 879 } |
853 } | 880 } |
OLD | NEW |