Skip to content

Commit de05f5f

Browse files
bors[bot]lnicola
andauthored
Merge #11935
11935: feat: Switch to LSP inlay hints r=lnicola a=lnicola Co-authored-by: Laurențiu Nicola <[email protected]>
2 parents 9093941 + bfa2a08 commit de05f5f

File tree

13 files changed

+87
-234
lines changed

13 files changed

+87
-234
lines changed

crates/rust-analyzer/src/caps.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,10 @@ pub fn server_capabilities(config: &Config) -> ServerCapabilities {
112112
.into(),
113113
),
114114
moniker_provider: None,
115-
inlay_hint_provider: None,
115+
inlay_hint_provider: Some(OneOf::Left(true)),
116116
experimental: Some(json!({
117117
"externalDocs": true,
118118
"hoverRange": true,
119-
"inlayHints": true,
120119
"joinLines": true,
121120
"matchingBrace": true,
122121
"moveItem": true,

crates/rust-analyzer/src/handlers.rs

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ use lsp_types::{
1919
CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem,
2020
CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams,
2121
CodeLens, CompletionItem, Diagnostic, DiagnosticTag, DocumentFormattingParams, FoldingRange,
22-
FoldingRangeParams, HoverContents, Location, LocationLink, NumberOrString, Position,
23-
PrepareRenameResponse, Range, RenameParams, SemanticTokensDeltaParams,
24-
SemanticTokensFullDeltaResult, SemanticTokensParams, SemanticTokensRangeParams,
25-
SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation, SymbolTag,
26-
TextDocumentIdentifier, Url, WorkspaceEdit,
22+
FoldingRangeParams, HoverContents, InlayHint, InlayHintParams, Location, LocationLink,
23+
NumberOrString, Position, PrepareRenameResponse, Range, RenameParams,
24+
SemanticTokensDeltaParams, SemanticTokensFullDeltaResult, SemanticTokensParams,
25+
SemanticTokensRangeParams, SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation,
26+
SymbolTag, TextDocumentIdentifier, Url, WorkspaceEdit,
2727
};
2828
use project_model::{ManifestPath, ProjectWorkspace, TargetKind};
2929
use serde_json::json;
@@ -38,10 +38,7 @@ use crate::{
3838
from_proto,
3939
global_state::{GlobalState, GlobalStateSnapshot},
4040
line_index::LineEndings,
41-
lsp_ext::{
42-
self, InlayHint, InlayHintsParams, PositionOrRange, ViewCrateGraphParams,
43-
WorkspaceSymbolParams,
44-
},
41+
lsp_ext::{self, PositionOrRange, ViewCrateGraphParams, WorkspaceSymbolParams},
4542
lsp_utils::{all_edits_are_disjoint, invalid_params_error},
4643
to_proto, LspError, Result,
4744
};
@@ -1322,29 +1319,25 @@ pub(crate) fn publish_diagnostics(
13221319

13231320
pub(crate) fn handle_inlay_hints(
13241321
snap: GlobalStateSnapshot,
1325-
params: InlayHintsParams,
1326-
) -> Result<Vec<InlayHint>> {
1322+
params: InlayHintParams,
1323+
) -> Result<Option<Vec<InlayHint>>> {
13271324
let _p = profile::span("handle_inlay_hints");
13281325
let document_uri = &params.text_document.uri;
13291326
let file_id = from_proto::file_id(&snap, document_uri)?;
13301327
let line_index = snap.file_line_index(file_id)?;
1331-
let range = params
1332-
.range
1333-
.map(|range| {
1334-
from_proto::file_range(
1335-
&snap,
1336-
TextDocumentIdentifier::new(document_uri.to_owned()),
1337-
range,
1338-
)
1339-
})
1340-
.transpose()?;
1328+
let range = from_proto::file_range(
1329+
&snap,
1330+
TextDocumentIdentifier::new(document_uri.to_owned()),
1331+
params.range,
1332+
)?;
13411333
let inlay_hints_config = snap.config.inlay_hints();
1342-
Ok(snap
1343-
.analysis
1344-
.inlay_hints(&inlay_hints_config, file_id, range)?
1345-
.into_iter()
1346-
.map(|it| to_proto::inlay_hint(inlay_hints_config.render_colons, &line_index, it))
1347-
.collect())
1334+
Ok(Some(
1335+
snap.analysis
1336+
.inlay_hints(&inlay_hints_config, file_id, Some(range))?
1337+
.into_iter()
1338+
.map(|it| to_proto::inlay_hint(inlay_hints_config.render_colons, &line_index, it))
1339+
.collect(),
1340+
))
13481341
}
13491342

13501343
pub(crate) fn handle_call_hierarchy_prepare(

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -236,59 +236,13 @@ pub struct TestInfo {
236236
pub runnable: Runnable,
237237
}
238238

239-
pub enum InlayHints {}
240-
241-
impl Request for InlayHints {
242-
type Params = InlayHintsParams;
243-
type Result = Vec<InlayHint>;
244-
const METHOD: &'static str = "experimental/inlayHints";
245-
}
246-
247239
#[derive(Serialize, Deserialize, Debug)]
248240
#[serde(rename_all = "camelCase")]
249241
pub struct InlayHintsParams {
250242
pub text_document: TextDocumentIdentifier,
251243
pub range: Option<lsp_types::Range>,
252244
}
253245

254-
#[derive(Eq, PartialEq, Debug, Copy, Clone, Serialize, Deserialize)]
255-
#[serde(transparent)]
256-
pub struct InlayHintKind(u8);
257-
258-
impl InlayHintKind {
259-
pub const TYPE: InlayHintKind = InlayHintKind(1);
260-
pub const PARAMETER: InlayHintKind = InlayHintKind(2);
261-
}
262-
263-
#[derive(Debug, Deserialize, Serialize)]
264-
#[serde(rename_all = "camelCase")]
265-
pub struct InlayHint {
266-
pub label: InlayHintLabel,
267-
pub position: Position,
268-
pub kind: Option<InlayHintKind>,
269-
pub tooltip: Option<String>,
270-
pub padding_left: Option<bool>,
271-
pub padding_right: Option<bool>,
272-
}
273-
274-
#[derive(Debug, Deserialize, Serialize)]
275-
#[serde(untagged)]
276-
pub enum InlayHintLabel {
277-
String(String),
278-
Parts(Vec<InlayHintLabelPart>),
279-
}
280-
281-
#[derive(Debug, Deserialize, Serialize)]
282-
#[serde(rename_all = "camelCase")]
283-
pub struct InlayHintLabelPart {
284-
pub value: String,
285-
#[serde(skip_serializing_if = "Option::is_none")]
286-
pub tooltip: Option<String>,
287-
#[serde(skip_serializing_if = "Option::is_none")]
288-
pub location: Option<lsp_types::LocationLink>,
289-
#[serde(skip_serializing_if = "Option::is_none")]
290-
pub command: Option<lsp_types::Command>,
291-
}
292246
pub enum Ssr {}
293247

294248
impl Request for Ssr {

crates/rust-analyzer/src/main_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,6 @@ impl GlobalState {
597597
.on::<lsp_ext::ParentModule>(handlers::handle_parent_module)
598598
.on::<lsp_ext::Runnables>(handlers::handle_runnables)
599599
.on::<lsp_ext::RelatedTests>(handlers::handle_related_tests)
600-
.on::<lsp_ext::InlayHints>(handlers::handle_inlay_hints)
601600
.on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)
602601
.on::<lsp_ext::CodeActionResolveRequest>(handlers::handle_code_action_resolve)
603602
.on::<lsp_ext::HoverRequest>(handlers::handle_hover)
@@ -611,6 +610,7 @@ impl GlobalState {
611610
.on::<lsp_types::request::GotoDeclaration>(handlers::handle_goto_declaration)
612611
.on::<lsp_types::request::GotoImplementation>(handlers::handle_goto_implementation)
613612
.on::<lsp_types::request::GotoTypeDefinition>(handlers::handle_goto_type_definition)
613+
.on::<lsp_types::request::InlayHintRequest>(handlers::handle_inlay_hints)
614614
.on::<lsp_types::request::Completion>(handlers::handle_completion)
615615
.on::<lsp_types::request::ResolveCompletionItem>(handlers::handle_completion_resolve)
616616
.on::<lsp_types::request::CodeLensRequest>(handlers::handle_code_lens)

crates/rust-analyzer/src/to_proto.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -418,14 +418,8 @@ pub(crate) fn inlay_hint(
418418
render_colons: bool,
419419
line_index: &LineIndex,
420420
inlay_hint: InlayHint,
421-
) -> lsp_ext::InlayHint {
422-
lsp_ext::InlayHint {
423-
label: lsp_ext::InlayHintLabel::String(match inlay_hint.kind {
424-
InlayKind::ParameterHint if render_colons => format!("{}:", inlay_hint.label),
425-
InlayKind::TypeHint if render_colons => format!(": {}", inlay_hint.label),
426-
InlayKind::ClosureReturnTypeHint => format!(" -> {}", inlay_hint.label),
427-
_ => inlay_hint.label.to_string(),
428-
}),
421+
) -> lsp_types::InlayHint {
422+
lsp_types::InlayHint {
429423
position: match inlay_hint.kind {
430424
// before annotated thing
431425
InlayKind::ParameterHint | InlayKind::ImplicitReborrow => {
@@ -438,10 +432,16 @@ pub(crate) fn inlay_hint(
438432
| InlayKind::GenericParamListHint
439433
| InlayKind::LifetimeHint => position(line_index, inlay_hint.range.end()),
440434
},
435+
label: lsp_types::InlayHintLabel::String(match inlay_hint.kind {
436+
InlayKind::ParameterHint if render_colons => format!("{}:", inlay_hint.label),
437+
InlayKind::TypeHint if render_colons => format!(": {}", inlay_hint.label),
438+
InlayKind::ClosureReturnTypeHint => format!(" -> {}", inlay_hint.label),
439+
_ => inlay_hint.label.to_string(),
440+
}),
441441
kind: match inlay_hint.kind {
442-
InlayKind::ParameterHint => Some(lsp_ext::InlayHintKind::PARAMETER),
442+
InlayKind::ParameterHint => Some(lsp_types::InlayHintKind::PARAMETER),
443443
InlayKind::ClosureReturnTypeHint | InlayKind::TypeHint | InlayKind::ChainingHint => {
444-
Some(lsp_ext::InlayHintKind::TYPE)
444+
Some(lsp_types::InlayHintKind::TYPE)
445445
}
446446
InlayKind::GenericParamListHint
447447
| InlayKind::LifetimeHint
@@ -465,6 +465,7 @@ pub(crate) fn inlay_hint(
465465
InlayKind::GenericParamListHint => false,
466466
InlayKind::ImplicitReborrow => false,
467467
}),
468+
text_edits: None,
468469
}
469470
}
470471

docs/dev/lsp-extensions.md

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!---
2-
lsp_ext.rs hash: a61de7db4504a4d1
2+
lsp_ext.rs hash: 326ad62235135223
33
44
If you need to change the above hash to make the test pass, please check if you
55
need to adjust this doc as well and ping this issue:
@@ -571,36 +571,6 @@ interface ExpandedMacro {
571571

572572
Expands macro call at a given position.
573573

574-
## Inlay Hints
575-
576-
**Method:** `experimental/inlayHints`
577-
578-
This request is sent from client to server to render "inlay hints" -- virtual text inserted into editor to show things like inferred types.
579-
Generally, the client should re-query inlay hints after every modification.
580-
Until it gets upstreamed, this follows the VS Code API.
581-
Upstream issues: https://github.com/microsoft/language-server-protocol/issues/956 , https://github.com/rust-analyzer/rust-analyzer/issues/2797
582-
583-
**Request:**
584-
585-
```typescript
586-
interface InlayHintsParams {
587-
textDocument: TextDocumentIdentifier,
588-
}
589-
```
590-
591-
**Response:** `InlayHint[]`
592-
593-
```typescript
594-
interface InlayHint {
595-
position: Position;
596-
label: string | InlayHintLabelPart[];
597-
tooltip?: string | MarkdownString | undefined;
598-
kind?: InlayHintKind;
599-
paddingLeft?: boolean;
600-
paddingRight?: boolean;
601-
}
602-
```
603-
604574
## Hover Actions
605575

606576
**Experimental Client Capability:** `{ "hoverActions": boolean }`

editors/code/package-lock.json

Lines changed: 39 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)