| 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*)/\*\*(.*)$'); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 if (entity is File) { | 26 if (entity is File) { |
| 27 var file = entity.path; | 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().then(fixContents).then((fixed) { | 36 file.readAsLines().then((lines) => fixContents(lines, path)).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.path); | 39 print(file.path); |
| 40 }); | 40 }); |
| 41 } | 41 } |
| 42 | 42 |
| 43 String fixContents(List<String> lines) { | 43 String fixContents(List<String> lines, String path) { |
| 44 var buffer = new StringBuffer(); | 44 var buffer = new StringBuffer(); |
| 45 var linesOut = 0; |
| 45 var inBlock = false; | 46 var inBlock = false; |
| 46 var indent; | 47 var indent; |
| 48 |
| 47 for (var line in lines) { | 49 for (var line in lines) { |
| 50 var oldLine = line; |
| 48 if (inBlock) { | 51 if (inBlock) { |
| 49 // See if it's the end of the comment. | 52 // See if it's the end of the comment. |
| 50 if (endBlock.hasMatch(line)) { | 53 if (endBlock.hasMatch(line)) { |
| 51 inBlock = false; | 54 inBlock = false; |
| 52 | 55 |
| 53 // Just a pointless line, delete it! | 56 // Just a pointless line, delete it! |
| 54 line = null; | 57 line = null; |
| 55 } else { | 58 } else { |
| 56 var match = blockLine.firstMatch(line); | 59 var match = blockLine.firstMatch(line); |
| 57 var comment = match[1]; | 60 var comment = match[1]; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 85 // Got comment on /** line. | 88 // Got comment on /** line. |
| 86 line = match[2]; | 89 line = match[2]; |
| 87 } else { | 90 } else { |
| 88 // Just a pointless line, delete it! | 91 // Just a pointless line, delete it! |
| 89 line = null; | 92 line = null; |
| 90 } | 93 } |
| 91 } | 94 } |
| 92 } | 95 } |
| 93 } | 96 } |
| 94 | 97 |
| 95 if (line != null) buffer.write('$line\n'); | 98 if (line != null) { |
| 99 linesOut++; |
| 100 |
| 101 // Warn about lines that crossed 80 columns as a result of the change. |
| 102 if (line.length > 80 && oldLine.length <= 80) { |
| 103 const _PURPLE = '\u001b[35m'; |
| 104 const _RED = '\u001b[31m'; |
| 105 const _NO_COLOR = '\u001b[0m'; |
| 106 |
| 107 print('$_PURPLE$path$_NO_COLOR:$_RED$linesOut$_NO_COLOR: ' |
| 108 'line exceeds 80 cols:\n $line'); |
| 109 } |
| 110 |
| 111 buffer.write('$line\n'); |
| 112 } |
| 96 } | 113 } |
| 97 | 114 |
| 98 return buffer.toString(); | 115 return buffer.toString(); |
| 99 } | 116 } |
| OLD | NEW |