Skip to content

Commit

Permalink
Fixes #13, make source root optional
Browse files Browse the repository at this point in the history
  • Loading branch information
maurobalbi committed Mar 14, 2024
1 parent e649b02 commit eae8528
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
8 changes: 6 additions & 2 deletions crates/glas/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ impl Server {
}

fn on_initialized(&mut self, _params: InitializedParams) -> NotifyResult {
for msg in std::mem::take(&mut *self.init_messages.lock().unwrap()) {
for msg in std::mem::take(&mut *self.init_messages.lock().unwrap()) {
tracing::warn!("Init message ({:?}): {}", msg.typ, msg.message);

let _: Result<_, _> = self.client.show_message(msg);
Expand Down Expand Up @@ -632,7 +632,7 @@ impl Server {
let src = vfs.content_for_file(gleam_file);
(gleam_file, src)
};
let gleam_toml = gleam_src.parse::<Table>().unwrap();
let gleam_toml = gleam_src.parse::<Table>().context("Could not parse toml")?;
let name: SmolStr = gleam_toml
.get("name")
.and_then(|n| n.as_str())
Expand Down Expand Up @@ -933,6 +933,10 @@ impl Server {
tracing::info!("Setting new sourceroot {:?}", path);
package_roots.insert(PackageRoot { path });
source_root_changed = true;
} else {
package_roots.insert(PackageRoot {
path: vpath.as_path().unwrap().to_path_buf(),
});
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/ide/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ pub trait SourceDatabase {
#[salsa::input]
fn file_source_root(&self, file_id: FileId) -> SourceRootId;

fn source_root_package(&self, source_root_id: SourceRootId) -> Arc<PackageId>;
fn source_root_package(&self, source_root_id: SourceRootId) -> Arc<Option<PackageId>>;

#[salsa::input]
fn package_graph(&self) -> Arc<PackageGraph>;
Expand All @@ -404,13 +404,13 @@ pub trait SourceDatabase {
fn module_map(&self, source: SourceRootId) -> Arc<ModuleMap>;
}

fn source_root_package(db: &dyn SourceDatabase, id: SourceRootId) -> Arc<PackageId> {
fn source_root_package(db: &dyn SourceDatabase, id: SourceRootId) -> Arc<Option<PackageId>> {
let graph = db.package_graph();
let mut iter = graph.iter().filter(|&package| {
let root_file = graph[package].gleam_toml;
db.file_source_root(root_file) == id
});
let res = iter.next().expect("should always find a package!");
let res = iter.next();
Arc::new(res)
}

Expand Down
12 changes: 8 additions & 4 deletions crates/ide/src/def/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ pub struct Package {

impl Package {
pub fn is_local(self, db: &dyn DefDatabase) -> bool {
let package_id = db.source_root_package(self.id);
let Some(package_id) = *db.source_root_package(self.id) else {
return true;
};
let graph = db.package_graph();
let package = graph[*package_id].clone();
let package = graph[package_id].clone();
package.is_local
}

pub fn dependencies(self, db: &dyn DefDatabase) -> Vec<Package> {
let package_id = db.source_root_package(self.id);
let Some(package_id) = *db.source_root_package(self.id) else {
return vec![]
};
let graph = db.package_graph();
let package = graph[*package_id].clone();
let package = graph[package_id].clone();
package
.dependencies
.iter()
Expand Down

0 comments on commit eae8528

Please sign in to comment.