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

Commit dd8dfcf

Browse files
committed
wip(solidity/references)
1 parent 0b3b947 commit dd8dfcf

File tree

6 files changed

+57
-48
lines changed

6 files changed

+57
-48
lines changed

libs/solc-wrapper/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn get_ast_for_file(base_path: String) -> Result<Vec<SolcAstFile>, SolcWrapp
4141
solc.current_dir(base_path.clone()).execute()?;
4242

4343
let current_time = SystemTime::now().duration_since(init_time).unwrap();
44-
println!("Finished compiling in: {:?} seconds", current_time.as_secs());
44+
eprintln!("Finished compiling in: {:?} seconds", current_time.as_secs());
4545

4646
get_ast_from_solc_output(base_path)
4747
}
@@ -73,7 +73,7 @@ pub fn get_ast_from_solc_output(base_path: String) -> Result<Vec<SolcAstFile>, S
7373
}
7474

7575
let current_time = SystemTime::now().duration_since(init_time).unwrap();
76-
println!("Finished parsing ast in: {:?} seconds", current_time.as_secs());
76+
eprintln!("Finished parsing ast in: {:?} seconds", current_time.as_secs());
7777
Ok(ast_files)
7878
}
7979

libs/solc-wrapper/src/output.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn get_files_from_solc_output(base_path: &str) -> Result<Vec<SolcJsonFile>,
2525

2626

2727
let current_time = SystemTime::now().duration_since(init_time).unwrap();
28-
println!("Finished retreiving json ast in: {:?} seconds", current_time.as_secs());
28+
eprintln!("Finished retreiving json ast in: {:?} seconds", current_time.as_secs());
2929

3030
Ok(files)
3131
}

toolchains/solidity/core/crates/references-server/src/main.rs

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use crate::utils::*;
55
use std::sync::Arc;
66
use tokio::sync::Mutex;
77
use tower_lsp::jsonrpc::Result;
8-
use tower_lsp::lsp_types;
98
use tower_lsp::lsp_types::*;
10-
use tower_lsp::lsp_types::Position as LspPosition;
11-
use tower_lsp::lsp_types::Range as LspRange;
129
use tower_lsp::lsp_types::Location as LspLocation;
1310
use tower_lsp::{Client, LanguageServer, LspService, Server};
1411
use solc_references::*;
@@ -59,21 +56,15 @@ impl LanguageServer for Backend {
5956
self.client
6057
.log_message(MessageType::INFO, "osmium-solidity-references initialized!")
6158
.await;
62-
let current_time = std::time::SystemTime::now();
6359
if let Err(e) = self.references_provider.lock().await.update_file_content() {
6460
self.client.log_message(MessageType::ERROR, format!("Error updating file content: {}", e)).await;
6561
}
66-
let elapsed = current_time.elapsed().unwrap();
67-
self.client.log_message(MessageType::INFO, format!("Finished updating ast in: {:?} seconds", elapsed)).await;
6862
}
6963

7064
async fn did_save(&self, _: DidSaveTextDocumentParams) {
71-
let current_time = std::time::SystemTime::now();
7265
if let Err(e) = self.references_provider.lock().await.update_file_content() {
7366
self.client.log_message(MessageType::ERROR, format!("Error updating file content: {}", e)).await;
7467
}
75-
let elapsed = current_time.elapsed().unwrap();
76-
self.client.log_message(MessageType::INFO, format!("Finished updating ast in: {:?} seconds", elapsed)).await;
7768
}
7869

7970
async fn references(&self, params: ReferenceParams) -> Result<Option<Vec<LspLocation>>> {
@@ -82,23 +73,13 @@ impl LanguageServer for Backend {
8273
let mut position = params.text_document_position.position;
8374
position.line += 1;
8475
position.character += 1;
76+
8577
let locations = self.references_provider.lock().await.get_references(&normalize_path(uri.path()), solc_references::Position { line: position.line, column: position.character });
8678
let ret: Vec<LspLocation> = locations.iter().map(|location| {
8779
let mut new_uri = uri.clone();
8880
new_uri.set_path(&escape_path(&location.uri));
89-
LspLocation {
90-
uri: new_uri,
91-
range: LspRange {
92-
start: LspPosition {
93-
line: location.start.line - 1,
94-
character: location.start.column - 1,
95-
},
96-
end: LspPosition {
97-
line: location.end.line - 1,
98-
character: location.end.column - 1,
99-
},
100-
},
101-
}}).collect();
81+
location_to_lsp_location(&new_uri, &location)
82+
}).collect();
10283
Ok(Some(ret))
10384
}
10485

