-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode_actions_provider.rs
96 lines (84 loc) · 2.98 KB
/
code_actions_provider.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use crate::completions::auto_complete_provider::AutoCompleteProvider;
use crate::error::CodeActionError;
use crate::references::reference_provider::ReferenceProvider;
use crate::types::{CompletionItem, Location, Position};
use osmium_libs_solidity_ast_extractor::extract::extract_ast_from_foundry;
use osmium_libs_solidity_ast_extractor::types::SolidityAstFile;
use std::sync::RwLock;
use std::sync::{Arc, Mutex};
#[derive(Debug)]
pub struct CodeActionsProvider {
pub files: Arc<Mutex<Vec<SolidityAstFile>>>,
pub base_path: RwLock<String>,
}
impl Default for CodeActionsProvider {
fn default() -> Self {
Self::new()
}
}
impl CodeActionsProvider {
pub fn new() -> Self {
Self {
files: Arc::new(Mutex::new(vec![])),
base_path: RwLock::new(String::new()),
}
}
pub fn set_base_path(&self, base_path: String) {
let mut r = self.base_path.write().unwrap();
*r = base_path;
}
pub fn update_file_content(&self) -> Result<(), CodeActionError> {
let new_files = extract_ast_from_foundry(&self.base_path.read().unwrap())?; // will always find the root foundry project
let mut files = self.files.lock().unwrap();
*files = new_files;
Ok(())
}
pub fn get_references(&self, uri: &str, position: Position) -> Vec<Location> {
let files = self.files.lock().unwrap();
let provider = ReferenceProvider::new();
provider.get_references(uri, position, &files)
}
pub fn get_definition(&self, uri: &str, position: Position) -> Option<Location> {
let files = self.files.lock().unwrap();
let provider = ReferenceProvider::new();
provider.get_definition(
uri,
position,
&files,
self.base_path.read().unwrap().as_str(),
)
}
pub fn get_completions(&self, uri: &str, position: Position) -> Vec<CompletionItem> {
let files = self.files.lock().unwrap();
let provider = AutoCompleteProvider::new();
provider.get_suggestions(uri, position, &files)
}
pub fn refactor(&self, uri: &str, position: Position) -> Vec<Location> {
let mut refactors: Vec<Location> = vec![];
if let Some(def) = self.get_definition(uri, position) {
let refs = self.get_references(def.uri.as_str(), def.clone().start);
refactors.push(def);
for reference in refs {
refactors.push(reference);
}
}
refactors
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_set_base_path() {
let provider = CodeActionsProvider::new();
provider.set_base_path("test".to_string());
assert_eq!(*provider.base_path.read().unwrap(), "test");
}
#[test]
fn test_update_file_content() {
let provider = CodeActionsProvider::new();
provider.set_base_path("tests".to_string());
let result = provider.update_file_content();
assert!(!result.is_ok());
}
}