|
1 |
| -use std::sync::Arc; |
2 |
| - |
3 |
| -use crate::core::{session::Session, token::Token}; |
| 1 | +use crate::{ |
| 2 | + core::{ |
| 3 | + session::{Documents, Session}, |
| 4 | + token::Token, |
| 5 | + token_type::{TokenType, VarBody}, |
| 6 | + }, |
| 7 | + utils::common::extract_visibility, |
| 8 | +}; |
4 | 9 | use lspower::lsp::{Hover, HoverContents, HoverParams, MarkupContent, MarkupKind};
|
| 10 | +use std::sync::Arc; |
5 | 11 |
|
6 | 12 | pub fn get_hover_data(session: Arc<Session>, params: HoverParams) -> Option<Hover> {
|
7 | 13 | let position = params.text_document_position_params.position;
|
8 | 14 | let url = ¶ms.text_document_position_params.text_document.uri;
|
9 | 15 |
|
10 |
| - session.get_token_hover_content(url, position) |
| 16 | + match session.documents.get(url.path()) { |
| 17 | + Some(ref document) => { |
| 18 | + if let Some(token) = document.get_token_at_position(position) { |
| 19 | + if token.is_initial_declaration() { |
| 20 | + return Some(get_hover_format(token, &session.documents)); |
| 21 | + } else { |
| 22 | + // todo: this logic is flawed at the moment |
| 23 | + // if there are multiple tokens with the same name and type in different files |
| 24 | + // there is no way for us to know which one is currently used in here |
| 25 | + for document_ref in &session.documents { |
| 26 | + if let Some(declared_token) = document_ref.get_declared_token(&token.name) { |
| 27 | + if declared_token.is_same_type(token) { |
| 28 | + return Some(get_hover_format(declared_token, &session.documents)); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + None |
| 33 | + } |
| 34 | + } else { |
| 35 | + None |
| 36 | + } |
| 37 | + } |
| 38 | + _ => None, |
| 39 | + } |
11 | 40 | }
|
12 | 41 |
|
13 |
| -pub fn to_hover_content(token: &Token) -> Hover { |
| 42 | +fn get_hover_format(token: &Token, documents: &Documents) -> Hover { |
| 43 | + let value = match &token.token_type { |
| 44 | + TokenType::Variable(var_details) => { |
| 45 | + let var_type = match &var_details.var_body { |
| 46 | + VarBody::FunctionCall(fn_name) => get_var_type_from_fn(&fn_name, documents), |
| 47 | + VarBody::Type(var_type) => var_type.clone(), |
| 48 | + _ => "".into(), |
| 49 | + }; |
| 50 | + |
| 51 | + format!( |
| 52 | + "let{} {}: {}", |
| 53 | + if var_details.is_mutable { " mut" } else { "" }, |
| 54 | + token.name, |
| 55 | + var_type |
| 56 | + ) |
| 57 | + } |
| 58 | + TokenType::FunctionDeclaration(func_details) => func_details.signature.clone(), |
| 59 | + TokenType::Struct(struct_details) => format!( |
| 60 | + "{}struct {}", |
| 61 | + extract_visibility(&struct_details.visibility), |
| 62 | + &token.name |
| 63 | + ), |
| 64 | + TokenType::Trait(trait_details) => format!( |
| 65 | + "{}trait {}", |
| 66 | + extract_visibility(&trait_details.visibility), |
| 67 | + &token.name |
| 68 | + ), |
| 69 | + TokenType::Enum => format!("enum {}", &token.name), |
| 70 | + _ => token.name.clone(), |
| 71 | + }; |
| 72 | + |
14 | 73 | Hover {
|
15 | 74 | contents: HoverContents::Markup(MarkupContent {
|
16 |
| - value: format!("{:?} : {}", token.content_type, token.name), |
17 |
| - kind: MarkupKind::PlainText, |
| 75 | + value: format!("```sway\n{}\n```", value), |
| 76 | + kind: MarkupKind::Markdown, |
18 | 77 | }),
|
19 | 78 | range: Some(token.range),
|
20 | 79 | }
|
21 | 80 | }
|
| 81 | + |
| 82 | +fn get_var_type_from_fn(fn_name: &str, documents: &Documents) -> String { |
| 83 | + for document_ref in documents { |
| 84 | + if let Some(declared_token) = document_ref.get_declared_token(&fn_name) { |
| 85 | + match &declared_token.token_type { |
| 86 | + TokenType::FunctionDeclaration(func_details) => { |
| 87 | + return func_details |
| 88 | + .get_return_type_from_signature() |
| 89 | + .unwrap_or_default(); |
| 90 | + } |
| 91 | + _ => {} |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + "".into() |
| 97 | +} |
0 commit comments