dev: more consistent way to get docs of decls (#752)

* dev: more consistent way to get docs of decls

* test: update snapshot
This commit is contained in:
Myriad-Dreamin 2024-10-30 22:29:07 +08:00 committed by GitHub
parent 8129c6741e
commit 1c1bc19caf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 462 additions and 451 deletions

View file

@ -3,9 +3,11 @@ use typst_shim::syntax::LinkedNodeExt;
use crate::{
adt::interner::Interned,
analysis::Definition,
prelude::*,
syntax::{get_check_target, get_deref_target, CheckTarget, ParamTarget},
DocTooltip, LspParamInfo, SemanticRequest,
syntax::{find_docs_before, get_check_target, get_deref_target, CheckTarget, ParamTarget},
upstream::plain_docs_sentence,
LspParamInfo, SemanticRequest,
};
/// The [`textDocument/signatureHelp`] request is sent from the client to the
@ -40,7 +42,7 @@ impl SemanticRequest for SignatureHelpRequest {
let deref_target = get_deref_target(callee, cursor)?;
let def_link = ctx.definition(&source, None, deref_target)?;
let def_link = ctx.def_of_syntax(&source, None, deref_target)?;
let documentation = DocTooltip::get(ctx, &def_link)
.as_deref()
@ -59,7 +61,7 @@ impl SemanticRequest for SignatureHelpRequest {
function = &inner.0;
}
let sig = ctx.signature_dyn(function.clone());
let sig = ctx.sig_of_func(function.clone());
log::debug!("got signature {sig:?}");
@ -145,6 +147,54 @@ impl SemanticRequest for SignatureHelpRequest {
}
}
pub(crate) struct DocTooltip;
impl DocTooltip {
pub fn get(ctx: &mut AnalysisContext, def: &Definition) -> Option<String> {
self::DocTooltip::get_inner(ctx, def).map(|s| "\n\n".to_owned() + &s)
}
fn get_inner(ctx: &mut AnalysisContext, def: &Definition) -> Option<String> {
let value = def.value();
if matches!(value, Some(Value::Func(..))) {
if let Some(builtin) = Self::builtin_func_tooltip(def) {
return Some(plain_docs_sentence(builtin).into());
}
};
let (fid, def_range) = def.def_at(ctx.shared()).clone()?;
let src = ctx.source_by_id(fid).ok()?;
find_docs_before(&src, def_range.start)
}
}
impl DocTooltip {
fn builtin_func_tooltip(def: &Definition) -> Option<&'_ str> {
let value = def.value();
let Some(Value::Func(func)) = &value else {
return None;
};
use typst::foundations::func::Repr;
let mut func = func;
let docs = 'search: loop {
match func.inner() {
Repr::Native(n) => break 'search n.docs,
Repr::Element(e) => break 'search e.docs(),
Repr::With(w) => {
func = &w.0;
}
Repr::Closure(..) => {
return None;
}
}
};
Some(docs)
}
}
fn markdown_docs(docs: &str) -> Documentation {
Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::Markdown,