summaryrefslogtreecommitdiff
path: root/cli/lsp/analysis.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-12-29 21:07:09 +0100
committerGitHub <noreply@github.com>2022-12-29 21:07:09 +0100
commitef5f8cd265b4bf161832ee23abfbe10605cf5b67 (patch)
tree4ca4542fec2a89b59ccbf2d1aeb2398d493227e4 /cli/lsp/analysis.rs
parent65ea554afe1ce387ea1d663e6178079ebcf0904f (diff)
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.
Diffstat (limited to 'cli/lsp/analysis.rs')
-rw-r--r--cli/lsp/analysis.rs46
1 files changed, 24 insertions, 22 deletions
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<String> = 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(),