Use smallvec for inlay-hint parts

This commit is contained in:
Lukas Wirth 2023-01-13 15:03:37 +01:00
parent a119352ada
commit aafb0f1f8d
4 changed files with 17 additions and 5 deletions

1
Cargo.lock generated
View file

@ -645,6 +645,7 @@ dependencies = [
"profile", "profile",
"pulldown-cmark", "pulldown-cmark",
"pulldown-cmark-to-cmark", "pulldown-cmark-to-cmark",
"smallvec",
"stdx", "stdx",
"syntax", "syntax",
"test-utils", "test-utils",

View file

@ -20,6 +20,7 @@ pulldown-cmark-to-cmark = "10.0.4"
pulldown-cmark = { version = "0.9.1", default-features = false } pulldown-cmark = { version = "0.9.1", default-features = false }
url = "2.3.1" url = "2.3.1"
dot = "0.1.4" dot = "0.1.4"
smallvec = "1.10.0"
stdx = { path = "../stdx", version = "0.0.0" } stdx = { path = "../stdx", version = "0.0.0" }
syntax = { path = "../syntax", version = "0.0.0" } syntax = { path = "../syntax", version = "0.0.0" }

View file

@ -7,6 +7,7 @@ use either::Either;
use hir::{known, HasVisibility, HirDisplay, HirWrite, ModuleDef, ModuleDefId, 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 smallvec::{smallvec, SmallVec};
use stdx::never; use stdx::never;
use syntax::{ use syntax::{
ast::{self, AstNode}, ast::{self, AstNode},
@ -83,7 +84,7 @@ pub enum AdjustmentHintsMode {
PreferPostfix, PreferPostfix,
} }
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum InlayKind { pub enum InlayKind {
BindingModeHint, BindingModeHint,
ChainingHint, ChainingHint,
@ -102,9 +103,15 @@ pub enum InlayKind {
#[derive(Debug)] #[derive(Debug)]
pub struct InlayHint { pub struct InlayHint {
/// The text range this inlay hint applies to.
pub range: TextRange, pub range: TextRange,
/// The kind of this inlay hint. This is used to determine side and padding of the hint for
/// rendering purposes.
pub kind: InlayKind, pub kind: InlayKind,
/// The actual label to show in the inlay hint.
pub label: InlayHintLabel, pub label: InlayHintLabel,
/// The tooltip to show when hovering over the inlay hint, this may invoke other actions like
/// hover requests to show.
pub tooltip: Option<InlayTooltip>, pub tooltip: Option<InlayTooltip>,
} }
@ -117,7 +124,7 @@ pub enum InlayTooltip {
#[derive(Default)] #[derive(Default)]
pub struct InlayHintLabel { pub struct InlayHintLabel {
pub parts: Vec<InlayHintLabelPart>, pub parts: SmallVec<[InlayHintLabelPart; 1]>,
} }
impl InlayHintLabel { impl InlayHintLabel {
@ -145,13 +152,13 @@ impl InlayHintLabel {
impl From<String> for InlayHintLabel { impl From<String> for InlayHintLabel {
fn from(s: String) -> Self { fn from(s: String) -> Self {
Self { parts: vec![InlayHintLabelPart { text: s, linked_location: None }] } Self { parts: smallvec![InlayHintLabelPart { text: s, linked_location: None }] }
} }
} }
impl From<&str> for InlayHintLabel { impl From<&str> for InlayHintLabel {
fn from(s: &str) -> Self { fn from(s: &str) -> Self {
Self { parts: vec![InlayHintLabelPart { text: s.into(), linked_location: None }] } Self { parts: smallvec![InlayHintLabelPart { text: s.into(), linked_location: None }] }
} }
} }

View file

@ -5,6 +5,7 @@
//! ``` //! ```
use hir::{HirDisplay, Semantics}; use hir::{HirDisplay, Semantics};
use ide_db::{base_db::FileRange, RootDatabase}; use ide_db::{base_db::FileRange, RootDatabase};
use smallvec::smallvec;
use syntax::{ use syntax::{
ast::{self, AstNode, HasName}, ast::{self, AstNode, HasName},
match_ast, SyntaxKind, SyntaxNode, T, match_ast, SyntaxKind, SyntaxNode, T,
@ -116,7 +117,9 @@ pub(super) fn hints(
acc.push(InlayHint { acc.push(InlayHint {
range: closing_token.text_range(), range: closing_token.text_range(),
kind: InlayKind::ClosingBraceHint, kind: InlayKind::ClosingBraceHint,
label: InlayHintLabel { parts: vec![InlayHintLabelPart { text: label, linked_location }] }, label: InlayHintLabel {
parts: smallvec![InlayHintLabelPart { text: label, linked_location }],
},
tooltip: None, // provided by label part location tooltip: None, // provided by label part location
}); });