From ef5f8cd265b4bf161832ee23abfbe10605cf5b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 29 Dec 2022 21:07:09 +0100 Subject: fix(lsp): "Add all missing imports" uses correct specifiers (#17216) This commit fixes "Add all missing imports" quick fix; before it was replacing all occurrences with the same specifier. Now every line returned from TSC is processed individually. --- cli/lsp/analysis.rs | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) (limited to 'cli/lsp') diff --git a/cli/lsp/analysis.rs b/cli/lsp/analysis.rs index c4b9f0a86..bce89cc35 100644 --- a/cli/lsp/analysis.rs +++ b/cli/lsp/analysis.rs @@ -184,28 +184,30 @@ pub fn fix_ts_import_changes( for change in changes { let mut text_changes = Vec::new(); for text_change in &change.text_changes { - if let Some(captures) = - IMPORT_SPECIFIER_RE.captures(&text_change.new_text) - { - let specifier = captures - .get(1) - .ok_or_else(|| anyhow!("Missing capture."))? - .as_str(); - if let Some(new_specifier) = - check_specifier(specifier, referrer, documents) - { - let new_text = - text_change.new_text.replace(specifier, &new_specifier); - text_changes.push(tsc::TextChange { - span: text_change.span.clone(), - new_text, - }); - } else { - text_changes.push(text_change.clone()); - } - } else { - text_changes.push(text_change.clone()); - } + let lines = text_change.new_text.split('\n'); + + let new_lines: Vec = lines + .map(|line| { + // This assumes that there's only one import per line. + if let Some(captures) = IMPORT_SPECIFIER_RE.captures(line) { + let specifier = captures.get(1).unwrap().as_str(); + if let Some(new_specifier) = + check_specifier(specifier, referrer, documents) + { + line.replace(specifier, &new_specifier) + } else { + line.to_string() + } + } else { + line.to_string() + } + }) + .collect(); + + text_changes.push(tsc::TextChange { + span: text_change.span.clone(), + new_text: new_lines.join("\n").to_string(), + }); } r.push(tsc::FileTextChanges { file_name: change.file_name.clone(), -- cgit v1.2.3