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 6e1ae1f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 33 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()
}
}
4 changes: 1 addition & 3 deletions crates/biome_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ impl Workspace {
.register_project_folder(params)
.map_err(into_error)?;

to_value(&result)
.map(IWorkspaceKey::from)
.map_err(into_error)
to_value(&result).map(IProjectKey::from).map_err(into_error)
}

#[wasm_bindgen(js_name = openFile)]
Expand Down
10 changes: 5 additions & 5 deletions packages/@biomejs/backend-jsonrpc/src/workspace.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 16 additions & 17 deletions packages/@biomejs/js-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import type {
PullDiagnosticsResult,
Workspace,
} from "@biomejs/wasm-nodejs";
import {Distribution, type WasmModule, loadModule, wrapError} from "./wasm";
import { Distribution, type WasmModule, loadModule, wrapError } from "./wasm";

// Re-export of some useful types for users
export type Configuration = PartialConfiguration;
export type {Diagnostic};
export {Distribution};
export type { Diagnostic };
export { Distribution };

export interface FormatContentDebugOptions extends FormatContentOptions {
/**
Expand Down Expand Up @@ -93,8 +93,7 @@ export class Biome {
private constructor(
private readonly module: WasmModule,
private readonly workspace: Workspace,
) {
}
) {}

/**
* It creates a new instance of the class {Biome}.
Expand Down Expand Up @@ -189,7 +188,7 @@ export class Biome {
return this.withFile(options.filePath, content, (path) => {
let code = content;

const {diagnostics} = this.workspace.pullDiagnostics({
const { diagnostics } = this.workspace.pullDiagnostics({
path,
categories: ["Syntax"],
max_diagnostics: Number.MAX_SAFE_INTEGER,
Expand Down Expand Up @@ -240,26 +239,26 @@ export class Biome {
*/
lintContent(
content: string,
{filePath, fixFileMode}: LintContentOptions,
{ filePath, fixFileMode }: LintContentOptions,
): LintResult {
const maybeFixedContent = fixFileMode
? this.withFile(filePath, content, (path) => {
let code = content;
let code = content;

const result = this.workspace.fixFile({
path,
fix_file_mode: fixFileMode,
should_format: false,
});
const result = this.workspace.fixFile({
path,
fix_file_mode: fixFileMode,
should_format: false,
});

code = result.code;
code = result.code;

return code;
})
return code;
})
: content;

return this.withFile(filePath, maybeFixedContent, (path) => {
const {diagnostics} = this.workspace.pullDiagnostics({
const { diagnostics } = this.workspace.pullDiagnostics({
path,
categories: ["Syntax", "Lint"],
max_diagnostics: Number.MAX_SAFE_INTEGER,
Expand Down

0 comments on commit 6e1ae1f

Please sign in to comment.