2022-03-06 12:01:30 -06:00
|
|
|
//! Random assortment of ide helpers for high-level ide features that don't fit in any other module.
|
2021-01-06 11:43:46 -06:00
|
|
|
|
2022-02-21 05:57:57 -06:00
|
|
|
use std::collections::VecDeque;
|
2021-03-15 06:18:52 -05:00
|
|
|
|
2023-03-02 08:51:12 -06:00
|
|
|
use base_db::{FileId, SourceDatabaseExt};
|
|
|
|
use hir::{Crate, ItemInNs, ModuleDef, Name, Semantics};
|
2021-06-22 10:28:07 -05:00
|
|
|
use syntax::{
|
2022-03-06 12:01:30 -06:00
|
|
|
ast::{self, make},
|
|
|
|
AstToken, SyntaxKind, SyntaxToken, TokenAtOffset,
|
2021-06-22 10:28:07 -05:00
|
|
|
};
|
2020-11-24 15:25:13 -06:00
|
|
|
|
2023-07-07 22:03:55 -05:00
|
|
|
use crate::{
|
|
|
|
defs::{Definition, IdentClass},
|
|
|
|
generated, RootDatabase,
|
|
|
|
};
|
2021-09-26 09:29:42 -05:00
|
|
|
|
2021-02-24 17:53:59 -06:00
|
|
|
pub fn item_name(db: &RootDatabase, item: ItemInNs) -> Option<Name> {
|
|
|
|
match item {
|
2022-03-12 06:43:53 -06:00
|
|
|
ItemInNs::Types(module_def_id) => module_def_id.name(db),
|
|
|
|
ItemInNs::Values(module_def_id) => module_def_id.name(db),
|
|
|
|
ItemInNs::Macros(macro_def_id) => Some(macro_def_id.name(db)),
|
2021-02-24 17:53:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 10:28:07 -05:00
|
|
|
/// Picks the token with the highest rank returned by the passed in function.
|
|
|
|
pub fn pick_best_token(
|
|
|
|
tokens: TokenAtOffset<SyntaxToken>,
|
|
|
|
f: impl Fn(SyntaxKind) -> usize,
|
|
|
|
) -> Option<SyntaxToken> {
|
|
|
|
tokens.max_by_key(move |t| f(t.kind()))
|
|
|
|
}
|
2022-02-11 15:06:03 -06:00
|
|
|
pub fn pick_token<T: AstToken>(mut tokens: TokenAtOffset<SyntaxToken>) -> Option<T> {
|
|
|
|
tokens.find_map(T::cast)
|
|
|
|
}
|
2021-06-22 10:28:07 -05:00
|
|
|
|
2020-11-27 04:26:20 -06:00
|
|
|
/// Converts the mod path struct into its ast representation.
|
2020-11-24 15:25:13 -06:00
|
|
|
pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
|
|
|
|
let _p = profile::span("mod_path_to_ast");
|
2020-11-27 04:26:20 -06:00
|
|
|
|
2020-11-24 15:25:13 -06:00
|
|
|
let mut segments = Vec::new();
|
|
|
|
let mut is_abs = false;
|
|
|
|
match path.kind {
|
|
|
|
hir::PathKind::Plain => {}
|
|
|
|
hir::PathKind::Super(0) => segments.push(make::path_segment_self()),
|
|
|
|
hir::PathKind::Super(n) => segments.extend((0..n).map(|_| make::path_segment_super())),
|
|
|
|
hir::PathKind::DollarCrate(_) | hir::PathKind::Crate => {
|
|
|
|
segments.push(make::path_segment_crate())
|
|
|
|
}
|
|
|
|
hir::PathKind::Abs => is_abs = true,
|
|
|
|
}
|
|
|
|
|
|
|
|
segments.extend(
|
2021-02-04 13:49:24 -06:00
|
|
|
path.segments()
|
2020-11-24 15:25:13 -06:00
|
|
|
.iter()
|
2021-11-04 12:12:05 -05:00
|
|
|
.map(|segment| make::path_segment(make::name_ref(&segment.to_smol_str()))),
|
2020-11-24 15:25:13 -06:00
|
|
|
);
|
|
|
|
make::path_from_segments(segments, is_abs)
|
|
|
|
}
|
|
|
|
|
2021-03-15 06:18:52 -05:00
|
|
|
/// Iterates all `ModuleDef`s and `Impl` blocks of the given file.
|
|
|
|
pub fn visit_file_defs(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2021-03-15 06:18:52 -05:00
|
|
|
file_id: FileId,
|
2021-11-11 11:20:54 -06:00
|
|
|
cb: &mut dyn FnMut(Definition),
|
2021-03-15 06:18:52 -05:00
|
|
|
) {
|
|
|
|
let db = sema.db;
|
|
|
|
let module = match sema.to_module_def(file_id) {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
let mut defs: VecDeque<_> = module.declarations(db).into();
|
|
|
|
while let Some(def) = defs.pop_front() {
|
|
|
|
if let ModuleDef::Module(submodule) = def {
|
|
|
|
if let hir::ModuleSource::Module(_) = submodule.definition_source(db).value {
|
|
|
|
defs.extend(submodule.declarations(db));
|
2021-11-11 11:20:54 -06:00
|
|
|
submodule.impl_defs(db).into_iter().for_each(|impl_| cb(impl_.into()));
|
2021-03-15 06:18:52 -05:00
|
|
|
}
|
|
|
|
}
|
2021-11-11 11:20:54 -06:00
|
|
|
cb(def.into());
|
2021-03-15 06:18:52 -05:00
|
|
|
}
|
2021-11-11 11:20:54 -06:00
|
|
|
module.impl_defs(db).into_iter().for_each(|impl_| cb(impl_.into()));
|
2022-03-18 05:55:53 -05:00
|
|
|
|
2023-06-01 07:46:36 -05:00
|
|
|
let is_root = module.is_crate_root();
|
2022-03-18 05:55:53 -05:00
|
|
|
module
|
|
|
|
.legacy_macros(db)
|
|
|
|
.into_iter()
|
|
|
|
// don't show legacy macros declared in the crate-root that were already covered in declarations earlier
|
|
|
|
.filter(|it| !(is_root && it.is_macro_export(db)))
|
|
|
|
.for_each(|mac| cb(mac.into()));
|
2021-03-15 06:18:52 -05:00
|
|
|
}
|
|
|
|
|
2021-11-22 07:14:21 -06:00
|
|
|
/// Checks if the given lint is equal or is contained by the other lint which may or may not be a group.
|
|
|
|
pub fn lint_eq_or_in_group(lint: &str, lint_is: &str) -> bool {
|
|
|
|
if lint == lint_is {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-06 12:01:30 -06:00
|
|
|
if let Some(group) = generated::lints::DEFAULT_LINT_GROUPS
|
2021-11-22 07:14:21 -06:00
|
|
|
.iter()
|
2022-03-06 12:01:30 -06:00
|
|
|
.chain(generated::lints::CLIPPY_LINT_GROUPS.iter())
|
|
|
|
.chain(generated::lints::RUSTDOC_LINT_GROUPS.iter())
|
2021-11-22 07:14:21 -06:00
|
|
|
.find(|&check| check.lint.label == lint_is)
|
|
|
|
{
|
|
|
|
group.children.contains(&lint)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2023-03-02 08:51:12 -06:00
|
|
|
|
|
|
|
pub fn is_editable_crate(krate: Crate, db: &RootDatabase) -> bool {
|
|
|
|
let root_file = krate.root_file(db);
|
|
|
|
let source_root_id = db.file_source_root(root_file);
|
|
|
|
!db.source_root(source_root_id).is_library
|
|
|
|
}
|
2023-07-07 22:03:55 -05:00
|
|
|
|
|
|
|
pub fn get_definition(
|
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
|
|
|
token: SyntaxToken,
|
|
|
|
) -> Option<Definition> {
|
2023-08-16 03:07:18 -05:00
|
|
|
for token in sema.descend_into_macros(token, 0.into()) {
|
2023-07-07 22:03:55 -05:00
|
|
|
let def = IdentClass::classify_token(sema, &token).map(IdentClass::definitions_no_ops);
|
|
|
|
if let Some(&[x]) = def.as_deref() {
|
|
|
|
return Some(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|