OLD | NEW |
1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
2 | 2 |
3 /// Converts block-style Doc comments in Dart code to line style. | 3 /// Converts block-style Doc comments in Dart code to line style. |
4 library line_doc_comments; | 4 library line_doc_comments; |
5 import 'dart:io'; | 5 import 'dart:io'; |
6 | 6 |
7 import '../pkg/path/lib/path.dart' as path; | 7 import '../pkg/path/lib/path.dart' as path; |
8 | 8 |
9 final oneLineBlock = new RegExp(r'^(\s*)/\*\*\s?(.*)\*/\s*$'); | 9 final oneLineBlock = new RegExp(r'^(\s*)/\*\*\s?(.*)\*/\s*$'); |
10 final startBlock = new RegExp(r'^(\s*)/\*\*(.*)$'); | 10 final startBlock = new RegExp(r'^(\s*)/\*\*(.*)$'); |
11 final blockLine = new RegExp(r'^\s*\*\s?(.*)$'); | 11 final blockLine = new RegExp(r'^\s*\*\s?(.*)$'); |
12 final endBlock = new RegExp(r'^\s*\*/\s*$'); | 12 final endBlock = new RegExp(r'^\s*\*/\s*$'); |
13 | 13 |
14 main(List<String> args) { | 14 main(List<String> args) { |
15 if (args.length != 1) { | 15 if (args.length != 1) { |
16 print('Converts "/**"-style block doc comments in a directory '); | 16 print('Converts "/**"-style block doc comments in a directory '); |
17 print('containing Dart code to "///"-style line doc comments.'); | 17 print('containing Dart code to "///"-style line doc comments.'); |
18 print(''); | 18 print(''); |
19 print('Usage: line_doc_coments.dart <dir>'); | 19 print('Usage: line_doc_coments.dart <dir>'); |
20 return; | 20 return; |
21 } | 21 } |
22 | 22 |
23 var dir = new Directory(args[0]); | 23 var dir = new Directory(args[0]); |
24 dir.list(recursive: true).listen( | 24 dir.list(recursive: true, followLinks: false).listen( |
25 (entity) { | 25 (entity) { |
26 if (entity is File) { | 26 if (entity is File) { |
27 var file = entity.name; | 27 var file = entity.path; |
28 if (path.extension(file) != '.dart') return; | 28 if (path.extension(file) != '.dart') return; |
29 fixFile(file); | 29 fixFile(file); |
30 } | 30 } |
31 }); | 31 }); |
32 } | 32 } |
33 | 33 |
34 void fixFile(String path) { | 34 void fixFile(String path) { |
35 var file = new File(path); | 35 var file = new File(path); |
36 file.readAsLines().transform(fixContents).chain((fixed) { | 36 file.readAsLines().then(fixContents).then((fixed) { |
37 return new File(path).writeAsString(fixed); | 37 return new File(path).writeAsString(fixed); |
38 }).then((file) { | 38 }).then((file) { |
39 print(file.name); | 39 print(file.path); |
40 }); | 40 }); |
41 } | 41 } |
42 | 42 |
43 String fixContents(List<String> lines) { | 43 String fixContents(List<String> lines) { |
44 var buffer = new StringBuffer(); | 44 var buffer = new StringBuffer(); |
45 var inBlock = false; | 45 var inBlock = false; |
46 var indent; | 46 var indent; |
47 for (var line in lines) { | 47 for (var line in lines) { |
48 if (inBlock) { | 48 if (inBlock) { |
49 // See if it's the end of the comment. | 49 // See if it's the end of the comment. |
50 if (endBlock.hasMatch(line)) { | 50 if (endBlock.hasMatch(line)) { |
51 inBlock = false; | 51 inBlock = false; |
52 | 52 |
53 // Just a pointless line, delete it! | 53 // Just a pointless line, delete it! |
54 line = null; | 54 line = null; |
55 } else { | 55 } else { |
56 var match = blockLine.firstMatch(line); | 56 var match = blockLine.firstMatch(line); |
57 line = '$indent/// ${match[1]}'; | 57 var comment = match[1]; |
| 58 if (comment != '') { |
| 59 line = '$indent/// $comment'; |
| 60 } else { |
| 61 line = '$indent///'; |
| 62 } |
58 } | 63 } |
59 } else { | 64 } else { |
60 // See if it's a one-line block comment like: /** Blah. */ | 65 // See if it's a one-line block comment like: /** Blah. */ |
61 var match = oneLineBlock.firstMatch(line); | 66 var match = oneLineBlock.firstMatch(line); |
62 if (match != null) { | 67 if (match != null) { |
63 if (match[2] != '') { | 68 var comment = match[2]; |
64 line = '${match[1]}/// ${match[2]}'; | 69 if (comment != '') { |
| 70 // Remove the extra space before the `*/` |
| 71 if (comment.endsWith(' ')) { |
| 72 comment = comment.substring(0, comment.length - 1); |
| 73 } |
| 74 line = '${match[1]}/// $comment'; |
65 } else { | 75 } else { |
66 line = '${match[1]}///'; | 76 line = '${match[1]}///'; |
67 } | 77 } |
68 } else { | 78 } else { |
69 // See if it's the start of a block doc comment. | 79 // See if it's the start of a block doc comment. |
70 match = startBlock.firstMatch(line); | 80 match = startBlock.firstMatch(line); |
71 if (match != null) { | 81 if (match != null) { |
72 inBlock = true; | 82 inBlock = true; |
73 indent = match[1]; | 83 indent = match[1]; |
74 if (match[2] != '') { | 84 if (match[2] != '') { |
75 // Got comment on /** line. | 85 // Got comment on /** line. |
76 line = match[2]; | 86 line = match[2]; |
77 } else { | 87 } else { |
78 // Just a pointless line, delete it! | 88 // Just a pointless line, delete it! |
79 line = null; | 89 line = null; |
80 } | 90 } |
81 } | 91 } |
82 } | 92 } |
83 } | 93 } |
84 | 94 |
85 if (line != null) buffer.write('$line\n'); | 95 if (line != null) buffer.write('$line\n'); |
86 } | 96 } |
87 | 97 |
88 return buffer.toString(); | 98 return buffer.toString(); |
89 } | 99 } |
OLD | NEW |