diff --git a/crates/aiken-lang/src/tipo.rs b/crates/aiken-lang/src/tipo.rs index fc3e2e0ce..f143ce866 100644 --- a/crates/aiken-lang/src/tipo.rs +++ b/crates/aiken-lang/src/tipo.rs @@ -1,8 +1,8 @@ use self::{environment::Environment, pretty::Printer}; use crate::{ ast::{ - well_known, Annotation, DataType, DataTypeKey, DefinitionLocation, ModuleKind, Span, - TypedDataType, + well_known, Annotation, DataType, DataTypeKey, DefinitionLocation, Located, ModuleKind, + Span, TypedDataType, TypedModule, }, tipo::fields::FieldMap, }; @@ -723,6 +723,14 @@ pub fn get_arg_type_name(tipo: &Type) -> String { } } +pub fn find_references(_ast: &TypedModule, _node: Located<'_>) -> Vec { + let references = Vec::new(); + + // Implement reference finding logic here + + references +} + pub fn convert_opaque_type( t: &Rc, data_types: &IndexMap<&DataTypeKey, &TypedDataType>, diff --git a/crates/aiken-lsp/src/lib.rs b/crates/aiken-lsp/src/lib.rs index 15beac070..ad4dcd765 100644 --- a/crates/aiken-lsp/src/lib.rs +++ b/crates/aiken-lsp/src/lib.rs @@ -61,6 +61,7 @@ fn capabilities() -> lsp_types::ServerCapabilities { // work_done_progress: None, // }, // }), + references_provider: Some(lsp_types::OneOf::Left(true)), code_action_provider: Some(lsp_types::CodeActionProviderCapability::Simple(true)), document_formatting_provider: Some(lsp_types::OneOf::Left(true)), definition_provider: Some(lsp_types::OneOf::Left(true)), diff --git a/crates/aiken-lsp/src/server.rs b/crates/aiken-lsp/src/server.rs index db35e26a6..7082d14d6 100644 --- a/crates/aiken-lsp/src/server.rs +++ b/crates/aiken-lsp/src/server.rs @@ -13,7 +13,7 @@ use aiken_lang::{ ast::{Definition, Located, ModuleKind, Span, Use}, error::ExtraData, line_numbers::LineNumbers, - parser, + parser, tipo, tipo::pretty::Printer, }; use aiken_project::{ @@ -30,10 +30,10 @@ use lsp_types::{ Notification, Progress, PublishDiagnostics, ShowMessage, }, request::{ - CodeActionRequest, Completion, Formatting, GotoDefinition, HoverRequest, Request, - WorkDoneProgressCreate, + CodeActionRequest, Completion, Formatting, GotoDefinition, HoverRequest, References, + Request, WorkDoneProgressCreate, }, - DocumentFormattingParams, InitializeParams, TextEdit, + DocumentFormattingParams, InitializeParams, ReferenceParams, TextEdit, }; use miette::Diagnostic; use std::{ @@ -314,6 +314,18 @@ impl Server { }) } + References::METHOD => { + let params = cast_request::(request)?; + + let locations = self.handle_references(params)?; + + Ok(lsp_server::Response { + id, + error: None, + result: Some(serde_json::to_value(locations)?), + }) + } + Completion::METHOD => { let params = cast_request::(request).expect("cast Completion"); @@ -498,6 +510,40 @@ impl Server { }) } + fn handle_references( + &self, + params: ReferenceParams, + ) -> Result>, ServerError> { + let position = params.text_document_position.position; + let text_document = params.text_document_position.text_document.clone(); + + let (line_numbers, node) = + match self.node_at_position(&lsp_types::TextDocumentPositionParams { + text_document: text_document.clone(), + position, + }) { + Some(location) => location, + None => return Ok(None), + }; + + let module = match self.module_for_uri(&text_document.uri) { + Some(module) => module, + None => return Ok(None), + }; + + let references = tipo::find_references(&module.ast, node); + + let locations = references + .into_iter() + .map(|span| lsp_types::Location { + uri: text_document.uri.clone(), + range: span_to_lsp_range(span, &line_numbers), + }) + .collect(); + + Ok(Some(locations)) + } + fn hover( &self, params: lsp_types::HoverParams,