Skip to content

textDocument/hover returns both type name and doc_text #414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions crates/ra_lsp_server/src/main_loop/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FileRange, FilePosition, Severity, NavigationTarget};
use ra_syntax::{TextUnit, text_utils::intersect};
use ra_text_edit::text_utils::contains_offset_nonstrict;
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -514,11 +514,20 @@ pub fn handle_hover(
Some(it) => it,
};
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,
};
if let Some(type_name) = get_type(&world, file_range) {
result.push(type_name);
}
for nav in rr.resolves_to {
if let Some(docs) = world.analysis().doc_text_for(nav)? {
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 {
Expand Down Expand Up @@ -750,3 +759,17 @@ 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) {
Copy link
Contributor

@DJMcNab DJMcNab Jan 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you throw away the Cancelled here?

Edit: This function could also be world.analysis().type_of(file_range).ok()?

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,
}
}