Skip to content

Commit 41ee5ca

Browse files
committed
Auto merge of rust-lang#14559 - Veykril:version-code-lens, r=Veykril
internal: Skip code lens resolution for mismatched document versions Closes rust-lang/rust-analyzer#12718
2 parents 2b62016 + 0286e46 commit 41ee5ca

File tree

5 files changed

+43
-10
lines changed

5 files changed

+43
-10
lines changed

crates/rust-analyzer/src/from_proto.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,17 @@ pub(crate) fn assist_kind(kind: lsp_types::CodeActionKind) -> Option<AssistKind>
9898
pub(crate) fn annotation(
9999
snap: &GlobalStateSnapshot,
100100
code_lens: lsp_types::CodeLens,
101-
) -> Result<Annotation> {
101+
) -> Result<Option<Annotation>> {
102102
let data =
103103
code_lens.data.ok_or_else(|| invalid_params_error("code lens without data".to_string()))?;
104104
let resolve = from_json::<lsp_ext::CodeLensResolveData>("CodeLensResolveData", &data)?;
105105

106-
match resolve {
107-
lsp_ext::CodeLensResolveData::Impls(params) => {
106+
match resolve.kind {
107+
lsp_ext::CodeLensResolveDataKind::Impls(params) => {
108+
if matches!(snap.url_file_version(&params.text_document_position_params.text_document.uri), Some(version) if version == resolve.version)
109+
{
110+
return Ok(None);
111+
}
108112
let pos @ FilePosition { file_id, .. } =
109113
file_position(snap, params.text_document_position_params)?;
110114
let line_index = snap.file_line_index(file_id)?;
@@ -114,7 +118,11 @@ pub(crate) fn annotation(
114118
kind: AnnotationKind::HasImpls { pos, data: None },
115119
})
116120
}
117-
lsp_ext::CodeLensResolveData::References(params) => {
121+
lsp_ext::CodeLensResolveDataKind::References(params) => {
122+
if matches!(snap.url_file_version(&params.text_document.uri), Some(version) if version == resolve.version)
123+
{
124+
return Ok(None);
125+
}
118126
let pos @ FilePosition { file_id, .. } = file_position(snap, params)?;
119127
let line_index = snap.file_line_index(file_id)?;
120128

@@ -123,5 +131,5 @@ pub(crate) fn annotation(
123131
kind: AnnotationKind::HasReferences { pos, data: None },
124132
})
125133
}
126-
}
134+
}.map(Some)
127135
}

crates/rust-analyzer/src/handlers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@ pub(crate) fn handle_code_lens_resolve(
12701270
snap: GlobalStateSnapshot,
12711271
code_lens: CodeLens,
12721272
) -> Result<CodeLens> {
1273-
let annotation = from_proto::annotation(&snap, code_lens.clone())?;
1273+
let Some(annotation) = from_proto::annotation(&snap, code_lens.clone())? else { return Ok(code_lens) };
12741274
let annotation = snap.analysis.resolve_annotation(annotation)?;
12751275

12761276
let mut acc = Vec::new();

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,14 @@ pub struct OpenCargoTomlParams {
495495
/// Information about CodeLens, that is to be resolved.
496496
#[derive(Debug, Serialize, Deserialize)]
497497
#[serde(rename_all = "camelCase")]
498-
pub(crate) enum CodeLensResolveData {
498+
pub struct CodeLensResolveData {
499+
pub version: i32,
500+
pub kind: CodeLensResolveDataKind,
501+
}
502+
503+
#[derive(Debug, Serialize, Deserialize)]
504+
#[serde(rename_all = "camelCase")]
505+
pub enum CodeLensResolveDataKind {
499506
Impls(lsp_types::request::GotoImplementationParams),
500507
References(lsp_types::TextDocumentPositionParams),
501508
}

crates/rust-analyzer/src/to_proto.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,16 @@ pub(crate) fn code_lens(
12571257
acc.push(lsp_types::CodeLens {
12581258
range: annotation_range,
12591259
command,
1260-
data: Some(to_value(lsp_ext::CodeLensResolveData::Impls(goto_params)).unwrap()),
1260+
data: (|| {
1261+
let version = snap.url_file_version(&url)?;
1262+
Some(
1263+
to_value(lsp_ext::CodeLensResolveData {
1264+
version,
1265+
kind: lsp_ext::CodeLensResolveDataKind::Impls(goto_params),
1266+
})
1267+
.unwrap(),
1268+
)
1269+
})(),
12611270
})
12621271
}
12631272
AnnotationKind::HasReferences { pos: file_range, data } => {
@@ -1287,7 +1296,16 @@ pub(crate) fn code_lens(
12871296
acc.push(lsp_types::CodeLens {
12881297
range: annotation_range,
12891298
command,
1290-
data: Some(to_value(lsp_ext::CodeLensResolveData::References(doc_pos)).unwrap()),
1299+
data: (|| {
1300+
let version = snap.url_file_version(&url)?;
1301+
Some(
1302+
to_value(lsp_ext::CodeLensResolveData {
1303+
version,
1304+
kind: lsp_ext::CodeLensResolveDataKind::References(doc_pos),
1305+
})
1306+
.unwrap(),
1307+
)
1308+
})(),
12911309
})
12921310
}
12931311
}

docs/dev/lsp-extensions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!---
2-
lsp_ext.rs hash: 7269e4cfab906e10
2+
lsp_ext.rs hash: be2f663a78beb7bd
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)