Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
chore: ran lint and format
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmemorygrinder committed Dec 6, 2023
1 parent 5c8c203 commit f1f25b0
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 92 deletions.
21 changes: 13 additions & 8 deletions libs/foundry-wrapper/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::{
error::Error,
types::ProjectCompileOutput,
utils::{check_executable_argument, find_forge_executable, find_projects_paths},
};
use std::process::Command;
use crate::{types::ProjectCompileOutput, error::Error, utils::{find_projects_paths, find_forge_executable, check_executable_argument}};

#[derive(Debug)]
struct CompilerInner {
Expand All @@ -22,12 +26,13 @@ impl Compiler {
root_path: String::new(),
workspaces: Vec::new(),
executable_path: executable_path.to_str().unwrap_or_default().to_string(),
}
},
})
}

fn find_closest_workspace(&self, file_path: &str) -> Option<String> {
self.inner.workspaces
self.inner
.workspaces
.iter()
.filter(|path| file_path.starts_with(path.as_str()))
.max_by_key(|path| path.len())
Expand All @@ -50,17 +55,17 @@ impl Compiler {
}

pub fn compile(&mut self, file_path: &str) -> Result<(String, ProjectCompileOutput), Error> {
let workspace_path = self.find_closest_workspace(&file_path).ok_or_else(|| Error::InvalidFilePath(file_path.to_string()))?;
let workspace_path = self
.find_closest_workspace(&file_path)
.ok_or_else(|| Error::InvalidFilePath(file_path.to_string()))?;
let json = Command::new(&self.inner.executable_path)
.current_dir(&workspace_path)
.arg("compile")
.arg("--format-json")
.output()
.map_err(|e| {
Error::ExecutableError(e)
})?;
.map_err(|e| Error::ExecutableError(e))?;
let output_str = String::from_utf8_lossy(&json.stdout);
let compile_output: ProjectCompileOutput = serde_json::from_str(&output_str)?;
return Ok((workspace_path, compile_output));
}
}
}
5 changes: 2 additions & 3 deletions libs/foundry-wrapper/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {

#[error("Workspace loading error: {0}")]
InvalidRootPath(#[from] glob::PatternError),

#[error("Invalid file path: {0}")]
InvalidFilePath(String),

Expand All @@ -23,4 +22,4 @@ pub enum Error {

#[error("Invalid json output: {0}")]
InvalidJsonOutput(#[from] serde_json::Error),
}
}
2 changes: 1 addition & 1 deletion libs/foundry-wrapper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ mod compiler;
pub use compiler::*;

mod error;
pub use error::*;
pub use error::*;
17 changes: 8 additions & 9 deletions libs/foundry-wrapper/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ProjectCompileOutput {
errors: Vec<CompilationError>
errors: Vec<CompilationError>,
}

impl ProjectCompileOutput {
Expand All @@ -11,7 +11,6 @@ impl ProjectCompileOutput {
}
}


#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CompilationError {
#[serde(rename = "sourceLocation")]
Expand Down Expand Up @@ -64,7 +63,7 @@ impl CompilationError {
pub fn get_range(&self, source_content: &str) -> Option<Range> {
Some(Range {
start: self.get_start_position(source_content)?,
end: self.get_end_position(source_content)?
end: self.get_end_position(source_content)?,
})
}

Expand All @@ -79,7 +78,7 @@ impl CompilationError {
#[derive(Clone, Debug)]
pub struct Position {
pub line: u32,
pub column: u32
pub column: u32,
}

impl Position {
Expand All @@ -89,8 +88,8 @@ impl Position {
if idx < l.len() {
return Some(Self {
line: i as u32,
column: idx as u32
})
column: idx as u32,
});
}
idx -= l.len() + 1;
}
Expand All @@ -101,7 +100,7 @@ impl Position {
#[derive(Clone, Debug)]
pub struct Range {
pub start: Position,
pub end: Position
pub end: Position,
}

#[derive(Clone, Debug)]
Expand All @@ -117,7 +116,7 @@ impl From<String> for Severity {
s if s.to_uppercase() == "ERROR" => Self::Error,
s if s.to_uppercase() == "WARNING" => Self::Warning,
s if s.to_uppercase() == "INFO" => Self::Info,
_ => Self::Info
_ => Self::Info,
}
}
}
}
2 changes: 1 addition & 1 deletion libs/foundry-wrapper/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod executable;
pub use executable::*;

mod path;
pub use path::*;
pub use path::*;
8 changes: 3 additions & 5 deletions libs/foundry-wrapper/src/utils/executable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::path::PathBuf;
use crate::Error;
use std::path::PathBuf;

pub fn find_forge_executable() -> Result<PathBuf, Error> {
which::which("forge").map_err(|_| Error::FoundryExecutableNotFound)
Expand All @@ -10,13 +10,11 @@ pub fn check_executable_argument(executable_path: &str) -> Result<(), Error> {
.arg("compile")
.arg("--format-json")
.output()
.map_err(|e| {
Error::ExecutableError(e)
})?;
.map_err(|e| Error::ExecutableError(e))?;

let stderr_str = String::from_utf8_lossy(&output.stderr);
if stderr_str.contains("unexpected argument '--format-json'") {
return Err(Error::InvalidFoundryVersion);
}
Ok(())
}
}
12 changes: 5 additions & 7 deletions libs/foundry-wrapper/src/utils/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ pub fn find_projects_paths(root_path: &str) -> Result<Vec<PathBuf>, glob::Patter
.collect::<Vec<PathBuf>>();

// remove foundry.toml at the end of the filepath
Ok(
filespaths
.iter()
.map(|path| path.parent().unwrap().to_path_buf())
.collect()
)
}
Ok(filespaths
.iter()
.map(|path| path.parent().unwrap().to_path_buf())
.collect())
}
9 changes: 6 additions & 3 deletions libs/lsp-handler/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ use wasm_bindgen::prelude::*;
pub struct Dispatcher {
handlers: Vec<Box<dyn Handler>>,
connection: Connection,
}
}

impl Dispatcher {
pub fn new(connection: Connection) -> Self {
Dispatcher {
handlers: Vec::new(),
connection,
}
}
}

pub fn setup<F>(&mut self, creators: Vec<F>)
Expand Down Expand Up @@ -1535,7 +1535,10 @@ impl Handler for Dispatcher {
Ok(Some(text_edit))
}

fn selection_range(&mut self, params: SelectionRangeParams) -> Result<Option<Vec<SelectionRange>>> {
fn selection_range(
&mut self,
params: SelectionRangeParams,
) -> Result<Option<Vec<SelectionRange>>> {
let mut text_edit: Vec<SelectionRange> = vec![];
for handler in &mut self.handlers {
let res = handler.selection_range(params.clone());
Expand Down
5 changes: 4 additions & 1 deletion libs/lsp-handler/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,10 @@ pub trait Handler {
///
/// This request was introduced in specification version 3.15.0.
fn selection_range(&mut self, params: SelectionRangeParams) -> Result<Option<Vec<SelectionRange>>> {
fn selection_range(
&mut self,
params: SelectionRangeParams,
) -> Result<Option<Vec<SelectionRange>>> {
let _ = params;
log("Got a textDocument/selectionRange request, but it is not implemented");
Err(Error::method_not_found())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ impl AffectedFilesStore {
pub fn get_affected_files(&self, project_path: &str) -> Vec<String> {
self.projects_files.get(project_path).unwrap().clone()
}
}
}
Loading

0 comments on commit f1f25b0

Please sign in to comment.