mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 20:42:04 +00:00
feat: Hover for literals showing additional value information
This commit is contained in:
parent
6584e63506
commit
384488c157
6 changed files with 377 additions and 25 deletions
|
@ -15,7 +15,7 @@ use ide_db::{
|
|||
FxIndexSet, RootDatabase,
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use syntax::{ast, AstNode, SyntaxKind::*, SyntaxNode, T};
|
||||
use syntax::{ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxNode, T};
|
||||
|
||||
use crate::{
|
||||
doc_links::token_as_doc_comment,
|
||||
|
@ -268,6 +268,64 @@ fn hover_simple(
|
|||
let c = token.parent().and_then(|x| x.parent()).and_then(ast::ClosureExpr::cast)?;
|
||||
render::closure_expr(sema, config, c)
|
||||
})
|
||||
})
|
||||
// tokens
|
||||
.or_else(|| {
|
||||
let mut res = HoverResult::default();
|
||||
match_ast! {
|
||||
match original_token {
|
||||
ast::String(string) => {
|
||||
res.markup = Markup::fenced_block_text(format_args!("{}", string.value()?));
|
||||
},
|
||||
ast::ByteString(string) => {
|
||||
res.markup = Markup::fenced_block_text(format_args!("{:?}", string.value()?));
|
||||
},
|
||||
ast::CString(string) => {
|
||||
let val = string.value()?;
|
||||
res.markup = Markup::fenced_block_text(format_args!("{}", std::str::from_utf8(val.as_ref()).ok()?));
|
||||
},
|
||||
ast::Char(char) => {
|
||||
let mut res = HoverResult::default();
|
||||
res.markup = Markup::fenced_block_text(format_args!("{}", char.value()?));
|
||||
},
|
||||
ast::Byte(byte) => {
|
||||
res.markup = Markup::fenced_block_text(format_args!("0x{:X}", byte.value()?));
|
||||
},
|
||||
ast::FloatNumber(num) => {
|
||||
res.markup = if num.suffix() == Some("f32") {
|
||||
match num.value_f32() {
|
||||
Ok(num) => {
|
||||
Markup::fenced_block_text(format_args!("{num} (bits: 0x{:X})", num.to_bits()))
|
||||
},
|
||||
Err(e) => {
|
||||
Markup::fenced_block_text(format_args!("{e}"))
|
||||
},
|
||||
}
|
||||
} else {
|
||||
match num.value() {
|
||||
Ok(num) => {
|
||||
Markup::fenced_block_text(format_args!("{num} (bits: 0x{:X})", num.to_bits()))
|
||||
},
|
||||
Err(e) => {
|
||||
Markup::fenced_block_text(format_args!("{e}"))
|
||||
},
|
||||
}
|
||||
};
|
||||
},
|
||||
ast::IntNumber(num) => {
|
||||
res.markup = match num.value() {
|
||||
Ok(num) => {
|
||||
Markup::fenced_block_text(format_args!("{num} (0x{num:X}|0x{num:b})"))
|
||||
},
|
||||
Err(e) => {
|
||||
Markup::fenced_block_text(format_args!("{e}"))
|
||||
},
|
||||
};
|
||||
},
|
||||
_ => return None
|
||||
}
|
||||
}
|
||||
Some(res)
|
||||
});
|
||||
|
||||
result.map(|mut res: HoverResult| {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue