internal: Skip code lens resolution for mismatched document versions

This commit is contained in:
Lukas Wirth 2023-04-13 10:32:48 +02:00
parent dd5c3c30b6
commit 0286e46e5f
5 changed files with 43 additions and 10 deletions

View file

@ -98,13 +98,17 @@ pub(crate) fn assist_kind(kind: lsp_types::CodeActionKind) -> Option<AssistKind>
pub(crate) fn annotation(
snap: &GlobalStateSnapshot,
code_lens: lsp_types::CodeLens,
) -> Result<Annotation> {
) -> Result<Option<Annotation>> {
let data =
code_lens.data.ok_or_else(|| invalid_params_error("code lens without data".to_string()))?;
let resolve = from_json::<lsp_ext::CodeLensResolveData>("CodeLensResolveData", &data)?;
match resolve {
lsp_ext::CodeLensResolveData::Impls(params) => {
match resolve.kind {
lsp_ext::CodeLensResolveDataKind::Impls(params) => {
if matches!(snap.url_file_version(&params.text_document_position_params.text_document.uri), Some(version) if version == resolve.version)
{
return Ok(None);
}
let pos @ FilePosition { file_id, .. } =
file_position(snap, params.text_document_position_params)?;
let line_index = snap.file_line_index(file_id)?;
@ -114,7 +118,11 @@ pub(crate) fn annotation(
kind: AnnotationKind::HasImpls { pos, data: None },
})
}
lsp_ext::CodeLensResolveData::References(params) => {
lsp_ext::CodeLensResolveDataKind::References(params) => {
if matches!(snap.url_file_version(&params.text_document.uri), Some(version) if version == resolve.version)
{
return Ok(None);
}
let pos @ FilePosition { file_id, .. } = file_position(snap, params)?;
let line_index = snap.file_line_index(file_id)?;
@ -123,5 +131,5 @@ pub(crate) fn annotation(
kind: AnnotationKind::HasReferences { pos, data: None },
})
}
}
}.map(Some)
}

View file

@ -1270,7 +1270,7 @@ pub(crate) fn handle_code_lens_resolve(
snap: GlobalStateSnapshot,
code_lens: CodeLens,
) -> Result<CodeLens> {
let annotation = from_proto::annotation(&snap, code_lens.clone())?;
let Some(annotation) = from_proto::annotation(&snap, code_lens.clone())? else { return Ok(code_lens) };
let annotation = snap.analysis.resolve_annotation(annotation)?;
let mut acc = Vec::new();

View file

@ -495,7 +495,14 @@ pub struct OpenCargoTomlParams {
/// Information about CodeLens, that is to be resolved.
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) enum CodeLensResolveData {
pub struct CodeLensResolveData {
pub version: i32,
pub kind: CodeLensResolveDataKind,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum CodeLensResolveDataKind {
Impls(lsp_types::request::GotoImplementationParams),
References(lsp_types::TextDocumentPositionParams),
}

View file

@ -1257,7 +1257,16 @@ pub(crate) fn code_lens(
acc.push(lsp_types::CodeLens {
range: annotation_range,
command,
data: Some(to_value(lsp_ext::CodeLensResolveData::Impls(goto_params)).unwrap()),
data: (|| {
let version = snap.url_file_version(&url)?;
Some(
to_value(lsp_ext::CodeLensResolveData {
version,
kind: lsp_ext::CodeLensResolveDataKind::Impls(goto_params),
})
.unwrap(),
)
})(),
})
}
AnnotationKind::HasReferences { pos: file_range, data } => {
@ -1287,7 +1296,16 @@ pub(crate) fn code_lens(
acc.push(lsp_types::CodeLens {
range: annotation_range,
command,
data: Some(to_value(lsp_ext::CodeLensResolveData::References(doc_pos)).unwrap()),
data: (|| {
let version = snap.url_file_version(&url)?;
Some(
to_value(lsp_ext::CodeLensResolveData {
version,
kind: lsp_ext::CodeLensResolveDataKind::References(doc_pos),
})
.unwrap(),
)
})(),
})
}
}

View file

@ -1,5 +1,5 @@
<!---
lsp_ext.rs hash: 7269e4cfab906e10
lsp_ext.rs hash: be2f663a78beb7bd
If you need to change the above hash to make the test pass, please check if you
need to adjust this doc as well and ping this issue: