mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-12 08:36:03 +00:00
move hover implementation to ra_analysis
This commit is contained in:
parent
2560a9e807
commit
3ad0037f90
57
crates/ra_analysis/src/hover.rs
Normal file
57
crates/ra_analysis/src/hover.rs
Normal file
@ -0,0 +1,57 @@
|
||||
use ra_db::{Cancelable, SyntaxDatabase};
|
||||
use ra_syntax::{ast, AstNode};
|
||||
|
||||
use crate::{db::RootDatabase, RangeInfo, FilePosition, FileRange};
|
||||
|
||||
pub(crate) fn hover(
|
||||
db: &RootDatabase,
|
||||
position: FilePosition,
|
||||
) -> Cancelable<Option<RangeInfo<String>>> {
|
||||
let mut res = Vec::new();
|
||||
let range = if let Some(rr) = db.approximately_resolve_symbol(position)? {
|
||||
for nav in rr.resolves_to {
|
||||
res.extend(db.doc_text_for(nav)?)
|
||||
}
|
||||
rr.reference_range
|
||||
} else {
|
||||
let file = db.source_file(position.file_id);
|
||||
let expr: ast::Expr = ctry!(ra_editor::find_node_at_offset(
|
||||
file.syntax(),
|
||||
position.offset
|
||||
));
|
||||
let frange = FileRange {
|
||||
file_id: position.file_id,
|
||||
range: expr.syntax().range(),
|
||||
};
|
||||
res.extend(db.type_of(frange)?);
|
||||
expr.syntax().range()
|
||||
};
|
||||
if res.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let res = RangeInfo::new(range, res.join("\n\n---\n"));
|
||||
Ok(Some(res))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ra_syntax::TextRange;
|
||||
|
||||
use crate::mock_analysis::single_file_with_position;
|
||||
|
||||
#[test]
|
||||
fn hover_shows_type_of_an_expression() {
|
||||
let (analysis, position) = single_file_with_position(
|
||||
"
|
||||
pub fn foo() -> u32 { 1 }
|
||||
|
||||
fn main() {
|
||||
let foo_test = foo()<|>;
|
||||
}
|
||||
",
|
||||
);
|
||||
let hover = analysis.hover(position).unwrap().unwrap();
|
||||
assert_eq!(hover.range, TextRange::from_to(95.into(), 100.into()));
|
||||
assert_eq!(hover.info, "u32");
|
||||
}
|
||||
}
|
@ -269,32 +269,6 @@ impl db::RootDatabase {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub(crate) fn hover(&self, position: FilePosition) -> Cancelable<Option<(TextRange, String)>> {
|
||||
let mut res = Vec::new();
|
||||
let range = if let Some(rr) = self.approximately_resolve_symbol(position)? {
|
||||
for nav in rr.resolves_to {
|
||||
res.extend(self.doc_text_for(nav)?)
|
||||
}
|
||||
rr.reference_range
|
||||
} else {
|
||||
let file = self.source_file(position.file_id);
|
||||
let expr: ast::Expr = ctry!(ra_editor::find_node_at_offset(
|
||||
file.syntax(),
|
||||
position.offset
|
||||
));
|
||||
let frange = FileRange {
|
||||
file_id: position.file_id,
|
||||
range: expr.syntax().range(),
|
||||
};
|
||||
res.extend(self.type_of(frange)?);
|
||||
expr.syntax().range()
|
||||
};
|
||||
if res.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
Ok(Some((range, res.join("\n\n---\n"))))
|
||||
}
|
||||
|
||||
pub(crate) fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
|
||||
let syntax = self.source_file(file_id);
|
||||
|
||||
|
@ -21,6 +21,7 @@ mod runnables;
|
||||
|
||||
mod extend_selection;
|
||||
mod syntax_highlighting;
|
||||
mod hover;
|
||||
|
||||
use std::{fmt, sync::Arc};
|
||||
|
||||
@ -260,6 +261,18 @@ impl NavigationTarget {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RangeInfo<T> {
|
||||
pub range: TextRange,
|
||||
pub info: T,
|
||||
}
|
||||
|
||||
impl<T> RangeInfo<T> {
|
||||
fn new(range: TextRange, info: T) -> RangeInfo<T> {
|
||||
RangeInfo { range, info }
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of "goto def" query.
|
||||
#[derive(Debug)]
|
||||
pub struct ReferenceResolution {
|
||||
@ -394,6 +407,10 @@ impl Analysis {
|
||||
pub fn doc_text_for(&self, nav: NavigationTarget) -> Cancelable<Option<String>> {
|
||||
self.db.doc_text_for(nav)
|
||||
}
|
||||
/// Returns a short text descrbing element at position.
|
||||
pub fn hover(&self, position: FilePosition) -> Cancelable<Option<RangeInfo<String>>> {
|
||||
hover::hover(&*self.db, position)
|
||||
}
|
||||
/// Returns a `mod name;` declaration which created the current module.
|
||||
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<NavigationTarget>> {
|
||||
self.db.parent_module(position)
|
||||
|
@ -9,7 +9,7 @@ use languageserver_types::{
|
||||
Range, WorkspaceEdit, ParameterInformation, ParameterLabel, SignatureInformation, Hover,
|
||||
HoverContents, DocumentFormattingParams, DocumentHighlight,
|
||||
};
|
||||
use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FileRange, FilePosition, Severity, NavigationTarget};
|
||||
use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FileRange, FilePosition, Severity};
|
||||
use ra_syntax::{TextUnit, text_utils::intersect};
|
||||
use ra_text_edit::text_utils::contains_offset_nonstrict;
|
||||
use rustc_hash::FxHashMap;
|
||||
@ -509,36 +509,18 @@ pub fn handle_hover(
|
||||
world: ServerWorld,
|
||||
params: req::TextDocumentPositionParams,
|
||||
) -> Result<Option<Hover>> {
|
||||
// TODO: Cut down on number of allocations
|
||||
let position = params.try_conv_with(&world)?;
|
||||
let line_index = world.analysis().file_line_index(position.file_id);
|
||||
let rr = match world.analysis().approximately_resolve_symbol(position)? {
|
||||
let info = match world.analysis().hover(position)? {
|
||||
None => return Ok(None),
|
||||
Some(it) => it,
|
||||
Some(info) => info,
|
||||
};
|
||||
let mut result = Vec::new();
|
||||
let file_id = params.text_document.try_conv_with(&world)?;
|
||||
let file_range = FileRange {
|
||||
file_id,
|
||||
range: rr.reference_range,
|
||||
let line_index = world.analysis.file_line_index(position.file_id);
|
||||
let range = info.range.conv_with(&line_index);
|
||||
let res = Hover {
|
||||
contents: HoverContents::Scalar(MarkedString::String(info.info)),
|
||||
range: Some(range),
|
||||
};
|
||||
if let Some(type_name) = get_type(&world, file_range) {
|
||||
result.push(type_name);
|
||||
}
|
||||
for nav in rr.resolves_to {
|
||||
if let Some(docs) = get_doc_text(&world, nav) {
|
||||
result.push(docs);
|
||||
}
|
||||
}
|
||||
|
||||
let range = rr.reference_range.conv_with(&line_index);
|
||||
if result.len() > 0 {
|
||||
return Ok(Some(Hover {
|
||||
contents: HoverContents::Scalar(MarkedString::String(result.join("\n\n---\n"))),
|
||||
range: Some(range),
|
||||
}));
|
||||
}
|
||||
Ok(None)
|
||||
Ok(Some(res))
|
||||
}
|
||||
|
||||
/// Test doc comment
|
||||
@ -762,17 +744,3 @@ fn to_diagnostic_severity(severity: Severity) -> DiagnosticSeverity {
|
||||
WeakWarning => DiagnosticSeverity::Hint,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_type(world: &ServerWorld, file_range: FileRange) -> Option<String> {
|
||||
match world.analysis().type_of(file_range) {
|
||||
Ok(result) => result,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_doc_text(world: &ServerWorld, nav: NavigationTarget) -> Option<String> {
|
||||
match world.analysis().doc_text_for(nav) {
|
||||
Ok(result) => result,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user