Check whether a `use` statement for the given FQN already exists in the file content.
(content: &str, fqn: &str)
| 1707 | /// Check whether a `use` statement for the given FQN already exists in |
| 1708 | /// the file content. |
| 1709 | pub(in crate::completion) fn has_use_import(content: &str, fqn: &str) -> bool { |
| 1710 | let target = format!("use {};", fqn); |
| 1711 | let target_with_alias = format!("use {} as", fqn); // alias import |
| 1712 | for line in content.lines() { |
| 1713 | let trimmed = line.trim(); |
| 1714 | if trimmed == target || trimmed.starts_with(&target_with_alias) { |
| 1715 | return true; |
| 1716 | } |
| 1717 | // Handle group imports: `use Foo\{Bar, Baz};` |
| 1718 | // Check if the FQN's namespace prefix is used in a group import |
| 1719 | // that includes the short name. |
| 1720 | if let Some(ns_sep) = fqn.rfind('\\') { |
| 1721 | let ns_prefix = &fqn[..ns_sep]; |
| 1722 | let short = &fqn[ns_sep + 1..]; |
| 1723 | let group_prefix = format!("use {}\\{{", ns_prefix); |
| 1724 | if trimmed.starts_with(&group_prefix) { |
| 1725 | // Check if short name is in the brace list |
| 1726 | if let Some(brace_start) = trimmed.find('{') |
| 1727 | && let Some(brace_end) = trimmed.find('}') |
| 1728 | { |
| 1729 | let names = &trimmed[brace_start + 1..brace_end]; |
| 1730 | if names.split(',').any(|n| n.trim() == short) { |
| 1731 | return true; |
| 1732 | } |
| 1733 | } |
| 1734 | } |
| 1735 | } |
| 1736 | } |
| 1737 | false |
| 1738 | } |
| 1739 | |
| 1740 | // ─── Tests ────────────────────────────────────────────────────────────────── |
| 1741 |
no test coverage detected