aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen.rs
diff options
context:
space:
mode:
Diffstat (limited to 'xtask/src/codegen.rs')
-rw-r--r--xtask/src/codegen.rs39
1 files changed, 24 insertions, 15 deletions
diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs
index f3917a244..5511c01d5 100644
--- a/xtask/src/codegen.rs
+++ b/xtask/src/codegen.rs
@@ -61,18 +61,18 @@ fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
61} 61}
62 62
63fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> { 63fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> {
64 do_extract_comment_blocks(text, false) 64 do_extract_comment_blocks(text, false).into_iter().map(|(_line, block)| block).collect()
65} 65}
66 66
67fn extract_comment_blocks_with_empty_lines(tag: &str, text: &str) -> Vec<CommentBlock> { 67fn extract_comment_blocks_with_empty_lines(tag: &str, text: &str) -> Vec<CommentBlock> {
68 assert!(tag.starts_with(char::is_uppercase)); 68 assert!(tag.starts_with(char::is_uppercase));
69 let tag = format!("{}:", tag); 69 let tag = format!("{}:", tag);
70 let mut res = Vec::new(); 70 let mut res = Vec::new();
71 for mut block in do_extract_comment_blocks(text, true) { 71 for (line, mut block) in do_extract_comment_blocks(text, true) {
72 let first = block.remove(0); 72 let first = block.remove(0);
73 if first.starts_with(&tag) { 73 if first.starts_with(&tag) {
74 let id = first[tag.len()..].trim().to_string(); 74 let id = first[tag.len()..].trim().to_string();
75 let block = CommentBlock { id, contents: block }; 75 let block = CommentBlock { id, line, contents: block };
76 res.push(block); 76 res.push(block);
77 } 77 }
78 } 78 }
@@ -81,31 +81,38 @@ fn extract_comment_blocks_with_empty_lines(tag: &str, text: &str) -> Vec<Comment
81 81
82struct CommentBlock { 82struct CommentBlock {
83 id: String, 83 id: String,
84 line: usize,
84 contents: Vec<String>, 85 contents: Vec<String>,
85} 86}
86 87
87fn do_extract_comment_blocks(text: &str, allow_blocks_with_empty_lines: bool) -> Vec<Vec<String>> { 88fn do_extract_comment_blocks(
89 text: &str,
90 allow_blocks_with_empty_lines: bool,
91) -> Vec<(usize, Vec<String>)> {
88 let mut res = Vec::new(); 92 let mut res = Vec::new();
89 93
90 let prefix = "// "; 94 let prefix = "// ";
91 let lines = text.lines().map(str::trim_start); 95 let lines = text.lines().map(str::trim_start);
92 96
93 let mut block = vec![]; 97 let mut block = (0, vec![]);
94 for line in lines { 98 for (line_num, line) in lines.enumerate() {
95 if line == "//" && allow_blocks_with_empty_lines { 99 if line == "//" && allow_blocks_with_empty_lines {
96 block.push(String::new()); 100 block.1.push(String::new());
97 continue; 101 continue;
98 } 102 }
99 103
100 let is_comment = line.starts_with(prefix); 104 let is_comment = line.starts_with(prefix);
101 if is_comment { 105 if is_comment {
102 block.push(line[prefix.len()..].to_string()); 106 block.1.push(line[prefix.len()..].to_string());
103 } else if !block.is_empty() { 107 } else {
104 res.push(mem::replace(&mut block, Vec::new())); 108 if !block.1.is_empty() {
109 res.push(mem::take(&mut block));
110 }
111 block.0 = line_num + 2;
105 } 112 }
106 } 113 }
107 if !block.is_empty() { 114 if !block.1.is_empty() {
108 res.push(mem::replace(&mut block, Vec::new())) 115 res.push(block)
109 } 116 }
110 res 117 res
111} 118}
@@ -113,11 +120,12 @@ fn do_extract_comment_blocks(text: &str, allow_blocks_with_empty_lines: bool) ->
113#[derive(Debug)] 120#[derive(Debug)]
114struct Location { 121struct Location {
115 file: PathBuf, 122 file: PathBuf,
123 line: usize,
116} 124}
117 125
118impl Location { 126impl Location {
119 fn new(file: PathBuf) -> Self { 127 fn new(file: PathBuf, line: usize) -> Self {
120 Self { file } 128 Self { file, line }
121 } 129 }
122} 130}
123 131
@@ -128,8 +136,9 @@ impl fmt::Display for Location {
128 let name = self.file.file_name().unwrap(); 136 let name = self.file.file_name().unwrap();
129 write!( 137 write!(
130 f, 138 f,
131 "https://github.com/rust-analyzer/rust-analyzer/blob/master/{}[{}]", 139 "https://github.com/rust-analyzer/rust-analyzer/blob/master/{}#L{}[{}]",
132 path, 140 path,
141 self.line,
133 name.to_str().unwrap() 142 name.to_str().unwrap()
134 ) 143 )
135 } 144 }