From: Dezhi Wu Date: Fri, 29 Oct 2021 02:00:10 +0000 (+0800) Subject: Fix: use a concise way to change link form when generating package.json X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=097d527cbdd288c9a702cfe6f6611e9c0831ad4e;p=rust.git Fix: use a concise way to change link form when generating package.json --- diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index ee6ab698fa4..32e4e8ebeeb 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -1289,17 +1289,17 @@ fn generate_package_json_config() { .to_string(); schema.push_str(",\n"); - let mut new_schema = schema.clone(); + // Transform the asciidoc form link to markdown style. let url_matches = schema.match_indices("https://"); - let mut cnt = 0; - for (idx, _) in url_matches { + let mut url_offsets = url_matches.map(|(idx, _)| idx).collect::>(); + url_offsets.reverse(); + for idx in url_offsets { let link = &schema[idx..]; // matching on whitespace to ignore normal links if let Some(link_end) = link.find(|c| c == ' ' || c == '[') { if link.chars().nth(link_end) == Some('[') { - new_schema.insert(idx + cnt, '('); - new_schema.insert(idx + link_end + cnt + 1, ')'); - cnt = cnt + 2; + schema.insert(idx, '('); + schema.insert(idx + link_end + 1, ')'); } } } @@ -1314,9 +1314,9 @@ fn generate_package_json_config() { let end = package_json.find(end_marker).unwrap(); let p = remove_ws(&package_json[start..end]); - let s = remove_ws(&new_schema); + let s = remove_ws(&schema); if !p.contains(&s) { - package_json.replace_range(start..end, &new_schema); + package_json.replace_range(start..end, &schema); ensure_file_contents(&package_json_path, &package_json) } }