]> git.lizzy.rs Git - rust.git/commitdiff
Fix: use a concise way to change link form when generating package.json
authorDezhi Wu <wu543065657@163.com>
Fri, 29 Oct 2021 02:00:10 +0000 (10:00 +0800)
committerDezhi Wu <wu543065657@163.com>
Fri, 29 Oct 2021 02:00:10 +0000 (10:00 +0800)
crates/rust-analyzer/src/config.rs

index ee6ab698fa4c8ac696b03f234990c94fff7ff61b..32e4e8ebeeb6af58172f8331d83761d0a7510f8d 100644 (file)
@@ -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::<Vec<usize>>();
+        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)
         }
     }