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

Commit

Permalink
wip(solidty/core/slither-server) added logs and security when initial…
Browse files Browse the repository at this point in the history
…izing workspace folder
  • Loading branch information
0xSwapFeeder committed Feb 22, 2024
1 parent a4c8a32 commit 745790b
Showing 1 changed file with 62 additions and 14 deletions.
76 changes: 62 additions & 14 deletions toolchains/solidity/core/crates/slither-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,9 @@ impl LanguageServer for Backend {
.log_message(MessageType::INFO, "Initializing diagnostic receiver ...")
.await;
let mut state = self.data.lock().await;
state.workspace = match params.workspace_folders {
Some(workspaces) => normalize_slither_path(workspaces[0].uri.path()),
None => normalize_slither_path(params.root_uri.unwrap().path()),
};
let mut receiver = state.receiver.take().unwrap();
let client = self.client.clone();

self.join_handle
.lock()
.await
Expand All @@ -80,15 +77,21 @@ impl LanguageServer for Backend {
)
.await;

match self.initialize_filters(&mut state) {
Ok(_) => {
eprintln!("Filters initialized!");
}
Err(e) => {
eprintln!("Error while initializing filters: {:?}", e);
}
}
self.client
.log_message(MessageType::INFO, "Initializing Workspace ...")
.await;
state.workspace = self
.fetch_workspace(params.workspace_folders, params.root_uri)
.await;

self.client
.log_message(MessageType::INFO, "Initializing filters ...")
.await;
self.initialize_filters(&mut state);

self.client
.log_message(MessageType::INFO, "Slither-Server initialized!")
.await;
Ok(InitializeResult {
server_info: None,
capabilities: ServerCapabilities {
Expand All @@ -113,6 +116,29 @@ impl LanguageServer for Backend {
.await;
}

async fn did_change_workspace_folders(&self, params: DidChangeWorkspaceFoldersParams) {
let mut state = self.data.lock().await;
if params.event.added.is_empty()
&& !params.event.removed.is_empty()
&& state.workspace == "."
{
self.client
.log_message(
MessageType::WARNING,
"No workspace folder found, please open a folder!",
)
.await;
return;
}
let folders: Vec<WorkspaceFolder> = params
.event
.added
.iter()
.map(|folder| folder.to_owned())
.collect();
state.workspace = self.fetch_workspace(Some(folders), None).await;
}

async fn shutdown(&self) -> Result<()> {
let state = self.data.lock().await;
for process in state.slither_processes.iter() {
Expand Down Expand Up @@ -210,7 +236,7 @@ impl Backend {
false
}

fn initialize_filters(&self, state: &mut MutexGuard<SlitherData>) -> Result<()> {
fn initialize_filters(&self, state: &mut MutexGuard<SlitherData>) {
//register all work directories folder aliases using foundry.toml for each workspace folder
let foundry_path = find_foundry_toml_config(&state.workspace);
if let Ok(path) = foundry_path {
Expand All @@ -227,7 +253,6 @@ impl Backend {
}
}
}
Ok(())
}

async fn launch_slither(&self, uri: Url) {
Expand Down Expand Up @@ -276,6 +301,29 @@ impl Backend {
}
});
}

async fn fetch_workspace(
&self,
workspace_folders: Option<Vec<WorkspaceFolder>>,
root_uri: Option<Url>,
) -> String {
let mut workspace = ".".to_string();
match workspace_folders {
Some(workspaces) => workspace = normalize_slither_path(workspaces[0].uri.path()),
None => match root_uri {
Some(uri) => workspace = normalize_slither_path(uri.path()),
None => {
self.client
.log_message(
MessageType::WARNING,
"No workspace folder found, please open a folder!",
)
.await;
}
},
}
workspace
}
}

#[tokio::main]
Expand Down

0 comments on commit 745790b

Please sign in to comment.