Skip to content

Commit

Permalink
apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Apr 25, 2024
1 parent 270be05 commit 9375835
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
10 changes: 5 additions & 5 deletions crates/biome_service/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ impl WorkspaceSettings {
pub fn current_settings(&self) -> &Settings {
let data = self
.data
.paths
.get(self.current_workspace)
.get_value_by_key(self.current_workspace)
.expect("You must have at least one workspace.");
&data.settings
}
Expand All @@ -76,7 +75,7 @@ impl WorkspaceSettings {
/// Register a new project using its folder. Use [WorkspaceSettings::get_current_settings_mut] to retrieve
/// its settings and change them.
pub fn insert_project(&mut self, workspace_path: impl Into<PathBuf>) -> ProjectKey {
self.data.paths.insert(PathToSettings {
self.data.insert(PathToSettings {
path: BiomePath::new(workspace_path.into()),
settings: Settings::default(),
})
Expand All @@ -86,10 +85,11 @@ impl WorkspaceSettings {
pub fn get_settings_by_path(&self, path: &BiomePath) -> &Settings {
debug_assert!(path.is_absolute(), "Workspaces paths must be absolutes.");
debug_assert!(
!self.data.paths.is_empty(),
!self.data.is_empty(),
"You must have at least one workspace."
);
for (_, path_to_settings) in &self.data.paths {
let iter = self.data.iter();
for (_, path_to_settings) in iter {
if path.strip_prefix(path_to_settings.path.as_path()).is_ok() {
return &path_to_settings.settings;
}
Expand Down
34 changes: 31 additions & 3 deletions crates/biome_service/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,13 +1022,13 @@ pub struct WorkspaceData<T> {
/// [DenseSlotMap] is the slowest type in insertion/removal, but the fastest in iteration
///
/// Users wouldn't change workspace folders very often,
pub paths: DenseSlotMap<ProjectKey, T>,
paths: DenseSlotMap<ProjectKey, T>,
}

impl<T> WorkspaceData<T> {
/// Inserts an item
pub fn insert(&mut self, item: impl Into<T>) {
self.paths.insert(Into::into(item));
pub fn insert(&mut self, item: T) -> ProjectKey {
self.paths.insert(item)
}

/// Removes an item
Expand All @@ -1043,4 +1043,32 @@ impl<T> WorkspaceData<T> {
pub fn get_mut(&mut self, key: ProjectKey) -> Option<&mut T> {
self.paths.get_mut(key)
}

pub fn is_empty(&self) -> bool {
self.paths.is_empty()
}

pub fn iter(&self) -> WorkspaceDataIterator<'_, T> {
WorkspaceDataIterator::new(&self)
}
}

pub struct WorkspaceDataIterator<'a, V> {
iterator: slotmap::dense::Iter<'a, ProjectKey, V>,
}

impl<'a, V> WorkspaceDataIterator<'a, V> {
fn new(data: &'a WorkspaceData<V>) -> Self {
Self {
iterator: data.paths.iter(),
}
}
}

impl<'a, V> Iterator for WorkspaceDataIterator<'a, V> {
type Item = (ProjectKey, &'a V);

fn next(&mut self) -> Option<Self::Item> {
self.iterator.next()
}
}

0 comments on commit 9375835

Please sign in to comment.