1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! Handler for the `add_import` command: add an import statement to a file.
//!
//! Analyzes existing imports, checks for duplicates, finds the correct
//! insertion point based on group and alphabetical ordering, and inserts
//! the new import with auto-backup and syntax validation.
use std::path::Path;
use crate::context::AppContext;
use crate::edit;
use crate::imports;
use crate::parser::{detect_language, LangId};
use crate::protocol::{RawRequest, Response};
/// Handle an `add_import` request.
///
/// Params:
/// - `file` (string, required) — target file path
/// - `module` (string, required) — the module path (e.g., "react", "./utils")
/// - `names` (array of strings, optional) — named imports (e.g., ["useState", "useEffect"])
/// - `default_import` (string, optional) — default import name (e.g., "React")
/// - `type_only` (bool, optional, default false) — whether this is a type-only import
///
/// Returns: `{ file, added, module, group, already_present?, syntax_valid?, backup_id? }`
pub fn handle_add_import(req: &RawRequest, ctx: &AppContext) -> Response {
let op_id = crate::backup::new_op_id();
// --- Extract params ---
let file = match req.params.get("file").and_then(|v| v.as_str()) {
Some(f) => f,
None => {
return Response::error(
&req.id,
"invalid_request",
"add_import: missing required param 'file'",
);
}
};
let module = match req.params.get("module").and_then(|v| v.as_str()) {
Some(m) => m,
None => {
return Response::error(
&req.id,
"invalid_request",
"add_import: missing required param 'module'",
);
}
};
let names: Vec<String> = req
.params
.get("names")
.and_then(|v| v.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str().map(|s| s.to_string()))
.collect()
})
.unwrap_or_default();
let default_import = req
.params
.get("default_import")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let type_only = req
.params
.get("type_only")
.and_then(|v| v.as_bool())
.unwrap_or(false);
// --- Validate ---
let path = match ctx.validate_path(&req.id, Path::new(file)) {
Ok(path) => path,
Err(resp) => return resp,
};
if !path.exists() {
return Response::error(
&req.id,
"file_not_found",
format!("add_import: file not found: {}", file),
);
}
let lang = match detect_language(&path) {
Some(l) => l,
None => {
return Response::error(
&req.id,
"invalid_request",
format!(
"add_import: unsupported file extension: {}",
path.extension()
.and_then(|e| e.to_str())
.unwrap_or("<none>")
),
);
}
};
if !imports::is_supported(lang) {
return Response::error(
&req.id,
"invalid_request",
format!(
"add_import: import management not yet supported for {:?}",
lang
),
);
}
// Must have at least one of: names, default_import, or neither (side-effect)
// All combinations are valid.
// --- Parse file and imports ---
let (source, tree, block) = match imports::parse_file_imports(&path, lang) {
Ok(result) => result,
Err(e) => {
return Response::error(&req.id, e.code(), e.to_string());
}
};
// --- Check for duplicates ---
if imports::is_duplicate(&block, module, &names, default_import.as_deref(), type_only) {
log::debug!("add_import: {} (already present)", file);
return Response::success(
&req.id,
serde_json::json!({
"file": file,
"added": false,
"module": module,
"already_present": true,
}),
);
}
// --- Determine group ---
let group = imports::classify_group(lang, module);
// --- Try to merge into an existing same-module, same-kind named import ---
//
// When adding `{ baz }` to a file that already has `import { foo } from "lib"`,
// the historical behavior inserted a second `import { baz } from "lib"` line
// (TS/JS, Rust, Py). That produces duplicate imports the linter then complains
// about, and the agent has to call `organize` afterwards to clean up.
//
// Instead, when the target module already has a value/type import statement of
// the matching kind with named specifiers, merge `names` into that statement's
// existing names (deduped + sorted) and replace its byte range. This only
// applies to languages where named imports are a list inside one statement —
// Go's "import (...)" block is handled separately by the insertion path.
let target_kind = if type_only {
imports::ImportKind::Type
} else {
imports::ImportKind::Value
};
let merge_target =
if !names.is_empty() && default_import.is_none() && !matches!(lang, LangId::Go) {
block.imports.iter().find(|imp| {
imp.module_path == module
&& imp.kind == target_kind
&& imp.namespace_import.is_none()
&& imp.default_import.is_none()
&& !imp.names.is_empty()
})
} else {
None
};
let (insert_offset, replace_end, insert_text, merged_into_existing) = if let Some(existing) =
merge_target
{
// Build the merged named-import list: union of existing + new, sorted.
let mut merged_names: Vec<String> = existing.names.clone();
for name in &names {
if !merged_names
.iter()
.any(|n| imports::specifier_matches(n, name))
{
merged_names.push(name.clone());
}
}
// Sort for deterministic output (matches generate_import_line behavior).
merged_names
.sort_by(|a, b| imports::specifier_local_name(a).cmp(imports::specifier_local_name(b)));
let merged_line = imports::generate_import_line(
lang,
&existing.module_path,
&merged_names,
None,
type_only,
);
(
existing.byte_range.start,
existing.byte_range.end,
merged_line,
true,
)
} else {
// Fall through to the original "find insertion point and insert a new
// statement" behavior.
let (insert_offset, needs_blank_before, needs_blank_after) =
imports::find_insertion_point(&source, &block, group, module, type_only);
// For Go, check if we're inserting into a grouped import block
let import_line = if matches!(lang, LangId::Go) {
let in_group = imports::go_has_grouped_import(&source, &tree).is_some();
imports::generate_go_import_line_pub(module, default_import.as_deref(), in_group)
} else {
imports::generate_import_line(
lang,
module,
&names,
default_import.as_deref(),
type_only,
)
};
// Build the text to insert
let mut insert_text = String::new();
if needs_blank_before {
insert_text.push('\n');
}
insert_text.push_str(&import_line);
insert_text.push('\n');
if needs_blank_after {
insert_text.push('\n');
}
(insert_offset, insert_offset, insert_text, false)
};
let _ = merged_into_existing;
// --- Auto-backup ---
let backup_id = match edit::auto_backup(
ctx,
req.session(),
&path,
"add_import: pre-edit backup",
Some(&op_id),
) {
Ok(id) => id,
Err(e) => {
return Response::error(&req.id, e.code(), e.to_string());
}
};
// --- Insert (or replace, when merging) ---
let new_source =
match edit::replace_byte_range(&source, insert_offset, replace_end, &insert_text) {
Ok(s) => s,
Err(e) => return Response::error(&req.id, e.code(), e.to_string()),
};
// --- Write, format, and validate ---
let mut write_result =
match edit::write_format_validate(&path, &new_source, &ctx.config(), &req.params) {
Ok(r) => r,
Err(e) => {
return Response::error(&req.id, e.code(), e.to_string());
}
};
if let Ok(final_content) = std::fs::read_to_string(&path) {
write_result.lsp_outcome = ctx.lsp_post_write(&path, &final_content, &req.params);
}
log::debug!("add_import: {}", file);
// --- Build response ---
let mut result = serde_json::json!({
"file": file,
"added": true,
"module": module,
"group": group.label(),
"formatted": write_result.formatted,
});
if let Some(valid) = write_result.syntax_valid {
result["syntax_valid"] = serde_json::json!(valid);
}
if let Some(ref reason) = write_result.format_skipped_reason {
result["format_skipped_reason"] = serde_json::json!(reason);
}
if write_result.validate_requested {
result["validation_errors"] = serde_json::json!(write_result.validation_errors);
}
if let Some(ref reason) = write_result.validate_skipped_reason {
result["validate_skipped_reason"] = serde_json::json!(reason);
}
if let Some(ref id) = backup_id {
result["backup_id"] = serde_json::json!(id);
}
write_result.append_lsp_diagnostics_to(&mut result);
Response::success(&req.id, result)
}