Files
openidea/sdk/rust/openidea-plugin/src/inlay_hints.rs
Pedro Pessoa 56796fb2c6 feat(plugins): let plugins show inlay hints in any language (#1937)
A new optional inlay-hints-provider export (worlds plugin-inlay-hints / inlay-hints), declared as the inlay-hints provider kind for any language (API 1.3). The host asks the language server and the plugins together for the visible range and returns both in position order; with no server hints the plugins' hints still show. Kinds map to the Inlay Hints settings; labels, tooltips and counts are capped.

Co-authored-by: yzPeedro <[email protected]>
2026-11-07 19:25:10 +08:00

85 lines
2.0 KiB
Rust

use crate::types::{Position, Range};
pub use crate::providers::Document;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum InlayKind {
Type,
Parameter,
Other,
}
impl InlayKind {
pub fn as_str(self) -> &'static str {
match self {
Self::Type => "type",
Self::Parameter => "parameter",
Self::Other => "other",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InlayHint {
pub position: Position,
pub label: String,
pub kind: InlayKind,
pub tooltip: Option<String>,
pub padding_left: bool,
pub padding_right: bool,
}
impl InlayHint {
pub fn new(position: Position, label: impl Into<String>) -> Self {
Self {
position,
label: label.into(),
kind: InlayKind::Other,
tooltip: None,
padding_left: false,
padding_right: false,
}
}
pub fn kind(mut self, kind: InlayKind) -> Self {
self.kind = kind;
self
}
pub fn tooltip(mut self, tooltip: impl Into<String>) -> Self {
self.tooltip = Some(tooltip.into());
self
}
pub fn padding_left(mut self) -> Self {
self.padding_left = true;
self
}
pub fn padding_right(mut self) -> Self {
self.padding_right = true;
self
}
}
pub trait InlayHints {
fn inlay_hints(&mut self, doc: &Document, range: Range) -> Vec<InlayHint>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_hint_is_built_step_by_step() {
let at = Position { line: 3, character: 14 };
let hint = InlayHint::new(at, "GET /orders").kind(InlayKind::Type).tooltip("orders.index").padding_left();
assert_eq!(hint.position, at);
assert_eq!(hint.kind.as_str(), "type");
assert_eq!(hint.tooltip.as_deref(), Some("orders.index"));
assert!(hint.padding_left && !hint.padding_right);
assert_eq!(InlayHint::new(at, "x").kind, InlayKind::Other);
}
}