mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Show documentation for hover requests
This commit is contained in:
parent
6df71da81f
commit
8ccd26adf3
6 changed files with 71 additions and 4 deletions
|
@ -364,6 +364,16 @@ impl AnalysisImpl {
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn doc_comment_for(
|
||||||
|
&self,
|
||||||
|
file_id: FileId,
|
||||||
|
symbol: FileSymbol,
|
||||||
|
) -> Cancelable<Option<String>> {
|
||||||
|
let file = self.db.file_syntax(file_id);
|
||||||
|
|
||||||
|
Ok(symbol.docs(&file))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
|
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
|
||||||
let module_tree = self.module_tree(file_id)?;
|
let module_tree = self.module_tree(file_id)?;
|
||||||
let syntax = self.db.file_syntax(file_id);
|
let syntax = self.db.file_syntax(file_id);
|
||||||
|
|
|
@ -258,6 +258,13 @@ impl Analysis {
|
||||||
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
|
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
|
||||||
Ok(self.imp.find_all_refs(position))
|
Ok(self.imp.find_all_refs(position))
|
||||||
}
|
}
|
||||||
|
pub fn doc_comment_for(
|
||||||
|
&self,
|
||||||
|
file_id: FileId,
|
||||||
|
symbol: FileSymbol
|
||||||
|
) -> Cancelable<Option<String>> {
|
||||||
|
self.imp.doc_comment_for(file_id, symbol)
|
||||||
|
}
|
||||||
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<(FileId, FileSymbol)>> {
|
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<(FileId, FileSymbol)>> {
|
||||||
self.imp.parent_module(position)
|
self.imp.parent_module(position)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use crate::TextRange;
|
||||||
|
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
algo::visit::{visitor, Visitor},
|
algo::visit::{visitor, Visitor},
|
||||||
ast::{self, NameOwner},
|
ast::{self, DocCommentsOwner, NameOwner},
|
||||||
AstNode, File, SmolStr, SyntaxKind, SyntaxNodeRef, WalkEvent,
|
AstNode, File, SmolStr, SyntaxKind, SyntaxNodeRef, WalkEvent,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -22,6 +22,30 @@ pub struct FileSymbol {
|
||||||
pub kind: SyntaxKind,
|
pub kind: SyntaxKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FileSymbol {
|
||||||
|
pub fn docs(&self, file: &File) -> Option<String> {
|
||||||
|
file.syntax().descendants()
|
||||||
|
.filter(|node| node.kind() == self.kind && node.range() == self.node_range)
|
||||||
|
.filter_map(|node: SyntaxNodeRef| {
|
||||||
|
fn doc_comments<'a, N: DocCommentsOwner<'a>>(node: N) -> Option<String> {
|
||||||
|
let comments = node.doc_comment_text();
|
||||||
|
if comments.is_empty() { None } else { Some(comments) }
|
||||||
|
}
|
||||||
|
|
||||||
|
visitor()
|
||||||
|
.visit(doc_comments::<ast::FnDef>)
|
||||||
|
.visit(doc_comments::<ast::StructDef>)
|
||||||
|
.visit(doc_comments::<ast::EnumDef>)
|
||||||
|
.visit(doc_comments::<ast::TraitDef>)
|
||||||
|
.visit(doc_comments::<ast::Module>)
|
||||||
|
.visit(doc_comments::<ast::TypeDef>)
|
||||||
|
.visit(doc_comments::<ast::ConstDef>)
|
||||||
|
.visit(doc_comments::<ast::StaticDef>)
|
||||||
|
.accept(node)?
|
||||||
|
}).nth(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn file_symbols(file: &File) -> Vec<FileSymbol> {
|
pub fn file_symbols(file: &File) -> Vec<FileSymbol> {
|
||||||
file.syntax().descendants().filter_map(to_symbol).collect()
|
file.syntax().descendants().filter_map(to_symbol).collect()
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ pub fn server_capabilities() -> ServerCapabilities {
|
||||||
save: None,
|
save: None,
|
||||||
},
|
},
|
||||||
)),
|
)),
|
||||||
hover_provider: None,
|
hover_provider: Some(true),
|
||||||
completion_provider: Some(CompletionOptions {
|
completion_provider: Some(CompletionOptions {
|
||||||
resolve_provider: None,
|
resolve_provider: None,
|
||||||
trigger_characters: None,
|
trigger_characters: None,
|
||||||
|
|
|
@ -4,9 +4,9 @@ use gen_lsp_server::ErrorCode;
|
||||||
use languageserver_types::{
|
use languageserver_types::{
|
||||||
CodeActionResponse, Command, CompletionItem, CompletionItemKind, Diagnostic,
|
CodeActionResponse, Command, CompletionItem, CompletionItemKind, Diagnostic,
|
||||||
DiagnosticSeverity, DocumentSymbol, Documentation, FoldingRange, FoldingRangeKind,
|
DiagnosticSeverity, DocumentSymbol, Documentation, FoldingRange, FoldingRangeKind,
|
||||||
FoldingRangeParams, InsertTextFormat, Location, MarkupContent, MarkupKind, Position,
|
FoldingRangeParams, InsertTextFormat, Location, MarkupContent, MarkupKind, MarkedString, Position,
|
||||||
PrepareRenameResponse, RenameParams, SymbolInformation, TextDocumentIdentifier, TextEdit,
|
PrepareRenameResponse, RenameParams, SymbolInformation, TextDocumentIdentifier, TextEdit,
|
||||||
WorkspaceEdit, ParameterInformation, SignatureInformation,
|
WorkspaceEdit, ParameterInformation, SignatureInformation, Hover, HoverContents,
|
||||||
};
|
};
|
||||||
use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition};
|
use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition};
|
||||||
use ra_syntax::text_utils::contains_offset_nonstrict;
|
use ra_syntax::text_utils::contains_offset_nonstrict;
|
||||||
|
@ -478,6 +478,31 @@ pub fn handle_signature_help(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn handle_hover(
|
||||||
|
world: ServerWorld,
|
||||||
|
params: req::TextDocumentPositionParams,
|
||||||
|
) -> Result<Option<Hover>> {
|
||||||
|
let position = params.try_conv_with(&world)?;
|
||||||
|
let line_index = world.analysis().file_line_index(position.file_id);
|
||||||
|
|
||||||
|
for (file_id, symbol) in world.analysis().approximately_resolve_symbol(position)? {
|
||||||
|
let range = symbol.node_range.conv_with(&line_index);
|
||||||
|
let name = symbol.name.to_string();
|
||||||
|
let comment = world.analysis.doc_comment_for(file_id, symbol)?;
|
||||||
|
|
||||||
|
if comment.is_some() {
|
||||||
|
let contents = HoverContents::Scalar(MarkedString::String(comment.unwrap()));
|
||||||
|
|
||||||
|
return Ok(Some(Hover {
|
||||||
|
contents,
|
||||||
|
range: Some(range)
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn handle_prepare_rename(
|
pub fn handle_prepare_rename(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::TextDocumentPositionParams,
|
params: req::TextDocumentPositionParams,
|
||||||
|
|
|
@ -259,6 +259,7 @@ fn on_request(
|
||||||
.on::<req::CodeActionRequest>(handlers::handle_code_action)?
|
.on::<req::CodeActionRequest>(handlers::handle_code_action)?
|
||||||
.on::<req::FoldingRangeRequest>(handlers::handle_folding_range)?
|
.on::<req::FoldingRangeRequest>(handlers::handle_folding_range)?
|
||||||
.on::<req::SignatureHelpRequest>(handlers::handle_signature_help)?
|
.on::<req::SignatureHelpRequest>(handlers::handle_signature_help)?
|
||||||
|
.on::<req::HoverRequest>(handlers::handle_hover)?
|
||||||
.on::<req::PrepareRenameRequest>(handlers::handle_prepare_rename)?
|
.on::<req::PrepareRenameRequest>(handlers::handle_prepare_rename)?
|
||||||
.on::<req::Rename>(handlers::handle_rename)?
|
.on::<req::Rename>(handlers::handle_rename)?
|
||||||
.on::<req::References>(handlers::handle_references)?
|
.on::<req::References>(handlers::handle_references)?
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue