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

Commit 19124b5

Browse files
0xSwapFeeder0xmemorygrinder
authored andcommitted
wip(solidity/reference)
1 parent 71cc2db commit 19124b5

File tree

16 files changed

+683
-145
lines changed

16 files changed

+683
-145
lines changed

libs/solc-references/Cargo.lock

Lines changed: 211 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/solc-references/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
solc-ast-rs-types = { version = "0.1.1", features = ["visit"]}
9+
solc-ast-rs-types = { version = "0.1.2", features = ["visit"]}
1010
solc-wrapper = { path="../solc-wrapper" }
1111
thiserror = "1.0.56"

libs/solc-references/src/definitions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::types::InteractableNode;
66

77
struct DefinitionFinder {
88
id: i64,
9-
token: String,
109
node: Option<InteractableNode>,
1110
to_find: Option<InteractableNode>,
1211
}
@@ -63,14 +62,15 @@ impl <'ast> Visit<'ast> for DefinitionFinder {
6362
}
6463

6564
impl DefinitionFinder {
65+
6666
pub fn new(id: i64, token: String) -> Self {
6767
DefinitionFinder {
6868
id,
69-
token,
7069
node: None,
7170
to_find: None,
7271
}
7372
}
73+
7474
pub fn find(&mut self, src: SourceUnit) -> Option<InteractableNode> {
7575
self.visit_source_unit(&src);
7676
self.node.clone()

libs/solc-references/src/lib.rs

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,30 @@ mod utils;
88

99
use error::ReferencesError;
1010
use node_finder::NodeVisitor;
11-
use solc_wrapper::{command::SolcCommand, get_ast_from_solc_output, SolcAstFile};
11+
use solc_wrapper::{get_ast_for_file, SolcAstFile};
1212
pub use solc_ast_rs_types::types::*;
13-
use types::{InteractableNode, Position};
13+
pub use types::{Location, Position};
14+
use types::{get_id, get_range, get_reference_id, InteractableNode};
1415
use usages::UsagesFinder;
16+
use utils::index_to_position;
1517

16-
18+
#[derive(Debug)]
1719
pub struct ReferencesProvider {
18-
files: Vec<SolcAstFile>
20+
pub files: Vec<SolcAstFile>
1921
}
2022

2123
impl ReferencesProvider {
2224
pub fn update_file_content(&mut self, filepath: String, content: String) -> Result<(), ReferencesError> {
23-
let solc = SolcCommand::new(filepath);
24-
let out = solc.execute_with_input(&content)?;
25-
let out = String::from_utf8_lossy(&out.stdout);
26-
let ast_files = get_ast_from_solc_output(&out)?;
25+
let ast_files = get_ast_for_file(filepath, content)?;
2726
for res_file in &ast_files {
2827
let mut found = false;
2928
for file in &mut self.files {
3029
if file.file == res_file.file {
3130
found = true;
3231
file.ast = res_file.ast.clone();
32+
if res_file.src.len() > 0 {
33+
file.src = res_file.src.clone();
34+
}
3335
break;
3436
}
3537
}
@@ -40,26 +42,43 @@ impl ReferencesProvider {
4042
Ok(())
4143
}
4244

43-
pub fn get_references(&self, position: Position) -> Vec<(Position, Position)> {
44-
let mut references: Vec<(Position, Position)> = Vec::new();
45-
let mut node: InteractableNode;
46-
let mut node_finder = NodeVisitor::new(position);
45+
pub fn get_references(&self, position: Position) -> Vec<Location> {
46+
let mut references: Vec<Location> = Vec::new();
47+
let mut found_node: Option<InteractableNode> = None;
48+
let mut node_finder = NodeVisitor::new(position.clone(), String::from(""));
4749
for file in &self.files {
48-
node_finder.find(file.ast);
49-
if let Some(node) = node_finder.node {
50-
node = node.clone();
50+
if let Some(node) = node_finder.find(&file.ast, &file.src) {
51+
found_node = Some(node.clone());
52+
eprintln!("Found node: {:?}", node);
5153
break;
5254
}
5355
}
54-
let mut usages_finder = UsagesFinder {
55-
id: get_id!(node),
56-
token: get_name!(node),
57-
to_find: references,
56+
if found_node.is_none() {
57+
eprintln!("No node found at position: {:?}", &position);
58+
return references;
59+
}
60+
let found_node = found_node.unwrap();
61+
let ref_id = match get_reference_id(&found_node.clone())
62+
{
63+
Some(id) => id,
64+
None => get_id(&found_node.clone())
5865
};
66+
67+
eprintln!("Ref id: {:?}", ref_id);
68+
69+
let mut usages_finder = UsagesFinder::new(ref_id);
5970
for file in &self.files {
6071
let nodes = usages_finder.find(&file.ast);
6172
for node in nodes {
62-
references.push(node);
73+
let range = get_range(&node);
74+
let start = index_to_position(range.index, &file.src);
75+
let end = index_to_position(range.index + range.length, &file.src);
76+
let location = Location {
77+
start,
78+
end,
79+
uri: file.file.clone()
80+
};
81+
references.push(location);
6382
}
6483
}
6584
references

0 commit comments

Comments
 (0)