mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-21 11:00:26 +00:00
internal: Remove redundant offset data in annotations
This commit is contained in:
parent
d121307977
commit
ca46c68b04
3 changed files with 84 additions and 152 deletions
|
@ -30,8 +30,8 @@ pub struct Annotation {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum AnnotationKind {
|
pub enum AnnotationKind {
|
||||||
Runnable(Runnable),
|
Runnable(Runnable),
|
||||||
HasImpls { position: FilePosition, data: Option<Vec<NavigationTarget>> },
|
HasImpls { file_id: FileId, data: Option<Vec<NavigationTarget>> },
|
||||||
HasReferences { position: FilePosition, data: Option<Vec<FileRange>> },
|
HasReferences { file_id: FileId, data: Option<Vec<FileRange>> },
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AnnotationConfig {
|
pub struct AnnotationConfig {
|
||||||
|
@ -83,10 +83,7 @@ pub(crate) fn annotations(
|
||||||
.for_each(|range| {
|
.for_each(|range| {
|
||||||
annotations.push(Annotation {
|
annotations.push(Annotation {
|
||||||
range,
|
range,
|
||||||
kind: AnnotationKind::HasReferences {
|
kind: AnnotationKind::HasReferences { file_id, data: None },
|
||||||
position: FilePosition { file_id, offset: range.start() },
|
|
||||||
data: None,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -107,27 +104,19 @@ pub(crate) fn annotations(
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let (range, offset) = match range {
|
let range = match range {
|
||||||
Some(range) => (range, range.start()),
|
Some(range) => range,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
if config.annotate_impls && !matches!(def, Definition::Const(_)) {
|
if config.annotate_impls && !matches!(def, Definition::Const(_)) {
|
||||||
annotations.push(Annotation {
|
annotations
|
||||||
range,
|
.push(Annotation { range, kind: AnnotationKind::HasImpls { file_id, data: None } });
|
||||||
kind: AnnotationKind::HasImpls {
|
|
||||||
position: FilePosition { file_id, offset },
|
|
||||||
data: None,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
if config.annotate_references {
|
if config.annotate_references {
|
||||||
annotations.push(Annotation {
|
annotations.push(Annotation {
|
||||||
range,
|
range,
|
||||||
kind: AnnotationKind::HasReferences {
|
kind: AnnotationKind::HasReferences { file_id, data: None },
|
||||||
position: FilePosition { file_id, offset },
|
|
||||||
data: None,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,10 +138,7 @@ pub(crate) fn annotations(
|
||||||
annotations.extend(find_all_methods(db, file_id).into_iter().map(
|
annotations.extend(find_all_methods(db, file_id).into_iter().map(
|
||||||
|FileRange { file_id, range }| Annotation {
|
|FileRange { file_id, range }| Annotation {
|
||||||
range,
|
range,
|
||||||
kind: AnnotationKind::HasReferences {
|
kind: AnnotationKind::HasReferences { file_id, data: None },
|
||||||
position: FilePosition { file_id, offset: range.start() },
|
|
||||||
data: None,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -161,12 +147,19 @@ pub(crate) fn annotations(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn resolve_annotation(db: &RootDatabase, mut annotation: Annotation) -> Annotation {
|
pub(crate) fn resolve_annotation(db: &RootDatabase, mut annotation: Annotation) -> Annotation {
|
||||||
match &mut annotation.kind {
|
match annotation.kind {
|
||||||
AnnotationKind::HasImpls { position, data } => {
|
AnnotationKind::HasImpls { file_id, ref mut data } => {
|
||||||
*data = goto_implementation(db, *position).map(|range| range.info);
|
*data =
|
||||||
|
goto_implementation(db, FilePosition { file_id, offset: annotation.range.start() })
|
||||||
|
.map(|range| range.info);
|
||||||
}
|
}
|
||||||
AnnotationKind::HasReferences { position, data } => {
|
AnnotationKind::HasReferences { file_id, ref mut data } => {
|
||||||
*data = find_all_refs(&Semantics::new(db), *position, None).map(|result| {
|
*data = find_all_refs(
|
||||||
|
&Semantics::new(db),
|
||||||
|
FilePosition { file_id, offset: annotation.range.start() },
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.map(|result| {
|
||||||
result
|
result
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flat_map(|res| res.references)
|
.flat_map(|res| res.references)
|
||||||
|
@ -254,12 +247,9 @@ fn main() {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 6..10,
|
range: 6..10,
|
||||||
kind: HasReferences {
|
kind: HasReferences {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 6,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[
|
[
|
||||||
FileRange {
|
FileRange {
|
||||||
|
@ -275,12 +265,9 @@ fn main() {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 30..36,
|
range: 30..36,
|
||||||
kind: HasReferences {
|
kind: HasReferences {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 30,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[],
|
[],
|
||||||
),
|
),
|
||||||
|
@ -289,12 +276,9 @@ fn main() {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 53..57,
|
range: 53..57,
|
||||||
kind: HasReferences {
|
kind: HasReferences {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 53,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[],
|
[],
|
||||||
),
|
),
|
||||||
|
@ -339,12 +323,9 @@ fn main() {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 7..11,
|
range: 7..11,
|
||||||
kind: HasImpls {
|
kind: HasImpls {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 7,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[],
|
[],
|
||||||
),
|
),
|
||||||
|
@ -353,12 +334,9 @@ fn main() {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 7..11,
|
range: 7..11,
|
||||||
kind: HasReferences {
|
kind: HasReferences {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 7,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[
|
[
|
||||||
FileRange {
|
FileRange {
|
||||||
|
@ -374,12 +352,9 @@ fn main() {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 17..21,
|
range: 17..21,
|
||||||
kind: HasReferences {
|
kind: HasReferences {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 17,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[],
|
[],
|
||||||
),
|
),
|
||||||
|
@ -428,12 +403,9 @@ fn main() {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 7..11,
|
range: 7..11,
|
||||||
kind: HasImpls {
|
kind: HasImpls {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 7,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[
|
[
|
||||||
NavigationTarget {
|
NavigationTarget {
|
||||||
|
@ -452,12 +424,9 @@ fn main() {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 7..11,
|
range: 7..11,
|
||||||
kind: HasReferences {
|
kind: HasReferences {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 7,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[
|
[
|
||||||
FileRange {
|
FileRange {
|
||||||
|
@ -479,12 +448,9 @@ fn main() {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 20..31,
|
range: 20..31,
|
||||||
kind: HasImpls {
|
kind: HasImpls {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 20,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[
|
[
|
||||||
NavigationTarget {
|
NavigationTarget {
|
||||||
|
@ -503,12 +469,9 @@ fn main() {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 20..31,
|
range: 20..31,
|
||||||
kind: HasReferences {
|
kind: HasReferences {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 20,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[
|
[
|
||||||
FileRange {
|
FileRange {
|
||||||
|
@ -524,12 +487,9 @@ fn main() {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 69..73,
|
range: 69..73,
|
||||||
kind: HasReferences {
|
kind: HasReferences {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 69,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[],
|
[],
|
||||||
),
|
),
|
||||||
|
@ -570,12 +530,9 @@ fn main() {}
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 3..7,
|
range: 3..7,
|
||||||
kind: HasReferences {
|
kind: HasReferences {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 3,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[],
|
[],
|
||||||
),
|
),
|
||||||
|
@ -624,12 +581,9 @@ fn main() {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 7..11,
|
range: 7..11,
|
||||||
kind: HasImpls {
|
kind: HasImpls {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 7,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[
|
[
|
||||||
NavigationTarget {
|
NavigationTarget {
|
||||||
|
@ -648,12 +602,9 @@ fn main() {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 7..11,
|
range: 7..11,
|
||||||
kind: HasReferences {
|
kind: HasReferences {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 7,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[
|
[
|
||||||
FileRange {
|
FileRange {
|
||||||
|
@ -675,12 +626,9 @@ fn main() {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 33..44,
|
range: 33..44,
|
||||||
kind: HasReferences {
|
kind: HasReferences {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 33,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[
|
[
|
||||||
FileRange {
|
FileRange {
|
||||||
|
@ -696,12 +644,9 @@ fn main() {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 61..65,
|
range: 61..65,
|
||||||
kind: HasReferences {
|
kind: HasReferences {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 61,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[],
|
[],
|
||||||
),
|
),
|
||||||
|
@ -795,12 +740,9 @@ mod tests {
|
||||||
Annotation {
|
Annotation {
|
||||||
range: 3..7,
|
range: 3..7,
|
||||||
kind: HasReferences {
|
kind: HasReferences {
|
||||||
position: FilePosition {
|
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
offset: 3,
|
|
||||||
},
|
|
||||||
data: Some(
|
data: Some(
|
||||||
[],
|
[],
|
||||||
),
|
),
|
||||||
|
|
|
@ -101,10 +101,7 @@ pub(crate) fn annotation(
|
||||||
|
|
||||||
Ok(Annotation {
|
Ok(Annotation {
|
||||||
range: text_range(&line_index, code_lens.range)?,
|
range: text_range(&line_index, code_lens.range)?,
|
||||||
kind: AnnotationKind::HasImpls {
|
kind: AnnotationKind::HasImpls { file_id, data: None },
|
||||||
position: file_position(snap, params.text_document_position_params)?,
|
|
||||||
data: None,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
lsp_ext::CodeLensResolveData::References(params) => {
|
lsp_ext::CodeLensResolveData::References(params) => {
|
||||||
|
@ -113,10 +110,7 @@ pub(crate) fn annotation(
|
||||||
|
|
||||||
Ok(Annotation {
|
Ok(Annotation {
|
||||||
range: text_range(&line_index, code_lens.range)?,
|
range: text_range(&line_index, code_lens.range)?,
|
||||||
kind: AnnotationKind::HasReferences {
|
kind: AnnotationKind::HasReferences { file_id, data: None },
|
||||||
position: file_position(snap, params)?,
|
|
||||||
data: None,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
if !client_commands_config.show_reference {
|
||||||
return Ok(());
|
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 annotation_range = range(&line_index, annotation.range);
|
||||||
let url = url(snap, file_position.file_id);
|
let url = url(snap, file_id);
|
||||||
|
|
||||||
let position = position(&line_index, file_position.offset);
|
|
||||||
|
|
||||||
let id = lsp_types::TextDocumentIdentifier { uri: url.clone() };
|
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 {
|
let goto_params = lsp_types::request::GotoImplementationParams {
|
||||||
text_document_position_params: doc_pos,
|
text_document_position_params: doc_pos,
|
||||||
|
@ -1138,7 +1136,7 @@ pub(crate) fn code_lens(
|
||||||
command::show_references(
|
command::show_references(
|
||||||
implementation_title(locations.len()),
|
implementation_title(locations.len()),
|
||||||
&url,
|
&url,
|
||||||
position,
|
annotation_range.start,
|
||||||
locations,
|
locations,
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
@ -1149,19 +1147,17 @@ pub(crate) fn code_lens(
|
||||||
data: Some(to_value(lsp_ext::CodeLensResolveData::Impls(goto_params)).unwrap()),
|
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 {
|
if !client_commands_config.show_reference {
|
||||||
return Ok(());
|
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 annotation_range = range(&line_index, annotation.range);
|
||||||
let url = url(snap, file_position.file_id);
|
let url = url(snap, file_id);
|
||||||
|
|
||||||
let position = position(&line_index, file_position.offset);
|
|
||||||
|
|
||||||
let id = lsp_types::TextDocumentIdentifier { uri: url.clone() };
|
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 command = data.map(|ranges| {
|
||||||
let locations: Vec<lsp_types::Location> =
|
let locations: Vec<lsp_types::Location> =
|
||||||
|
@ -1170,7 +1166,7 @@ pub(crate) fn code_lens(
|
||||||
command::show_references(
|
command::show_references(
|
||||||
reference_title(locations.len()),
|
reference_title(locations.len()),
|
||||||
&url,
|
&url,
|
||||||
position,
|
annotation_range.start,
|
||||||
locations,
|
locations,
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue