Skip to content

Commit

Permalink
[move-ide] Added on-hover for inlay type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
awelc committed May 23, 2024
1 parent c2cc23b commit 40bd4af
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Move source file (a file with a `.move` file extension) and:
- go to references
- type on hover
- outline view showing symbol tree for Move source files
- inlay type hints (local declarations and lambda parameters)
- If the opened Move source file is located within a buildable project you can build and (locally)
test this project using `Move: Build a Move package` and `Move: Test a Move package` commands from
VSCode's command palette
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"publisher": "mysten",
"icon": "images/move.png",
"license": "Apache-2.0",
"version": "1.0.3",
"version": "1.0.4",
"preview": true,
"repository": {
"url": "https://github.com/MystenLabs/sui.git",
Expand Down
52 changes: 49 additions & 3 deletions external-crates/move/crates/move-analyzer/src/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

use crate::{
context::Context,
symbols::{type_to_ide_string, DefInfo, Symbols},
symbols::{on_hover_markup, type_to_ide_string, DefInfo, DefLoc, Symbols},
};
use lsp_server::Request;
use lsp_types::{
InlayHint, InlayHintKind, InlayHintLabel, InlayHintLabelPart, InlayHintParams, Position,
InlayHint, InlayHintKind, InlayHintLabel, InlayHintLabelPart, InlayHintParams,
InlayHintTooltip, Position,
};

use move_compiler::{naming::ast as N, shared::Identifier};

/// Handles inlay hints request of the language server
pub fn on_inlay_hint_request(context: &Context, request: &Request, symbols: &Symbols) {
let parameters = serde_json::from_value::<InlayHintParams>(request.params.clone())
Expand Down Expand Up @@ -48,7 +51,7 @@ pub fn on_inlay_hint_request(context: &Context, request: &Request, symbols: &Sym
label: InlayHintLabel::LabelParts(vec![colon_label, type_label]),
kind: Some(InlayHintKind::TYPE),
text_edits: None,
tooltip: None,
tooltip: additional_hint_info(t, symbols),
padding_left: None,
padding_right: None,
data: None,
Expand All @@ -69,3 +72,46 @@ pub fn on_inlay_hint_request(context: &Context, request: &Request, symbols: &Sym
eprintln!("could not send inlay thing response: {:?}", err);
}
}

/// Helper function to compute additional optional info for the hint.
/// At this point it's just the on-hover information as support
/// for adding location of type definition does not seem to quite
/// work in the current version of VSCode
///
/// TODO: revisit adding location of type definition once current problems
/// are resolved (the main problem is that adding it enables a drop-down menu
/// containing options that are not supported for the type definition, such
/// as go-to-declaration, and which jump to weird locations in the file).
fn additional_hint_info(sp!(_, t): &N::Type, symbols: &Symbols) -> Option<InlayHintTooltip> {
if let N::Type_::Ref(_, t) = t {
return additional_hint_info(t, symbols);
}
let N::Type_::Apply(_, sp!(_, type_name), _) = t else {
return None;
};
let N::TypeName_::ModuleType(mod_ident, struct_name) = type_name else {
return None;
};

let Some(mod_defs) = symbols
.file_mods()
.values()
.flatten()
.find(|m| m.ident() == &mod_ident.value)
else {
return None;
};

let Some(struct_def) = mod_defs.structs().get(&struct_name.value()) else {
return None;
};

let struct_def_loc = DefLoc::new(mod_defs.fhash(), struct_def.name_start());
let Some(struct_def_info) = symbols.def_info(&struct_def_loc) else {
return None;
};

Some(InlayHintTooltip::MarkupContent(on_hover_markup(
struct_def_info,
)))
}
Loading

0 comments on commit 40bd4af

Please sign in to comment.