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

Commit

Permalink
merge: PR #196 from chore/183-vscode-linter-analyze-all-files-from-th…
Browse files Browse the repository at this point in the history
…e-project-staging into dev

183 - Vscode linter analyze all files from the project
  • Loading branch information
0xtekgrinder authored Nov 4, 2023
2 parents 91c4f89 + d6e5af7 commit 651f405
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 25 deletions.
4 changes: 2 additions & 2 deletions toolchains/solidity/core/Cargo.lock

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

2 changes: 1 addition & 1 deletion toolchains/solidity/core/crates/linter-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn main() -> Result<(), SolidHunterError> {
let mut linter: SolidLinter = SolidLinter::new();
linter.initialize_rules(&args.rules_file)?;

let result = linter.parse_path(args.path);
let result = linter.parse_path(&args.path);
if !args.to_json {
print_result(result);
} else {
Expand Down
18 changes: 9 additions & 9 deletions toolchains/solidity/core/crates/linter-lib/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ impl SolidLinter {
}
}

pub fn parse_file(&mut self, filepath: String) -> LintResult {
let content = fs::read_to_string(filepath.clone())?;
pub fn parse_file(&mut self, filepath: &str) -> LintResult {
let content = fs::read_to_string(filepath)?;
self.parse_content(filepath, content.as_str())
}

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 @@ -110,24 +110,24 @@ 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 parse_path(&mut self, path: String) -> Vec<LintResult> {
pub fn parse_path(&mut self, path: &str) -> Vec<LintResult> {
if Path::new(&path).is_file() {
vec![self.parse_file(path)]
} else {
self.parse_folder(path)
}
}

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
2 changes: 1 addition & 1 deletion toolchains/solidity/core/crates/linter-lib/tests/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn test_linter(config: &str, source: &str, expected_findings: &Vec<Finding>) {
let mut linter: SolidLinter = SolidLinter::new();
let _ = linter.initialize_rules(&String::from(config));

let result = linter.parse_file(String::from(source));
let result = linter.parse_file(source);
let mut found_findings: Vec<&Finding> = Vec::new();
let mut not_found_findings: Vec<&Finding> = Vec::new();
let mut not_needed_findings: Vec<&LintDiag> = Vec::new();
Expand Down
41 changes: 31 additions & 10 deletions toolchains/solidity/core/crates/linter-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use std::cell::RefCell;

use osmium_libs_lsp_handler::{
lsp_types::{
Diagnostic, DiagnosticSeverity, DidChangeTextDocumentParams, DidOpenTextDocumentParams,
InitializeParams, InitializeResult, InitializedParams, MessageType, Position, Range,
ServerCapabilities, TextDocumentSyncCapability, TextDocumentSyncKind, Url,
Diagnostic, DiagnosticSeverity, DidChangeTextDocumentParams, DidChangeWatchedFilesParams,
DidOpenTextDocumentParams, InitializeParams, InitializeResult, InitializedParams,
MessageType, Position, Range, ServerCapabilities, TextDocumentSyncCapability,
TextDocumentSyncKind, Url,
},
Connection, Handler, Result,
};
Expand Down Expand Up @@ -43,9 +44,12 @@ 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);
self.linter.replace(Some(linter));
} else {
self.linter
.borrow_mut()
Expand All @@ -63,8 +67,10 @@ impl Handler for Backend {
}

fn did_open(&self, params: DidOpenTextDocumentParams) {
self.connection
.log_message(MessageType::INFO, "file opened!");
self.connection.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 +81,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 +98,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 +112,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 All @@ -119,6 +127,19 @@ impl Handler for Backend {
.log_message(MessageType::ERROR, e.to_string());
}
}

fn did_change_watched_files(&self, _: DidChangeWatchedFilesParams) {
self.connection
.log_message(MessageType::INFO, "configuration file changed!");

if std::path::Path::new(&self.config_file_path).is_file() {
let mut linter = SolidLinter::new();
let res = linter.initialize_rules(&self.config_file_path);
if res.is_ok() {
self.linter.replace(Some(linter));
}
}
}
}

pub fn filepath_from_uri(uri: &Url) -> String {
Expand Down
15 changes: 13 additions & 2 deletions toolchains/solidity/extension/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import { glob } from 'glob';
import * as path from 'path';
import { workspace, ExtensionContext } from 'vscode';

Expand All @@ -15,7 +16,7 @@ import {

let client: LanguageClient;

export function activate(context: ExtensionContext) {
export async function activate(context: ExtensionContext) {
// The server is implemented in node
const serverModule = context.asAbsolutePath(
path.join('dist', 'server.js')
Expand All @@ -37,7 +38,7 @@ export function activate(context: ExtensionContext) {
documentSelector: [{ scheme: 'file', language: 'solidity' }],
synchronize: {
// Notify the server about file changes to '.clientrc files contained in the workspace
fileEvents: workspace.createFileSystemWatcher('**/.clientrc')
fileEvents: workspace.createFileSystemWatcher('**/.solidhunter.json')
}
};

Expand All @@ -51,6 +52,16 @@ export function activate(context: ExtensionContext) {

// Start the client. This will also launch the server
client.start();

const folders = workspace.workspaceFolders;
if (folders) {
const folder = folders[0];
const files = await workspace.findFiles('**/*.sol', `${folder.uri.fsPath}/**`);
files.forEach(file => {
workspace.openTextDocument(file);
});
}

}

export function deactivate(): Thenable<void> | undefined {
Expand Down

0 comments on commit 651f405

Please sign in to comment.