MCPcopy Create free account
hub / github.com/PHPantom-dev/phpantom_lsp / insert_position_for_key

Method insert_position_for_key

src/completion/use_edit.rs:66–123  ·  view source on GitHub ↗

Like [`insert_position_for`](Self::insert_position_for) but accepts a pre-computed sort key instead of deriving one from the FQN. This is useful for `use function` and `use const` imports whose sort keys carry a `"function "` or `"const "` prefix so they sort into their own group. Import statements are organized into three groups that never interleave: 1. **Class** imports (bare `use Foo\Bar;`)

(&self, key: &str)

Source from the content-addressed store, hash-verified

64 /// first entry of a higher-priority group if no lower group
65 /// exists).
66 pub(crate) fn insert_position_for_key(&self, key: &str) -> Position {
67 if self.existing.is_empty() {
68 return Position {
69 line: self.fallback_line,
70 character: 0,
71 };
72 }
73
74 let new_group = Self::key_group(key);
75
76 // Collect entries that belong to the same group.
77 let same_group: Vec<&(u32, String)> = self
78 .existing
79 .iter()
80 .filter(|(_, k)| Self::key_group(k) == new_group)
81 .collect();
82
83 if !same_group.is_empty() {
84 // Insert alphabetically within the group.
85 for (line, existing_key) in &same_group {
86 if existing_key.as_str() > key {
87 return Position {
88 line: *line,
89 character: 0,
90 };
91 }
92 }
93 // Sorts after every entry in the group — append after the last one.
94 let last_line = same_group.last().expect("non-empty").0;
95 return Position {
96 line: last_line + 1,
97 character: 0,
98 };
99 }
100
101 // The target group has no entries yet. Place after the last
102 // entry of a lower-priority group, or before the first entry
103 // of a higher-priority group.
104 let lower: Vec<&(u32, String)> = self
105 .existing
106 .iter()
107 .filter(|(_, k)| Self::key_group(k) < new_group)
108 .collect();
109
110 if let Some(&&(last_line, _)) = lower.last() {
111 return Position {
112 line: last_line + 1,
113 character: 0,
114 };
115 }
116
117 // No lower-priority group — insert before the very first import.
118 let first_line = self.existing.first().expect("non-empty checked above").0;
119 Position {
120 line: first_line,
121 character: 0,
122 }
123 }

Callers 2

insert_position_forMethod · 0.80
build_use_function_editFunction · 0.80

Calls 6

filterMethod · 0.80
iterMethod · 0.80
as_strMethod · 0.80
lastMethod · 0.80
is_emptyMethod · 0.45
firstMethod · 0.45

Tested by

no test coverage detected