Skip to content

Commit 4ccf8c9

Browse files
committed
[IMP] server: autocompletion take sections into account
1 parent 9d2a52e commit 4ccf8c9

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

server/src/core/symbols/symbol.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,18 @@ impl Symbol {
12341234
}
12351235
}
12361236

1237+
/// Return all symbols before the given position that are visible in the body of this symbol.
1238+
pub fn get_all_visible_symbols(&self, name_prefix: &String, position: u32) -> HashMap<OYarn, Vec<Rc<RefCell<Symbol>>>> {
1239+
match self {
1240+
Symbol::Class(c) => c.get_all_visible_symbols(name_prefix, position),
1241+
Symbol::File(f) => f.get_all_visible_symbols(name_prefix, position),
1242+
Symbol::Package(PackageSymbol::Module(m)) => m.get_all_visible_symbols(name_prefix, position),
1243+
Symbol::Package(PackageSymbol::PythonPackage(p)) => p.get_all_visible_symbols(name_prefix, position),
1244+
Symbol::Function(f) => f.get_all_visible_symbols(name_prefix, position),
1245+
_ => HashMap::new(),
1246+
}
1247+
}
1248+
12371249
/**
12381250
* Return a symbol that can be called from outside of the body of the symbol
12391251
*/
@@ -2100,17 +2112,18 @@ impl Symbol {
21002112
/*
21012113
Return all the symbols that are available at a given position or in a scope for a given start name
21022114
*/
2103-
pub fn get_all_inferred_names(on_symbol: &Rc<RefCell<Symbol>>, name: &String, position: Option<u32>) -> HashMap<OYarn, Vec<Rc<RefCell<Symbol>>>> {
2115+
pub fn get_all_inferred_names(on_symbol: &Rc<RefCell<Symbol>>, name: &String, position: u32) -> HashMap<OYarn, Vec<Rc<RefCell<Symbol>>>> {
21042116
fn helper(
2105-
on_symbol: &Rc<RefCell<Symbol>>, name: &String, position: Option<u32>, acc: &mut HashMap<OYarn, Vec<Rc<RefCell<Symbol>>>>
2117+
on_symbol: &Rc<RefCell<Symbol>>, name: &String, position: u32, acc: &mut HashMap<OYarn, Vec<Rc<RefCell<Symbol>>>>
21062118
) {
21072119
// Add symbols from files and functions
21082120
if matches!(on_symbol.borrow().typ(), SymType::FILE | SymType::FUNCTION) {
2109-
on_symbol.borrow().all_symbols().filter(|sym|
2110-
sym.borrow().name().starts_with(name) && (position.is_none() || !sym.borrow().has_range() || position.unwrap() > sym.borrow().range().end().to_u32())
2111-
).for_each(| sym| {
2112-
acc.entry(sym.borrow().name().clone()).or_default().push(sym.clone());
2113-
});
2121+
let symbols_map = on_symbol.borrow().get_all_visible_symbols(name, position);
2122+
for (sym_name, sym_vec) in symbols_map {
2123+
acc.entry(sym_name)
2124+
.or_default()
2125+
.extend(sym_vec);
2126+
}
21142127
}
21152128
// Traverse upwards if we are under a class or a function
21162129
if matches!(on_symbol.borrow().typ(), SymType::CLASS | SymType::FUNCTION) {

server/src/core/symbols/symbol_mgr.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub trait SymbolMgr {
3737
fn get_ext_symbol(&self, name: OYarn) -> Option<&Vec<Rc<RefCell<Symbol>>>>;
3838
fn _init_symbol_mgr(&mut self);
3939
fn _get_loc_symbol(&self, map: &HashMap<u32, Vec<Rc<RefCell<Symbol>>>>, position: u32, index: &SectionIndex, acc: &mut HashSet<u32>) -> ContentSymbols;
40+
fn get_all_visible_symbols(&self, name_prefix: &String, position: u32) -> HashMap<OYarn, Vec<Rc<RefCell<Symbol>>>>;
4041
}
4142

4243

@@ -156,6 +157,24 @@ macro_rules! impl_section_mgr_for {
156157
res
157158
}
158159

160+
fn get_all_visible_symbols(&self, name_prefix: &String, position: u32) -> HashMap<OYarn, Vec<Rc<RefCell<Symbol>>>> {
161+
let mut result = HashMap::new();
162+
let current_section = self.get_section_for(position);
163+
let current_index = SectionIndex::INDEX(current_section.index);
164+
165+
for (name, section_map) in self.symbols.iter() {
166+
if !name.starts_with(name_prefix) {
167+
continue;
168+
}
169+
let mut seen = HashSet::new();
170+
let content = self._get_loc_symbol(section_map, position, &current_index, &mut seen);
171+
172+
if !content.symbols.is_empty() {
173+
result.insert(name.clone(), content.symbols);
174+
}
175+
}
176+
result
177+
}
159178
}
160179
)+)
161180
}

server/src/features/completion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ fn complete_name_expression(session: &mut SessionInfo, file: &Rc<RefCell<Symbol>
800800

801801
fn complete_name(session: &mut SessionInfo, file: &Rc<RefCell<Symbol>>, offset: usize, is_param: bool, name: &String) -> Option<CompletionResponse> {
802802
let scope = Symbol::get_scope_symbol(file.clone(), offset as u32, is_param);
803-
let symbols = Symbol::get_all_inferred_names(&scope, name, Some(offset as u32));
803+
let symbols = Symbol::get_all_inferred_names(&scope, name, offset as u32);
804804
Some(CompletionResponse::List(CompletionList {
805805
is_incomplete: false,
806806
items: symbols.into_iter().map(|(_symbol_name, symbols)| {

0 commit comments

Comments
 (0)