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 10, 2023
1 parent 18772ce commit aa1c508
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 36 deletions.
4 changes: 3 additions & 1 deletion libs/foundry-wrapper/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::{
error::Error,
types::ProjectCompileOutput,
utils::{check_executable_argument, find_forge_executable, find_projects_paths, normalize_path},
utils::{
check_executable_argument, find_forge_executable, find_projects_paths, normalize_path,
},
};
use std::process::Command;

Expand Down
4 changes: 2 additions & 2 deletions libs/foundry-wrapper/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ pub struct Position {
impl Position {
pub fn from_index(idx: i32, source: &str) -> Option<Self> {
let mut idx: usize = idx as usize;
for (i, l) in source.split("\n").enumerate() {
let line_length = l.len() + if l.ends_with("\r") { 2 } else { 1 };
for (i, l) in source.split('\n').enumerate() {
let line_length = l.len() + if l.ends_with('\r') { 2 } else { 1 };
if idx < line_length {
return Some(Self {
line: i as u32,
Expand Down
4 changes: 3 additions & 1 deletion libs/foundry-wrapper/src/utils/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ pub fn find_projects_paths(root_path: &str) -> Result<Vec<PathBuf>, glob::Patter
}

pub fn normalize_path(path: &str) -> String {
path.replace("\\", "/").replace("//", "/").replace("\\\\", "/")
path.replace('\\', "/")
.replace("//", "/")
.replace("\\\\", "/")
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ impl AffectedFilesStore {
* @param {String} project_path Project path
* @returns {Vec<String>} List of files that are not raising an error anymore
*/
pub fn fill_affected_files(&mut self, raised_files: Vec<String>, project_path: &str) -> Vec<String> {
pub fn fill_affected_files(
&mut self,
raised_files: Vec<String>,
project_path: &str,
) -> Vec<String> {
let mut affected_files = Vec::new();
if let Some(project_files) = self.projects_files.get_mut(project_path) {
project_files.retain(|file| !raised_files.contains(&file));
project_files.retain(|file| !raised_files.contains(file));
affected_files = project_files.clone();
project_files.extend(raised_files);
} else {
self.projects_files.insert(project_path.to_string(), raised_files);
self.projects_files
.insert(project_path.to_string(), raised_files);
}
affected_files
}
Expand Down
75 changes: 48 additions & 27 deletions toolchains/solidity/core/crates/foundry-compiler-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer, LspService, Server};
mod utils;
use utils::{convert_severity, get_root_path, slashify_path, normalized_slash_path};
use utils::{convert_severity, get_root_path, normalized_slash_path, slashify_path};
mod affected_files_store;
use affected_files_store::AffectedFilesStore;

Expand All @@ -30,7 +30,7 @@ impl LanguageServer for Backend {
self.client
.log_message(MessageType::INFO, "Foundry server initializing!")
.await;
if let Some(root_path) = get_root_path(params.clone()) {
if let Some(root_path) = get_root_path(params.clone()) {
self.client
.log_message(
MessageType::INFO,
Expand Down Expand Up @@ -73,7 +73,8 @@ impl LanguageServer for Backend {
format!("file opened!: {:}", params.text_document.uri),
)
.await;
let _ = self.compile(normalized_slash_path(params.text_document.uri.path()))
let _ = self
.compile(normalized_slash_path(params.text_document.uri.path()))
.await;
}

Expand All @@ -84,7 +85,8 @@ impl LanguageServer for Backend {
format!("file changed!: {:}", params.text_document.uri),
)
.await;
let _ = self.compile(normalized_slash_path(params.text_document.uri.path()))
let _ = self
.compile(normalized_slash_path(params.text_document.uri.path()))
.await;
}

Expand Down Expand Up @@ -158,7 +160,7 @@ impl Backend {
.unwrap()
.to_string();
self.load_workspace(folder_path).await?
}
}
Ok(())
}

Expand Down Expand Up @@ -207,10 +209,11 @@ impl Backend {

for error in output.get_errors() {
// Generate diagnostic from compilation error
let (affected_file, diagnostic) = match self.extract_diagnostic(&error, &project_path).await {
Some(diagnostic) => diagnostic,
None => continue,
};
let (affected_file, diagnostic) =
match self.extract_diagnostic(error, &project_path).await {
Some(diagnostic) => diagnostic,
None => continue,
};

// Add diagnostic to the hashmap
let url = match affected_file.to_str() {
Expand All @@ -224,14 +227,20 @@ impl Backend {
}
}

self.reset_not_affected_files(project_path, filepath, &raised_diagnostics).await;
self.reset_not_affected_files(project_path, filepath, &raised_diagnostics)
.await;
for (uri, diags) in raised_diagnostics.iter() {
if let Ok(url) = Url::parse(&format!("file://{}", &uri)) {
self.client
.publish_diagnostics(url, diags.clone(), None)
.await;
.publish_diagnostics(url, diags.clone(), None)
.await;
} else {
self.client.log_message(MessageType::ERROR, format!("error, cannot parse file uri : {}", uri)).await;
self.client
.log_message(
MessageType::ERROR,
format!("error, cannot parse file uri : {}", uri),
)
.await;
}
}
}
Expand All @@ -243,13 +252,19 @@ impl Backend {
* @returns {Option<(PathBuf, Diagnostic)>} Diagnostic
* @returns {None} If the diagnostic cannot be extracted
*/
async fn extract_diagnostic(&self, compilation_error: &CompilationError, project_path: &str) -> Option<(PathBuf, Diagnostic)> {
async fn extract_diagnostic(
&self,
compilation_error: &CompilationError,
project_path: &str,
) -> Option<(PathBuf, Diagnostic)> {
eprintln!("Compilation error: {:?}", compilation_error);
let (source_content_filepath, range) =
match self.extract_diagnostic_range(&project_path, compilation_error).await {
Some((source_content_filepath, range)) => (source_content_filepath, range),
None => return None,
};
let (source_content_filepath, range) = match self
.extract_diagnostic_range(project_path, compilation_error)
.await
{
Some((source_content_filepath, range)) => (source_content_filepath, range),
None => return None,
};
let diagnostic = Diagnostic {
range: Range {
start: Position {
Expand Down Expand Up @@ -294,8 +309,11 @@ impl Backend {
}
None => {
self.client
.log_message(MessageType::ERROR, format!("error, cannot get filepath: {:?}", error))
.await;
.log_message(
MessageType::ERROR,
format!("error, cannot get filepath: {:?}", error),
)
.await;
return None;
}
};
Expand Down Expand Up @@ -347,7 +365,9 @@ impl Backend {
.affected_files
.add_project_file(project_path.clone(), filepath.clone());
let raised_files = raised_diagnostics.keys().cloned().collect::<Vec<String>>();
let without_diagnostics = state.affected_files.fill_affected_files(raised_files, &project_path);
let without_diagnostics = state
.affected_files
.fill_affected_files(raised_files, &project_path);

self.client
.log_message(
Expand All @@ -358,15 +378,16 @@ impl Backend {

for file in without_diagnostics.iter() {
if let Ok(url) = Url::parse(&format!("file://{}", &file)) {
self.client.publish_diagnostics(url, vec![], None).await;
} else {
self.client
.publish_diagnostics(url, vec![], None)
.log_message(
MessageType::ERROR,
format!("error, cannot parse file uri : {}", file),
)
.await;
} else {
self.client.log_message(MessageType::ERROR, format!("error, cannot parse file uri : {}", file)).await;
}
}


}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ pub fn normalize_path(path: &str) -> String {
* @returns {String} Slashified path
*/
pub fn slashify_path(path: &str) -> String {
path.replace("\\", "/").replace("\\\\", "/").replace("//", "/")
path.replace('\\', "/")
.replace("\\\\", "/")
.replace("//", "/")
}

pub fn normalized_slash_path(path: &str) -> String {
slashify_path(&normalize_path(path))
}
}

0 comments on commit aa1c508

Please sign in to comment.