feat: support querying label with paper name in bib items (#365)

* feat: support querying label with paper name in bib items

* dev: distinguish ref and bib title

* dev: distinguish ref and bib title 2

---------

Co-authored-by: Myriad-Dreamin <camiyoru@gmail.com>
This commit is contained in:
Zike Xu 2024-07-25 10:14:31 +08:00 committed by GitHub
parent ff72962334
commit 7b8b3938a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 34 additions and 7 deletions

View file

@ -86,6 +86,8 @@ pub struct DynLabel {
pub label_desc: Option<EcoString>,
/// Additional details about the label.
pub detail: Option<EcoString>,
/// The title of the bibliography entry. Not present for non-bibliography labels.
pub bib_title: Option<EcoString>,
}
/// Find all labels and details for them.
@ -119,6 +121,7 @@ pub fn analyze_labels(document: &Document) -> (Vec<DynLabel>, usize) {
eco_format!("{}(..)", elem.func().name())
}),
detail: Some(details),
bib_title: None,
});
}
@ -129,7 +132,8 @@ pub fn analyze_labels(document: &Document) -> (Vec<DynLabel>, usize) {
output.push(DynLabel {
label: Label::new(&key),
label_desc: detail.clone(),
detail,
detail: detail.clone(),
bib_title: detail,
});
}

View file

@ -215,6 +215,7 @@ impl StatefulRequest for CompletionRequest {
kind: Some(completion_kind(typst_completion.kind.clone())),
detail: typst_completion.detail.as_ref().map(String::from),
sort_text: typst_completion.sort_text.as_ref().map(String::from),
filter_text: typst_completion.filter_text.as_ref().map(String::from),
label_details: typst_completion.label_detail.as_ref().map(|e| {
CompletionItemLabelDetails {
detail: None,
@ -256,6 +257,7 @@ pub(crate) fn completion_kind(typst_completion_kind: TypstCompletionKind) -> Lsp
TypstCompletionKind::Field => LspCompletionKind::FIELD,
TypstCompletionKind::Variable => LspCompletionKind::VARIABLE,
TypstCompletionKind::Constant => LspCompletionKind::CONSTANT,
TypstCompletionKind::Reference => LspCompletionKind::REFERENCE,
TypstCompletionKind::Symbol(_) => LspCompletionKind::FIELD,
TypstCompletionKind::Type => LspCompletionKind::CLASS,
TypstCompletionKind::Module => LspCompletionKind::MODULE,

View file

@ -62,6 +62,8 @@ pub struct Completion {
pub label_detail: Option<EcoString>,
/// The label the completion is shown with.
pub sort_text: Option<EcoString>,
/// The composed text used for filtering.
pub filter_text: Option<EcoString>,
/// The completed version of the input, possibly described with snippet
/// syntax like `${lhs} + ${rhs}`.
///
@ -90,6 +92,8 @@ pub enum CompletionKind {
/// A constant.
#[default]
Constant,
/// A reference.
Reference,
/// A symbol.
Symbol(char),
/// A variable.
@ -1107,10 +1111,12 @@ impl<'a, 'w> CompletionContext<'a, 'w> {
label,
label_desc,
detail,
bib_title,
} in labels.into_iter().skip(skip).take(take)
{
self.completions.push(Completion {
kind: CompletionKind::Constant,
let label: EcoString = label.as_str().into();
let completion = Completion {
kind: CompletionKind::Reference,
apply: (open || close).then(|| {
eco_format!(
"{}{}{}",
@ -1119,11 +1125,25 @@ impl<'a, 'w> CompletionContext<'a, 'w> {
if close { ">" } else { "" }
)
}),
label_detail: label_desc,
label: label.as_str().into(),
detail,
label: label.clone(),
label_detail: label_desc.clone(),
filter_text: Some(label.clone()),
detail: detail.clone(),
..Completion::default()
});
};
if let Some(bib_title) = bib_title {
self.completions.push(Completion {
kind: CompletionKind::Constant,
label: bib_title.clone(),
label_detail: Some(label),
filter_text: Some(bib_title),
detail,
..completion.clone()
});
}
self.completions.push(completion);
}
}

View file

@ -165,6 +165,7 @@ fn label_tooltip(document: &Document, leaf: &LinkedNode) -> Option<Tooltip> {
label,
label_desc: _,
detail,
..
} in analyze_labels(document).0
{
if label.as_str() == target {