Skip to content

Commit

Permalink
Merge pull request #294 from baszalmstra/feature/new_vfs
Browse files Browse the repository at this point in the history
feat(language server): integrated new vfs
  • Loading branch information
baszalmstra committed Jan 15, 2021
2 parents 388cee1 + 1a06e0f commit 044ba56
Show file tree
Hide file tree
Showing 40 changed files with 1,663 additions and 522 deletions.
1 change: 1 addition & 0 deletions crates/mun_codegen/Cargo.toml
Expand Up @@ -37,6 +37,7 @@ insta = "0.16"
libloader = { path = "../mun_libloader", package = "mun_libloader" }
mun_test = { path = "../mun_test" }
runtime = { path = "../mun_runtime", package = "mun_runtime" }
paths = {path="../mun_paths", package="mun_paths"}

[build-dependencies]
semver = "0.9.0"
Expand Down
4 changes: 2 additions & 2 deletions crates/mun_codegen/src/code_gen/module_builder.rs
Expand Up @@ -23,8 +23,8 @@ impl<'db, 'ink, 'ctx> ModuleBuilder<'db, 'ink, 'ctx> {
let file_id = module
.file_id(code_gen.db)
.expect("module must have a file");
let assembly_name = code_gen.db.file_relative_path(file_id);
let assembly_module = code_gen.create_module(assembly_name);
let assembly_module =
code_gen.create_module(code_gen.db.file_relative_path(file_id).as_str());

Ok(Self {
code_gen,
Expand Down
13 changes: 8 additions & 5 deletions crates/mun_codegen/src/mock.rs
Expand Up @@ -2,9 +2,10 @@ use crate::{
db::{CodeGenDatabase, CodeGenDatabaseStorage},
OptimizationLevel,
};
use hir::{FileId, HirDatabase, RelativePathBuf, SourceDatabase, SourceRoot, SourceRootId};
use hir::{FileId, HirDatabase, SourceDatabase, SourceRoot, SourceRootId};
use mun_target::spec::Target;
use parking_lot::Mutex;
use paths::RelativePathBuf;
use std::sync::Arc;

/// A mock implementation of the IR database. It can be used to set up a simple test case.
Expand Down Expand Up @@ -83,15 +84,17 @@ impl MockDatabase {
let text = Arc::from(text.to_owned());
let rel_path = RelativePathBuf::from("main.mun");
let file_id = FileId(0);
db.set_file_relative_path(file_id, rel_path.clone());
db.set_file_text(file_id, text);
db.set_file_source_root(file_id, source_root_id);
source_root.insert_file(file_id);
source_root.insert_file(file_id, rel_path);

db.set_source_root(source_root_id, Arc::new(source_root));
db.set_optimization_level(OptimizationLevel::None);
db.set_package_source_root(hir::PackageId(0), source_root_id);

let mut packages = hir::PackageSet::default();
packages.add_package(source_root_id);
db.set_packages(Arc::new(packages));

db.set_optimization_level(OptimizationLevel::None);
(db, file_id)
}

Expand Down
3 changes: 2 additions & 1 deletion crates/mun_compiler/Cargo.toml
Expand Up @@ -16,7 +16,8 @@ categories = ["game-development", "mun"]
anyhow = "1.0.31"
mun_codegen = { version = "=0.2.0", path="../mun_codegen" }
mun_syntax = { version = "=0.2.0", path="../mun_syntax" }
hir = { version = "=0.2.0", path = "../mun_hir", package = "mun_hir" }
hir = { version="=0.2.0", path="../mun_hir", package="mun_hir" }
paths = {path="../mun_paths", package="mun_paths"}
mun_target = { version = "=0.2.0", path="../mun_target" }
mun_project = { version = "=0.1.0", path = "../mun_project" }
mun_diagnostics = { version = "=0.1.0", path = "../mun_diagnostics" }
Expand Down
18 changes: 9 additions & 9 deletions crates/mun_compiler/src/diagnostics_snippets.rs
@@ -1,13 +1,13 @@
use hir::{line_index::LineIndex, FileId, HirDatabase, RelativePathBuf};
use annotate_snippets::{
display_list::DisplayList,
display_list::FormatOptions,
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
};
use hir::{line_index::LineIndex, FileId, HirDatabase};
use mun_diagnostics::DiagnosticForWith;
use mun_syntax::SyntaxError;

use std::sync::Arc;

use annotate_snippets::display_list::DisplayList;
use annotate_snippets::display_list::FormatOptions;
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation};
use std::collections::HashMap;
use paths::RelativePathBuf;
use std::{collections::HashMap, sync::Arc};

/// Writes the specified syntax error to the output stream.
pub(crate) fn emit_syntax_error(
Expand Down Expand Up @@ -93,7 +93,7 @@ fn emit_diagnostic(

// Add primary annotations
annotations.push(AnnotationFile {
relative_file_path: db.file_relative_path(file_id),
relative_file_path: db.file_relative_path(file_id).to_relative_path_buf(),
source_code: db.file_text(file_id),
line_index: db.line_index(file_id),
annotations: vec![match diagnostic.primary_annotation() {
Expand Down
43 changes: 25 additions & 18 deletions crates/mun_compiler/src/driver.rs
Expand Up @@ -6,10 +6,11 @@ use crate::{
PathOrInline, RelativePath,
};
use hir::{
AstDatabase, DiagnosticSink, FileId, Module, PackageId, RelativePathBuf, SourceDatabase,
SourceRoot, SourceRootId, Upcast,
AstDatabase, DiagnosticSink, FileId, Module, PackageSet, SourceDatabase, SourceRoot,
SourceRootId, Upcast,
};
use mun_codegen::{AssemblyIR, CodeGenDatabase, TargetAssembly};
use paths::RelativePathBuf;

use std::{path::PathBuf, sync::Arc};

Expand All @@ -27,7 +28,6 @@ use std::path::Path;
use walkdir::WalkDir;

pub const WORKSPACE: SourceRootId = SourceRootId(0);
pub const PACKAGE: PackageId = PackageId(0);

pub struct Driver {
db: CompilerDatabase,
Expand Down Expand Up @@ -91,14 +91,16 @@ impl Driver {
// Store the file information in the database together with the source root
let file_id = FileId(driver.next_file_id as u32);
driver.next_file_id += 1;
driver.db.set_file_relative_path(file_id, rel_path);
driver.db.set_file_text(file_id, Arc::from(text));
driver.db.set_file_source_root(file_id, WORKSPACE);
driver.source_root.insert_file(file_id);
driver.source_root.insert_file(file_id, rel_path);
driver
.db
.set_source_root(WORKSPACE, Arc::new(driver.source_root.clone()));
driver.db.set_package_source_root(PACKAGE, WORKSPACE);

let mut package_set = PackageSet::default();
package_set.add_package(WORKSPACE);
driver.db.set_packages(Arc::new(package_set));

Ok((driver, file_id))
}
Expand All @@ -120,9 +122,10 @@ impl Driver {

// Iterate over all files in the source directory of the package and store their information in
// the database
let source_directory = package
.source_directory()
.ok_or_else(|| anyhow::anyhow!("the source directory does not exist"))?;
let source_directory = package.source_directory();
if !source_directory.is_dir() {
anyhow::bail!("the source directory does not exist")
}

for source_file_path in iter_source_files(&source_directory) {
let relative_path = compute_source_relative_path(&source_directory, &source_file_path)?;
Expand All @@ -137,19 +140,21 @@ impl Driver {
})?;

let file_id = driver.alloc_file_id(&relative_path)?;
driver
.db
.set_file_relative_path(file_id, relative_path.clone());
driver.db.set_file_text(file_id, Arc::from(file_contents));
driver.db.set_file_source_root(file_id, WORKSPACE);
driver.source_root.insert_file(file_id);
driver
.source_root
.insert_file(file_id, relative_path.clone());
}

// Store the source root in the database
driver
.db
.set_source_root(WORKSPACE, Arc::new(driver.source_root.clone()));
driver.db.set_package_source_root(PACKAGE, WORKSPACE);

let mut package_set = PackageSet::default();
package_set.add_package(WORKSPACE);
driver.db.set_packages(Arc::new(package_set));

Ok((package, driver))
}
Expand Down Expand Up @@ -378,13 +383,12 @@ impl Driver {
let file_id = self.alloc_file_id(path.as_ref()).unwrap();

// Insert the new file
self.db
.set_file_relative_path(file_id, path.as_ref().to_relative_path_buf());
self.db.set_file_text(file_id, Arc::from(contents));
self.db.set_file_source_root(file_id, WORKSPACE);

// Update the source root
self.source_root.insert_file(file_id);
self.source_root
.insert_file(file_id, path.as_ref().to_relative_path_buf());
self.db
.set_source_root(WORKSPACE, Arc::new(self.source_root.clone()));

Expand Down Expand Up @@ -425,8 +429,11 @@ impl Driver {
.insert(file_id, to.as_ref().to_relative_path_buf());
self.path_to_file_id.remove(from.as_ref()); // FileId now belongs to to

self.source_root.remove_file(file_id);
self.source_root
.insert_file(file_id, to.as_ref().to_relative_path_buf());
self.db
.set_file_relative_path(file_id, to.as_ref().to_relative_path_buf());
.set_source_root(WORKSPACE, Arc::new(self.source_root.clone()));

file_id
}
Expand Down
3 changes: 2 additions & 1 deletion crates/mun_compiler/src/lib.rs
Expand Up @@ -6,8 +6,9 @@ pub mod diagnostics;
mod diagnostics_snippets;
mod driver;

pub use hir::{FileId, RelativePath, RelativePathBuf};
pub use hir::FileId;
pub use mun_target::spec::Target;
pub use paths::{RelativePath, RelativePathBuf};
use std::path::{Path, PathBuf};

pub use crate::driver::DisplayColor;
Expand Down
5 changes: 2 additions & 3 deletions crates/mun_compiler_daemon/src/lib.rs
Expand Up @@ -19,9 +19,8 @@ pub fn compile_and_watch_manifest(
// Start watching the source directory
let (watcher_tx, watcher_rx) = channel();
let mut watcher: RecommendedWatcher = Watcher::new(watcher_tx, Duration::from_millis(10))?;
let source_directory = package
.source_directory()
.expect("missing source directory");
let source_directory = package.source_directory();

watcher.watch(&source_directory, RecursiveMode::Recursive)?;
println!("Watching: {}", source_directory.display());

Expand Down
2 changes: 1 addition & 1 deletion crates/mun_hir/Cargo.toml
Expand Up @@ -17,9 +17,9 @@ salsa = "0.15.0"
superslice = "1.0"
mun_syntax = { version = "=0.2.0", path = "../mun_syntax" }
mun_target = { version = "=0.2.0", path = "../mun_target" }
paths = {path="../mun_paths", package="mun_paths"}
rustc-hash = "1.1"
once_cell = "1.4.0"
relative-path = "1.2"
ena = "0.14"
drop_bomb = "0.1.4"
either = "1.5.3"
Expand Down
9 changes: 3 additions & 6 deletions crates/mun_hir/src/code_model/package.rs
@@ -1,6 +1,5 @@
use super::Module;
use crate::ids::PackageId;
use crate::{HirDatabase, ModuleId};
use crate::{HirDatabase, ModuleId, PackageId};

/// A `Package` describes a single package.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand All @@ -10,10 +9,8 @@ pub struct Package {

impl Package {
/// Returns all the packages defined in the database
pub fn all(_db: &dyn HirDatabase) -> Vec<Package> {
// TODO: Currently we assume there is only a single package with ID 0. This has to be
// implemented when have multiple packages. See CrateGraph in rust-analyzer.
vec![Package { id: PackageId(0) }]
pub fn all(db: &dyn HirDatabase) -> Vec<Package> {
db.packages().iter().map(|id| Package { id }).collect()
}

/// Returns the root module of the package (represented by the `mod.rs` in the source root)
Expand Down
27 changes: 16 additions & 11 deletions crates/mun_hir/src/db.rs
@@ -1,7 +1,7 @@
#![allow(clippy::type_repetition_in_bounds)]

use crate::expr::BodySourceMap;
use crate::ids::{DefWithBodyId, FunctionId, PackageId};
use crate::ids::{DefWithBodyId, FunctionId};
use crate::input::{SourceRoot, SourceRootId};
use crate::item_tree::{self, ItemTree};
use crate::module_tree::ModuleTree;
Expand All @@ -14,12 +14,12 @@ use crate::{
ids,
line_index::LineIndex,
ty::InferenceResult,
AstIdMap, Body, ExprScopes, FileId, Struct, TypeAlias,
AstIdMap, Body, ExprScopes, FileId, PackageId, PackageSet, Struct, TypeAlias,
};
use mun_syntax::{ast, Parse, SourceFile};
use mun_target::abi;
use mun_target::spec::Target;
pub use relative_path::RelativePathBuf;
use paths::RelativePathBuf;
use std::sync::Arc;

// TODO(bas): In the future maybe move this to a seperate crate (mun_db?)
Expand All @@ -34,29 +34,28 @@ pub trait SourceDatabase: salsa::Database {
#[salsa::input]
fn file_text(&self, file_id: FileId) -> Arc<str>;

/// Path to a file, relative to the root of its source root.
#[salsa::input]
fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf;

/// Source root of a file
#[salsa::input]
fn file_source_root(&self, file_id: FileId) -> SourceRootId;

/// Returns the relative path of a file
fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf;

/// Contents of the source root
#[salsa::input]
fn source_root(&self, id: SourceRootId) -> Arc<SourceRoot>;

/// The source root for a specific package
#[salsa::input]
fn package_source_root(&self, package: PackageId) -> SourceRootId;

/// For a package, returns its hierarchy of modules.
#[salsa::invoke(ModuleTree::module_tree_query)]
fn module_tree(&self, package: PackageId) -> Arc<ModuleTree>;

/// Returns the line index of a file
#[salsa::invoke(line_index_query)]
fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;

/// Returns the set of packages
#[salsa::input]
fn packages(&self) -> Arc<PackageSet>;
}

/// The `AstDatabase` provides queries that transform text from the `SourceDatabase` into an
Expand Down Expand Up @@ -158,3 +157,9 @@ fn target_data_layout(db: &dyn HirDatabase) -> Arc<abi::TargetDataLayout> {
.expect("unable to create TargetDataLayout from target");
Arc::new(data_layout)
}

fn file_relative_path(db: &dyn SourceDatabase, file_id: FileId) -> RelativePathBuf {
let source_root_id = db.file_source_root(file_id);
let source_root = db.source_root(source_root_id);
source_root.relative_path(file_id).to_relative_path_buf()
}
15 changes: 7 additions & 8 deletions crates/mun_hir/src/fixture.rs
@@ -1,10 +1,8 @@
#![cfg(test)]

use crate::ids::PackageId;
use crate::{FileId, SourceDatabase, SourceRoot, SourceRootId};
use crate::{FileId, PackageSet, SourceDatabase, SourceRoot, SourceRootId};
pub use mun_test::Fixture;
use std::convert::TryInto;
use std::sync::Arc;
use std::{convert::TryInto, sync::Arc};

impl<DB: SourceDatabase + Default + 'static> WithFixture for DB {}

Expand Down Expand Up @@ -32,20 +30,21 @@ fn with_files(db: &mut dyn SourceDatabase, fixture: &str) -> Vec<FileId> {

let mut source_root = SourceRoot::default();
let source_root_id = SourceRootId(0);
let package_id = PackageId(0);
let mut files = Vec::new();

for (idx, entry) in fixture.into_iter().enumerate() {
let file_id = FileId(idx.try_into().expect("too many files"));
db.set_file_relative_path(file_id, entry.relative_path);
db.set_file_text(file_id, Arc::from(entry.text));
db.set_file_source_root(file_id, source_root_id);
source_root.insert_file(file_id);
source_root.insert_file(file_id, entry.relative_path);
files.push(file_id);
}

db.set_source_root(source_root_id, Arc::new(source_root));
db.set_package_source_root(package_id, source_root_id);

let mut packages = PackageSet::default();
packages.add_package(source_root_id);
db.set_packages(Arc::new(packages));

return files;
}
15 changes: 6 additions & 9 deletions crates/mun_hir/src/ids.rs
@@ -1,7 +1,9 @@
use crate::item_tree::{Function, ItemTreeId, ItemTreeNode, Struct, TypeAlias};
use crate::module_tree::LocalModuleId;
use crate::primitive_type::PrimitiveType;
use crate::DefDatabase;
use crate::{
item_tree::{Function, ItemTreeId, ItemTreeNode, Struct, TypeAlias},
module_tree::LocalModuleId,
primitive_type::PrimitiveType,
DefDatabase, PackageId,
};
use std::hash::{Hash, Hasher};

#[derive(Debug)]
Expand Down Expand Up @@ -94,11 +96,6 @@ macro_rules! impl_intern {
};
}

/// Represents the id of a single package, all packages have a unique id, the main package and all
/// dependent packages.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PackageId(pub u32);

/// Represents an id of a module inside a package.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ModuleId {
Expand Down

0 comments on commit 044ba56

Please sign in to comment.