mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 15:14:32 +00:00
6207: Extract ImportAssets out of auto_import r=matklad a=Veykril
See https://github.com/rust-analyzer/rust-analyzer/pull/6172#issuecomment-707182140
I couldn't fully pull out `AssistContext` as `find_node_at_offset_with_descend`: 81fa00c5b5/crates/assists/src/assist_context.rs (L90-L92)
requires the `SourceFile` which is private in it and I don't think making it public just for this is the right call?
6224: ⬆️ salsa r=matklad a=matklad
bors r+
🤖
6226: Add reminder to update lsp-extensions.md r=matklad a=matklad
bors r+
🤖
6227: Reduce bors timeout r=matklad a=matklad
bors r+
🤖
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
3e450cf89f
8 changed files with 359 additions and 247 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -1361,11 +1361,11 @@ checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
|
|||
|
||||
[[package]]
|
||||
name = "salsa"
|
||||
version = "0.15.2"
|
||||
version = "0.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ab29056d4fb4048a5f0d169c9b6e5526160c9ec37aded5a6879c2c9c445a8e4"
|
||||
checksum = "d8fadca2ab5de17acf66d744f4888049ca8f1bb9b8a1ab8afd9d032cc959c5dc"
|
||||
dependencies = [
|
||||
"crossbeam-utils 0.7.2",
|
||||
"crossbeam-utils 0.8.0",
|
||||
"indexmap",
|
||||
"lock_api",
|
||||
"log",
|
||||
|
@ -1378,9 +1378,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "salsa-macros"
|
||||
version = "0.15.2"
|
||||
version = "0.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1c3aec007c63c4ed4cd7a018529fb0b5575c4562575fc6a40d6cd2ae0b792ef"
|
||||
checksum = "cd3904a4ba0a9d0211816177fd34b04c7095443f8cdacd11175064fe541c8fe2"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
|
|
|
@ -6,3 +6,4 @@ status = [
|
|||
"TypeScript (windows-latest)",
|
||||
]
|
||||
delete_merged_branches = true
|
||||
timeout_sec = 1200 # 20 min
|
||||
|
|
|
@ -1,21 +1,9 @@
|
|||
use std::collections::BTreeSet;
|
||||
|
||||
use either::Either;
|
||||
use hir::{
|
||||
AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait,
|
||||
Type,
|
||||
};
|
||||
use ide_db::{imports_locator, RootDatabase};
|
||||
use insert_use::ImportScope;
|
||||
use rustc_hash::FxHashSet;
|
||||
use syntax::{
|
||||
ast::{self, AstNode},
|
||||
SyntaxNode,
|
||||
};
|
||||
use syntax::ast;
|
||||
|
||||
use crate::{
|
||||
utils::insert_use, utils::mod_path_to_ast, AssistContext, AssistId, AssistKind, Assists,
|
||||
GroupLabel,
|
||||
utils::import_assets::{ImportAssets, ImportCandidate},
|
||||
utils::{insert_use, mod_path_to_ast, ImportScope},
|
||||
AssistContext, AssistId, AssistKind, Assists, GroupLabel,
|
||||
};
|
||||
|
||||
// Assist: auto_import
|
||||
|
@ -38,16 +26,24 @@ use crate::{
|
|||
// # pub mod std { pub mod collections { pub struct HashMap { } } }
|
||||
// ```
|
||||
pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||
let auto_import_assets = AutoImportAssets::new(ctx)?;
|
||||
let proposed_imports = auto_import_assets.search_for_imports(ctx);
|
||||
let import_assets =
|
||||
if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() {
|
||||
ImportAssets::for_regular_path(path_under_caret, &ctx.sema)
|
||||
} else if let Some(method_under_caret) =
|
||||
ctx.find_node_at_offset_with_descend::<ast::MethodCallExpr>()
|
||||
{
|
||||
ImportAssets::for_method_call(method_under_caret, &ctx.sema)
|
||||
} else {
|
||||
None
|
||||
}?;
|
||||
let proposed_imports = import_assets.search_for_imports(&ctx.sema, &ctx.config.insert_use);
|
||||
if proposed_imports.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let range = ctx.sema.original_range(&auto_import_assets.syntax_under_caret).range;
|
||||
let group = auto_import_assets.get_import_group_message();
|
||||
let scope =
|
||||
ImportScope::find_insert_use_container(&auto_import_assets.syntax_under_caret, ctx)?;
|
||||
let range = ctx.sema.original_range(import_assets.syntax_under_caret()).range;
|
||||
let group = import_group_message(import_assets.import_candidate());
|
||||
let scope = ImportScope::find_insert_use_container(import_assets.syntax_under_caret(), ctx)?;
|
||||
let syntax = scope.as_syntax_node();
|
||||
for import in proposed_imports {
|
||||
acc.add_group(
|
||||
|
@ -65,229 +61,20 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
|||
Some(())
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct AutoImportAssets {
|
||||
import_candidate: ImportCandidate,
|
||||
module_with_name_to_import: Module,
|
||||
syntax_under_caret: SyntaxNode,
|
||||
fn import_group_message(import_candidate: &ImportCandidate) -> GroupLabel {
|
||||
let name = match import_candidate {
|
||||
ImportCandidate::UnqualifiedName(candidate)
|
||||
| ImportCandidate::QualifierStart(candidate) => format!("Import {}", &candidate.name),
|
||||
ImportCandidate::TraitAssocItem(candidate) => {
|
||||
format!("Import a trait for item {}", &candidate.name)
|
||||
}
|
||||
|
||||
impl AutoImportAssets {
|
||||
fn new(ctx: &AssistContext) -> Option<Self> {
|
||||
if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() {
|
||||
Self::for_regular_path(path_under_caret, &ctx)
|
||||
} else {
|
||||
Self::for_method_call(ctx.find_node_at_offset_with_descend()?, &ctx)
|
||||
}
|
||||
}
|
||||
|
||||
fn for_method_call(method_call: ast::MethodCallExpr, ctx: &AssistContext) -> Option<Self> {
|
||||
let syntax_under_caret = method_call.syntax().to_owned();
|
||||
let module_with_name_to_import = ctx.sema.scope(&syntax_under_caret).module()?;
|
||||
Some(Self {
|
||||
import_candidate: ImportCandidate::for_method_call(&ctx.sema, &method_call)?,
|
||||
module_with_name_to_import,
|
||||
syntax_under_caret,
|
||||
})
|
||||
}
|
||||
|
||||
fn for_regular_path(path_under_caret: ast::Path, ctx: &AssistContext) -> Option<Self> {
|
||||
let syntax_under_caret = path_under_caret.syntax().to_owned();
|
||||
if syntax_under_caret.ancestors().find_map(ast::Use::cast).is_some() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let module_with_name_to_import = ctx.sema.scope(&syntax_under_caret).module()?;
|
||||
Some(Self {
|
||||
import_candidate: ImportCandidate::for_regular_path(&ctx.sema, &path_under_caret)?,
|
||||
module_with_name_to_import,
|
||||
syntax_under_caret,
|
||||
})
|
||||
}
|
||||
|
||||
fn get_search_query(&self) -> &str {
|
||||
match &self.import_candidate {
|
||||
ImportCandidate::UnqualifiedName(name) => name,
|
||||
ImportCandidate::QualifierStart(qualifier_start) => qualifier_start,
|
||||
ImportCandidate::TraitAssocItem(_, trait_assoc_item_name) => trait_assoc_item_name,
|
||||
ImportCandidate::TraitMethod(_, trait_method_name) => trait_method_name,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_import_group_message(&self) -> GroupLabel {
|
||||
let name = match &self.import_candidate {
|
||||
ImportCandidate::UnqualifiedName(name) => format!("Import {}", name),
|
||||
ImportCandidate::QualifierStart(qualifier_start) => {
|
||||
format!("Import {}", qualifier_start)
|
||||
}
|
||||
ImportCandidate::TraitAssocItem(_, trait_assoc_item_name) => {
|
||||
format!("Import a trait for item {}", trait_assoc_item_name)
|
||||
}
|
||||
ImportCandidate::TraitMethod(_, trait_method_name) => {
|
||||
format!("Import a trait for method {}", trait_method_name)
|
||||
ImportCandidate::TraitMethod(candidate) => {
|
||||
format!("Import a trait for method {}", &candidate.name)
|
||||
}
|
||||
};
|
||||
GroupLabel(name)
|
||||
}
|
||||
|
||||
fn search_for_imports(&self, ctx: &AssistContext) -> BTreeSet<ModPath> {
|
||||
let _p = profile::span("auto_import::search_for_imports");
|
||||
let db = ctx.db();
|
||||
let current_crate = self.module_with_name_to_import.krate();
|
||||
imports_locator::find_imports(&ctx.sema, current_crate, &self.get_search_query())
|
||||
.into_iter()
|
||||
.filter_map(|candidate| match &self.import_candidate {
|
||||
ImportCandidate::TraitAssocItem(assoc_item_type, _) => {
|
||||
let located_assoc_item = match candidate {
|
||||
Either::Left(ModuleDef::Function(located_function)) => located_function
|
||||
.as_assoc_item(db)
|
||||
.map(|assoc| assoc.container(db))
|
||||
.and_then(Self::assoc_to_trait),
|
||||
Either::Left(ModuleDef::Const(located_const)) => located_const
|
||||
.as_assoc_item(db)
|
||||
.map(|assoc| assoc.container(db))
|
||||
.and_then(Self::assoc_to_trait),
|
||||
_ => None,
|
||||
}?;
|
||||
|
||||
let mut trait_candidates = FxHashSet::default();
|
||||
trait_candidates.insert(located_assoc_item.into());
|
||||
|
||||
assoc_item_type
|
||||
.iterate_path_candidates(
|
||||
db,
|
||||
current_crate,
|
||||
&trait_candidates,
|
||||
None,
|
||||
|_, assoc| Self::assoc_to_trait(assoc.container(db)),
|
||||
)
|
||||
.map(ModuleDef::from)
|
||||
.map(Either::Left)
|
||||
}
|
||||
ImportCandidate::TraitMethod(function_callee, _) => {
|
||||
let located_assoc_item =
|
||||
if let Either::Left(ModuleDef::Function(located_function)) = candidate {
|
||||
located_function
|
||||
.as_assoc_item(db)
|
||||
.map(|assoc| assoc.container(db))
|
||||
.and_then(Self::assoc_to_trait)
|
||||
} else {
|
||||
None
|
||||
}?;
|
||||
|
||||
let mut trait_candidates = FxHashSet::default();
|
||||
trait_candidates.insert(located_assoc_item.into());
|
||||
|
||||
function_callee
|
||||
.iterate_method_candidates(
|
||||
db,
|
||||
current_crate,
|
||||
&trait_candidates,
|
||||
None,
|
||||
|_, function| {
|
||||
Self::assoc_to_trait(function.as_assoc_item(db)?.container(db))
|
||||
},
|
||||
)
|
||||
.map(ModuleDef::from)
|
||||
.map(Either::Left)
|
||||
}
|
||||
_ => Some(candidate),
|
||||
})
|
||||
.filter_map(|candidate| match candidate {
|
||||
Either::Left(module_def) => self.module_with_name_to_import.find_use_path_prefixed(
|
||||
db,
|
||||
module_def,
|
||||
ctx.config.insert_use.prefix_kind,
|
||||
),
|
||||
Either::Right(macro_def) => self.module_with_name_to_import.find_use_path_prefixed(
|
||||
db,
|
||||
macro_def,
|
||||
ctx.config.insert_use.prefix_kind,
|
||||
),
|
||||
})
|
||||
.filter(|use_path| !use_path.segments.is_empty())
|
||||
.take(20)
|
||||
.collect::<BTreeSet<_>>()
|
||||
}
|
||||
|
||||
fn assoc_to_trait(assoc: AssocItemContainer) -> Option<Trait> {
|
||||
if let AssocItemContainer::Trait(extracted_trait) = assoc {
|
||||
Some(extracted_trait)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ImportCandidate {
|
||||
/// Simple name like 'HashMap'
|
||||
UnqualifiedName(String),
|
||||
/// First part of the qualified name.
|
||||
/// For 'std::collections::HashMap', that will be 'std'.
|
||||
QualifierStart(String),
|
||||
/// A trait associated function (with no self parameter) or associated constant.
|
||||
/// For 'test_mod::TestEnum::test_function', `Type` is the `test_mod::TestEnum` expression type
|
||||
/// and `String` is the `test_function`
|
||||
TraitAssocItem(Type, String),
|
||||
/// A trait method with self parameter.
|
||||
/// For 'test_enum.test_method()', `Type` is the `test_enum` expression type
|
||||
/// and `String` is the `test_method`
|
||||
TraitMethod(Type, String),
|
||||
}
|
||||
|
||||
impl ImportCandidate {
|
||||
fn for_method_call(
|
||||
sema: &Semantics<RootDatabase>,
|
||||
method_call: &ast::MethodCallExpr,
|
||||
) -> Option<Self> {
|
||||
if sema.resolve_method_call(method_call).is_some() {
|
||||
return None;
|
||||
}
|
||||
Some(Self::TraitMethod(
|
||||
sema.type_of_expr(&method_call.receiver()?)?,
|
||||
method_call.name_ref()?.syntax().to_string(),
|
||||
))
|
||||
}
|
||||
|
||||
fn for_regular_path(
|
||||
sema: &Semantics<RootDatabase>,
|
||||
path_under_caret: &ast::Path,
|
||||
) -> Option<Self> {
|
||||
if sema.resolve_path(path_under_caret).is_some() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let segment = path_under_caret.segment()?;
|
||||
if let Some(qualifier) = path_under_caret.qualifier() {
|
||||
let qualifier_start = qualifier.syntax().descendants().find_map(ast::NameRef::cast)?;
|
||||
let qualifier_start_path =
|
||||
qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?;
|
||||
if let Some(qualifier_start_resolution) = sema.resolve_path(&qualifier_start_path) {
|
||||
let qualifier_resolution = if qualifier_start_path == qualifier {
|
||||
qualifier_start_resolution
|
||||
} else {
|
||||
sema.resolve_path(&qualifier)?
|
||||
};
|
||||
if let PathResolution::Def(ModuleDef::Adt(assoc_item_path)) = qualifier_resolution {
|
||||
Some(ImportCandidate::TraitAssocItem(
|
||||
assoc_item_path.ty(sema.db),
|
||||
segment.syntax().to_string(),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
Some(ImportCandidate::QualifierStart(qualifier_start.syntax().to_string()))
|
||||
}
|
||||
} else {
|
||||
Some(ImportCandidate::UnqualifiedName(
|
||||
segment.syntax().descendants().find_map(ast::NameRef::cast)?.syntax().to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
//! Assorted functions shared by several assists.
|
||||
pub(crate) mod insert_use;
|
||||
pub(crate) mod import_assets;
|
||||
|
||||
use std::{iter, ops};
|
||||
|
||||
|
|
268
crates/assists/src/utils/import_assets.rs
Normal file
268
crates/assists/src/utils/import_assets.rs
Normal file
|
@ -0,0 +1,268 @@
|
|||
//! Look up accessible paths for items.
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use either::Either;
|
||||
use hir::{AsAssocItem, AssocItemContainer, ModuleDef, Semantics};
|
||||
use ide_db::{imports_locator, RootDatabase};
|
||||
use rustc_hash::FxHashSet;
|
||||
use syntax::{ast, AstNode, SyntaxNode};
|
||||
|
||||
use crate::assist_config::InsertUseConfig;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum ImportCandidate {
|
||||
/// Simple name like 'HashMap'
|
||||
UnqualifiedName(PathImportCandidate),
|
||||
/// First part of the qualified name.
|
||||
/// For 'std::collections::HashMap', that will be 'std'.
|
||||
QualifierStart(PathImportCandidate),
|
||||
/// A trait associated function (with no self parameter) or associated constant.
|
||||
/// For 'test_mod::TestEnum::test_function', `ty` is the `test_mod::TestEnum` expression type
|
||||
/// and `name` is the `test_function`
|
||||
TraitAssocItem(TraitImportCandidate),
|
||||
/// A trait method with self parameter.
|
||||
/// For 'test_enum.test_method()', `ty` is the `test_enum` expression type
|
||||
/// and `name` is the `test_method`
|
||||
TraitMethod(TraitImportCandidate),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct TraitImportCandidate {
|
||||
pub ty: hir::Type,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct PathImportCandidate {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct ImportAssets {
|
||||
import_candidate: ImportCandidate,
|
||||
module_with_name_to_import: hir::Module,
|
||||
syntax_under_caret: SyntaxNode,
|
||||
}
|
||||
|
||||
impl ImportAssets {
|
||||
pub(crate) fn for_method_call(
|
||||
method_call: ast::MethodCallExpr,
|
||||
sema: &Semantics<RootDatabase>,
|
||||
) -> Option<Self> {
|
||||
let syntax_under_caret = method_call.syntax().to_owned();
|
||||
let module_with_name_to_import = sema.scope(&syntax_under_caret).module()?;
|
||||
Some(Self {
|
||||
import_candidate: ImportCandidate::for_method_call(sema, &method_call)?,
|
||||
module_with_name_to_import,
|
||||
syntax_under_caret,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn for_regular_path(
|
||||
path_under_caret: ast::Path,
|
||||
sema: &Semantics<RootDatabase>,
|
||||
) -> Option<Self> {
|
||||
let syntax_under_caret = path_under_caret.syntax().to_owned();
|
||||
if syntax_under_caret.ancestors().find_map(ast::Use::cast).is_some() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let module_with_name_to_import = sema.scope(&syntax_under_caret).module()?;
|
||||
Some(Self {
|
||||
import_candidate: ImportCandidate::for_regular_path(sema, &path_under_caret)?,
|
||||
module_with_name_to_import,
|
||||
syntax_under_caret,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn syntax_under_caret(&self) -> &SyntaxNode {
|
||||
&self.syntax_under_caret
|
||||
}
|
||||
|
||||
pub(crate) fn import_candidate(&self) -> &ImportCandidate {
|
||||
&self.import_candidate
|
||||
}
|
||||
|
||||
fn get_search_query(&self) -> &str {
|
||||
match &self.import_candidate {
|
||||
ImportCandidate::UnqualifiedName(candidate)
|
||||
| ImportCandidate::QualifierStart(candidate) => &candidate.name,
|
||||
ImportCandidate::TraitAssocItem(candidate)
|
||||
| ImportCandidate::TraitMethod(candidate) => &candidate.name,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn search_for_imports(
|
||||
&self,
|
||||
sema: &Semantics<RootDatabase>,
|
||||
config: &InsertUseConfig,
|
||||
) -> BTreeSet<hir::ModPath> {
|
||||
let _p = profile::span("import_assists::search_for_imports");
|
||||
self.search_for(sema, Some(config.prefix_kind))
|
||||
}
|
||||
|
||||
/// This may return non-absolute paths if a part of the returned path is already imported into scope.
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn search_for_relative_paths(
|
||||
&self,
|
||||
sema: &Semantics<RootDatabase>,
|
||||
) -> BTreeSet<hir::ModPath> {
|
||||
let _p = profile::span("import_assists::search_for_relative_paths");
|
||||
self.search_for(sema, None)
|
||||
}
|
||||
|
||||
fn search_for(
|
||||
&self,
|
||||
sema: &Semantics<RootDatabase>,
|
||||
prefixed: Option<hir::PrefixKind>,
|
||||
) -> BTreeSet<hir::ModPath> {
|
||||
let db = sema.db;
|
||||
let mut trait_candidates = FxHashSet::default();
|
||||
let current_crate = self.module_with_name_to_import.krate();
|
||||
|
||||
let filter = |candidate: Either<hir::ModuleDef, hir::MacroDef>| {
|
||||
trait_candidates.clear();
|
||||
match &self.import_candidate {
|
||||
ImportCandidate::TraitAssocItem(trait_candidate) => {
|
||||
let located_assoc_item = match candidate {
|
||||
Either::Left(ModuleDef::Function(located_function)) => {
|
||||
located_function.as_assoc_item(db)
|
||||
}
|
||||
Either::Left(ModuleDef::Const(located_const)) => {
|
||||
located_const.as_assoc_item(db)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
.map(|assoc| assoc.container(db))
|
||||
.and_then(Self::assoc_to_trait)?;
|
||||
|
||||
trait_candidates.insert(located_assoc_item.into());
|
||||
|
||||
trait_candidate
|
||||
.ty
|
||||
.iterate_path_candidates(
|
||||
db,
|
||||
current_crate,
|
||||
&trait_candidates,
|
||||
None,
|
||||
|_, assoc| Self::assoc_to_trait(assoc.container(db)),
|
||||
)
|
||||
.map(ModuleDef::from)
|
||||
.map(Either::Left)
|
||||
}
|
||||
ImportCandidate::TraitMethod(trait_candidate) => {
|
||||
let located_assoc_item =
|
||||
if let Either::Left(ModuleDef::Function(located_function)) = candidate {
|
||||
located_function
|
||||
.as_assoc_item(db)
|
||||
.map(|assoc| assoc.container(db))
|
||||
.and_then(Self::assoc_to_trait)
|
||||
} else {
|
||||
None
|
||||
}?;
|
||||
|
||||
trait_candidates.insert(located_assoc_item.into());
|
||||
|
||||
trait_candidate
|
||||
.ty
|
||||
.iterate_method_candidates(
|
||||
db,
|
||||
current_crate,
|
||||
&trait_candidates,
|
||||
None,
|
||||
|_, function| {
|
||||
Self::assoc_to_trait(function.as_assoc_item(db)?.container(db))
|
||||
},
|
||||
)
|
||||
.map(ModuleDef::from)
|
||||
.map(Either::Left)
|
||||
}
|
||||
_ => Some(candidate),
|
||||
}
|
||||
};
|
||||
|
||||
imports_locator::find_imports(sema, current_crate, &self.get_search_query())
|
||||
.into_iter()
|
||||
.filter_map(filter)
|
||||
.filter_map(|candidate| {
|
||||
let item: hir::ItemInNs = candidate.either(Into::into, Into::into);
|
||||
if let Some(prefix_kind) = prefixed {
|
||||
self.module_with_name_to_import.find_use_path_prefixed(db, item, prefix_kind)
|
||||
} else {
|
||||
self.module_with_name_to_import.find_use_path(db, item)
|
||||
}
|
||||
})
|
||||
.filter(|use_path| !use_path.segments.is_empty())
|
||||
.take(20)
|
||||
.collect::<BTreeSet<_>>()
|
||||
}
|
||||
|
||||
fn assoc_to_trait(assoc: AssocItemContainer) -> Option<hir::Trait> {
|
||||
if let AssocItemContainer::Trait(extracted_trait) = assoc {
|
||||
Some(extracted_trait)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ImportCandidate {
|
||||
fn for_method_call(
|
||||
sema: &Semantics<RootDatabase>,
|
||||
method_call: &ast::MethodCallExpr,
|
||||
) -> Option<Self> {
|
||||
match sema.resolve_method_call(method_call) {
|
||||
Some(_) => None,
|
||||
None => Some(Self::TraitMethod(TraitImportCandidate {
|
||||
ty: sema.type_of_expr(&method_call.receiver()?)?,
|
||||
name: method_call.name_ref()?.syntax().to_string(),
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
fn for_regular_path(
|
||||
sema: &Semantics<RootDatabase>,
|
||||
path_under_caret: &ast::Path,
|
||||
) -> Option<Self> {
|
||||
if sema.resolve_path(path_under_caret).is_some() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let segment = path_under_caret.segment()?;
|
||||
let candidate = if let Some(qualifier) = path_under_caret.qualifier() {
|
||||
let qualifier_start = qualifier.syntax().descendants().find_map(ast::NameRef::cast)?;
|
||||
let qualifier_start_path =
|
||||
qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?;
|
||||
if let Some(qualifier_start_resolution) = sema.resolve_path(&qualifier_start_path) {
|
||||
let qualifier_resolution = if qualifier_start_path == qualifier {
|
||||
qualifier_start_resolution
|
||||
} else {
|
||||
sema.resolve_path(&qualifier)?
|
||||
};
|
||||
match qualifier_resolution {
|
||||
hir::PathResolution::Def(hir::ModuleDef::Adt(assoc_item_path)) => {
|
||||
ImportCandidate::TraitAssocItem(TraitImportCandidate {
|
||||
ty: assoc_item_path.ty(sema.db),
|
||||
name: segment.syntax().to_string(),
|
||||
})
|
||||
}
|
||||
_ => return None,
|
||||
}
|
||||
} else {
|
||||
ImportCandidate::QualifierStart(PathImportCandidate {
|
||||
name: qualifier_start.syntax().to_string(),
|
||||
})
|
||||
}
|
||||
} else {
|
||||
ImportCandidate::UnqualifiedName(PathImportCandidate {
|
||||
name: segment
|
||||
.syntax()
|
||||
.descendants()
|
||||
.find_map(ast::NameRef::cast)?
|
||||
.syntax()
|
||||
.to_string(),
|
||||
})
|
||||
};
|
||||
Some(candidate)
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@ edition = "2018"
|
|||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
salsa = "0.15.2"
|
||||
salsa = "0.16.0"
|
||||
rustc-hash = "1.1.0"
|
||||
|
||||
syntax = { path = "../syntax", version = "0.0.0" }
|
||||
|
|
|
@ -1,3 +1,13 @@
|
|||
<!---
|
||||
lsp_ext.rs hash: 286f8bbac885531a
|
||||
|
||||
If you need to change the above hash to make the test pass, please check if you
|
||||
need to adjust this doc as well and ping this issue:
|
||||
|
||||
https://github.com/rust-analyzer/rust-analyzer/issues/4604
|
||||
|
||||
--->
|
||||
|
||||
# LSP Extensions
|
||||
|
||||
This document describes LSP extensions used by rust-analyzer.
|
||||
|
|
|
@ -44,6 +44,41 @@ fn smoke_test_docs_generation() {
|
|||
codegen::generate_feature_docs(Mode::Overwrite).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_lsp_extensions_docs() {
|
||||
let expected_hash = {
|
||||
let lsp_ext_rs =
|
||||
fs2::read_to_string(project_root().join("crates/rust-analyzer/src/lsp_ext.rs"))
|
||||
.unwrap();
|
||||
stable_hash(lsp_ext_rs.as_str())
|
||||
};
|
||||
|
||||
let actual_hash = {
|
||||
let lsp_extensions_md =
|
||||
fs2::read_to_string(project_root().join("docs/dev/lsp-extensions.md")).unwrap();
|
||||
let text = lsp_extensions_md
|
||||
.lines()
|
||||
.find_map(|line| line.strip_prefix("lsp_ext.rs hash:"))
|
||||
.unwrap()
|
||||
.trim();
|
||||
u64::from_str_radix(text, 16).unwrap()
|
||||
};
|
||||
|
||||
if actual_hash != expected_hash {
|
||||
panic!(
|
||||
"
|
||||
lsp_ext.rs was changed without touching lsp-extensions.md.
|
||||
|
||||
Expected hash: {:x}
|
||||
Actual hash: {:x}
|
||||
|
||||
Please adjust docs/dev/lsp-extensions.md.
|
||||
",
|
||||
expected_hash, actual_hash
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rust_files_are_tidy() {
|
||||
let mut tidy_docs = TidyDocs::default();
|
||||
|
@ -280,3 +315,13 @@ fn is_exclude_dir(p: &Path, dirs_to_exclude: &[&str]) -> bool {
|
|||
.filter_map(|it| it.as_os_str().to_str())
|
||||
.any(|it| dirs_to_exclude.contains(&it))
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
fn stable_hash(text: &str) -> u64 {
|
||||
use std::hash::{Hash, Hasher, SipHasher};
|
||||
|
||||
let text = text.replace('\r', "");
|
||||
let mut hasher = SipHasher::default();
|
||||
text.hash(&mut hasher);
|
||||
hasher.finish()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue