Skip to content

Commit dfc37f0

Browse files
bors[bot]kjeremy
andcommitted
Merge #196
196: Hover: Show documentation r=matklad a=kjeremy Closes #186 Co-authored-by: Jeremy A. Kolb <[email protected]>
2 parents 2e24454 + a9df487 commit dfc37f0

File tree

8 files changed

+90
-6
lines changed

8 files changed

+90
-6
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: 31 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,36 @@ pub struct FileSymbol {
2222
pub kind: SyntaxKind,
2323
}
2424

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

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: 26 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,30 @@ 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 comment = world.analysis.doc_comment_for(file_id, symbol)?;
491+
492+
if comment.is_some() {
493+
let contents = HoverContents::Scalar(MarkedString::String(comment.unwrap()));
494+
495+
return Ok(Some(Hover {
496+
contents,
497+
range: Some(range),
498+
}));
499+
}
500+
}
501+
502+
Ok(None)
503+
}
504+
481505
pub fn handle_prepare_rename(
482506
world: ServerWorld,
483507
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)?

crates/ra_syntax/src/ast/generated.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ impl<R: TreeRoot<RaTypes>> ConstDefNode<R> {
608608
impl<'a> ast::NameOwner<'a> for ConstDef<'a> {}
609609
impl<'a> ast::TypeParamsOwner<'a> for ConstDef<'a> {}
610610
impl<'a> ast::AttrsOwner<'a> for ConstDef<'a> {}
611+
impl<'a> ast::DocCommentsOwner<'a> for ConstDef<'a> {}
611612
impl<'a> ConstDef<'a> {}
612613

613614
// ContinueExpr
@@ -722,6 +723,7 @@ impl<R: TreeRoot<RaTypes>> EnumDefNode<R> {
722723
impl<'a> ast::NameOwner<'a> for EnumDef<'a> {}
723724
impl<'a> ast::TypeParamsOwner<'a> for EnumDef<'a> {}
724725
impl<'a> ast::AttrsOwner<'a> for EnumDef<'a> {}
726+
impl<'a> ast::DocCommentsOwner<'a> for EnumDef<'a> {}
725727
impl<'a> EnumDef<'a> {}
726728

727729
// Expr
@@ -1886,6 +1888,7 @@ impl<R: TreeRoot<RaTypes>> ModuleNode<R> {
18861888

18871889
impl<'a> ast::NameOwner<'a> for Module<'a> {}
18881890
impl<'a> ast::AttrsOwner<'a> for Module<'a> {}
1891+
impl<'a> ast::DocCommentsOwner<'a> for Module<'a> {}
18891892
impl<'a> Module<'a> {
18901893
pub fn item_list(self) -> Option<ItemList<'a>> {
18911894
super::child_opt(self)
@@ -3205,6 +3208,7 @@ impl<R: TreeRoot<RaTypes>> StaticDefNode<R> {
32053208
impl<'a> ast::NameOwner<'a> for StaticDef<'a> {}
32063209
impl<'a> ast::TypeParamsOwner<'a> for StaticDef<'a> {}
32073210
impl<'a> ast::AttrsOwner<'a> for StaticDef<'a> {}
3211+
impl<'a> ast::DocCommentsOwner<'a> for StaticDef<'a> {}
32083212
impl<'a> StaticDef<'a> {}
32093213

32103214
// Stmt
@@ -3270,6 +3274,7 @@ impl<R: TreeRoot<RaTypes>> StructDefNode<R> {
32703274
impl<'a> ast::NameOwner<'a> for StructDef<'a> {}
32713275
impl<'a> ast::TypeParamsOwner<'a> for StructDef<'a> {}
32723276
impl<'a> ast::AttrsOwner<'a> for StructDef<'a> {}
3277+
impl<'a> ast::DocCommentsOwner<'a> for StructDef<'a> {}
32733278
impl<'a> StructDef<'a> {
32743279
pub fn fields(self) -> impl Iterator<Item = NamedFieldDef<'a>> + 'a {
32753280
super::children(self)
@@ -3424,6 +3429,7 @@ impl<R: TreeRoot<RaTypes>> TraitDefNode<R> {
34243429

34253430
impl<'a> ast::NameOwner<'a> for TraitDef<'a> {}
34263431
impl<'a> ast::AttrsOwner<'a> for TraitDef<'a> {}
3432+
impl<'a> ast::DocCommentsOwner<'a> for TraitDef<'a> {}
34273433
impl<'a> TraitDef<'a> {}
34283434

34293435
// TryExpr
@@ -3649,6 +3655,7 @@ impl<R: TreeRoot<RaTypes>> TypeDefNode<R> {
36493655
impl<'a> ast::NameOwner<'a> for TypeDef<'a> {}
36503656
impl<'a> ast::TypeParamsOwner<'a> for TypeDef<'a> {}
36513657
impl<'a> ast::AttrsOwner<'a> for TypeDef<'a> {}
3658+
impl<'a> ast::DocCommentsOwner<'a> for TypeDef<'a> {}
36523659
impl<'a> TypeDef<'a> {}
36533660

36543661
// TypeParam

crates/ra_syntax/src/grammar.ron

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ Grammar(
260260
"NameOwner",
261261
"TypeParamsOwner",
262262
"AttrsOwner",
263+
"DocCommentsOwner"
263264
],
264265
collections: [
265266
["fields", "NamedFieldDef"]
@@ -270,10 +271,11 @@ Grammar(
270271
"NameOwner",
271272
"TypeParamsOwner",
272273
"AttrsOwner",
274+
"DocCommentsOwner"
273275
] ),
274-
"TraitDef": ( traits: ["NameOwner", "AttrsOwner"] ),
276+
"TraitDef": ( traits: ["NameOwner", "AttrsOwner", "DocCommentsOwner"] ),
275277
"Module": (
276-
traits: ["NameOwner", "AttrsOwner" ],
278+
traits: ["NameOwner", "AttrsOwner", "DocCommentsOwner" ],
277279
options: [ "ItemList" ]
278280
),
279281
"ItemList": (
@@ -283,16 +285,19 @@ Grammar(
283285
"NameOwner",
284286
"TypeParamsOwner",
285287
"AttrsOwner",
288+
"DocCommentsOwner"
286289
] ),
287290
"StaticDef": ( traits: [
288291
"NameOwner",
289292
"TypeParamsOwner",
290293
"AttrsOwner",
294+
"DocCommentsOwner"
291295
] ),
292296
"TypeDef": ( traits: [
293297
"NameOwner",
294298
"TypeParamsOwner",
295299
"AttrsOwner",
300+
"DocCommentsOwner"
296301
] ),
297302
"ImplItem": (),
298303

0 commit comments

Comments
 (0)