mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Speed up completion
This commit is contained in:
parent
349c4fceca
commit
a36ff4a100
8 changed files with 19 additions and 20 deletions
|
@ -568,6 +568,7 @@ fn find_root(node: &SyntaxNode) -> SyntaxNode {
|
||||||
node.ancestors().last().unwrap()
|
node.ancestors().last().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct SemanticsScope<'a> {
|
pub struct SemanticsScope<'a> {
|
||||||
pub db: &'a dyn HirDatabase,
|
pub db: &'a dyn HirDatabase,
|
||||||
resolver: Resolver,
|
resolver: Resolver,
|
||||||
|
|
|
@ -195,7 +195,7 @@ fn parse_derive_input(derive_input: ast::TokenTree) -> Result<FxHashSet<String>,
|
||||||
|
|
||||||
fn get_derive_names_in_scope(ctx: &CompletionContext) -> FxHashSet<String> {
|
fn get_derive_names_in_scope(ctx: &CompletionContext) -> FxHashSet<String> {
|
||||||
let mut result = FxHashSet::default();
|
let mut result = FxHashSet::default();
|
||||||
ctx.scope().process_all_names(&mut |name, scope_def| {
|
ctx.scope.process_all_names(&mut |name, scope_def| {
|
||||||
if let hir::ScopeDef::MacroDef(mac) = scope_def {
|
if let hir::ScopeDef::MacroDef(mac) = scope_def {
|
||||||
if mac.is_derive_macro() {
|
if mac.is_derive_macro() {
|
||||||
result.insert(name.to_string());
|
result.insert(name.to_string());
|
||||||
|
|
|
@ -29,7 +29,7 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) {
|
fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) {
|
||||||
for receiver in receiver.autoderef(ctx.db) {
|
for receiver in receiver.autoderef(ctx.db) {
|
||||||
for (field, ty) in receiver.fields(ctx.db) {
|
for (field, ty) in receiver.fields(ctx.db) {
|
||||||
if ctx.scope().module().map_or(false, |m| !field.is_visible_from(ctx.db, m)) {
|
if ctx.scope.module().map_or(false, |m| !field.is_visible_from(ctx.db, m)) {
|
||||||
// Skip private field. FIXME: If the definition location of the
|
// Skip private field. FIXME: If the definition location of the
|
||||||
// field is editable, we should show the completion
|
// field is editable, we should show the completion
|
||||||
continue;
|
continue;
|
||||||
|
@ -46,10 +46,10 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: &Ty
|
||||||
fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) {
|
fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) {
|
||||||
if let Some(krate) = ctx.krate {
|
if let Some(krate) = ctx.krate {
|
||||||
let mut seen_methods = FxHashSet::default();
|
let mut seen_methods = FxHashSet::default();
|
||||||
let traits_in_scope = ctx.scope().traits_in_scope();
|
let traits_in_scope = ctx.scope.traits_in_scope();
|
||||||
receiver.iterate_method_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, func| {
|
receiver.iterate_method_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, func| {
|
||||||
if func.has_self_param(ctx.db)
|
if func.has_self_param(ctx.db)
|
||||||
&& ctx.scope().module().map_or(true, |m| func.is_visible_from(ctx.db, m))
|
&& ctx.scope.module().map_or(true, |m| func.is_visible_from(ctx.db, m))
|
||||||
&& seen_methods.insert(func.name(ctx.db))
|
&& seen_methods.insert(func.name(ctx.db))
|
||||||
{
|
{
|
||||||
acc.add_function(ctx, func, None);
|
acc.add_function(ctx, func, None);
|
||||||
|
|
|
@ -5,7 +5,7 @@ use crate::completion::{CompletionContext, Completions};
|
||||||
pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
// Show only macros in top level.
|
// Show only macros in top level.
|
||||||
if ctx.is_new_item {
|
if ctx.is_new_item {
|
||||||
ctx.scope().process_all_names(&mut |name, res| {
|
ctx.scope.process_all_names(&mut |name, res| {
|
||||||
if let hir::ScopeDef::MacroDef(mac) = res {
|
if let hir::ScopeDef::MacroDef(mac) = res {
|
||||||
acc.add_macro(ctx, Some(name.to_string()), mac);
|
acc.add_macro(ctx, Some(name.to_string()), mac);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
|
|
||||||
// FIXME: ideally, we should look at the type we are matching against and
|
// FIXME: ideally, we should look at the type we are matching against and
|
||||||
// suggest variants + auto-imports
|
// suggest variants + auto-imports
|
||||||
ctx.scope().process_all_names(&mut |name, res| {
|
ctx.scope.process_all_names(&mut |name, res| {
|
||||||
match &res {
|
match &res {
|
||||||
hir::ScopeDef::ModuleDef(def) => match def {
|
hir::ScopeDef::ModuleDef(def) => match def {
|
||||||
hir::ModuleDef::Adt(hir::Adt::Enum(..))
|
hir::ModuleDef::Adt(hir::Adt::Enum(..))
|
||||||
|
|
|
@ -17,21 +17,20 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let scope = ctx.scope();
|
let context_module = ctx.scope.module();
|
||||||
let context_module = scope.module();
|
|
||||||
|
|
||||||
let res = match scope.resolve_hir_path_qualifier(&path) {
|
let resolution = match ctx.scope.resolve_hir_path_qualifier(&path) {
|
||||||
Some(res) => res,
|
Some(res) => res,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add associated types on type parameters and `Self`.
|
// Add associated types on type parameters and `Self`.
|
||||||
res.assoc_type_shorthand_candidates(ctx.db, |alias| {
|
resolution.assoc_type_shorthand_candidates(ctx.db, |alias| {
|
||||||
acc.add_type_alias(ctx, alias);
|
acc.add_type_alias(ctx, alias);
|
||||||
None::<()>
|
None::<()>
|
||||||
});
|
});
|
||||||
|
|
||||||
match res {
|
match resolution {
|
||||||
PathResolution::Def(hir::ModuleDef::Module(module)) => {
|
PathResolution::Def(hir::ModuleDef::Module(module)) => {
|
||||||
let module_scope = module.scope(ctx.db, context_module);
|
let module_scope = module.scope(ctx.db, context_module);
|
||||||
for (name, def) in module_scope {
|
for (name, def) in module_scope {
|
||||||
|
@ -68,7 +67,7 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
|
||||||
|
|
||||||
let krate = ctx.krate;
|
let krate = ctx.krate;
|
||||||
if let Some(krate) = krate {
|
if let Some(krate) = krate {
|
||||||
let traits_in_scope = ctx.scope().traits_in_scope();
|
let traits_in_scope = ctx.scope.traits_in_scope();
|
||||||
ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
|
ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
|
||||||
if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
|
if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
|
||||||
return None;
|
return None;
|
||||||
|
@ -113,13 +112,13 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
|
||||||
}
|
}
|
||||||
PathResolution::TypeParam(_) | PathResolution::SelfType(_) => {
|
PathResolution::TypeParam(_) | PathResolution::SelfType(_) => {
|
||||||
if let Some(krate) = ctx.krate {
|
if let Some(krate) = ctx.krate {
|
||||||
let ty = match res {
|
let ty = match resolution {
|
||||||
PathResolution::TypeParam(param) => param.ty(ctx.db),
|
PathResolution::TypeParam(param) => param.ty(ctx.db),
|
||||||
PathResolution::SelfType(impl_def) => impl_def.target_ty(ctx.db),
|
PathResolution::SelfType(impl_def) => impl_def.target_ty(ctx.db),
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
let traits_in_scope = ctx.scope().traits_in_scope();
|
let traits_in_scope = ctx.scope.traits_in_scope();
|
||||||
let mut seen = FxHashSet::default();
|
let mut seen = FxHashSet::default();
|
||||||
ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
|
ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
|
||||||
if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
|
if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
|
||||||
|
|
|
@ -25,7 +25,7 @@ pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.scope().process_all_names(&mut |name, res| {
|
ctx.scope.process_all_names(&mut |name, res| {
|
||||||
if ctx.use_item_syntax.is_some() {
|
if ctx.use_item_syntax.is_some() {
|
||||||
if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) {
|
if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) {
|
||||||
if name_ref.syntax().text() == name.to_string().as_str() {
|
if name_ref.syntax().text() == name.to_string().as_str() {
|
||||||
|
@ -42,7 +42,7 @@ fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &T
|
||||||
if let Some(Adt::Enum(enum_data)) = ty.as_adt() {
|
if let Some(Adt::Enum(enum_data)) = ty.as_adt() {
|
||||||
let variants = enum_data.variants(ctx.db);
|
let variants = enum_data.variants(ctx.db);
|
||||||
|
|
||||||
let module = if let Some(module) = ctx.scope().module() {
|
let module = if let Some(module) = ctx.scope.module() {
|
||||||
// Compute path from the completion site if available.
|
// Compute path from the completion site if available.
|
||||||
module
|
module
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -24,6 +24,7 @@ use test_utils::mark;
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct CompletionContext<'a> {
|
pub(crate) struct CompletionContext<'a> {
|
||||||
pub(super) sema: Semantics<'a, RootDatabase>,
|
pub(super) sema: Semantics<'a, RootDatabase>,
|
||||||
|
pub(super) scope: SemanticsScope<'a>,
|
||||||
pub(super) db: &'a RootDatabase,
|
pub(super) db: &'a RootDatabase,
|
||||||
pub(super) config: &'a CompletionConfig,
|
pub(super) config: &'a CompletionConfig,
|
||||||
pub(super) offset: TextSize,
|
pub(super) offset: TextSize,
|
||||||
|
@ -106,8 +107,10 @@ impl<'a> CompletionContext<'a> {
|
||||||
let original_token =
|
let original_token =
|
||||||
original_file.syntax().token_at_offset(position.offset).left_biased()?;
|
original_file.syntax().token_at_offset(position.offset).left_biased()?;
|
||||||
let token = sema.descend_into_macros(original_token.clone());
|
let token = sema.descend_into_macros(original_token.clone());
|
||||||
|
let scope = sema.scope_at_offset(&token.parent(), position.offset);
|
||||||
let mut ctx = CompletionContext {
|
let mut ctx = CompletionContext {
|
||||||
sema,
|
sema,
|
||||||
|
scope,
|
||||||
db,
|
db,
|
||||||
config,
|
config,
|
||||||
original_token,
|
original_token,
|
||||||
|
@ -207,10 +210,6 @@ impl<'a> CompletionContext<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn scope(&self) -> SemanticsScope<'_> {
|
|
||||||
self.sema.scope_at_offset(&self.token.parent(), self.offset)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) {
|
fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) {
|
||||||
let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap();
|
let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap();
|
||||||
let syntax_element = NodeOrToken::Token(fake_ident_token.clone());
|
let syntax_element = NodeOrToken::Token(fake_ident_token.clone());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue