|
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; |
@@ -310,6 +311,39 @@ fn identifier_text_at( |
310 | 311 | .map(|id| id.identifier.id.to_string()) |
311 | 312 | } |
312 | 313 |
|
| 314 | +fn docstring_for_class_object_type( |
| 315 | + transaction: &Transaction<'_>, |
| 316 | + handle: &Handle, |
| 317 | + type_: &Type, |
| 318 | +) -> Option<Docstring> { |
| 319 | + let qname = match type_ { |
| 320 | + Type::ClassDef(cls) => cls.qname(), |
| 321 | + Type::Type(inner) => match inner.as_ref() { |
| 322 | + Type::ClassType(cls) => cls.qname(), |
| 323 | + _ => return None, |
| 324 | + }, |
| 325 | + _ => return None, |
| 326 | + }; |
| 327 | + let definition_handle = Handle::new( |
| 328 | + qname.module_name(), |
| 329 | + qname.module_path().dupe(), |
| 330 | + handle.sys_info().dupe(), |
| 331 | + ); |
| 332 | + let definition = transaction |
| 333 | + .find_definition( |
| 334 | + &definition_handle, |
| 335 | + qname.range().start(), |
| 336 | + FindPreference { |
| 337 | + resolve_call_dunders: false, |
| 338 | + ..Default::default() |
| 339 | + }, |
| 340 | + ) |
| 341 | + .ok()? |
| 342 | + .into_iter() |
| 343 | + .find(|item| item.definition_range == qname.range())?; |
| 344 | + Some(Docstring(definition.docstring_range?, definition.module)) |
| 345 | +} |
| 346 | + |
313 | 347 | fn collect_typed_dict_fields_for_hover<'a>( |
314 | 348 | solver: &AnswersSolver<TransactionHandle<'a>>, |
315 | 349 | ty: &Type, |
@@ -631,7 +665,7 @@ pub fn get_hover( |
631 | 665 | let docstring = if let (Some(docstring), Some(module)) = (docstring_range, module) { |
632 | 666 | Some(Docstring(docstring, module)) |
633 | 667 | } else { |
634 | | - None |
| 668 | + docstring_for_class_object_type(transaction, handle, &type_) |
635 | 669 | }; |
636 | 670 |
|
637 | 671 | let mut parameter_doc = keyword_argument_documentation(transaction, handle, position) |
|
0 commit comments