Go-to-definition basic support

This commit is contained in:
Ayaz Hafiz 2023-10-22 12:45:11 -04:00
parent e954e074fb
commit 886a367026
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
10 changed files with 465 additions and 313 deletions

View file

@ -727,6 +727,17 @@ pub fn find_closest_type_at(
visitor.region_typ
}
/// Given an ability Foo has foo : ..., returns (T, foo1) if the symbol at the given region is a
/// symbol foo1 that specializes foo for T. Otherwise if the symbol is foo but the specialization
/// is unknown, (Foo, foo) is returned. Otherwise [None] is returned.
pub fn find_closest_symbol_at(
position: Position,
decls: &Declarations,
abilities_store: &AbilitiesStore,
) -> Option<FoundSymbol> {
find_symbol_at_impl(Region::from_pos(position), decls, abilities_store, true)
}
/// Given an ability Foo has foo : ..., returns (T, foo1) if the symbol at the given region is a
/// symbol foo1 that specializes foo for T. Otherwise if the symbol is foo but the specialization
/// is unknown, (Foo, foo) is returned. Otherwise [None] is returned.
@ -734,11 +745,21 @@ pub fn find_symbol_at(
region: Region,
decls: &Declarations,
abilities_store: &AbilitiesStore,
) -> Option<FoundSymbol> {
find_symbol_at_impl(region, decls, abilities_store, false)
}
pub fn find_symbol_at_impl(
region: Region,
decls: &Declarations,
abilities_store: &AbilitiesStore,
allow_subregion: bool,
) -> Option<FoundSymbol> {
let mut visitor = Finder {
region,
found: None,
abilities_store,
allow_subregion,
};
visitor.visit_decls(decls);
return visitor.found;
@ -747,6 +768,17 @@ pub fn find_symbol_at(
region: Region,
abilities_store: &'a AbilitiesStore,
found: Option<FoundSymbol>,
allow_subregion: bool,
}
impl<'a> Finder<'a> {
fn is_at_wanted_region(&self, region: Region) -> bool {
if self.allow_subregion {
region.contains(&self.region)
} else {
region == self.region
}
}
}
impl Visitor for Finder<'_> {
@ -755,7 +787,7 @@ pub fn find_symbol_at(
}
fn visit_pattern(&mut self, pattern: &Pattern, region: Region, _opt_var: Option<Variable>) {
if region == self.region {
if self.is_at_wanted_region(region) {
match pattern {
Pattern::AbilityMemberSpecialization {
ident: spec_symbol,
@ -776,7 +808,7 @@ pub fn find_symbol_at(
}
fn visit_expr(&mut self, expr: &Expr, region: Region, var: Variable) {
if region == self.region {
if self.is_at_wanted_region(region) {
match expr {
&Expr::AbilityMember(member_symbol, specialization_id, _var) => {
debug_assert!(self.found.is_none());

View file

@ -22,7 +22,7 @@ pub use roc_load_internal::file::{
Threading,
};
pub use roc_load_internal::module::{
EntryPoint, Expectations, ExposedToHost, LoadedModule, MonomorphizedModule,
CheckedModule, EntryPoint, Expectations, ExposedToHost, LoadedModule, MonomorphizedModule,
};
pub use roc_solve::FunctionKind;

View file

@ -1116,32 +1116,7 @@ impl<'a> LoadStart<'a> {
);
match res_loaded {
Ok(header_output) => {
if let Msg::Header(ModuleHeader {
module_id: header_id,
header_type,
is_root_module,
..
}) = &header_output.msg
{
debug_assert_eq!(*header_id, header_output.module_id);
debug_assert!(is_root_module);
if let HeaderType::Interface { name, .. } = header_type {
// Interface modules can have names like Foo.Bar.Baz,
// in which case we need to adjust the src_dir to
// remove the "Bar/Baz" directories in order to correctly
// resolve this interface module's imports!
let dirs_to_pop = name.as_str().matches('.').count();
for _ in 0..dirs_to_pop {
src_dir.pop();
}
}
}
header_output
}
Ok(header_output) => adjust_header_paths(header_output, &mut src_dir),
Err(problem) => {
let module_ids = Arc::try_unwrap(arc_modules)
@ -1175,7 +1150,7 @@ impl<'a> LoadStart<'a> {
filename: PathBuf,
src: &'a str,
roc_cache_dir: RocCacheDir<'_>,
src_dir: PathBuf,
mut src_dir: PathBuf,
) -> Result<Self, LoadingProblem<'a>> {
let arc_modules = Arc::new(Mutex::new(PackageModuleIds::default()));
let root_exposed_ident_ids = IdentIds::exposed_builtins(0);
@ -1189,7 +1164,7 @@ impl<'a> LoadStart<'a> {
} = {
let root_start_time = Instant::now();
load_from_str(
let header_output = load_from_str(
arena,
filename,
src,
@ -1197,7 +1172,9 @@ impl<'a> LoadStart<'a> {
Arc::clone(&ident_ids_by_module),
roc_cache_dir,
root_start_time,
)?
)?;
adjust_header_paths(header_output, &mut src_dir)
};
Ok(LoadStart {
@ -1211,6 +1188,34 @@ impl<'a> LoadStart<'a> {
}
}
fn adjust_header_paths<'a>(
header_output: HeaderOutput<'a>,
src_dir: &mut PathBuf,
) -> HeaderOutput<'a> {
if let Msg::Header(ModuleHeader {
module_id: header_id,
header_type,
..
}) = &header_output.msg
{
debug_assert_eq!(*header_id, header_output.module_id);
if let HeaderType::Interface { name, .. } = header_type {
// Interface modules can have names like Foo.Bar.Baz,
// in which case we need to adjust the src_dir to
// remove the "Bar/Baz" directories in order to correctly
// resolve this interface module's imports!
let dirs_to_pop = name.as_str().matches('.').count();
for _ in 0..dirs_to_pop {
src_dir.pop();
}
}
}
header_output
}
pub enum LoadResult<'a> {
TypeChecked(LoadedModule),
Monomorphized(MonomorphizedModule<'a>),