@@ -108,22 +89,12 @@ impl LanguageServer for Backend {
10889
let mut position = params.text_document_position_params.position;
10990
position.line += 1;
11091
position.character += 1;
92+
11193
let location = self.references_provider.lock().await.get_definition(&normalize_path(uri.path()), solc_references::Position { line: position.line, column: position.character });
94+
11295
if let Some(location) = location {
11396
uri.set_path(&escape_path(&location.uri));
114-
return Ok(Some(GotoDefinitionResponse::Scalar(lsp_types::Location {
115-
uri: uri,
116-
range: LspRange {
117-
start: LspPosition {
118-
line: location.start.line - 1,
119-
character: location.start.column - 1,
120-
},
121-
end: LspPosition {
122-
line: location.end.line - 1,
123-
character: location.end.column - 1,
124-
},
125-
},
126-
})));
97+
return Ok(Some(GotoDefinitionResponse::Scalar(location_to_lsp_location(&uri, &location))));
12798
}
12899
self.client.log_message(MessageType::INFO, "No definition found").await;
129100
Ok(None)
@@ -140,12 +111,21 @@ impl Backend {
140111
}
141112

142113
#[tokio::main]
143-
async fn main() -> tokio::io::Result<()> {
114+
async fn main() {
115+
/*
116+
117+
USE THIS CODE TO DEBUG
118+
144119
let listener = tokio::net::TcpListener::bind("127.0.0.1:9001").await?;
145120
let (stream, _) = listener.accept().await?;
146121
let (read, write) = tokio::io::split(stream);
122+
Server::new(read, write, socket).serve(service).await;
123+
*/
147124

148125
let (service, socket) = LspService::new(Backend::new);
149-
Server::new(read, write, socket).serve(service).await;
150-
Ok(())
126+
let stdin = tokio::io::stdin();
127+
let stdout = tokio::io::stdout();
128+
Server::new(stdin, stdout, socket).serve(service).await;
129+
130+
//Ok(())
151131
}

toolchains/solidity/core/crates/references-server/src/utils.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use tower_lsp::lsp_types::{Location as LspLocation, Position as LspPosition, Range as LspRange, Url};
2+
13
#[cfg(target_family = "windows")]
24
pub fn normalize_path(path: &str) -> String {
35
let mut path = path.replace("%3A/", "://");
@@ -20,4 +22,20 @@ pub fn escape_path(path: &str) -> String {
2022
#[cfg(not(target_family = "windows"))]
2123
pub fn escape_path(path: &str) -> String {
2224
path.to_string()
25+
}
26+
27+
pub fn location_to_lsp_location(new_uri: &Url, location: &solc_references::Location) -> LspLocation {
28+
LspLocation {
29+
uri: new_uri.clone(),
30+
range: LspRange {
31+
start: LspPosition {
32+
line: location.start.line - 1,
33+
character: location.start.column - 1,
34+
},
35+
end: LspPosition {
36+
line: location.end.line - 1,
37+
character: location.end.column - 1,
38+
},
39+
},
40+
}
2341
}

toolchains/solidity/extension/src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ let testManager: TestManager;
2020

2121
export async function activate(context: ExtensionContext) {
2222
linterClient = await createLinterClient(context);
23-
// foundryCompilerClient = createFoundryCompilerClient(context);
23+
foundryCompilerClient = createFoundryCompilerClient(context);
2424
slitherClient = await createSlitherClient(context);
2525
referencesClient = await createReferencesClient(context);
2626
testsPositionsClient = await createTestsPositionsClient(context);
2727
if (workspace.workspaceFolders?.length) {
2828
testManager = new TestManager(testsPositionsClient, workspace.workspaceFolders[0].uri.fsPath);
2929
}
3030

31-
context.subscriptions.push(linterClient, slitherClient, testsPositionsClient, testManager.testController, referencesClient);
31+
context.subscriptions.push(linterClient, slitherClient, foundryCompilerClient, testsPositionsClient, testManager.testController, referencesClient);
3232

3333
registerForgeFmtLinter(context);
3434
registerGasEstimation();

toolchains/solidity/extension/src/references.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { TextDecoder } from 'util';
1313
import * as net from 'net';
1414

1515
export async function createReferencesClient(context: ExtensionContext): Promise<LanguageClient> {
16+
/*
1617
let connectionInfo = {
1718
port: 9001,
1819
host: "127.0.0.1"
@@ -26,6 +27,14 @@ export async function createReferencesClient(context: ExtensionContext): Promise
2627
};
2728
return Promise.resolve(result);
2829
};
30+
// If the extension is launched in debug mode then the debug server options are used
31+
// Otherwise the run options are used
32+
const socketOptions: SocketTransport = {
33+
port: 9001,
34+
kind: TransportKind.socket,
35+
};
36+
*/
37+
2938
// The server is implemented in node
3039
const serverBinary = context.asAbsolutePath(
3140
path.join(
@@ -34,13 +43,15 @@ export async function createReferencesClient(context: ExtensionContext): Promise
3443
)
3544
);
3645

37-
// If the extension is launched in debug mode then the debug server options are used
38-
// Otherwise the run options are used
39-
const socketOptions: SocketTransport = {
40-
port: 9001,
41-
kind: TransportKind.socket,
46+
const serverOptions: ServerOptions = {
47+
run: { command: serverBinary, transport: TransportKind.stdio },
48+
debug: {
49+
command: serverBinary,
50+
transport: TransportKind.stdio,
51+
}
4252
};
4353

54+
4455
// Options to control the language client
4556
const clientOptions: LanguageClientOptions = {
4657
// Register the server for plain text documents

0 commit comments

Comments
 (0)