Skip to content

Commit 0dda36f

Browse files
committed
Show documentation for hover requests
1 parent 9712267 commit 0dda36f

File tree

6 files changed

+71
-4
lines changed

6 files changed

+71
-4
lines changed

crates/ra_analysis/src/imp.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,16 @@ impl AnalysisImpl {
364364
ret
365365
}
366366

367+
pub fn doc_comment_for(
368+
&self,
369+
file_id: FileId,
370+
symbol: FileSymbol,
371+
) -> Cancelable<Option<String>> {
372+
let file = self.db.file_syntax(file_id);
373+
374+
Ok(symbol.docs(&file))
375+
}
376+
367377
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
368378
let module_tree = self.module_tree(file_id)?;
369379
let syntax = self.db.file_syntax(file_id);

crates/ra_analysis/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ impl Analysis {
258258
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
259259
Ok(self.imp.find_all_refs(position))
260260
}
261+
pub fn doc_comment_for(
262+
&self,
263+
file_id: FileId,
264+
symbol: FileSymbol
265+
) -> Cancelable<Option<String>> {
266+
self.imp.doc_comment_for(file_id, symbol)
267+
}
261268
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<(FileId, FileSymbol)>> {
262269
self.imp.parent_module(position)
263270
}

crates/ra_editor/src/symbols.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::TextRange;
22

33
use ra_syntax::{
44
algo::visit::{visitor, Visitor},
5-
ast::{self, NameOwner},
5+
ast::{self, DocCommentsOwner, NameOwner},
66
AstNode, File, SmolStr, SyntaxKind, SyntaxNodeRef, WalkEvent,
77
};
88

@@ -22,6 +22,30 @@ pub struct FileSymbol {
2222
pub kind: SyntaxKind,
2323
}
2424

25+
impl FileSymbol {
26+
pub fn docs(&self, file: &File) -> Option<String> {
27+
file.syntax().descendants()
28+
.filter(|node| node.kind() == self.kind && node.range() == self.node_range)
29+
.filter_map(|node: SyntaxNodeRef| {
30+
fn doc_comments<'a, N: DocCommentsOwner<'a>>(node: N) -> Option<String> {
31+
let comments = node.doc_comment_text();
32+
if comments.is_empty() { None } else { Some(comments) }
33+
}
34+
35+
visitor()
36+
.visit(doc_comments::<ast::FnDef>)
37+
.visit(doc_comments::<ast::StructDef>)
38+
.visit(doc_comments::<ast::EnumDef>)
39+
.visit(doc_comments::<ast::TraitDef>)
40+
.visit(doc_comments::<ast::Module>)
41+
.visit(doc_comments::<ast::TypeDef>)
42+
.visit(doc_comments::<ast::ConstDef>)
43+
.visit(doc_comments::<ast::StaticDef>)
44+
.accept(node)?
45+
}).nth(0)
46+
}
47+
}
48+
2549
pub fn file_symbols(file: &File) -> Vec<FileSymbol> {
2650
file.syntax().descendants().filter_map(to_symbol).collect()
2751
}

crates/ra_lsp_server/src/caps.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn server_capabilities() -> ServerCapabilities {
1616
save: None,
1717
},
1818
)),
19-
hover_provider: None,
19+
hover_provider: Some(true),
2020
completion_provider: Some(CompletionOptions {
2121
resolve_provider: None,
2222
trigger_characters: None,

crates/ra_lsp_server/src/main_loop/handlers.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use gen_lsp_server::ErrorCode;
44
use languageserver_types::{
55
CodeActionResponse, Command, CompletionItem, CompletionItemKind, Diagnostic,
66
DiagnosticSeverity, DocumentSymbol, Documentation, FoldingRange, FoldingRangeKind,
7-
FoldingRangeParams, InsertTextFormat, Location, MarkupContent, MarkupKind, Position,
7+
FoldingRangeParams, InsertTextFormat, Location, MarkupContent, MarkupKind, MarkedString, Position,
88
PrepareRenameResponse, RenameParams, SymbolInformation, TextDocumentIdentifier, TextEdit,
9-
WorkspaceEdit, ParameterInformation, SignatureInformation,
9+
WorkspaceEdit, ParameterInformation, SignatureInformation, Hover, HoverContents,
1010
};
1111
use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition};
1212
use ra_syntax::text_utils::contains_offset_nonstrict;
@@ -478,6 +478,31 @@ pub fn handle_signature_help(
478478
}
479479
}
480480

481+
pub fn handle_hover(
482+
world: ServerWorld,
483+
params: req::TextDocumentPositionParams,
484+
) -> Result<Option<Hover>> {
485+
let position = params.try_conv_with(&world)?;
486+
let line_index = world.analysis().file_line_index(position.file_id);
487+
488+
for (file_id, symbol) in world.analysis().approximately_resolve_symbol(position)? {
489+
let range = symbol.node_range.conv_with(&line_index);
490+
let name = symbol.name.to_string();
491+
let comment = world.analysis.doc_comment_for(file_id, symbol)?;
492+
493+
if comment.is_some() {
494+
let contents = HoverContents::Scalar(MarkedString::String(comment.unwrap()));
495+
496+
return Ok(Some(Hover {
497+
contents,
498+
range: Some(range)
499+
}))
500+
}
501+
}
502+
503+
Ok(None)
504+
}
505+
481506
pub fn handle_prepare_rename(
482507
world: ServerWorld,
483508
params: req::TextDocumentPositionParams,

crates/ra_lsp_server/src/main_loop/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ fn on_request(
259259
.on::<req::CodeActionRequest>(handlers::handle_code_action)?
260260
.on::<req::FoldingRangeRequest>(handlers::handle_folding_range)?
261261
.on::<req::SignatureHelpRequest>(handlers::handle_signature_help)?
262+
.on::<req::HoverRequest>(handlers::handle_hover)?
262263
.on::<req::PrepareRenameRequest>(handlers::handle_prepare_rename)?
263264
.on::<req::Rename>(handlers::handle_rename)?
264265
.on::<req::References>(handlers::handle_references)?

0 commit comments

Comments
 (0)