Skip to content

Commit 398a71a

Browse files
committed
Auto merge of #13697 - jonas-schievink:version-inlay-hint-resolve-data, r=jonas-schievink
internal: Version the inlay hint resolve data cc #13657 cc #13372 cc #13170 This will make us log an error and return the unmodified inlay hints when the client attempts to resolve inlay hints in a file that has since been modified.
2 parents e69fb5e + 32f59cf commit 398a71a

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

crates/rust-analyzer/src/from_proto.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ pub(crate) fn file_range(
6767
text_document_identifier: lsp_types::TextDocumentIdentifier,
6868
range: lsp_types::Range,
6969
) -> Result<FileRange> {
70-
let file_id = file_id(snap, &text_document_identifier.uri)?;
70+
file_range_uri(snap, &text_document_identifier.uri, range)
71+
}
72+
73+
pub(crate) fn file_range_uri(
74+
snap: &GlobalStateSnapshot,
75+
document: &lsp_types::Url,
76+
range: lsp_types::Range,
77+
) -> Result<FileRange> {
78+
let file_id = file_id(snap, document)?;
7179
let line_index = snap.file_line_index(file_id)?;
7280
let range = text_range(&line_index, range)?;
7381
Ok(FileRange { file_id, range })

crates/rust-analyzer/src/handlers.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use project_model::{ManifestPath, ProjectWorkspace, TargetKind};
2929
use serde_json::json;
3030
use stdx::{format_to, never};
3131
use syntax::{algo, ast, AstNode, TextRange, TextSize};
32+
use tracing::error;
3233
use vfs::AbsPathBuf;
3334

3435
use crate::{
@@ -1372,9 +1373,26 @@ pub(crate) fn handle_inlay_hints_resolve(
13721373

13731374
let resolve_data: lsp_ext::InlayHintResolveData = serde_json::from_value(data)?;
13741375

1375-
let file_range = from_proto::file_range(
1376+
match snap.url_file_version(&resolve_data.text_document.uri) {
1377+
Some(version) if version == resolve_data.text_document.version => {}
1378+
Some(version) => {
1379+
error!(
1380+
"attempted inlayHints/resolve of '{}' at version {} while server version is {}",
1381+
resolve_data.text_document.uri, resolve_data.text_document.version, version,
1382+
);
1383+
return Ok(hint);
1384+
}
1385+
None => {
1386+
error!(
1387+
"attempted inlayHints/resolve of unknown file '{}' at version {}",
1388+
resolve_data.text_document.uri, resolve_data.text_document.version,
1389+
);
1390+
return Ok(hint);
1391+
}
1392+
}
1393+
let file_range = from_proto::file_range_uri(
13761394
&snap,
1377-
resolve_data.text_document,
1395+
&resolve_data.text_document.uri,
13781396
match resolve_data.position {
13791397
PositionOrRange::Position(pos) => Range::new(pos, pos),
13801398
PositionOrRange::Range(range) => range,

crates/rust-analyzer/src/lsp_ext.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
use std::{collections::HashMap, path::PathBuf};
44

55
use lsp_types::request::Request;
6-
use lsp_types::PositionEncodingKind;
76
use lsp_types::{
87
notification::Notification, CodeActionKind, DocumentOnTypeFormattingParams,
98
PartialResultParams, Position, Range, TextDocumentIdentifier, WorkDoneProgressParams,
109
};
10+
use lsp_types::{PositionEncodingKind, VersionedTextDocumentIdentifier};
1111
use serde::{Deserialize, Serialize};
1212

1313
pub enum AnalyzerStatus {}
@@ -550,7 +550,7 @@ pub struct CompletionResolveData {
550550

551551
#[derive(Debug, Serialize, Deserialize)]
552552
pub struct InlayHintResolveData {
553-
pub text_document: TextDocumentIdentifier,
553+
pub text_document: VersionedTextDocumentIdentifier,
554554
pub position: PositionOrRange,
555555
}
556556

crates/rust-analyzer/src/to_proto.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,10 @@ pub(crate) fn inlay_hint(
492492
let uri = url(snap, file_id);
493493
let line_index = snap.file_line_index(file_id).ok()?;
494494

495-
let text_document = lsp_types::TextDocumentIdentifier { uri };
495+
let text_document = lsp_types::VersionedTextDocumentIdentifier {
496+
version: snap.url_file_version(&uri)?,
497+
uri,
498+
};
496499
to_value(lsp_ext::InlayHintResolveData {
497500
text_document,
498501
position: lsp_ext::PositionOrRange::Position(position(&line_index, offset)),
@@ -501,7 +504,10 @@ pub(crate) fn inlay_hint(
501504
}
502505
Some(ide::InlayTooltip::HoverRanged(file_id, text_range)) => {
503506
let uri = url(snap, file_id);
504-
let text_document = lsp_types::TextDocumentIdentifier { uri };
507+
let text_document = lsp_types::VersionedTextDocumentIdentifier {
508+
version: snap.url_file_version(&uri)?,
509+
uri,
510+
};
505511
let line_index = snap.file_line_index(file_id).ok()?;
506512
to_value(lsp_ext::InlayHintResolveData {
507513
text_document,

docs/dev/lsp-extensions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!---
2-
lsp_ext.rs hash: 62068e53ac202dc8
2+
lsp_ext.rs hash: 61fe425627f9baaa
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:

0 commit comments

Comments
 (0)