sway_lsp/capabilities/hover/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
pub(crate) mod hover_link_contents;

use self::hover_link_contents::HoverLinkContents;
use crate::config::LspClient;
use crate::{
    core::{
        session::Session,
        token::{SymbolKind, Token, TypedAstToken},
    },
    utils::{
        attributes::doc_comment_attributes, keyword_docs::KeywordDocs, markdown, markup::Markup,
    },
};
use lsp_types::{self, Position, Url};
use std::sync::Arc;
use sway_core::{
    language::{ty, Visibility},
    Engines, TypeId,
};
use sway_types::{Span, Spanned};

/// Extracts the hover information for a token at the current position.
pub fn hover_data(
    session: Arc<Session>,
    keyword_docs: &KeywordDocs,
    url: &Url,
    position: Position,
    client_config: LspClient,
) -> Option<lsp_types::Hover> {
    let t = session.token_map().token_at_position(url, position)?;
    let (ident, token) = t.pair();
    let range = ident.range;

    // check if our token is a keyword
    if matches!(
        token.kind,
        SymbolKind::BoolLiteral
            | SymbolKind::Keyword
            | SymbolKind::SelfKeyword
            | SymbolKind::ProgramTypeKeyword
    ) {
        let name = &ident.name;
        let documentation = keyword_docs.get(name).unwrap();
        let prefix = format!("\n```sway\n{name}\n```\n\n---\n\n");
        let formatted_doc = format!("{prefix}{documentation}");
        let content = Markup::new().text(&formatted_doc);
        let contents = lsp_types::HoverContents::Markup(markup_content(&content));
        return Some(lsp_types::Hover {
            contents,
            range: Some(range),
        });
    }

    let contents = match &token.declared_token_ident(&session.engines.read()) {
        Some(decl_ident) => {
            let t = session.token_map().try_get(decl_ident).try_unwrap()?;
            let decl_token = t.value();
            hover_format(
                session.clone(),
                &session.engines.read(),
                decl_token,
                &decl_ident.name,
                client_config.clone(),
            )
        }
        // The `TypeInfo` of the token does not contain an `Ident`. In this case,
        // we use the `Ident` of the token itself.
        None => hover_format(
            session.clone(),
            &session.engines.read(),
            token,
            &ident.name,
            client_config.clone(),
        ),
    };

    Some(lsp_types::Hover {
        contents,
        range: Some(range),
    })
}

fn visibility_as_str(visibility: Visibility) -> &'static str {
    match visibility {
        Visibility::Private => "",
        Visibility::Public => "pub ",
    }
}

/// Expects a span from either a `FunctionDeclaration` or a `TypedFunctionDeclaration`.
fn extract_fn_signature(span: &Span) -> String {
    let value = span.as_str();
    value.split('{').take(1).map(str::trim).collect()
}

fn format_doc_attributes(engines: &Engines, token: &Token) -> String {
    let mut doc_comment = String::new();
    doc_comment_attributes(engines, token, |attributes| {
        doc_comment = attributes.iter().fold(String::new(), |output, attribute| {
            let comment = attribute.args.first().unwrap().name.as_str();
            format!("{output}{comment}\n")
        });
    });
    doc_comment
}

fn format_visibility_hover(visibility: Visibility, decl_name: &str, token_name: &str) -> String {
    format!(
        "{}{} {}",
        visibility_as_str(visibility),
        decl_name,
        token_name
    )
}

fn format_variable_hover(is_mutable: bool, type_name: &str, token_name: &str) -> String {
    let mutability = if is_mutable { " mut" } else { "" };
    format!("let{mutability} {token_name}: {type_name}")
}

fn markup_content(markup: &Markup) -> lsp_types::MarkupContent {
    let kind = lsp_types::MarkupKind::Markdown;
    let value = markdown::format_docs(markup.as_str());
    lsp_types::MarkupContent { kind, value }
}

