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

Commit

Permalink
feat(solidity/linter): the LSP now diagnostics all files when launching
Browse files Browse the repository at this point in the history
  • Loading branch information
startonmateo committed Oct 31, 2023
1 parent f360be9 commit 00d2bb8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
1 change: 0 additions & 1 deletion remove-me-759554465d5041259aca.txt

This file was deleted.

4 changes: 2 additions & 2 deletions toolchains/solidity/core/crates/linter-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn lint_folder(args: Args) -> Result<(), SolidHunterError> {
linter.initialize_rules(&args.rules_file)?;
let mut result = Vec::new();
for path in args.project_path {
result.append(&mut linter.parse_folder(path));
result.append(&mut linter.parse_folder(&path));
}
for res in result {
print_result(res);
Expand Down Expand Up @@ -124,7 +124,7 @@ fn main() -> Result<(), SolidHunterError> {
let mut linter: SolidLinter = SolidLinter::new();
linter.initialize_rules(&args.rules_file)?;

let result = linter.parse_file(args.file_to_lint);
let result = linter.parse_file(&args.file_to_lint);
if !args.to_json {
print_result(result);
} else {
Expand Down
16 changes: 8 additions & 8 deletions toolchains/solidity/core/crates/linter-lib/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ impl SolidLinter {
}
}

pub fn parse_file(&mut self, filepath: String) -> LintResult {
pub fn parse_file(&mut self, filepath: &str) -> LintResult {
let content = fs::read_to_string(filepath.clone())?;
let res = osmium_libs_solidity_ast_extractor::extract::extract_ast_from_content(&content)?;

self._add_file(filepath.as_str(), res, content.as_str());
self._add_file(filepath, res, content.as_str());
let mut res: Vec<LintDiag> = Vec::new();

for rule in &self.rules {
Expand All @@ -105,10 +105,10 @@ impl SolidLinter {
Ok(res)
}

pub fn parse_content(&mut self, filepath: String, content: &str) -> LintResult {
pub fn parse_content(&mut self, filepath: &str, content: &str) -> LintResult {
let res = osmium_libs_solidity_ast_extractor::extract::extract_ast_from_content(content)?;

self._add_file(filepath.as_str(), res, content);
self._add_file(filepath, res, content);
let mut res: Vec<LintDiag> = Vec::new();

for rule in &self.rules {
Expand All @@ -118,17 +118,17 @@ impl SolidLinter {
Ok(res)
}

pub fn parse_folder(&mut self, folder: String) -> Vec<LintResult> {
pub fn parse_folder(&mut self, folder: &str) -> Vec<LintResult> {
let mut result: Vec<LintResult> = Vec::new();
if let Ok(entries) = glob(&(folder + "/**/*.sol")) {
if let Ok(entries) = glob(&(folder.to_owned() + "/**/*.sol")) {
for entry in entries.flatten() {
result.push(self.parse_file(entry.into_os_string().into_string().unwrap()));
result.push(self.parse_file(&entry.into_os_string().into_string().unwrap()));
}
}
result
}

pub fn delete_file(&mut self, path: String) {
pub fn delete_file(&mut self, path: &str) {
loop {
let idx = self.files.iter().position(|x| x.path == path);
match idx {
Expand Down
37 changes: 31 additions & 6 deletions toolchains/solidity/core/crates/linter-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use solidhunter_lib::{linter::SolidLinter, types::LintDiag};
struct Backend {
connection: Connection,
config_file_path: String,
working_directory: String,
linter: RefCell<Option<SolidLinter>>,
}

Expand Down Expand Up @@ -43,9 +44,30 @@ impl Handler for Backend {
let res = linter.initialize_rules(&self.config_file_path);
if let Err(e) = res {
eprintln!("Error initializing rules: {:?}", e);
self.linter
.borrow_mut()
.replace(SolidLinter::new_fileless());
return;
}
self.linter.borrow_mut().replace(linter);

let diags_res = linter.parse_folder(&self.working_directory);
for diag_res in diags_res {
if let Ok(diags) = diag_res {
if diags.len() == 0 {
continue;
}
let diags_parsed = diags
.iter()
.map(|d| diagnostic_from_lintdiag(d.clone()))
.collect();
eprintln!("diags: {:#?}", diags_parsed);
let uri = Url::parse(diags[0].uri.to_string().as_str());
if let Ok(uri) = uri {
self.connection.publish_diagnostics(uri, diags_parsed, None);
}
}
}
self.linter.replace(Some(linter));
} else {
self.linter
.borrow_mut()
Expand All @@ -64,7 +86,7 @@ impl Handler for Backend {

fn did_open(&self, params: DidOpenTextDocumentParams) {
self.connection
.log_message(MessageType::INFO, "file opened!");
.log_message(MessageType::INFO, format!("file opened!: {:}", params.text_document.uri));

let filepath = filepath_from_uri(&params.text_document.uri);
let mut linter = self.linter.borrow_mut();
Expand All @@ -75,7 +97,7 @@ impl Handler for Backend {
return;
}
};
let diags_res = linter.parse_content(filepath, &params.text_document.text);
let diags_res = linter.parse_content(&filepath, &params.text_document.text);

if let Ok(diags) = diags_res {
let diags = diags
Expand All @@ -92,8 +114,10 @@ impl Handler for Backend {
}

fn did_change(&self, params: DidChangeTextDocumentParams) {
self.connection
.log_message(MessageType::INFO, "file changed!");
self.connection.log_message(
MessageType::INFO,
format!("file changed!: {:}", params.text_document.uri),
);

let filepath = filepath_from_uri(&params.text_document.uri);
let mut linter = self.linter.borrow_mut();
Expand All @@ -104,7 +128,7 @@ impl Handler for Backend {
return;
}
};
let diags_res = linter.parse_content(filepath, &params.content_changes[0].text);
let diags_res = linter.parse_content(&filepath, &params.content_changes[0].text);

if let Ok(diags) = diags_res {
let diags = diags
Expand Down Expand Up @@ -153,6 +177,7 @@ pub fn create_linter(connection: Connection) -> Box<dyn Handler> {
Box::new(Backend {
connection,
config_file_path: ".solidhunter.json".to_string(),
working_directory: ".".to_string(),
linter: RefCell::new(None),
})
}

0 comments on commit 00d2bb8

Please sign in to comment.