diff options
Diffstat (limited to 'xtask')
-rw-r--r-- | xtask/src/codegen.rs | 39 | ||||
-rw-r--r-- | xtask/src/codegen/gen_assists_docs.rs | 4 | ||||
-rw-r--r-- | xtask/src/codegen/gen_feature_docs.rs | 3 |
3 files changed, 29 insertions, 17 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 | ||
63 | fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> { | 63 | fn 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 | ||
67 | fn extract_comment_blocks_with_empty_lines(tag: &str, text: &str) -> Vec<CommentBlock> { | 67 | fn 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 | ||
82 | struct CommentBlock { | 82 | struct CommentBlock { |
83 | id: String, | 83 | id: String, |
84 | line: usize, | ||
84 | contents: Vec<String>, | 85 | contents: Vec<String>, |
85 | } | 86 | } |
86 | 87 | ||
87 | fn do_extract_comment_blocks(text: &str, allow_blocks_with_empty_lines: bool) -> Vec<Vec<String>> { | 88 | fn 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)] |
114 | struct Location { | 121 | struct Location { |
115 | file: PathBuf, | 122 | file: PathBuf, |
123 | line: usize, | ||
116 | } | 124 | } |
117 | 125 | ||
118 | impl Location { | 126 | impl 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 | } |
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 { | |||
64 | assert_eq!(lines.next().unwrap().as_str(), "->"); | 64 | assert_eq!(lines.next().unwrap().as_str(), "->"); |
65 | assert_eq!(lines.next().unwrap().as_str(), "```"); | 65 | assert_eq!(lines.next().unwrap().as_str(), "```"); |
66 | let after = take_until(lines.by_ref(), "```"); | 66 | let after = take_until(lines.by_ref(), "```"); |
67 | let location = Location::new(path.to_path_buf()); | 67 | let location = Location::new(path.to_path_buf(), block.line); |
68 | acc.push(Assist { id, location, doc, before, after }) | 68 | acc.push(Assist { id, location, doc, before, after }) |
69 | } | 69 | } |
70 | 70 | ||
@@ -90,6 +90,7 @@ impl fmt::Display for Assist { | |||
90 | writeln!( | 90 | writeln!( |
91 | f, | 91 | f, |
92 | "[discrete]\n=== `{}` | 92 | "[discrete]\n=== `{}` |
93 | **Source:** {} | ||
93 | 94 | ||
94 | {} | 95 | {} |
95 | 96 | ||
@@ -101,6 +102,7 @@ impl fmt::Display for Assist { | |||
101 | ```rust | 102 | ```rust |
102 | {}```", | 103 | {}```", |
103 | self.id, | 104 | self.id, |
105 | self.location, | ||
104 | self.doc, | 106 | self.doc, |
105 | hide_hash_comments(&before), | 107 | hide_hash_comments(&before), |
106 | hide_hash_comments(&after) | 108 | 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 { | |||
40 | let id = block.id; | 40 | let id = block.id; |
41 | assert!(is_valid_feature_name(&id), "invalid feature name: {:?}", id); | 41 | assert!(is_valid_feature_name(&id), "invalid feature name: {:?}", id); |
42 | let doc = block.contents.join("\n"); | 42 | let doc = block.contents.join("\n"); |
43 | acc.push(Feature { id, location: Location::new(path.clone()), doc }) | 43 | let location = Location::new(path.clone(), block.line); |
44 | acc.push(Feature { id, location, doc }) | ||
44 | } | 45 | } |
45 | 46 | ||
46 | Ok(()) | 47 | Ok(()) |