Switch to LSP inlay hints
This commit is contained in:
@@ -112,11 +112,10 @@ pub fn server_capabilities(config: &Config) -> ServerCapabilities {
|
||||
.into(),
|
||||
),
|
||||
moniker_provider: None,
|
||||
inlay_hint_provider: None,
|
||||
inlay_hint_provider: Some(OneOf::Left(true)),
|
||||
experimental: Some(json!({
|
||||
"externalDocs": true,
|
||||
"hoverRange": true,
|
||||
"inlayHints": true,
|
||||
"joinLines": true,
|
||||
"matchingBrace": true,
|
||||
"moveItem": true,
|
||||
|
||||
@@ -19,11 +19,11 @@ use lsp_types::{
|
||||
CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem,
|
||||
CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams,
|
||||
CodeLens, CompletionItem, Diagnostic, DiagnosticTag, DocumentFormattingParams, FoldingRange,
|
||||
FoldingRangeParams, HoverContents, Location, LocationLink, NumberOrString, Position,
|
||||
PrepareRenameResponse, Range, RenameParams, SemanticTokensDeltaParams,
|
||||
SemanticTokensFullDeltaResult, SemanticTokensParams, SemanticTokensRangeParams,
|
||||
SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation, SymbolTag,
|
||||
TextDocumentIdentifier, Url, WorkspaceEdit,
|
||||
FoldingRangeParams, HoverContents, InlayHint, InlayHintParams, Location, LocationLink,
|
||||
NumberOrString, Position, PrepareRenameResponse, Range, RenameParams,
|
||||
SemanticTokensDeltaParams, SemanticTokensFullDeltaResult, SemanticTokensParams,
|
||||
SemanticTokensRangeParams, SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation,
|
||||
SymbolTag, TextDocumentIdentifier, Url, WorkspaceEdit,
|
||||
};
|
||||
use project_model::{ManifestPath, ProjectWorkspace, TargetKind};
|
||||
use serde_json::json;
|
||||
@@ -38,10 +38,7 @@ use crate::{
|
||||
from_proto,
|
||||
global_state::{GlobalState, GlobalStateSnapshot},
|
||||
line_index::LineEndings,
|
||||
lsp_ext::{
|
||||
self, InlayHint, InlayHintsParams, PositionOrRange, ViewCrateGraphParams,
|
||||
WorkspaceSymbolParams,
|
||||
},
|
||||
lsp_ext::{self, PositionOrRange, ViewCrateGraphParams, WorkspaceSymbolParams},
|
||||
lsp_utils::{all_edits_are_disjoint, invalid_params_error},
|
||||
to_proto, LspError, Result,
|
||||
};
|
||||
@@ -1322,29 +1319,25 @@ pub(crate) fn publish_diagnostics(
|
||||
|
||||
pub(crate) fn handle_inlay_hints(
|
||||
snap: GlobalStateSnapshot,
|
||||
params: InlayHintsParams,
|
||||
) -> Result<Vec<InlayHint>> {
|
||||
params: InlayHintParams,
|
||||
) -> Result<Option<Vec<InlayHint>>> {
|
||||
let _p = profile::span("handle_inlay_hints");
|
||||
let document_uri = ¶ms.text_document.uri;
|
||||
let file_id = from_proto::file_id(&snap, document_uri)?;
|
||||
let line_index = snap.file_line_index(file_id)?;
|
||||
let range = params
|
||||
.range
|
||||
.map(|range| {
|
||||
from_proto::file_range(
|
||||
&snap,
|
||||
TextDocumentIdentifier::new(document_uri.to_owned()),
|
||||
range,
|
||||
)
|
||||
})
|
||||
.transpose()?;
|
||||
let range = from_proto::file_range(
|
||||
&snap,
|
||||
TextDocumentIdentifier::new(document_uri.to_owned()),
|
||||
params.range,
|
||||
)?;
|
||||
let inlay_hints_config = snap.config.inlay_hints();
|
||||
Ok(snap
|
||||
.analysis
|
||||
.inlay_hints(&inlay_hints_config, file_id, range)?
|
||||
.into_iter()
|
||||
.map(|it| to_proto::inlay_hint(inlay_hints_config.render_colons, &line_index, it))
|
||||
.collect())
|
||||
Ok(Some(
|
||||
snap.analysis
|
||||
.inlay_hints(&inlay_hints_config, file_id, Some(range))?
|
||||
.into_iter()
|
||||
.map(|it| to_proto::inlay_hint(inlay_hints_config.render_colons, &line_index, it))
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn handle_call_hierarchy_prepare(
|
||||
|
||||
@@ -236,14 +236,6 @@ pub struct TestInfo {
|
||||
pub runnable: Runnable,
|
||||
}
|
||||
|
||||
pub enum InlayHints {}
|
||||
|
||||
impl Request for InlayHints {
|
||||
type Params = InlayHintsParams;
|
||||
type Result = Vec<InlayHint>;
|
||||
const METHOD: &'static str = "experimental/inlayHints";
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct InlayHintsParams {
|
||||
@@ -251,44 +243,6 @@ pub struct InlayHintsParams {
|
||||
pub range: Option<lsp_types::Range>,
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Debug, Copy, Clone, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct InlayHintKind(u8);
|
||||
|
||||
impl InlayHintKind {
|
||||
pub const TYPE: InlayHintKind = InlayHintKind(1);
|
||||
pub const PARAMETER: InlayHintKind = InlayHintKind(2);
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct InlayHint {
|
||||
pub label: InlayHintLabel,
|
||||
pub position: Position,
|
||||
pub kind: Option<InlayHintKind>,
|
||||
pub tooltip: Option<String>,
|
||||
pub padding_left: Option<bool>,
|
||||
pub padding_right: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum InlayHintLabel {
|
||||
String(String),
|
||||
Parts(Vec<InlayHintLabelPart>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct InlayHintLabelPart {
|
||||
pub value: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub tooltip: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub location: Option<lsp_types::LocationLink>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub command: Option<lsp_types::Command>,
|
||||
}
|
||||
pub enum Ssr {}
|
||||
|
||||
impl Request for Ssr {
|
||||
|
||||
@@ -597,7 +597,6 @@ impl GlobalState {
|
||||
.on::<lsp_ext::ParentModule>(handlers::handle_parent_module)
|
||||
.on::<lsp_ext::Runnables>(handlers::handle_runnables)
|
||||
.on::<lsp_ext::RelatedTests>(handlers::handle_related_tests)
|
||||
.on::<lsp_ext::InlayHints>(handlers::handle_inlay_hints)
|
||||
.on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)
|
||||
.on::<lsp_ext::CodeActionResolveRequest>(handlers::handle_code_action_resolve)
|
||||
.on::<lsp_ext::HoverRequest>(handlers::handle_hover)
|
||||
@@ -611,6 +610,7 @@ impl GlobalState {
|
||||
.on::<lsp_types::request::GotoDeclaration>(handlers::handle_goto_declaration)
|
||||
.on::<lsp_types::request::GotoImplementation>(handlers::handle_goto_implementation)
|
||||
.on::<lsp_types::request::GotoTypeDefinition>(handlers::handle_goto_type_definition)
|
||||
.on::<lsp_types::request::InlayHintRequest>(handlers::handle_inlay_hints)
|
||||
.on::<lsp_types::request::Completion>(handlers::handle_completion)
|
||||
.on::<lsp_types::request::ResolveCompletionItem>(handlers::handle_completion_resolve)
|
||||
.on::<lsp_types::request::CodeLensRequest>(handlers::handle_code_lens)
|
||||
|
||||
@@ -418,14 +418,8 @@ pub(crate) fn inlay_hint(
|
||||
render_colons: bool,
|
||||
line_index: &LineIndex,
|
||||
inlay_hint: InlayHint,
|
||||
) -> lsp_ext::InlayHint {
|
||||
lsp_ext::InlayHint {
|
||||
label: lsp_ext::InlayHintLabel::String(match inlay_hint.kind {
|
||||
InlayKind::ParameterHint if render_colons => format!("{}:", inlay_hint.label),
|
||||
InlayKind::TypeHint if render_colons => format!(": {}", inlay_hint.label),
|
||||
InlayKind::ClosureReturnTypeHint => format!(" -> {}", inlay_hint.label),
|
||||
_ => inlay_hint.label.to_string(),
|
||||
}),
|
||||
) -> lsp_types::InlayHint {
|
||||
lsp_types::InlayHint {
|
||||
position: match inlay_hint.kind {
|
||||
// before annotated thing
|
||||
InlayKind::ParameterHint | InlayKind::ImplicitReborrow => {
|
||||
@@ -438,10 +432,16 @@ pub(crate) fn inlay_hint(
|
||||
| InlayKind::GenericParamListHint
|
||||
| InlayKind::LifetimeHint => position(line_index, inlay_hint.range.end()),
|
||||
},
|
||||
label: lsp_types::InlayHintLabel::String(match inlay_hint.kind {
|
||||
InlayKind::ParameterHint if render_colons => format!("{}:", inlay_hint.label),
|
||||
InlayKind::TypeHint if render_colons => format!(": {}", inlay_hint.label),
|
||||
InlayKind::ClosureReturnTypeHint => format!(" -> {}", inlay_hint.label),
|
||||
_ => inlay_hint.label.to_string(),
|
||||
}),
|
||||
kind: match inlay_hint.kind {
|
||||
InlayKind::ParameterHint => Some(lsp_ext::InlayHintKind::PARAMETER),
|
||||
InlayKind::ParameterHint => Some(lsp_types::InlayHintKind::PARAMETER),
|
||||
InlayKind::ClosureReturnTypeHint | InlayKind::TypeHint | InlayKind::ChainingHint => {
|
||||
Some(lsp_ext::InlayHintKind::TYPE)
|
||||
Some(lsp_types::InlayHintKind::TYPE)
|
||||
}
|
||||
InlayKind::GenericParamListHint
|
||||
| InlayKind::LifetimeHint
|
||||
@@ -465,6 +465,7 @@ pub(crate) fn inlay_hint(
|
||||
InlayKind::GenericParamListHint => false,
|
||||
InlayKind::ImplicitReborrow => false,
|
||||
}),
|
||||
text_edits: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user