From 899570ad7abbc644e87d8379862c1b9f940eaf07 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 31 May 2020 15:36:20 +0200 Subject: Link to the specific source line --- xtask/src/codegen.rs | 39 +++++++++++++++++++++-------------- xtask/src/codegen/gen_assists_docs.rs | 4 +++- xtask/src/codegen/gen_feature_docs.rs | 3 ++- 3 files changed, 29 insertions(+), 17 deletions(-) (limited to 'xtask') 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<()> { } fn extract_comment_blocks(text: &str) -> Vec> { - do_extract_comment_blocks(text, false) + do_extract_comment_blocks(text, false).into_iter().map(|(_line, block)| block).collect() } fn extract_comment_blocks_with_empty_lines(tag: &str, text: &str) -> Vec { assert!(tag.starts_with(char::is_uppercase)); let tag = format!("{}:", tag); let mut res = Vec::new(); - for mut block in do_extract_comment_blocks(text, true) { + for (line, mut block) in do_extract_comment_blocks(text, true) { let first = block.remove(0); if first.starts_with(&tag) { let id = first[tag.len()..].trim().to_string(); - let block = CommentBlock { id, contents: block }; + let block = CommentBlock { id, line, contents: block }; res.push(block); } } @@ -81,31 +81,38 @@ fn extract_comment_blocks_with_empty_lines(tag: &str, text: &str) -> Vec, } -fn do_extract_comment_blocks(text: &str, allow_blocks_with_empty_lines: bool) -> Vec> { +fn do_extract_comment_blocks( + text: &str, + allow_blocks_with_empty_lines: bool, +) -> Vec<(usize, Vec)> { let mut res = Vec::new(); let prefix = "// "; let lines = text.lines().map(str::trim_start); - let mut block = vec![]; - for line in lines { + let mut block = (0, vec![]); + for (line_num, line) in lines.enumerate() { if line == "//" && allow_blocks_with_empty_lines { - block.push(String::new()); + block.1.push(String::new()); continue; } let is_comment = line.starts_with(prefix); if is_comment { - block.push(line[prefix.len()..].to_string()); - } else if !block.is_empty() { - res.push(mem::replace(&mut block, Vec::new())); + block.1.push(line[prefix.len()..].to_string()); + } else { + if !block.1.is_empty() { + res.push(mem::take(&mut block)); + } + block.0 = line_num + 2; } } - if !block.is_empty() { - res.push(mem::replace(&mut block, Vec::new())) + if !block.1.is_empty() { + res.push(block) } res } @@ -113,11 +120,12 @@ fn do_extract_comment_blocks(text: &str, allow_blocks_with_empty_lines: bool) -> #[derive(Debug)] struct Location { file: PathBuf, + line: usize, } impl Location { - fn new(file: PathBuf) -> Self { - Self { file } + fn new(file: PathBuf, line: usize) -> Self { + Self { file, line } } } @@ -128,8 +136,9 @@ impl fmt::Display for Location { let name = self.file.file_name().unwrap(); write!( f, - "https://github.com/rust-analyzer/rust-analyzer/blob/master/{}[{}]", + "https://github.com/rust-analyzer/rust-analyzer/blob/master/{}#L{}[{}]", path, + self.line, name.to_str().unwrap() ) } diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs index 15a02d317..6c1be5350 100644 --- a/xtask/src/codegen/gen_assists_docs.rs +++ b/xtask/src/codegen/gen_assists_docs.rs @@ -64,7 +64,7 @@ impl Assist { assert_eq!(lines.next().unwrap().as_str(), "->"); assert_eq!(lines.next().unwrap().as_str(), "```"); let after = take_until(lines.by_ref(), "```"); - let location = Location::new(path.to_path_buf()); + let location = Location::new(path.to_path_buf(), block.line); acc.push(Assist { id, location, doc, before, after }) } @@ -90,6 +90,7 @@ impl fmt::Display for Assist { writeln!( f, "[discrete]\n=== `{}` +**Source:** {} {} @@ -101,6 +102,7 @@ impl fmt::Display for Assist { ```rust {}```", self.id, + self.location, self.doc, hide_hash_comments(&before), hide_hash_comments(&after) diff --git a/xtask/src/codegen/gen_feature_docs.rs b/xtask/src/codegen/gen_feature_docs.rs index 731e7ecf2..31bc3839d 100644 --- a/xtask/src/codegen/gen_feature_docs.rs +++ b/xtask/src/codegen/gen_feature_docs.rs @@ -40,7 +40,8 @@ impl Feature { let id = block.id; assert!(is_valid_feature_name(&id), "invalid feature name: {:?}", id); let doc = block.contents.join("\n"); - acc.push(Feature { id, location: Location::new(path.clone()), doc }) + let location = Location::new(path.clone(), block.line); + acc.push(Feature { id, location, doc }) } Ok(()) -- cgit v1.2.3