mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Implement location link for type inlay hints
This commit is contained in:
parent
927d56a67d
commit
801a2231bf
6 changed files with 362 additions and 82 deletions
|
@ -16,7 +16,7 @@ use hir_def::{
|
||||||
path::{Path, PathKind},
|
path::{Path, PathKind},
|
||||||
type_ref::{ConstScalar, TraitBoundModifier, TypeBound, TypeRef},
|
type_ref::{ConstScalar, TraitBoundModifier, TypeBound, TypeRef},
|
||||||
visibility::Visibility,
|
visibility::Visibility,
|
||||||
HasModule, ItemContainerId, Lookup, ModuleId, TraitId,
|
HasModule, ItemContainerId, Lookup, ModuleDefId, ModuleId, TraitId,
|
||||||
};
|
};
|
||||||
use hir_expand::{hygiene::Hygiene, name::Name};
|
use hir_expand::{hygiene::Hygiene, name::Name};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
@ -35,9 +35,27 @@ use crate::{
|
||||||
TraitRefExt, Ty, TyExt, TyKind, WhereClause,
|
TraitRefExt, Ty, TyExt, TyKind, WhereClause,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub trait HirWrite: fmt::Write {
|
||||||
|
fn start_location_link(&mut self, location: ModuleDefId);
|
||||||
|
fn end_location_link(&mut self);
|
||||||
|
}
|
||||||
|
|
||||||
|
// String will ignore link metadata
|
||||||
|
impl HirWrite for String {
|
||||||
|
fn start_location_link(&mut self, _: ModuleDefId) {}
|
||||||
|
|
||||||
|
fn end_location_link(&mut self) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// `core::Formatter` will ignore metadata
|
||||||
|
impl HirWrite for fmt::Formatter<'_> {
|
||||||
|
fn start_location_link(&mut self, _: ModuleDefId) {}
|
||||||
|
fn end_location_link(&mut self) {}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct HirFormatter<'a> {
|
pub struct HirFormatter<'a> {
|
||||||
pub db: &'a dyn HirDatabase,
|
pub db: &'a dyn HirDatabase,
|
||||||
fmt: &'a mut dyn fmt::Write,
|
fmt: &'a mut dyn HirWrite,
|
||||||
buf: String,
|
buf: String,
|
||||||
curr_size: usize,
|
curr_size: usize,
|
||||||
pub(crate) max_size: Option<usize>,
|
pub(crate) max_size: Option<usize>,
|
||||||
|
@ -45,6 +63,16 @@ pub struct HirFormatter<'a> {
|
||||||
display_target: DisplayTarget,
|
display_target: DisplayTarget,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl HirFormatter<'_> {
|
||||||
|
fn start_location_link(&mut self, location: ModuleDefId) {
|
||||||
|
self.fmt.start_location_link(location);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end_location_link(&mut self) {
|
||||||
|
self.fmt.end_location_link();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait HirDisplay {
|
pub trait HirDisplay {
|
||||||
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError>;
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError>;
|
||||||
|
|
||||||
|
@ -245,12 +273,9 @@ pub struct HirDisplayWrapper<'a, T> {
|
||||||
display_target: DisplayTarget,
|
display_target: DisplayTarget,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> fmt::Display for HirDisplayWrapper<'a, T>
|
impl<T: HirDisplay> HirDisplayWrapper<'_, T> {
|
||||||
where
|
pub fn write_to<F: HirWrite>(&self, f: &mut F) -> Result<(), HirDisplayError> {
|
||||||
T: HirDisplay,
|
self.t.hir_fmt(&mut HirFormatter {
|
||||||
{
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
match self.t.hir_fmt(&mut HirFormatter {
|
|
||||||
db: self.db,
|
db: self.db,
|
||||||
fmt: f,
|
fmt: f,
|
||||||
buf: String::with_capacity(20),
|
buf: String::with_capacity(20),
|
||||||
|
@ -258,7 +283,16 @@ where
|
||||||
max_size: self.max_size,
|
max_size: self.max_size,
|
||||||
omit_verbose_types: self.omit_verbose_types,
|
omit_verbose_types: self.omit_verbose_types,
|
||||||
display_target: self.display_target,
|
display_target: self.display_target,
|
||||||
}) {
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T> fmt::Display for HirDisplayWrapper<'a, T>
|
||||||
|
where
|
||||||
|
T: HirDisplay,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self.write_to(f) {
|
||||||
Ok(()) => Ok(()),
|
Ok(()) => Ok(()),
|
||||||
Err(HirDisplayError::FmtError) => Err(fmt::Error),
|
Err(HirDisplayError::FmtError) => Err(fmt::Error),
|
||||||
Err(HirDisplayError::DisplaySourceCodeError(_)) => {
|
Err(HirDisplayError::DisplaySourceCodeError(_)) => {
|
||||||
|
@ -530,6 +564,7 @@ impl HirDisplay for Ty {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TyKind::Adt(AdtId(def_id), parameters) => {
|
TyKind::Adt(AdtId(def_id), parameters) => {
|
||||||
|
f.start_location_link((*def_id).into());
|
||||||
match f.display_target {
|
match f.display_target {
|
||||||
DisplayTarget::Diagnostics | DisplayTarget::Test => {
|
DisplayTarget::Diagnostics | DisplayTarget::Test => {
|
||||||
let name = match *def_id {
|
let name = match *def_id {
|
||||||
|
@ -554,6 +589,7 @@ impl HirDisplay for Ty {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
f.end_location_link();
|
||||||
|
|
||||||
if parameters.len(Interner) > 0 {
|
if parameters.len(Interner) > 0 {
|
||||||
let parameters_to_write = if f.display_target.is_source_code()
|
let parameters_to_write = if f.display_target.is_source_code()
|
||||||
|
|
|
@ -114,12 +114,20 @@ pub use {
|
||||||
path::{ModPath, PathKind},
|
path::{ModPath, PathKind},
|
||||||
type_ref::{Mutability, TypeRef},
|
type_ref::{Mutability, TypeRef},
|
||||||
visibility::Visibility,
|
visibility::Visibility,
|
||||||
|
// FIXME: This is here since it is input of a method in `HirWrite`
|
||||||
|
// and things outside of hir need to implement that trait. We probably
|
||||||
|
// should move whole `hir_ty::display` to this crate so we will become
|
||||||
|
// able to use `ModuleDef` or `Definition` instead of `ModuleDefId`.
|
||||||
|
ModuleDefId,
|
||||||
},
|
},
|
||||||
hir_expand::{
|
hir_expand::{
|
||||||
name::{known, Name},
|
name::{known, Name},
|
||||||
ExpandResult, HirFileId, InFile, MacroFile, Origin,
|
ExpandResult, HirFileId, InFile, MacroFile, Origin,
|
||||||
},
|
},
|
||||||
hir_ty::{display::HirDisplay, PointerCast, Safety},
|
hir_ty::{
|
||||||
|
display::{HirDisplay, HirWrite},
|
||||||
|
PointerCast, Safety,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// These are negative re-exports: pub using these names is forbidden, they
|
// These are negative re-exports: pub using these names is forbidden, they
|
||||||
|
|
|
@ -1,15 +1,19 @@
|
||||||
use std::fmt;
|
use std::{
|
||||||
|
fmt::{self, Write},
|
||||||
|
mem::take,
|
||||||
|
};
|
||||||
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir::{known, HasVisibility, HirDisplay, Semantics};
|
use hir::{known, HasVisibility, HirDisplay, HirWrite, ModuleDef, ModuleDefId, Semantics};
|
||||||
use ide_db::{base_db::FileRange, famous_defs::FamousDefs, RootDatabase};
|
use ide_db::{base_db::FileRange, famous_defs::FamousDefs, RootDatabase};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
use stdx::never;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, AstNode},
|
ast::{self, AstNode},
|
||||||
match_ast, NodeOrToken, SyntaxNode, TextRange, TextSize,
|
match_ast, NodeOrToken, SyntaxNode, TextRange, TextSize,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::FileId;
|
use crate::{navigation_target::TryToNav, FileId};
|
||||||
|
|
||||||
mod closing_brace;
|
mod closing_brace;
|
||||||
mod implicit_static;
|
mod implicit_static;
|
||||||
|
@ -89,6 +93,7 @@ pub enum InlayTooltip {
|
||||||
HoverOffset(FileId, TextSize),
|
HoverOffset(FileId, TextSize),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
pub struct InlayHintLabel {
|
pub struct InlayHintLabel {
|
||||||
pub parts: Vec<InlayHintLabelPart>,
|
pub parts: Vec<InlayHintLabelPart>,
|
||||||
}
|
}
|
||||||
|
@ -172,6 +177,96 @@ impl fmt::Debug for InlayHintLabelPart {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct InlayHintLabelBuilder<'a> {
|
||||||
|
db: &'a RootDatabase,
|
||||||
|
result: InlayHintLabel,
|
||||||
|
last_part: String,
|
||||||
|
location: Option<FileRange>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Write for InlayHintLabelBuilder<'_> {
|
||||||
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||||
|
self.last_part.write_str(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HirWrite for InlayHintLabelBuilder<'_> {
|
||||||
|
fn start_location_link(&mut self, def: ModuleDefId) {
|
||||||
|
if self.location.is_some() {
|
||||||
|
never!("location link is already started");
|
||||||
|
}
|
||||||
|
self.make_new_part();
|
||||||
|
let Some(location) = ModuleDef::from(def).try_to_nav(self.db) else { return };
|
||||||
|
let location =
|
||||||
|
FileRange { file_id: location.file_id, range: location.focus_or_full_range() };
|
||||||
|
self.location = Some(location);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end_location_link(&mut self) {
|
||||||
|
self.make_new_part();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InlayHintLabelBuilder<'_> {
|
||||||
|
fn make_new_part(&mut self) {
|
||||||
|
self.result.parts.push(InlayHintLabelPart {
|
||||||
|
text: take(&mut self.last_part),
|
||||||
|
linked_location: self.location.take(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn finish(mut self) -> InlayHintLabel {
|
||||||
|
self.make_new_part();
|
||||||
|
self.result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn label_of_ty(
|
||||||
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
|
desc_pat: &impl AstNode,
|
||||||
|
config: &InlayHintsConfig,
|
||||||
|
ty: hir::Type,
|
||||||
|
) -> Option<InlayHintLabel> {
|
||||||
|
fn rec(
|
||||||
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
|
famous_defs: &FamousDefs<'_, '_>,
|
||||||
|
mut max_length: Option<usize>,
|
||||||
|
ty: hir::Type,
|
||||||
|
label_builder: &mut InlayHintLabelBuilder<'_>,
|
||||||
|
) {
|
||||||
|
let iter_item_type = hint_iterator(sema, &famous_defs, &ty);
|
||||||
|
match iter_item_type {
|
||||||
|
Some(ty) => {
|
||||||
|
const LABEL_START: &str = "impl Iterator<Item = ";
|
||||||
|
const LABEL_END: &str = ">";
|
||||||
|
|
||||||
|
max_length =
|
||||||
|
max_length.map(|len| len.saturating_sub(LABEL_START.len() + LABEL_END.len()));
|
||||||
|
|
||||||
|
label_builder.write_str(LABEL_START).unwrap();
|
||||||
|
rec(sema, famous_defs, max_length, ty, label_builder);
|
||||||
|
label_builder.write_str(LABEL_END).unwrap();
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
let _ = ty.display_truncated(sema.db, max_length).write_to(label_builder);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let krate = sema.scope(desc_pat.syntax())?.krate();
|
||||||
|
let famous_defs = FamousDefs(sema, krate);
|
||||||
|
let mut label_builder = InlayHintLabelBuilder {
|
||||||
|
db: sema.db,
|
||||||
|
last_part: String::new(),
|
||||||
|
location: None,
|
||||||
|
result: InlayHintLabel::default(),
|
||||||
|
};
|
||||||
|
rec(sema, &famous_defs, config.max_length, ty, &mut label_builder);
|
||||||
|
let r = label_builder.finish();
|
||||||
|
Some(r)
|
||||||
|
}
|
||||||
|
|
||||||
// Feature: Inlay Hints
|
// Feature: Inlay Hints
|
||||||
//
|
//
|
||||||
// rust-analyzer shows additional information inline with the source code.
|
// rust-analyzer shows additional information inline with the source code.
|
||||||
|
@ -224,7 +319,7 @@ pub(crate) fn inlay_hints(
|
||||||
|
|
||||||
fn hints(
|
fn hints(
|
||||||
hints: &mut Vec<InlayHint>,
|
hints: &mut Vec<InlayHint>,
|
||||||
famous_defs @ FamousDefs(sema, _): &FamousDefs<'_, '_>,
|
FamousDefs(sema, _): &FamousDefs<'_, '_>,
|
||||||
config: &InlayHintsConfig,
|
config: &InlayHintsConfig,
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
node: SyntaxNode,
|
node: SyntaxNode,
|
||||||
|
@ -233,14 +328,14 @@ fn hints(
|
||||||
match_ast! {
|
match_ast! {
|
||||||
match node {
|
match node {
|
||||||
ast::Expr(expr) => {
|
ast::Expr(expr) => {
|
||||||
chaining::hints(hints, sema, &famous_defs, config, file_id, &expr);
|
chaining::hints(hints, sema, config, file_id, &expr);
|
||||||
adjustment::hints(hints, sema, config, &expr);
|
adjustment::hints(hints, sema, config, &expr);
|
||||||
match expr {
|
match expr {
|
||||||
ast::Expr::CallExpr(it) => param_name::hints(hints, sema, config, ast::Expr::from(it)),
|
ast::Expr::CallExpr(it) => param_name::hints(hints, sema, config, ast::Expr::from(it)),
|
||||||
ast::Expr::MethodCallExpr(it) => {
|
ast::Expr::MethodCallExpr(it) => {
|
||||||
param_name::hints(hints, sema, config, ast::Expr::from(it))
|
param_name::hints(hints, sema, config, ast::Expr::from(it))
|
||||||
}
|
}
|
||||||
ast::Expr::ClosureExpr(it) => closure_ret::hints(hints, sema, &famous_defs, config, file_id, it),
|
ast::Expr::ClosureExpr(it) => closure_ret::hints(hints, sema, config, file_id, it),
|
||||||
// We could show reborrows for all expressions, but usually that is just noise to the user
|
// We could show reborrows for all expressions, but usually that is just noise to the user
|
||||||
// and the main point here is to show why "moving" a mutable reference doesn't necessarily move it
|
// and the main point here is to show why "moving" a mutable reference doesn't necessarily move it
|
||||||
// ast::Expr::PathExpr(_) => reborrow_hints(hints, sema, config, &expr),
|
// ast::Expr::PathExpr(_) => reborrow_hints(hints, sema, config, &expr),
|
||||||
|
@ -270,13 +365,12 @@ fn hints(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if the type is an Iterator from std::iter and replaces its hint with an `impl Iterator<Item = Ty>`.
|
/// Checks if the type is an Iterator from std::iter and returns its item type.
|
||||||
fn hint_iterator(
|
fn hint_iterator(
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
famous_defs: &FamousDefs<'_, '_>,
|
famous_defs: &FamousDefs<'_, '_>,
|
||||||
config: &InlayHintsConfig,
|
|
||||||
ty: &hir::Type,
|
ty: &hir::Type,
|
||||||
) -> Option<String> {
|
) -> Option<hir::Type> {
|
||||||
let db = sema.db;
|
let db = sema.db;
|
||||||
let strukt = ty.strip_references().as_adt()?;
|
let strukt = ty.strip_references().as_adt()?;
|
||||||
let krate = strukt.module(db).krate();
|
let krate = strukt.module(db).krate();
|
||||||
|
@ -299,21 +393,7 @@ fn hint_iterator(
|
||||||
_ => None,
|
_ => None,
|
||||||
})?;
|
})?;
|
||||||
if let Some(ty) = ty.normalize_trait_assoc_type(db, &[], assoc_type_item) {
|
if let Some(ty) = ty.normalize_trait_assoc_type(db, &[], assoc_type_item) {
|
||||||
const LABEL_START: &str = "impl Iterator<Item = ";
|
return Some(ty);
|
||||||
const LABEL_END: &str = ">";
|
|
||||||
|
|
||||||
let ty_display = hint_iterator(sema, famous_defs, config, &ty)
|
|
||||||
.map(|assoc_type_impl| assoc_type_impl.to_string())
|
|
||||||
.unwrap_or_else(|| {
|
|
||||||
ty.display_truncated(
|
|
||||||
db,
|
|
||||||
config
|
|
||||||
.max_length
|
|
||||||
.map(|len| len.saturating_sub(LABEL_START.len() + LABEL_END.len())),
|
|
||||||
)
|
|
||||||
.to_string()
|
|
||||||
});
|
|
||||||
return Some(format!("{}{}{}", LABEL_START, ty_display, LABEL_END));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
//! fn f(a: i32, b: i32) -> i32 { a + b }
|
//! fn f(a: i32, b: i32) -> i32 { a + b }
|
||||||
//! let _x /* i32 */= f(4, 4);
|
//! let _x /* i32 */= f(4, 4);
|
||||||
//! ```
|
//! ```
|
||||||
use hir::{HirDisplay, Semantics, TypeInfo};
|
use hir::{Semantics, TypeInfo};
|
||||||
use ide_db::{base_db::FileId, famous_defs::FamousDefs, RootDatabase};
|
use ide_db::{base_db::FileId, RootDatabase};
|
||||||
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
|
@ -13,10 +13,11 @@ use syntax::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
inlay_hints::{closure_has_block_body, hint_iterator},
|
inlay_hints::closure_has_block_body, InlayHint, InlayHintsConfig, InlayKind, InlayTooltip,
|
||||||
InlayHint, InlayHintsConfig, InlayKind, InlayTooltip,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::label_of_ty;
|
||||||
|
|
||||||
pub(super) fn hints(
|
pub(super) fn hints(
|
||||||
acc: &mut Vec<InlayHint>,
|
acc: &mut Vec<InlayHint>,
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
|
@ -36,22 +37,13 @@ pub(super) fn hints(
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let krate = sema.scope(desc_pat.syntax())?.krate();
|
let label = label_of_ty(sema, desc_pat, config, ty)?;
|
||||||
let famous_defs = FamousDefs(sema, krate);
|
|
||||||
let label = hint_iterator(sema, &famous_defs, config, &ty);
|
|
||||||
|
|
||||||
let label = match label {
|
if config.hide_named_constructor_hints
|
||||||
Some(label) => label,
|
&& is_named_constructor(sema, pat, &label.to_string()).is_some()
|
||||||
None => {
|
{
|
||||||
let ty_name = ty.display_truncated(sema.db, config.max_length).to_string();
|
return None;
|
||||||
if config.hide_named_constructor_hints
|
}
|
||||||
&& is_named_constructor(sema, pat, &ty_name).is_some()
|
|
||||||
{
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
ty_name
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
acc.push(InlayHint {
|
acc.push(InlayHint {
|
||||||
range: match pat.name() {
|
range: match pat.name() {
|
||||||
|
@ -59,7 +51,7 @@ pub(super) fn hints(
|
||||||
None => pat.syntax().text_range(),
|
None => pat.syntax().text_range(),
|
||||||
},
|
},
|
||||||
kind: InlayKind::TypeHint,
|
kind: InlayKind::TypeHint,
|
||||||
label: label.into(),
|
label,
|
||||||
tooltip: pat
|
tooltip: pat
|
||||||
.name()
|
.name()
|
||||||
.map(|it| it.syntax().text_range())
|
.map(|it| it.syntax().text_range())
|
||||||
|
@ -346,7 +338,31 @@ fn main(a: SliceIter<'_, Container>) {
|
||||||
range: 484..485,
|
range: 484..485,
|
||||||
kind: ChainingHint,
|
kind: ChainingHint,
|
||||||
label: [
|
label: [
|
||||||
"SliceIter<Container>",
|
"",
|
||||||
|
InlayHintLabelPart {
|
||||||
|
text: "SliceIter",
|
||||||
|
linked_location: Some(
|
||||||
|
FileRange {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
range: 289..298,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"<",
|
||||||
|
InlayHintLabelPart {
|
||||||
|
text: "Container",
|
||||||
|
linked_location: Some(
|
||||||
|
FileRange {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
range: 238..247,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
">",
|
||||||
],
|
],
|
||||||
tooltip: Some(
|
tooltip: Some(
|
||||||
HoverRanged(
|
HoverRanged(
|
||||||
|
|
|
@ -1,19 +1,18 @@
|
||||||
//! Implementation of "chaining" inlay hints.
|
//! Implementation of "chaining" inlay hints.
|
||||||
use hir::{HirDisplay, Semantics};
|
use hir::Semantics;
|
||||||
use ide_db::{famous_defs::FamousDefs, RootDatabase};
|
use ide_db::RootDatabase;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, AstNode},
|
ast::{self, AstNode},
|
||||||
Direction, NodeOrToken, SyntaxKind, T,
|
Direction, NodeOrToken, SyntaxKind, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{FileId, InlayHint, InlayHintsConfig, InlayKind, InlayTooltip};
|
||||||
inlay_hints::hint_iterator, FileId, InlayHint, InlayHintsConfig, InlayKind, InlayTooltip,
|
|
||||||
};
|
use super::label_of_ty;
|
||||||
|
|
||||||
pub(super) fn hints(
|
pub(super) fn hints(
|
||||||
acc: &mut Vec<InlayHint>,
|
acc: &mut Vec<InlayHint>,
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
famous_defs: &FamousDefs<'_, '_>,
|
|
||||||
config: &InlayHintsConfig,
|
config: &InlayHintsConfig,
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
expr: &ast::Expr,
|
expr: &ast::Expr,
|
||||||
|
@ -62,9 +61,7 @@ pub(super) fn hints(
|
||||||
acc.push(InlayHint {
|
acc.push(InlayHint {
|
||||||
range: expr.syntax().text_range(),
|
range: expr.syntax().text_range(),
|
||||||
kind: InlayKind::ChainingHint,
|
kind: InlayKind::ChainingHint,
|
||||||
label: hint_iterator(sema, &famous_defs, config, &ty)
|
label: label_of_ty(sema, desc_expr, config, ty)?,
|
||||||
.unwrap_or_else(|| ty.display_truncated(sema.db, config.max_length).to_string())
|
|
||||||
.into(),
|
|
||||||
tooltip: Some(InlayTooltip::HoverRanged(file_id, expr.syntax().text_range())),
|
tooltip: Some(InlayTooltip::HoverRanged(file_id, expr.syntax().text_range())),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -110,7 +107,19 @@ fn main() {
|
||||||
range: 147..172,
|
range: 147..172,
|
||||||
kind: ChainingHint,
|
kind: ChainingHint,
|
||||||
label: [
|
label: [
|
||||||
"B",
|
"",
|
||||||
|
InlayHintLabelPart {
|
||||||
|
text: "B",
|
||||||
|
linked_location: Some(
|
||||||
|
FileRange {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
range: 63..64,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"",
|
||||||
],
|
],
|
||||||
tooltip: Some(
|
tooltip: Some(
|
||||||
HoverRanged(
|
HoverRanged(
|
||||||
|
@ -125,7 +134,19 @@ fn main() {
|
||||||
range: 147..154,
|
range: 147..154,
|
||||||
kind: ChainingHint,
|
kind: ChainingHint,
|
||||||
label: [
|
label: [
|
||||||
"A",
|
"",
|
||||||
|
InlayHintLabelPart {
|
||||||
|
text: "A",
|
||||||
|
linked_location: Some(
|
||||||
|
FileRange {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
range: 7..8,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"",
|
||||||
],
|
],
|
||||||
tooltip: Some(
|
tooltip: Some(
|
||||||
HoverRanged(
|
HoverRanged(
|
||||||
|
@ -185,7 +206,19 @@ fn main() {
|
||||||
range: 143..190,
|
range: 143..190,
|
||||||
kind: ChainingHint,
|
kind: ChainingHint,
|
||||||
label: [
|
label: [
|
||||||
"C",
|
"",
|
||||||
|
InlayHintLabelPart {
|
||||||
|
text: "C",
|
||||||
|
linked_location: Some(
|
||||||
|
FileRange {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
range: 51..52,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"",
|
||||||
],
|
],
|
||||||
tooltip: Some(
|
tooltip: Some(
|
||||||
HoverRanged(
|
HoverRanged(
|
||||||
|
@ -200,7 +233,19 @@ fn main() {
|
||||||
range: 143..179,
|
range: 143..179,
|
||||||
kind: ChainingHint,
|
kind: ChainingHint,
|
||||||
label: [
|
label: [
|
||||||
"B",
|
"",
|
||||||
|
InlayHintLabelPart {
|
||||||
|
text: "B",
|
||||||
|
linked_location: Some(
|
||||||
|
FileRange {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
range: 29..30,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"",
|
||||||
],
|
],
|
||||||
tooltip: Some(
|
tooltip: Some(
|
||||||
HoverRanged(
|
HoverRanged(
|
||||||
|
@ -245,7 +290,31 @@ fn main() {
|
||||||
range: 246..283,
|
range: 246..283,
|
||||||
kind: ChainingHint,
|
kind: ChainingHint,
|
||||||
label: [
|
label: [
|
||||||
"B<X<i32, bool>>",
|
"",
|
||||||
|
InlayHintLabelPart {
|
||||||
|
text: "B",
|
||||||
|
linked_location: Some(
|
||||||
|
FileRange {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
range: 23..24,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"<",
|
||||||
|
InlayHintLabelPart {
|
||||||
|
text: "X",
|
||||||
|
linked_location: Some(
|
||||||
|
FileRange {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
range: 55..56,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"<i32, bool>>",
|
||||||
],
|
],
|
||||||
tooltip: Some(
|
tooltip: Some(
|
||||||
HoverRanged(
|
HoverRanged(
|
||||||
|
@ -260,7 +329,31 @@ fn main() {
|
||||||
range: 246..265,
|
range: 246..265,
|
||||||
kind: ChainingHint,
|
kind: ChainingHint,
|
||||||
label: [
|
label: [
|
||||||
"A<X<i32, bool>>",
|
"",
|
||||||
|
InlayHintLabelPart {
|
||||||
|
text: "A",
|
||||||
|
linked_location: Some(
|
||||||
|
FileRange {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
range: 7..8,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"<",
|
||||||
|
InlayHintLabelPart {
|
||||||
|
text: "X",
|
||||||
|
linked_location: Some(
|
||||||
|
FileRange {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
range: 55..56,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"<i32, bool>>",
|
||||||
],
|
],
|
||||||
tooltip: Some(
|
tooltip: Some(
|
||||||
HoverRanged(
|
HoverRanged(
|
||||||
|
@ -352,7 +445,19 @@ fn main() {
|
||||||
range: 174..189,
|
range: 174..189,
|
||||||
kind: ChainingHint,
|
kind: ChainingHint,
|
||||||
label: [
|
label: [
|
||||||
"&mut MyIter",
|
"&mut ",
|
||||||
|
InlayHintLabelPart {
|
||||||
|
text: "MyIter",
|
||||||
|
linked_location: Some(
|
||||||
|
FileRange {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
range: 24..30,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"",
|
||||||
],
|
],
|
||||||
tooltip: Some(
|
tooltip: Some(
|
||||||
HoverRanged(
|
HoverRanged(
|
||||||
|
@ -396,7 +501,19 @@ fn main() {
|
||||||
range: 124..130,
|
range: 124..130,
|
||||||
kind: TypeHint,
|
kind: TypeHint,
|
||||||
label: [
|
label: [
|
||||||
"Struct",
|
"",
|
||||||
|
InlayHintLabelPart {
|
||||||
|
text: "Struct",
|
||||||
|
linked_location: Some(
|
||||||
|
FileRange {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
range: 7..13,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"",
|
||||||
],
|
],
|
||||||
tooltip: Some(
|
tooltip: Some(
|
||||||
HoverRanged(
|
HoverRanged(
|
||||||
|
@ -411,7 +528,19 @@ fn main() {
|
||||||
range: 145..185,
|
range: 145..185,
|
||||||
kind: ChainingHint,
|
kind: ChainingHint,
|
||||||
label: [
|
label: [
|
||||||
"Struct",
|
"",
|
||||||
|
InlayHintLabelPart {
|
||||||
|
text: "Struct",
|
||||||
|
linked_location: Some(
|
||||||
|
FileRange {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
range: 7..13,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"",
|
||||||
],
|
],
|
||||||
tooltip: Some(
|
tooltip: Some(
|
||||||
HoverRanged(
|
HoverRanged(
|
||||||
|
@ -426,7 +555,19 @@ fn main() {
|
||||||
range: 145..168,
|
range: 145..168,
|
||||||
kind: ChainingHint,
|
kind: ChainingHint,
|
||||||
label: [
|
label: [
|
||||||
"Struct",
|
"",
|
||||||
|
InlayHintLabelPart {
|
||||||
|
text: "Struct",
|
||||||
|
linked_location: Some(
|
||||||
|
FileRange {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
range: 7..13,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"",
|
||||||
],
|
],
|
||||||
tooltip: Some(
|
tooltip: Some(
|
||||||
HoverRanged(
|
HoverRanged(
|
||||||
|
|
|
@ -1,17 +1,18 @@
|
||||||
//! Implementation of "closure return type" inlay hints.
|
//! Implementation of "closure return type" inlay hints.
|
||||||
use hir::{HirDisplay, Semantics};
|
use hir::Semantics;
|
||||||
use ide_db::{base_db::FileId, famous_defs::FamousDefs, RootDatabase};
|
use ide_db::{base_db::FileId, RootDatabase};
|
||||||
use syntax::ast::{self, AstNode};
|
use syntax::ast::{self, AstNode};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
inlay_hints::{closure_has_block_body, hint_iterator},
|
inlay_hints::closure_has_block_body, ClosureReturnTypeHints, InlayHint, InlayHintsConfig,
|
||||||
ClosureReturnTypeHints, InlayHint, InlayHintsConfig, InlayKind, InlayTooltip,
|
InlayKind, InlayTooltip,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::label_of_ty;
|
||||||
|
|
||||||
pub(super) fn hints(
|
pub(super) fn hints(
|
||||||
acc: &mut Vec<InlayHint>,
|
acc: &mut Vec<InlayHint>,
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
famous_defs: &FamousDefs<'_, '_>,
|
|
||||||
config: &InlayHintsConfig,
|
config: &InlayHintsConfig,
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
closure: ast::ClosureExpr,
|
closure: ast::ClosureExpr,
|
||||||
|
@ -42,9 +43,7 @@ pub(super) fn hints(
|
||||||
acc.push(InlayHint {
|
acc.push(InlayHint {
|
||||||
range: param_list.syntax().text_range(),
|
range: param_list.syntax().text_range(),
|
||||||
kind: InlayKind::ClosureReturnTypeHint,
|
kind: InlayKind::ClosureReturnTypeHint,
|
||||||
label: hint_iterator(sema, &famous_defs, config, &ty)
|
label: label_of_ty(sema, ¶m_list, config, ty)?,
|
||||||
.unwrap_or_else(|| ty.display_truncated(sema.db, config.max_length).to_string())
|
|
||||||
.into(),
|
|
||||||
tooltip: Some(InlayTooltip::HoverRanged(file_id, param_list.syntax().text_range())),
|
tooltip: Some(InlayTooltip::HoverRanged(file_id, param_list.syntax().text_range())),
|
||||||
});
|
});
|
||||||
Some(())
|
Some(())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue