GotoDefinition on a Range or InclusiveRange operator will link to the struct definition

This commit is contained in:
duncanproctor 2024-10-21 11:29:05 -04:00
parent b0b5d38768
commit 3bc6e27993
2 changed files with 68 additions and 4 deletions

View file

@ -1,7 +1,7 @@
//! See [`FamousDefs`].
use base_db::{CrateOrigin, LangCrateOrigin, SourceDatabase};
use hir::{Crate, Enum, Function, Macro, Module, ScopeDef, Semantics, Trait};
use hir::{Crate, Enum, Function, Macro, Module, ScopeDef, Semantics, Struct, Trait};
use crate::RootDatabase;
@ -102,6 +102,14 @@ impl FamousDefs<'_, '_> {
self.find_trait("core:ops:Drop")
}
pub fn core_ops_Range(&self) -> Option<Struct> {
self.find_struct("core:ops:Range")
}
pub fn core_ops_RangeInclusive(&self) -> Option<Struct> {
self.find_struct("core:ops:RangeInclusive")
}
pub fn core_marker_Copy(&self) -> Option<Trait> {
self.find_trait("core:marker:Copy")
}
@ -137,6 +145,13 @@ impl FamousDefs<'_, '_> {
.flatten()
}
fn find_struct(&self, path: &str) -> Option<Struct> {
match self.find_def(path)? {
hir::ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Struct(it))) => Some(it),
_ => None,
}
}
fn find_trait(&self, path: &str) -> Option<Trait> {
match self.find_def(path)? {
hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),