Skip to content

Commit

Permalink
Display docstrings for the resolved scope in language-server
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Jan 17, 2025
1 parent e671692 commit f143227
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 42 deletions.
21 changes: 10 additions & 11 deletions crates/language-server/src/functionality/hover.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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()
Expand Down
45 changes: 14 additions & 31 deletions crates/language-server/src/functionality/item_info.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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::<Vec<_>>();

if docstring.is_empty() {
None
} else {
Some(docstring.join("\n"))
}
pub fn get_docstring(scope: ScopeId, hir_db: &dyn HirDb) -> Option<String> {
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<String> {
Expand Down

0 comments on commit f143227

Please sign in to comment.