|
9 | 9 |
|
10 | 10 | use std::collections::HashMap; |
11 | 11 |
|
| 12 | +use dupe::Dupe; |
12 | 13 | use lsp_types::Hover; |
13 | 14 | use lsp_types::HoverContents; |
14 | 15 | use lsp_types::MarkupContent; |
@@ -309,6 +310,38 @@ fn identifier_text_at( |
309 | 310 | .map(|id| id.identifier.id.to_string()) |
310 | 311 | } |
311 | 312 |
|
| 313 | +fn docstring_for_class_object_type( |
| 314 | + transaction: &Transaction<'_>, |
| 315 | + handle: &Handle, |
| 316 | + type_: &Type, |
| 317 | +) -> Option<Docstring> { |
| 318 | + let qname = match type_ { |
| 319 | + Type::ClassDef(cls) => cls.qname(), |
| 320 | + Type::Type(inner) => match inner.as_ref() { |
| 321 | + Type::ClassType(cls) => cls.qname(), |
| 322 | + _ => return None, |
| 323 | + }, |
| 324 | + _ => return None, |
| 325 | + }; |
| 326 | + let definition_handle = Handle::new( |
| 327 | + qname.module_name(), |
| 328 | + qname.module_path().dupe(), |
| 329 | + handle.sys_info().dupe(), |
| 330 | + ); |
| 331 | + let definition = transaction |
| 332 | + .find_definition( |
| 333 | + &definition_handle, |
| 334 | + qname.range().start(), |
| 335 | + FindPreference { |
| 336 | + resolve_call_dunders: false, |
| 337 | + ..Default::default() |
| 338 | + }, |
| 339 | + ) |
| 340 | + .into_iter() |
| 341 | + .find(|item| item.definition_range == qname.range())?; |
| 342 | + Some(Docstring(definition.docstring_range?, definition.module)) |
| 343 | +} |
| 344 | + |
312 | 345 | fn collect_typed_dict_fields_for_hover<'a>( |
313 | 346 | solver: &AnswersSolver<TransactionHandle<'a>>, |
314 | 347 | ty: &Type, |
@@ -624,7 +657,7 @@ pub fn get_hover( |
624 | 657 | let docstring = if let (Some(docstring), Some(module)) = (docstring_range, module) { |
625 | 658 | Some(Docstring(docstring, module)) |
626 | 659 | } else { |
627 | | - None |
| 660 | + docstring_for_class_object_type(transaction, handle, &type_) |
628 | 661 | }; |
629 | 662 |
|
630 | 663 | let mut parameter_doc = keyword_argument_documentation(transaction, handle, position) |
|
0 commit comments