Skip to content

Commit

Permalink
6/n Move IncludeCtx into base_db crate
Browse files Browse the repository at this point in the history
Summary:
Step two if the `IncludeCtx` movement.  Move it into `base_db`.

We will start using it for include file resolution too next diff.

Reviewed By: michalmuskala

Differential Revision: D60451709

fbshipit-source-id: db81848ffdfda2e762e8e66bac59450a32d2ab82
  • Loading branch information
alanz authored and facebook-github-bot committed Aug 1, 2024
1 parent fac535a commit 96d066b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 66 deletions.
72 changes: 72 additions & 0 deletions crates/base_db/src/include.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under both the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree and the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree.
*/

use std::sync::Arc;

use vfs::FileId;

use crate::SourceDatabase;
use crate::SourceRoot;
use crate::SourceRootId;

pub struct IncludeCtx<'a> {
db: &'a dyn SourceDatabase,
source_root_id: SourceRootId,
source_root: Arc<SourceRoot>,
pub file_id: FileId,
}

impl<'a> IncludeCtx<'a> {
pub fn new(db: &'a dyn SourceDatabase, file_id: FileId) -> Self {
// Context for T171541590
let _ = stdx::panic_context::enter(format!("\nIncludeCtx::new: {:?}", file_id));
let source_root_id = db.file_source_root(file_id);
let source_root = db.source_root(source_root_id);
Self {
db,
file_id,
source_root_id,
source_root,
}
}

pub fn resolve_include(&self, path: &str) -> Option<FileId> {
self.resolve_relative(path)
.or_else(|| self.resolve_local(path))
}

pub fn resolve_include_lib(&self, path: &str) -> Option<FileId> {
self.resolve_include(path)
.or_else(|| self.resolve_remote(path))
}

fn resolve_relative(&self, path: &str) -> Option<FileId> {
self.source_root.relative_path(self.file_id, path)
}

fn resolve_local(&self, path: &str) -> Option<FileId> {
let app_data = self.db.app_data(self.source_root_id)?;
app_data.include_path.iter().find_map(|include| {
let name = include.join(path);
self.source_root.file_for_path(&name.into())
})
}

fn resolve_remote(&self, path: &str) -> Option<FileId> {
let app_data = self.db.app_data(self.source_root_id)?;
let project_data = self.db.project_data(app_data.project_id);

let (app_name, path) = path.split_once('/')?;
let source_root_id = project_data.app_roots.get(app_name)?;
let source_root = self.db.source_root(source_root_id);
let target_app_data = self.db.app_data(source_root_id)?;
let path = target_app_data.dir.join(path);
source_root.file_for_path(&path.into())
}
}
2 changes: 2 additions & 0 deletions crates/base_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use elp_syntax::TextSize;
use lazy_static::lazy_static;

mod change;
mod include;
mod input;
mod module_index;

Expand All @@ -29,6 +30,7 @@ pub mod fixture;
pub mod test_utils;
pub use change::Change;
pub use elp_project_model::AppType;
pub use include::IncludeCtx;
pub use input::AppData;
pub use input::AppRoots;
pub use input::AppStructure;
Expand Down
70 changes: 4 additions & 66 deletions crates/hir/src/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,21 @@
* of this source tree.
*/

use std::sync::Arc;

use elp_base_db::FileId;
use elp_base_db::SourceRoot;
use elp_base_db::SourceRootId;
use elp_base_db::IncludeCtx;

use crate::db::DefDatabase;
use crate::InFile;
use crate::IncludeAttribute;
use crate::IncludeAttributeId;

struct IncludeCtx<'a> {
db: &'a dyn DefDatabase,
source_root_id: SourceRootId,
source_root: Arc<SourceRoot>,
file_id: FileId,
}

pub(crate) fn resolve(
db: &dyn DefDatabase,
include_id: InFile<IncludeAttributeId>,
) -> Option<FileId> {
resolve_in_ctx(&IncludeCtx::new(db, include_id.file_id), include_id.value)
}

fn resolve_in_ctx(ctx: &IncludeCtx, id: IncludeAttributeId) -> Option<FileId> {
let form_list = ctx.db.file_form_list(ctx.file_id);
let (path, file_id) = match &form_list[id] {
let ctx = &IncludeCtx::new(db.upcast(), include_id.file_id);
let form_list = db.file_form_list(ctx.file_id);
let (path, file_id) = match &form_list[include_id.value] {
IncludeAttribute::Include { path, .. } => (path, ctx.resolve_include(path)),
IncludeAttribute::IncludeLib { path, .. } => (path, ctx.resolve_include_lib(path)),
};
Expand All @@ -49,55 +36,6 @@ fn resolve_in_ctx(ctx: &IncludeCtx, id: IncludeAttributeId) -> Option<FileId> {
file_id
}

impl<'a> IncludeCtx<'a> {
fn new(db: &'a dyn DefDatabase, file_id: FileId) -> Self {
// Context for T171541590
let _ = stdx::panic_context::enter(format!("\nIncludeCtx::new: {:?}", file_id));
let source_root_id = db.file_source_root(file_id);
let source_root = db.source_root(source_root_id);
Self {
db,
file_id,
source_root_id,
source_root,
}
}

fn resolve_include(&self, path: &str) -> Option<FileId> {
self.resolve_relative(path)
.or_else(|| self.resolve_local(path))
}

fn resolve_include_lib(&self, path: &str) -> Option<FileId> {
self.resolve_include(path)
.or_else(|| self.resolve_remote(path))
}

fn resolve_relative(&self, path: &str) -> Option<FileId> {
self.source_root.relative_path(self.file_id, path)
}

fn resolve_local(&self, path: &str) -> Option<FileId> {
let app_data = self.db.app_data(self.source_root_id)?;
app_data.include_path.iter().find_map(|include| {
let name = include.join(path);
self.source_root.file_for_path(&name.into())
})
}

fn resolve_remote(&self, path: &str) -> Option<FileId> {
let app_data = self.db.app_data(self.source_root_id)?;
let project_data = self.db.project_data(app_data.project_id);

let (app_name, path) = path.split_once('/')?;
let source_root_id = project_data.app_roots.get(app_name)?;
let source_root = self.db.source_root(source_root_id);
let target_app_data = self.db.app_data(source_root_id)?;
let path = target_app_data.dir.join(path);
source_root.file_for_path(&path.into())
}
}

#[cfg(test)]
mod tests {
use elp_base_db::fixture::WithFixture;
Expand Down

0 comments on commit 96d066b

Please sign in to comment.