Build an `additional_text_edits` entry that inserts a `use` statement for the given fully-qualified class name at the alphabetically correct position in the file's existing use block. When the FQN has no namespace separator (e.g. `PDO`, `DateTime`), an import is only needed if the current file declares a namespace — otherwise we are already in the global namespace and no `use` statement is requir
(
fqn: &str,
use_block: &UseBlockInfo,
file_namespace: &Option<String>,
)
| 356 | /// namespace, a blank line (`\n`) is prepended to separate the new |
| 357 | /// import from the `namespace` declaration. |
| 358 | pub(crate) fn build_use_edit( |
| 359 | fqn: &str, |
| 360 | use_block: &UseBlockInfo, |
| 361 | file_namespace: &Option<String>, |
| 362 | ) -> Option<Vec<TextEdit>> { |
| 363 | // No namespace separator → this is a global class (e.g. `PDO`, `DateTime`). |
| 364 | // Only needs an import when the current file declares a namespace; |
| 365 | // otherwise we're already in the global namespace. |
| 366 | if !fqn.contains('\\') && file_namespace.is_none() { |
| 367 | return None; |
| 368 | } |
| 369 | |
| 370 | let insert_pos = use_block.insert_position_for(fqn); |
| 371 | |
| 372 | // When there are no existing imports and the file has a namespace, |
| 373 | // prepend a blank line to separate the namespace declaration from |
| 374 | // the use block. |
| 375 | let prefix = if use_block.existing.is_empty() && use_block.has_namespace { |
| 376 | "\n" |
| 377 | } else { |
| 378 | "" |
| 379 | }; |
| 380 | |
| 381 | Some(vec![TextEdit { |
| 382 | range: Range { |
| 383 | start: insert_pos, |
| 384 | end: insert_pos, |
| 385 | }, |
| 386 | new_text: format!("{}use {};\n", prefix, fqn), |
| 387 | }]) |
| 388 | } |
| 389 | |
| 390 | /// Build an `additional_text_edits` entry that inserts a `use function` |
| 391 | /// statement for the given fully-qualified function name at the |