mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
minore: improve readability
Tuples are hard to understand
This commit is contained in:
parent
c45792ae8f
commit
80991356e1
@ -43,6 +43,7 @@ pub fn list_files(dir: &Path) -> Vec<PathBuf> {
|
|||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct CommentBlock {
|
pub struct CommentBlock {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub line: usize,
|
pub line: usize,
|
||||||
@ -55,11 +56,10 @@ impl CommentBlock {
|
|||||||
|
|
||||||
let tag = format!("{}:", tag);
|
let tag = format!("{}:", tag);
|
||||||
let mut res = Vec::new();
|
let mut res = Vec::new();
|
||||||
for (line, mut block) in do_extract_comment_blocks(text, true) {
|
for mut block in CommentBlock::do_extract(text, true) {
|
||||||
let first = block.remove(0);
|
let first = block.contents.remove(0);
|
||||||
if let Some(id) = first.strip_prefix(&tag) {
|
if let Some(id) = first.strip_prefix(&tag) {
|
||||||
let id = id.trim().to_string();
|
block.id = id.trim().to_string();
|
||||||
let block = CommentBlock { id, line, contents: block };
|
|
||||||
res.push(block);
|
res.push(block);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -67,48 +67,40 @@ impl CommentBlock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn extract_untagged(text: &str) -> Vec<CommentBlock> {
|
pub fn extract_untagged(text: &str) -> Vec<CommentBlock> {
|
||||||
|
CommentBlock::do_extract(text, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn do_extract(text: &str, allow_blocks_with_empty_lines: bool) -> Vec<CommentBlock> {
|
||||||
let mut res = Vec::new();
|
let mut res = Vec::new();
|
||||||
for (line, block) in do_extract_comment_blocks(text, false) {
|
|
||||||
let id = String::new();
|
let prefix = "// ";
|
||||||
let block = CommentBlock { id, line, contents: block };
|
let lines = text.lines().map(str::trim_start);
|
||||||
res.push(block);
|
|
||||||
|
let dummy_block = CommentBlock { id: String::new(), line: 0, contents: Vec::new() };
|
||||||
|
let mut block = dummy_block.clone();
|
||||||
|
for (line_num, line) in lines.enumerate() {
|
||||||
|
if line == "//" && allow_blocks_with_empty_lines {
|
||||||
|
block.contents.push(String::new());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let is_comment = line.starts_with(prefix);
|
||||||
|
if is_comment {
|
||||||
|
block.contents.push(line[prefix.len()..].to_string());
|
||||||
|
} else {
|
||||||
|
if !block.contents.is_empty() {
|
||||||
|
res.push(mem::replace(&mut block, dummy_block.clone()));
|
||||||
|
}
|
||||||
|
block.line = line_num + 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !block.contents.is_empty() {
|
||||||
|
res.push(block)
|
||||||
}
|
}
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_extract_comment_blocks(
|
|
||||||
text: &str,
|
|
||||||
allow_blocks_with_empty_lines: bool,
|
|
||||||
) -> Vec<(usize, Vec<String>)> {
|
|
||||||
let mut res = Vec::new();
|
|
||||||
|
|
||||||
let prefix = "// ";
|
|
||||||
let lines = text.lines().map(str::trim_start);
|
|
||||||
|
|
||||||
let mut block = (0, vec![]);
|
|
||||||
for (line_num, line) in lines.enumerate() {
|
|
||||||
if line == "//" && allow_blocks_with_empty_lines {
|
|
||||||
block.1.push(String::new());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let is_comment = line.starts_with(prefix);
|
|
||||||
if is_comment {
|
|
||||||
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.1.is_empty() {
|
|
||||||
res.push(block)
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Location {
|
pub struct Location {
|
||||||
pub file: PathBuf,
|
pub file: PathBuf,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user