From 5bb92c2d1af5f3045617a665d4e5c676700eb3c1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 11 Nov 2019 11:59:19 +0300 Subject: impl fmt::Display for BuiltinType --- crates/ra_hir_def/src/builtin_type.rs | 32 ++++++++++++++++++++++++++++++++ crates/ra_ide_api/src/hover.rs | 8 ++------ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/crates/ra_hir_def/src/builtin_type.rs b/crates/ra_hir_def/src/builtin_type.rs index 12929caa9..2ec0c83fe 100644 --- a/crates/ra_hir_def/src/builtin_type.rs +++ b/crates/ra_hir_def/src/builtin_type.rs @@ -3,6 +3,8 @@ //! A peculiarity of built-in types is that they are always available and are //! not associated with any particular crate. +use std::fmt; + use hir_expand::name::{self, Name}; #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] @@ -61,3 +63,33 @@ impl BuiltinType { (name::F64, BuiltinType::Float { bitness: FloatBitness::X64 }), ]; } + +impl fmt::Display for BuiltinType { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let type_name = match self { + BuiltinType::Char => "char", + BuiltinType::Bool => "bool", + BuiltinType::Str => "str", + BuiltinType::Int { signedness, bitness } => match (signedness, bitness) { + (Signedness::Signed, IntBitness::Xsize) => "isize", + (Signedness::Signed, IntBitness::X8) => "i8", + (Signedness::Signed, IntBitness::X16) => "i16", + (Signedness::Signed, IntBitness::X32) => "i32", + (Signedness::Signed, IntBitness::X64) => "i64", + (Signedness::Signed, IntBitness::X128) => "i128", + + (Signedness::Unsigned, IntBitness::Xsize) => "usize", + (Signedness::Unsigned, IntBitness::X8) => "u8", + (Signedness::Unsigned, IntBitness::X16) => "u16", + (Signedness::Unsigned, IntBitness::X32) => "u32", + (Signedness::Unsigned, IntBitness::X64) => "u64", + (Signedness::Unsigned, IntBitness::X128) => "u128", + }, + BuiltinType::Float { bitness } => match bitness { + FloatBitness::X32 => "f32", + FloatBitness::X64 => "f64", + }, + }; + f.write_str(type_name) + } +} diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index 244c65814..cc41390b2 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use hir::{Adt, BuiltinType, HasSource, HirDisplay}; +use hir::{Adt, HasSource, HirDisplay}; use ra_db::SourceDatabase; use ra_syntax::{ algo::{ancestors_at_offset, find_covering_element, find_node_at_offset}, @@ -132,11 +132,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option res.extend(from_def_source(db, it)), hir::ModuleDef::Trait(it) => res.extend(from_def_source(db, it)), hir::ModuleDef::TypeAlias(it) => res.extend(from_def_source(db, it)), - hir::ModuleDef::BuiltinType(it) => { - if let Some(b) = BuiltinType::ALL.iter().find(|(_, ty)| *ty == it) { - res.extend(Some(b.0.to_string())) - } - } + hir::ModuleDef::BuiltinType(it) => res.extend(Some(it.to_string())), }, Some(SelfType(ty)) => { if let Some((adt_def, _)) = ty.as_adt() { -- cgit v1.2.3