Skip to content

Commit

Permalink
fix(servers/linter-configuration): added opened_file registery + wind…
Browse files Browse the repository at this point in the history
…ows support
  • Loading branch information
0xSwapFeeder committed Oct 7, 2024
1 parent faa0873 commit 3655e03
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions servers/linter-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"

[dependencies]
osmium-libs-lsp-server-wrapper = { path = "../../libs/lsp-server-wrapper", version = "0.2.0" }
osmium-libs-solidity-path-utils = { path = "../../libs/path-utils", version = "0.1.0" }
solidhunter = { path = "../../libs/solidhunter", version = "0.3.0" }
glob = "0.3.0"
serde = { version = "1.0.149", features = ["derive"] }
Expand Down
16 changes: 15 additions & 1 deletion servers/linter-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use osmium_libs_lsp_server_wrapper::{
lsp_types::*, Client, LanguageServer, LspStdioServer, RequestId, Result,
};
use solidhunter::{linter::SolidLinter, types::LintDiag};
use std::{cell::RefCell, rc::Rc};
use std::{cell::RefCell, iter::Map, rc::Rc};
mod utils;
use utils::get_closest_config_filepath;
mod get_content;
Expand All @@ -11,6 +11,7 @@ use get_content::{ContentRequest, ContentRequestParams, ContentResponse};
struct Backend {
connection: Rc<RefCell<Client>>,
linter: RefCell<Option<SolidLinter>>,
opened_files: RefCell<Vec<(Url, String)>>,
}

impl LanguageServer for Backend {
Expand Down Expand Up @@ -76,6 +77,9 @@ impl LanguageServer for Backend {
}

fn did_open(&self, params: DidOpenTextDocumentParams) {
let mut opened_files = self.opened_files.borrow_mut();
opened_files.push((params.text_document.uri.clone(), params.text_document.text.clone()));

self.connection.borrow_mut().log_message(
MessageType::INFO,
format!("file opened!: {:}", params.text_document.uri),
Expand All @@ -84,6 +88,11 @@ impl LanguageServer for Backend {
self.lint(params.text_document.uri, params.text_document.text);
}

fn did_close(&self, params: DidCloseTextDocumentParams) {
let mut opened_files = self.opened_files.borrow_mut();
opened_files.retain(|x| x.0 != params.text_document.uri);
}

fn did_save(&self, params: DidSaveTextDocumentParams) {
self.connection.borrow_mut().log_message(
MessageType::INFO,
Expand Down Expand Up @@ -157,6 +166,10 @@ impl LanguageServer for Backend {
.borrow_mut()
.log_message(MessageType::INFO, "configuration file loaded!");
self.linter.replace(Some(linter));
let opened_files = self.opened_files.borrow_mut();
for file in opened_files.to_owned() {
self.lint(file.0, file.1)
}
} else {
self.connection
.borrow_mut()
Expand All @@ -170,6 +183,7 @@ impl Backend {
Self {
connection,
linter: RefCell::new(None),
opened_files: RefCell::new(vec![])
}
}

Expand Down
13 changes: 7 additions & 6 deletions servers/linter-server/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use osmium_libs_lsp_server_wrapper::{
lsp_types::{InitializeParams, MessageType, WorkspaceFolder},
Client,
};
use osmium_libs_solidity_path_utils::normalize_path;

pub fn get_closest_config_filepath(
connection: &Client,
Expand Down Expand Up @@ -31,7 +32,7 @@ pub fn get_closest_config_filepath(
if all_configs.is_empty() {
return Ok(None);
}
Ok(Some(all_configs[0].clone()))
Ok(Some(normalize_path(&all_configs[0].clone())))
}

fn get_closest_workspace_config_filepath(
Expand All @@ -43,7 +44,7 @@ fn get_closest_workspace_config_filepath(
let workspace_path = folder.uri.path();

let file_content =
match std::fs::read_to_string(format!("{}/.solidhunter.json", workspace_path)) {
match std::fs::read_to_string(normalize_path(&format!("{}/.solidhunter.json", workspace_path))) {
Ok(content) => content,
Err(err) => {
connection.log_message(
Expand All @@ -62,9 +63,9 @@ fn get_closest_workspace_config_filepath(
format!("file_content: {:?}", file_content),
);

let pattern = format!("{}/**/.solidhunter.json", workspace_path);
let pattern = normalize_path(&format!("{}/**/.solidhunter.json", workspace_path));
connection.log_message(MessageType::INFO, format!("pattern: {:?}", pattern));
let workspaces_paths = glob(&pattern).map_err(|err| {
let workspaces_paths = glob(&pattern).map_err(|err| {
connection.log_message(MessageType::ERROR, format!("error: {:?}", err));
err
})?;
Expand All @@ -73,7 +74,7 @@ fn get_closest_workspace_config_filepath(
match path {
Ok(path) => {
connection.log_message(MessageType::INFO, format!("pushing path: {:?}", path));
all_configs.push(path.to_str().unwrap().to_string());
all_configs.push(normalize_path(path.to_str().unwrap()));
}
Err(err) => {
connection.log_message(MessageType::ERROR, format!("error: {:?}", err));
Expand All @@ -96,5 +97,5 @@ fn get_closest_workspace_config_filepath(
if paths.is_empty() {
return Ok(None);
}
Ok(Some(paths[0].clone()))
Ok(Some(normalize_path(&paths[0].clone())))
}

0 comments on commit 3655e03

Please sign in to comment.