From ca46c68b041480b27d1573a9cfc352f85d87ca28 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 12 May 2022 12:55:25 +0200 Subject: [PATCH] internal: Remove redundant offset data in annotations --- crates/ide/src/annotations.rs | 202 +++++++++---------------- crates/rust-analyzer/src/from_proto.rs | 10 +- crates/rust-analyzer/src/to_proto.rs | 24 ++- 3 files changed, 84 insertions(+), 152 deletions(-) diff --git a/crates/ide/src/annotations.rs b/crates/ide/src/annotations.rs index 676d4d51df9..210c5c7facd 100644 --- a/crates/ide/src/annotations.rs +++ b/crates/ide/src/annotations.rs @@ -30,8 +30,8 @@ pub struct Annotation { #[derive(Debug)] pub enum AnnotationKind { Runnable(Runnable), - HasImpls { position: FilePosition, data: Option> }, - HasReferences { position: FilePosition, data: Option> }, + HasImpls { file_id: FileId, data: Option> }, + HasReferences { file_id: FileId, data: Option> }, } pub struct AnnotationConfig { @@ -83,10 +83,7 @@ pub(crate) fn annotations( .for_each(|range| { annotations.push(Annotation { range, - kind: AnnotationKind::HasReferences { - position: FilePosition { file_id, offset: range.start() }, - data: None, - }, + kind: AnnotationKind::HasReferences { file_id, data: None }, }) }) } @@ -107,27 +104,19 @@ pub(crate) fn annotations( _ => None, }; - let (range, offset) = match range { - Some(range) => (range, range.start()), + let range = match range { + Some(range) => range, None => return, }; if config.annotate_impls && !matches!(def, Definition::Const(_)) { - annotations.push(Annotation { - range, - kind: AnnotationKind::HasImpls { - position: FilePosition { file_id, offset }, - data: None, - }, - }); + annotations + .push(Annotation { range, kind: AnnotationKind::HasImpls { file_id, data: None } }); } if config.annotate_references { annotations.push(Annotation { range, - kind: AnnotationKind::HasReferences { - position: FilePosition { file_id, offset }, - data: None, - }, + kind: AnnotationKind::HasReferences { file_id, data: None }, }); } @@ -149,10 +138,7 @@ fn name_range( annotations.extend(find_all_methods(db, file_id).into_iter().map( |FileRange { file_id, range }| Annotation { range, - kind: AnnotationKind::HasReferences { - position: FilePosition { file_id, offset: range.start() }, - data: None, - }, + kind: AnnotationKind::HasReferences { file_id, data: None }, }, )); } @@ -161,12 +147,19 @@ fn name_range( } pub(crate) fn resolve_annotation(db: &RootDatabase, mut annotation: Annotation) -> Annotation { - match &mut annotation.kind { - AnnotationKind::HasImpls { position, data } => { - *data = goto_implementation(db, *position).map(|range| range.info); + match annotation.kind { + AnnotationKind::HasImpls { file_id, ref mut data } => { + *data = + goto_implementation(db, FilePosition { file_id, offset: annotation.range.start() }) + .map(|range| range.info); } - AnnotationKind::HasReferences { position, data } => { - *data = find_all_refs(&Semantics::new(db), *position, None).map(|result| { + AnnotationKind::HasReferences { file_id, ref mut data } => { + *data = find_all_refs( + &Semantics::new(db), + FilePosition { file_id, offset: annotation.range.start() }, + None, + ) + .map(|result| { result .into_iter() .flat_map(|res| res.references) @@ -254,12 +247,9 @@ fn main() { Annotation { range: 6..10, kind: HasReferences { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 6, - }, + file_id: FileId( + 0, + ), data: Some( [ FileRange { @@ -275,12 +265,9 @@ fn main() { Annotation { range: 30..36, kind: HasReferences { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 30, - }, + file_id: FileId( + 0, + ), data: Some( [], ), @@ -289,12 +276,9 @@ fn main() { Annotation { range: 53..57, kind: HasReferences { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 53, - }, + file_id: FileId( + 0, + ), data: Some( [], ), @@ -339,12 +323,9 @@ fn main() { Annotation { range: 7..11, kind: HasImpls { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 7, - }, + file_id: FileId( + 0, + ), data: Some( [], ), @@ -353,12 +334,9 @@ fn main() { Annotation { range: 7..11, kind: HasReferences { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 7, - }, + file_id: FileId( + 0, + ), data: Some( [ FileRange { @@ -374,12 +352,9 @@ fn main() { Annotation { range: 17..21, kind: HasReferences { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 17, - }, + file_id: FileId( + 0, + ), data: Some( [], ), @@ -428,12 +403,9 @@ fn main() { Annotation { range: 7..11, kind: HasImpls { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 7, - }, + file_id: FileId( + 0, + ), data: Some( [ NavigationTarget { @@ -452,12 +424,9 @@ fn main() { Annotation { range: 7..11, kind: HasReferences { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 7, - }, + file_id: FileId( + 0, + ), data: Some( [ FileRange { @@ -479,12 +448,9 @@ fn main() { Annotation { range: 20..31, kind: HasImpls { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 20, - }, + file_id: FileId( + 0, + ), data: Some( [ NavigationTarget { @@ -503,12 +469,9 @@ fn main() { Annotation { range: 20..31, kind: HasReferences { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 20, - }, + file_id: FileId( + 0, + ), data: Some( [ FileRange { @@ -524,12 +487,9 @@ fn main() { Annotation { range: 69..73, kind: HasReferences { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 69, - }, + file_id: FileId( + 0, + ), data: Some( [], ), @@ -570,12 +530,9 @@ fn main() {} Annotation { range: 3..7, kind: HasReferences { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 3, - }, + file_id: FileId( + 0, + ), data: Some( [], ), @@ -624,12 +581,9 @@ fn main() { Annotation { range: 7..11, kind: HasImpls { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 7, - }, + file_id: FileId( + 0, + ), data: Some( [ NavigationTarget { @@ -648,12 +602,9 @@ fn main() { Annotation { range: 7..11, kind: HasReferences { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 7, - }, + file_id: FileId( + 0, + ), data: Some( [ FileRange { @@ -675,12 +626,9 @@ fn main() { Annotation { range: 33..44, kind: HasReferences { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 33, - }, + file_id: FileId( + 0, + ), data: Some( [ FileRange { @@ -696,12 +644,9 @@ fn main() { Annotation { range: 61..65, kind: HasReferences { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 61, - }, + file_id: FileId( + 0, + ), data: Some( [], ), @@ -795,12 +740,9 @@ fn my_cool_test() {} Annotation { range: 3..7, kind: HasReferences { - position: FilePosition { - file_id: FileId( - 0, - ), - offset: 3, - }, + file_id: FileId( + 0, + ), data: Some( [], ), diff --git a/crates/rust-analyzer/src/from_proto.rs b/crates/rust-analyzer/src/from_proto.rs index 1c67ecc22c8..ffc69a419a0 100644 --- a/crates/rust-analyzer/src/from_proto.rs +++ b/crates/rust-analyzer/src/from_proto.rs @@ -101,10 +101,7 @@ pub(crate) fn annotation( Ok(Annotation { range: text_range(&line_index, code_lens.range)?, - kind: AnnotationKind::HasImpls { - position: file_position(snap, params.text_document_position_params)?, - data: None, - }, + kind: AnnotationKind::HasImpls { file_id, data: None }, }) } lsp_ext::CodeLensResolveData::References(params) => { @@ -113,10 +110,7 @@ pub(crate) fn annotation( Ok(Annotation { range: text_range(&line_index, code_lens.range)?, - kind: AnnotationKind::HasReferences { - position: file_position(snap, params)?, - data: None, - }, + kind: AnnotationKind::HasReferences { file_id, data: None }, }) } } diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index ea4ca5d82e5..ff348959add 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -1103,19 +1103,17 @@ pub(crate) fn code_lens( }) } } - AnnotationKind::HasImpls { position: file_position, data } => { + AnnotationKind::HasImpls { file_id, data } => { if !client_commands_config.show_reference { return Ok(()); } - let line_index = snap.file_line_index(file_position.file_id)?; + let line_index = snap.file_line_index(file_id)?; let annotation_range = range(&line_index, annotation.range); - let url = url(snap, file_position.file_id); - - let position = position(&line_index, file_position.offset); + let url = url(snap, file_id); let id = lsp_types::TextDocumentIdentifier { uri: url.clone() }; - let doc_pos = lsp_types::TextDocumentPositionParams::new(id, position); + let doc_pos = lsp_types::TextDocumentPositionParams::new(id, annotation_range.start); let goto_params = lsp_types::request::GotoImplementationParams { text_document_position_params: doc_pos, @@ -1138,7 +1136,7 @@ pub(crate) fn code_lens( command::show_references( implementation_title(locations.len()), &url, - position, + annotation_range.start, locations, ) }); @@ -1149,19 +1147,17 @@ pub(crate) fn code_lens( data: Some(to_value(lsp_ext::CodeLensResolveData::Impls(goto_params)).unwrap()), }) } - AnnotationKind::HasReferences { position: file_position, data } => { + AnnotationKind::HasReferences { file_id, data } => { if !client_commands_config.show_reference { return Ok(()); } - let line_index = snap.file_line_index(file_position.file_id)?; + let line_index = snap.file_line_index(file_id)?; let annotation_range = range(&line_index, annotation.range); - let url = url(snap, file_position.file_id); - - let position = position(&line_index, file_position.offset); + let url = url(snap, file_id); let id = lsp_types::TextDocumentIdentifier { uri: url.clone() }; - let doc_pos = lsp_types::TextDocumentPositionParams::new(id, position); + let doc_pos = lsp_types::TextDocumentPositionParams::new(id, annotation_range.start); let command = data.map(|ranges| { let locations: Vec = @@ -1170,7 +1166,7 @@ pub(crate) fn code_lens( command::show_references( reference_title(locations.len()), &url, - position, + annotation_range.start, locations, ) });