MCPcopy Index your code
hub / github.com/RustPython/RustPython / complete_opt

Method complete_opt

src/shell/helper.rs:102–146  ·  view source on GitHub ↗
(&self, line: &str)

Source from the content-addressed store, hash-verified

100 }
101
102 fn complete_opt(&self, line: &str) -> Option<(usize, Vec<String>)> {
103 let (startpos, words) = split_idents_on_dot(line)?;
104
105 let (word_start, iter) = self.get_available_completions(&words)?;
106
107 let all_completions = iter
108 .filter(|res| {
109 res.as_ref()
110 .ok()
111 .is_none_or(|s| s.as_bytes().starts_with(word_start.as_bytes()))
112 })
113 .collect::<Result<Vec<_>, _>>()
114 .ok()?;
115 let mut completions = if word_start.starts_with('_') {
116 // if they're already looking for something starting with a '_', just give
117 // them all the completions
118 all_completions
119 } else {
120 // only the completions that don't start with a '_'
121 let no_underscore = all_completions
122 .iter()
123 .filter(|&s| !s.as_bytes().starts_with(b"_"))
124 .cloned()
125 .collect::<Vec<_>>();
126
127 // if there are only completions that start with a '_', give them all of the
128 // completions, otherwise only the ones that don't start with '_'
129 if no_underscore.is_empty() {
130 all_completions
131 } else {
132 no_underscore
133 }
134 };
135
136 // sort the completions alphabetically
137 completions.sort_by(|a, b| a.as_wtf8().cmp(b.as_wtf8()));
138
139 Some((
140 startpos,
141 completions
142 .into_iter()
143 .map(|s| s.expect_str().to_owned())
144 .collect(),
145 ))
146 }
147}
148
149cfg_if::cfg_if! {

Callers

nothing calls this directly

Calls 15

split_idents_on_dotFunction · 0.85
okMethod · 0.80
starts_withMethod · 0.80
collectMethod · 0.80
expect_strMethod · 0.80
SomeClass · 0.50
filterMethod · 0.45
as_refMethod · 0.45
as_bytesMethod · 0.45
iterMethod · 0.45
is_emptyMethod · 0.45

Tested by

no test coverage detected