Skip to content

Commit c305d3d

Browse files
committed
add go to definition functionality
1 parent 6bbd1a2 commit c305d3d

1 file changed

Lines changed: 42 additions & 6 deletions

File tree

src/backend.rs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ use tower_lsp_server::lsp_types::{
1010
CompletionOptions, CompletionParams, CompletionResponse, Diagnostic,
1111
DidChangeConfigurationParams, DidChangeTextDocumentParams, DidChangeWatchedFilesParams,
1212
DidChangeWorkspaceFoldersParams, DidCloseTextDocumentParams, DidOpenTextDocumentParams,
13-
DidSaveTextDocumentParams, ExecuteCommandParams, Hover, HoverParams, HoverProviderCapability,
14-
InitializeParams, InitializeResult, InitializedParams, MarkupContent, MarkupKind, MessageType,
15-
OneOf, Range, SaveOptions, SemanticTokensParams, SemanticTokensResult, ServerCapabilities,
16-
TextDocumentSyncCapability, TextDocumentSyncKind, TextDocumentSyncOptions,
17-
TextDocumentSyncSaveOptions, Uri, WorkDoneProgressOptions, WorkspaceFoldersServerCapabilities,
18-
WorkspaceServerCapabilities,
13+
DidSaveTextDocumentParams, ExecuteCommandParams, GotoDefinitionParams, GotoDefinitionResponse,
14+
Hover, HoverParams, HoverProviderCapability, InitializeParams, InitializeResult,
15+
InitializedParams, Location, MarkupContent, MarkupKind, MessageType, OneOf, Range, SaveOptions,
16+
SemanticTokensParams, SemanticTokensResult, ServerCapabilities, TextDocumentSyncCapability,
17+
TextDocumentSyncKind, TextDocumentSyncOptions, TextDocumentSyncSaveOptions, Uri,
18+
WorkDoneProgressOptions, WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities,
1919
};
2020
use tower_lsp_server::{Client, LanguageServer};
2121

@@ -83,6 +83,7 @@ impl LanguageServer for Backend {
8383
file_operations: None,
8484
}),
8585
hover_provider: Some(HoverProviderCapability::Simple(true)),
86+
definition_provider: Some(OneOf::Left(true)),
8687
..ServerCapabilities::default()
8788
},
8889
})
@@ -202,6 +203,41 @@ impl LanguageServer for Backend {
202203
async fn hover(&self, params: HoverParams) -> Result<Option<Hover>> {
203204
Ok(self.provide_hover(&params).await)
204205
}
206+
207+
async fn goto_definition(
208+
&self,
209+
params: GotoDefinitionParams,
210+
) -> Result<Option<GotoDefinitionResponse>> {
211+
let documents = self.document_map.read().await;
212+
let uri = &params.text_document_position_params.text_document.uri;
213+
214+
let result = || -> Option<GotoDefinitionResponse> {
215+
let document = documents.get(uri)?;
216+
217+
let token_position = params.text_document_position_params.position;
218+
let token_span = positions_to_span((token_position, token_position)).ok()?;
219+
220+
let call = find_related_call(&document.functions, token_span)?;
221+
222+
match call.name() {
223+
simplicityhl::parse::CallName::Custom(func) => {
224+
let function = document
225+
.functions
226+
.iter()
227+
.find(|function| function.name() == func)?;
228+
229+
let (start, end) = span_to_positions(function.as_ref()).ok()?;
230+
Some(GotoDefinitionResponse::from(Location::new(
231+
uri.clone(),
232+
Range::new(start, end),
233+
)))
234+
}
235+
_ => None,
236+
}
237+
}();
238+
239+
Ok(result)
240+
}
205241
}
206242

207243
impl Backend {

0 commit comments

Comments
 (0)