|
| 1 | +mod utils; |
| 2 | + |
| 3 | +use crate::utils::*; |
| 4 | + |
| 5 | +use std::sync::Arc; |
| 6 | +use tokio::sync::Mutex; |
| 7 | +use tower_lsp::jsonrpc::Result; |
| 8 | +use tower_lsp::lsp_types::*; |
| 9 | +use tower_lsp::lsp_types::Location as LspLocation; |
| 10 | +use tower_lsp::{Client, LanguageServer, LspService, Server}; |
| 11 | +use solc_references::*; |
| 12 | + |
| 13 | + |
| 14 | +struct Backend { |
| 15 | + client: Client, |
| 16 | + references_provider: Arc<Mutex<ReferencesProvider>>, |
| 17 | +} |
| 18 | + |
| 19 | +impl Backend { |
| 20 | + pub fn new(client: Client) -> Self { |
| 21 | + Self { client, references_provider: Arc::new(Mutex::new(ReferencesProvider { files: Vec::new(), base_path: String::new()})) } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +#[tower_lsp::async_trait] |
| 26 | +impl LanguageServer for Backend { |
| 27 | + async fn initialize(&self, params: InitializeParams) -> Result<InitializeResult> { |
| 28 | + if let Some(workspace) = params.workspace_folders { |
| 29 | + self.references_provider.lock().await.set_base_path(normalize_path(workspace[0].uri.path())); |
| 30 | + } else { |
| 31 | + self.references_provider.lock().await.set_base_path(normalize_path(¶ms.root_uri.unwrap().path())); |
| 32 | + } |
| 33 | + Ok(InitializeResult { |
| 34 | + server_info: None, |
| 35 | + capabilities: ServerCapabilities { |
| 36 | + text_document_sync: Some(TextDocumentSyncCapability::Kind( |
| 37 | + TextDocumentSyncKind::INCREMENTAL, |
| 38 | + )), |
| 39 | + workspace: Some(WorkspaceServerCapabilities { |
| 40 | + workspace_folders: Some(WorkspaceFoldersServerCapabilities { |
| 41 | + supported: Some(true), |
| 42 | + change_notifications: Some(OneOf::Left(true)), |
| 43 | + }), |
| 44 | + file_operations: None, |
| 45 | + }), |
| 46 | + definition_provider: Some(OneOf::Left(true)), |
| 47 | + references_provider: Some(OneOf::Left(true)), |
| 48 | + implementation_provider: Some(ImplementationProviderCapability::Simple(true)), |
| 49 | + type_definition_provider: Some(TypeDefinitionProviderCapability::Simple(true)), |
| 50 | + ..ServerCapabilities::default() |
| 51 | + }, |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + async fn initialized(&self, _: InitializedParams) { |
| 56 | + self.client |
| 57 | + .log_message(MessageType::INFO, "osmium-solidity-references initialized!") |
| 58 | + .await; |
| 59 | + if let Err(e) = self.references_provider.lock().await.update_file_content() { |
| 60 | + self.client.log_message(MessageType::ERROR, format!("Error updating file content: {}", e)).await; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + async fn did_save(&self, _: DidSaveTextDocumentParams) { |
| 65 | + if let Err(e) = self.references_provider.lock().await.update_file_content() { |
| 66 | + self.client.log_message(MessageType::ERROR, format!("Error updating file content: {}", e)).await; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + async fn references(&self, params: ReferenceParams) -> Result<Option<Vec<LspLocation>>> { |
| 71 | + self.client.log_message(MessageType::INFO, "References requested").await; |
| 72 | + let uri = params.text_document_position.text_document.uri; |
| 73 | + let mut position = params.text_document_position.position; |
| 74 | + position.line += 1; |
| 75 | + position.character += 1; |
| 76 | + |
| 77 | + let locations = self.references_provider.lock().await.get_references(&normalize_path(uri.path()), solc_references::Position { line: position.line, column: position.character }); |
| 78 | + let ret: Vec<LspLocation> = locations.iter().map(|location| { |
| 79 | + let mut new_uri = uri.clone(); |
| 80 | + new_uri.set_path(&escape_path(&location.uri)); |
| 81 | + location_to_lsp_location(&new_uri, &location) |
| 82 | + }).collect(); |
| 83 | + Ok(Some(ret)) |
| 84 | + } |
| 85 | + |
| 86 | + async fn goto_definition(&self, params: GotoDefinitionParams) -> Result<Option<GotoDefinitionResponse>> { |
| 87 | + self.client.log_message(MessageType::INFO, "Goto definition requested").await; |
| 88 | + let mut uri = params.text_document_position_params.text_document.uri; |
| 89 | + let mut position = params.text_document_position_params.position; |
| 90 | + position.line += 1; |
| 91 | + position.character += 1; |
| 92 | + |
| 93 | + let location = self.references_provider.lock().await.get_definition(&normalize_path(uri.path()), solc_references::Position { line: position.line, column: position.character }); |
| 94 | + |
| 95 | + if let Some(location) = location { |
| 96 | + uri.set_path(&escape_path(&location.uri)); |
| 97 | + return Ok(Some(GotoDefinitionResponse::Scalar(location_to_lsp_location(&uri, &location)))); |
| 98 | + } |
| 99 | + self.client.log_message(MessageType::INFO, "No definition found").await; |
| 100 | + Ok(None) |
| 101 | + } |
| 102 | + |
| 103 | + async fn shutdown(&self) -> Result<()> { |
| 104 | + Ok(()) |
| 105 | + } |
| 106 | + |
| 107 | +} |
| 108 | + |
| 109 | +impl Backend { |
| 110 | + |
| 111 | +} |
| 112 | + |
| 113 | +#[tokio::main] |
| 114 | +async fn main() { |
| 115 | + /* |
| 116 | + |
| 117 | + USE THIS CODE TO DEBUG |
| 118 | + |
| 119 | + let listener = tokio::net::TcpListener::bind("127.0.0.1:9001").await?; |
| 120 | + let (stream, _) = listener.accept().await?; |
| 121 | + let (read, write) = tokio::io::split(stream); |
| 122 | + Server::new(read, write, socket).serve(service).await; |
| 123 | + */ |
| 124 | + |
| 125 | + let (service, socket) = LspService::new(Backend::new); |
| 126 | + let stdin = tokio::io::stdin(); |
| 127 | + let stdout = tokio::io::stdout(); |
| 128 | + Server::new(stdin, stdout, socket).serve(service).await; |
| 129 | + |
| 130 | + //Ok(()) |
| 131 | +} |
0 commit comments