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

Method get_available_completions

src/shell/helper.rs:61–100  ·  view source on GitHub ↗
(
        &self,
        words: &'w [String],
    )

Source from the content-addressed store, hash-verified

59 }
60
61 fn get_available_completions<'w>(
62 &self,
63 words: &'w [String],
64 ) -> Option<(&'w str, impl Iterator<Item = PyResult<PyStrRef>> + 'vm)> {
65 // the very first word and then all the ones after the dot
66 let (first, rest) = words.split_first().unwrap();
67
68 let str_iter_method = |obj, name| {
69 let iter = self.vm.call_special_method(obj, name, ())?;
70 ArgIterable::<PyStrRef>::try_from_object(self.vm, iter)?.iter(self.vm)
71 };
72
73 let (word_start, iter1, iter2) = if let Some((last, parents)) = rest.split_last() {
74 // we need to get an attribute based off of the dir() of an object
75
76 // last: the last word, could be empty if it ends with a dot
77 // parents: the words before the dot
78
79 let mut current = self.globals.get_item_opt(first.as_str(), self.vm).ok()??;
80
81 for attr in parents {
82 let attr = self.vm.ctx.new_str(attr.as_str());
83 current = current.get_attr(&attr, self.vm).ok()?;
84 }
85
86 let current_iter = str_iter_method(&current, identifier!(self.vm, __dir__)).ok()?;
87
88 (last, current_iter, None)
89 } else {
90 // we need to get a variable based off of globals/builtins
91
92 let globals =
93 str_iter_method(self.globals.as_object(), identifier!(self.vm, keys)).ok()?;
94 let builtins =
95 str_iter_method(self.vm.builtins.as_object(), identifier!(self.vm, __dir__))
96 .ok()?;
97 (first, globals, Some(builtins))
98 };
99 Some((word_start, iter1.chain(iter2.into_iter().flatten())))
100 }
101
102 fn complete_opt(&self, line: &str) -> Option<(usize, Vec<String>)> {
103 let (startpos, words) = split_idents_on_dot(line)?;

Callers 1

complete_optMethod · 0.80

Calls 13

call_special_methodMethod · 0.80
okMethod · 0.80
get_item_optMethod · 0.80
chainMethod · 0.80
SomeClass · 0.50
unwrapMethod · 0.45
iterMethod · 0.45
as_strMethod · 0.45
new_strMethod · 0.45
get_attrMethod · 0.45
as_objectMethod · 0.45
flattenMethod · 0.45

Tested by

no test coverage detected