fn hover_format(
    session: Arc<Session>,
    engines: &Engines,
    token: &Token,
    ident_name: &str,
    client_config: LspClient,
) -> lsp_types::HoverContents {
    let decl_engine = engines.de();
    let doc_comment = format_doc_attributes(engines, token);

    let format_name_with_type = |name: &str, type_id: &TypeId| -> String {
        let type_name = format!("{}", engines.help_out(type_id));
        format!("{name}: {type_name}")
    };

    // Used to collect all the information we need to generate links for the hover component.
    let mut hover_link_contents = HoverLinkContents::new(session, engines);

    let sway_block = token
        .as_typed()
        .as_ref()
        .and_then(|typed_token| match typed_token {
            TypedAstToken::TypedDeclaration(decl) => match decl {
                ty::TyDecl::VariableDecl(var_decl) => {
                    let type_name =
                        format!("{}", engines.help_out(var_decl.type_ascription.type_id));
                    hover_link_contents.add_related_types(&var_decl.type_ascription.type_id);
                    Some(format_variable_hover(
                        var_decl.mutability.is_mutable(),
                        &type_name,
                        ident_name,
                    ))
                }
                ty::TyDecl::StructDecl(ty::StructDecl { decl_id, .. }) => {
                    let struct_decl = decl_engine.get_struct(decl_id);
                    hover_link_contents.add_implementations_for_decl(decl);
                    Some(format_visibility_hover(
                        struct_decl.visibility,
                        decl.friendly_type_name(),
                        ident_name,
                    ))
                }
                ty::TyDecl::TraitDecl(ty::TraitDecl { decl_id, .. }) => {
                    let trait_decl = decl_engine.get_trait(decl_id);
                    hover_link_contents.add_implementations_for_trait(&trait_decl);
                    Some(format_visibility_hover(
                        trait_decl.visibility,
                        decl.friendly_type_name(),
                        ident_name,
                    ))
                }
                ty::TyDecl::EnumDecl(ty::EnumDecl { decl_id, .. }) => {
                    let enum_decl = decl_engine.get_enum(decl_id);
                    hover_link_contents.add_implementations_for_decl(decl);
                    Some(format_visibility_hover(
                        enum_decl.visibility,
                        decl.friendly_type_name(),
                        ident_name,
                    ))
                }
                ty::TyDecl::AbiDecl(ty::AbiDecl { .. }) => {
                    hover_link_contents.add_implementations_for_decl(decl);
                    Some(format!("{} {}", decl.friendly_type_name(), &ident_name))
                }
                _ => None,
            },
            TypedAstToken::TypedFunctionDeclaration(func) => {
                hover_link_contents.add_related_types(&func.return_type.type_id);
                Some(extract_fn_signature(&func.span()))
            }
            TypedAstToken::TypedFunctionParameter(param) => {
                hover_link_contents.add_related_types(&param.type_argument.type_id);
                Some(format_name_with_type(
                    param.name.as_str(),
                    &param.type_argument.type_id,
                ))
            }
            TypedAstToken::TypedStructField(field) => {
                hover_link_contents.add_implementations_for_type(
                    &field.type_argument.span(),
                    field.type_argument.type_id,
                );
                Some(format_name_with_type(
                    field.name.as_str(),
                    &field.type_argument.type_id,
                ))
            }
            TypedAstToken::TypedExpression(expr) => match expr.expression {
                ty::TyExpressionVariant::Literal { .. } => {
                    Some(format!("{}", engines.help_out(expr.return_type)))
                }
                _ => None,
            },
            _ => None,
        });

    let content = Markup::new()
        .maybe_add_sway_block(sway_block)
        .text(&doc_comment)
        .maybe_add_links(
            engines.se(),
            &hover_link_contents.related_types,
            &hover_link_contents.implementations,
            client_config,
        );

    lsp_types::HoverContents::Markup(markup_content(&content))
}