diff --git a/crates/language-server/src/functionality/hover.rs b/crates/language-server/src/functionality/hover.rs index 7cddb0919..659280f30 100644 --- a/crates/language-server/src/functionality/hover.rs +++ b/crates/language-server/src/functionality/hover.rs @@ -1,15 +1,14 @@ use anyhow::Error; +use async_lsp::lsp_types::Hover; use common::{InputFile, InputIngot}; use hir::lower::map_file_to_mod; - -use async_lsp::lsp_types::Hover; use tracing::info; -use crate::backend::db::LanguageServerDb; -use crate::util::to_offset_from_position; - -use super::goto::{get_goto_target_scopes_for_cursor, Cursor}; -use super::item_info::{get_item_definition_markdown, get_item_docstring, get_item_path_markdown}; +use super::{ + goto::{get_goto_target_scopes_for_cursor, Cursor}, + item_info::{get_docstring, get_item_definition_markdown, get_item_path_markdown}, +}; +use crate::{backend::db::LanguageServerDb, util::to_offset_from_position}; pub fn hover_helper( db: &dyn LanguageServerDb, @@ -32,10 +31,10 @@ pub fn hover_helper( let scopes_info = goto_info .iter() .map(|scope| { - let item = &scope.item(); - let pretty_path = get_item_path_markdown(*item, hir_db); - let definition_source = get_item_definition_markdown(*item, db.as_spanned_hir_db()); - let docs = get_item_docstring(*item, hir_db); + let item = scope.item(); + let pretty_path = get_item_path_markdown(item, hir_db); + let definition_source = get_item_definition_markdown(item, db.as_spanned_hir_db()); + let docs = get_docstring(*scope, hir_db); let result = [pretty_path, definition_source, docs] .iter() diff --git a/crates/language-server/src/functionality/item_info.rs b/crates/language-server/src/functionality/item_info.rs index 94cc48c7b..267740909 100644 --- a/crates/language-server/src/functionality/item_info.rs +++ b/crates/language-server/src/functionality/item_info.rs @@ -1,39 +1,22 @@ use hir::{ - hir_def::{Attr, ItemKind}, + hir_def::{scope_graph::ScopeId, Attr, ItemKind}, span::LazySpan, HirDb, SpannedHirDb, }; -pub fn get_item_docstring(item: ItemKind, hir_db: &dyn HirDb) -> Option { - let docstring = match item { - ItemKind::Func(func) => func.attributes(hir_db).data(hir_db), - ItemKind::Mod(mod_) => mod_.attributes(hir_db).data(hir_db), - ItemKind::Struct(struct_) => struct_.attributes(hir_db).data(hir_db), - ItemKind::Enum(enum_) => enum_.attributes(hir_db).data(hir_db), - ItemKind::TypeAlias(type_alias) => type_alias.attributes(hir_db).data(hir_db), - ItemKind::Trait(trait_) => trait_.attributes(hir_db).data(hir_db), - ItemKind::Impl(impl_) => impl_.attributes(hir_db).data(hir_db), - // ItemKind::Body(body) => body.attributes(hir_db).data(hir_db).clone(), - // ItemKind::Const(const_) => const_.attributes(hir_db).data(hir_db).clone(), - // ItemKind::Use(use_) => use_.attributes(hir_db).data(hir_db).clone(), - ItemKind::Contract(contract) => contract.attributes(hir_db).data(hir_db), - _ => return None, - } - .iter() - .filter_map(|attr| { - if let Attr::DocComment(doc) = attr { - Some(doc.text.data(hir_db).clone()) - } else { - None - } - }) - .collect::>(); - - if docstring.is_empty() { - None - } else { - Some(docstring.join("\n")) - } +pub fn get_docstring(scope: ScopeId, hir_db: &dyn HirDb) -> Option { + scope + .attrs(hir_db)? + .data(hir_db) + .iter() + .filter_map(|attr| { + if let Attr::DocComment(doc) = attr { + Some(doc.text.data(hir_db).clone()) + } else { + None + } + }) + .reduce(|a, b| a + "\n" + &b) } pub fn get_item_path_markdown(item: ItemKind, hir_db: &dyn HirDb) -> Option {