mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
reorganize completion
This commit is contained in:
parent
22949dab26
commit
af17fc9697
2 changed files with 75 additions and 88 deletions
|
@ -4,7 +4,7 @@ use ra_syntax::{
|
||||||
ast::{self, AstChildren, LoopBodyOwner, ModuleItemOwner},
|
ast::{self, AstChildren, LoopBodyOwner, ModuleItemOwner},
|
||||||
AstNode, AtomEdit, SourceFileNode,
|
AstNode, AtomEdit, SourceFileNode,
|
||||||
SyntaxKind::*,
|
SyntaxKind::*,
|
||||||
SyntaxNodeRef, TextUnit,
|
SyntaxNodeRef,
|
||||||
};
|
};
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ use crate::{
|
||||||
descriptors::module::{ModuleId, ModuleScope, ModuleTree, ModuleSource},
|
descriptors::module::{ModuleId, ModuleScope, ModuleTree, ModuleSource},
|
||||||
descriptors::DescriptorDatabase,
|
descriptors::DescriptorDatabase,
|
||||||
input::FilesDatabase,
|
input::FilesDatabase,
|
||||||
Cancelable, FilePosition,
|
Cancelable, FilePosition, FileId,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -27,47 +27,87 @@ pub struct CompletionItem {
|
||||||
pub snippet: Option<String>,
|
pub snippet: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn resolve_based_completion(
|
pub(crate) fn completions(
|
||||||
db: &db::RootDatabase,
|
db: &db::RootDatabase,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
) -> Cancelable<Option<Vec<CompletionItem>>> {
|
) -> Cancelable<Option<Vec<CompletionItem>>> {
|
||||||
let source_root_id = db.file_source_root(position.file_id);
|
let original_file = db.file_syntax(position.file_id);
|
||||||
let file = db.file_syntax(position.file_id);
|
// Insert a fake ident to get a valid parse tree
|
||||||
let module_tree = db.module_tree(source_root_id)?;
|
|
||||||
let module_id =
|
|
||||||
match module_tree.any_module_for_source(ModuleSource::SourceFile(position.file_id)) {
|
|
||||||
None => return Ok(None),
|
|
||||||
Some(it) => it,
|
|
||||||
};
|
|
||||||
let file = {
|
let file = {
|
||||||
let edit = AtomEdit::insert(position.offset, "intellijRulezz".to_string());
|
let edit = AtomEdit::insert(position.offset, "intellijRulezz".to_string());
|
||||||
file.reparse(&edit)
|
original_file.reparse(&edit)
|
||||||
};
|
};
|
||||||
let target_module_id = match find_target_module(&module_tree, module_id, &file, position.offset)
|
|
||||||
|
let mut res = Vec::new();
|
||||||
|
let mut has_completions = false;
|
||||||
|
// First, let's try to complete a reference to some declaration.
|
||||||
|
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) {
|
||||||
|
has_completions = true;
|
||||||
|
// completion from lexical scope
|
||||||
|
complete_name_ref(&file, name_ref, &mut res);
|
||||||
|
// special case, `trait T { fn foo(i_am_a_name_ref) {} }`
|
||||||
|
if is_node::<ast::Param>(name_ref.syntax()) {
|
||||||
|
param_completions(name_ref.syntax(), &mut res);
|
||||||
|
}
|
||||||
|
// snippet completions
|
||||||
{
|
{
|
||||||
None => return Ok(None),
|
let name_range = name_ref.syntax().range();
|
||||||
|
let top_node = name_ref
|
||||||
|
.syntax()
|
||||||
|
.ancestors()
|
||||||
|
.take_while(|it| it.range() == name_range)
|
||||||
|
.last()
|
||||||
|
.unwrap();
|
||||||
|
match top_node.parent().map(|it| it.kind()) {
|
||||||
|
Some(SOURCE_FILE) | Some(ITEM_LIST) => complete_mod_item_snippets(&mut res),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
complete_path(db, position.file_id, name_ref, &mut res)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, if this is a declaration, use heuristics to suggest a name.
|
||||||
|
if let Some(name) = find_node_at_offset::<ast::Name>(file.syntax(), position.offset) {
|
||||||
|
if is_node::<ast::Param>(name.syntax()) {
|
||||||
|
has_completions = true;
|
||||||
|
param_completions(name.syntax(), &mut res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let res = if has_completions { Some(res) } else { None };
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn complete_path(
|
||||||
|
db: &db::RootDatabase,
|
||||||
|
file_id: FileId,
|
||||||
|
name_ref: ast::NameRef,
|
||||||
|
acc: &mut Vec<CompletionItem>,
|
||||||
|
) -> Cancelable<()> {
|
||||||
|
let source_root_id = db.file_source_root(file_id);
|
||||||
|
let module_tree = db.module_tree(source_root_id)?;
|
||||||
|
let module_id = match module_tree.any_module_for_source(ModuleSource::SourceFile(file_id)) {
|
||||||
|
None => return Ok(()),
|
||||||
|
Some(it) => it,
|
||||||
|
};
|
||||||
|
let target_module_id = match find_target_module(&module_tree, module_id, name_ref) {
|
||||||
|
None => return Ok(()),
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
};
|
};
|
||||||
let module_scope = db.module_scope(source_root_id, target_module_id)?;
|
let module_scope = db.module_scope(source_root_id, target_module_id)?;
|
||||||
let res: Vec<_> = module_scope
|
let completions = module_scope.entries().iter().map(|entry| CompletionItem {
|
||||||
.entries()
|
|
||||||
.iter()
|
|
||||||
.map(|entry| CompletionItem {
|
|
||||||
label: entry.name().to_string(),
|
label: entry.name().to_string(),
|
||||||
lookup: None,
|
lookup: None,
|
||||||
snippet: None,
|
snippet: None,
|
||||||
})
|
});
|
||||||
.collect();
|
acc.extend(completions);
|
||||||
Ok(Some(res))
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn find_target_module(
|
fn find_target_module(
|
||||||
module_tree: &ModuleTree,
|
module_tree: &ModuleTree,
|
||||||
module_id: ModuleId,
|
module_id: ModuleId,
|
||||||
file: &SourceFileNode,
|
name_ref: ast::NameRef,
|
||||||
offset: TextUnit,
|
|
||||||
) -> Option<ModuleId> {
|
) -> Option<ModuleId> {
|
||||||
let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), offset)?;
|
|
||||||
let mut crate_path = crate_path(name_ref)?;
|
let mut crate_path = crate_path(name_ref)?;
|
||||||
|
|
||||||
crate_path.pop();
|
crate_path.pop();
|
||||||
|
@ -98,50 +138,6 @@ fn crate_path(name_ref: ast::NameRef) -> Option<Vec<ast::NameRef>> {
|
||||||
Some(res)
|
Some(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn scope_completion(
|
|
||||||
db: &db::RootDatabase,
|
|
||||||
position: FilePosition,
|
|
||||||
) -> Option<Vec<CompletionItem>> {
|
|
||||||
let original_file = db.file_syntax(position.file_id);
|
|
||||||
// Insert a fake ident to get a valid parse tree
|
|
||||||
let file = {
|
|
||||||
let edit = AtomEdit::insert(position.offset, "intellijRulezz".to_string());
|
|
||||||
original_file.reparse(&edit)
|
|
||||||
};
|
|
||||||
let mut has_completions = false;
|
|
||||||
let mut res = Vec::new();
|
|
||||||
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) {
|
|
||||||
has_completions = true;
|
|
||||||
complete_name_ref(&file, name_ref, &mut res);
|
|
||||||
// special case, `trait T { fn foo(i_am_a_name_ref) {} }`
|
|
||||||
if is_node::<ast::Param>(name_ref.syntax()) {
|
|
||||||
param_completions(name_ref.syntax(), &mut res);
|
|
||||||
}
|
|
||||||
let name_range = name_ref.syntax().range();
|
|
||||||
let top_node = name_ref
|
|
||||||
.syntax()
|
|
||||||
.ancestors()
|
|
||||||
.take_while(|it| it.range() == name_range)
|
|
||||||
.last()
|
|
||||||
.unwrap();
|
|
||||||
match top_node.parent().map(|it| it.kind()) {
|
|
||||||
Some(SOURCE_FILE) | Some(ITEM_LIST) => complete_mod_item_snippets(&mut res),
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if let Some(name) = find_node_at_offset::<ast::Name>(file.syntax(), position.offset) {
|
|
||||||
if is_node::<ast::Param>(name.syntax()) {
|
|
||||||
has_completions = true;
|
|
||||||
param_completions(name.syntax(), &mut res);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if has_completions {
|
|
||||||
Some(res)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn complete_module_items(
|
fn complete_module_items(
|
||||||
file: &SourceFileNode,
|
file: &SourceFileNode,
|
||||||
items: AstChildren<ast::ModuleItem>,
|
items: AstChildren<ast::ModuleItem>,
|
||||||
|
@ -383,7 +379,8 @@ mod tests {
|
||||||
|
|
||||||
fn check_scope_completion(code: &str, expected_completions: &str) {
|
fn check_scope_completion(code: &str, expected_completions: &str) {
|
||||||
let (analysis, position) = single_file_with_position(code);
|
let (analysis, position) = single_file_with_position(code);
|
||||||
let completions = scope_completion(&analysis.imp.db, position)
|
let completions = completions(&analysis.imp.db, position)
|
||||||
|
.unwrap()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|c| c.snippet.is_none())
|
.filter(|c| c.snippet.is_none())
|
||||||
|
@ -393,7 +390,8 @@ mod tests {
|
||||||
|
|
||||||
fn check_snippet_completion(code: &str, expected_completions: &str) {
|
fn check_snippet_completion(code: &str, expected_completions: &str) {
|
||||||
let (analysis, position) = single_file_with_position(code);
|
let (analysis, position) = single_file_with_position(code);
|
||||||
let completions = scope_completion(&analysis.imp.db, position)
|
let completions = completions(&analysis.imp.db, position)
|
||||||
|
.unwrap()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|c| c.snippet.is_some())
|
.filter(|c| c.snippet.is_some())
|
||||||
|
|
|
@ -17,7 +17,7 @@ use rustc_hash::FxHashSet;
|
||||||
use salsa::{Database, ParallelDatabase};
|
use salsa::{Database, ParallelDatabase};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
completion::{resolve_based_completion, scope_completion, CompletionItem},
|
completion::{completions, CompletionItem},
|
||||||
db::{self, FileSyntaxQuery, SyntaxDatabase},
|
db::{self, FileSyntaxQuery, SyntaxDatabase},
|
||||||
descriptors::{
|
descriptors::{
|
||||||
function::{FnDescriptor, FnId},
|
function::{FnDescriptor, FnId},
|
||||||
|
@ -267,18 +267,7 @@ impl AnalysisImpl {
|
||||||
self.db.crate_graph().crate_roots[&crate_id]
|
self.db.crate_graph().crate_roots[&crate_id]
|
||||||
}
|
}
|
||||||
pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> {
|
pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> {
|
||||||
let mut res = Vec::new();
|
completions(&self.db, position)
|
||||||
let mut has_completions = false;
|
|
||||||
if let Some(scope_based) = scope_completion(&self.db, position) {
|
|
||||||
res.extend(scope_based);
|
|
||||||
has_completions = true;
|
|
||||||
}
|
|
||||||
if let Some(scope_based) = resolve_based_completion(&self.db, position)? {
|
|
||||||
res.extend(scope_based);
|
|
||||||
has_completions = true;
|
|
||||||
}
|
|
||||||
let res = if has_completions { Some(res) } else { None };
|
|
||||||
Ok(res)
|
|
||||||
}
|
}
|
||||||
pub fn approximately_resolve_symbol(
|
pub fn approximately_resolve_symbol(
|
||||||
&self,
|
&self,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue