feat: resolve full ranges of bib items (#1627)

* feat: resolve full ranges of bib items

* chore: update todo
This commit is contained in:
Myriad-Dreamin 2025-04-04 17:06:09 +08:00 committed by GitHub
parent 04c013f3a4
commit 7f5bb5dc45
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 36 additions and 20 deletions

View file

@ -35,24 +35,21 @@ impl Definition {
self.decl.name()
}
/// The location of the definition.
// todo: cache
pub(crate) fn location(&self, ctx: &SharedContext) -> Option<(TypstFileId, Range<usize>)> {
let fid = self.decl.file_id()?;
let range = self.decl.name_range(ctx).unwrap_or_default();
Some((fid, range))
}
/// Gets file location of the definition.
pub fn file_id(&self) -> Option<TypstFileId> {
self.decl.file_id()
}
/// The range of the name of the definition.
/// Gets name range of the definition.
pub fn name_range(&self, ctx: &SharedContext) -> Option<Range<usize>> {
self.decl.name_range(ctx)
}
/// Gets full range of the definition.
pub fn full_range(&self) -> Option<Range<usize>> {
self.decl.full_range()
}
pub(crate) fn value(&self) -> Option<Value> {
self.term.as_ref()?.value()
}
@ -159,7 +156,12 @@ fn bib_definition(
crate::log_debug_ct!("find_bib_definition: {key} => {entry:?}");
// todo: rename with regard to string format: yaml-key/bib etc.
let decl = Decl::bib_entry(key.into(), entry.file_id, entry.name_range.clone());
let decl = Decl::bib_entry(
key.into(),
entry.file_id,
entry.name_range.clone(),
Some(entry.range.clone()),
);
Some(Definition::new(decl.into(), None))
}

View file

@ -6,7 +6,7 @@ input_file: crates/tinymist-query/src/fixtures/goto_definition/bib.typ
[
{
"originSelectionRange": "2:20:2:33",
"targetRange": "0:9:0:21",
"targetRange": "0:0:6:11",
"targetSelectionRange": "0:9:0:21",
"targetUri": "references.bib"
}

View file

@ -34,15 +34,15 @@ impl StatefulRequest for GotoDefinitionRequest {
let def = ctx.def_of_syntax(&source, doc, syntax)?;
let (fid, name_range) = def.location(ctx.shared())?;
let uri = ctx.uri_for_id(fid).ok()?;
let range = ctx.to_lsp_range_(name_range, fid)?;
let fid = def.file_id()?;
let name_range = def.name_range(ctx.shared()).unwrap_or_default();
let full_range = def.full_range().unwrap_or_else(|| name_range.clone());
let res = Some(GotoDefinitionResponse::Link(vec![LocationLink {
origin_selection_range: Some(origin_selection_range),
target_uri: uri,
target_range: range,
target_selection_range: range,
target_uri: ctx.uri_for_id(fid).ok()?,
target_range: ctx.to_lsp_range_(full_range, fid)?,
target_selection_range: ctx.to_lsp_range_(name_range, fid)?,
}]));
crate::log_debug_ct!("goto_definition: {fid:?} {res:?}");

View file

@ -397,10 +397,15 @@ impl Decl {
Self::Generated(GeneratedDecl(def_id))
}
pub fn bib_entry(name: Interned<str>, fid: TypstFileId, range: Range<usize>) -> Self {
pub fn bib_entry(
name: Interned<str>,
fid: TypstFileId,
name_range: Range<usize>,
range: Option<Range<usize>>,
) -> Self {
Self::BibEntry(NameRangeDecl {
name,
at: Box::new((fid, range)),
at: Box::new((fid, name_range, range)),
})
}
@ -466,6 +471,15 @@ impl Decl {
src.range(span)
}
/// Gets full range of the declaration.
pub fn full_range(&self) -> Option<Range<usize>> {
if let Decl::BibEntry(decl) = self {
return decl.at.2.clone();
}
None
}
pub fn as_def(this: &Interned<Self>, val: Option<Ty>) -> Interned<RefExpr> {
let def: Expr = this.clone().into();
Interned::new(RefExpr {
@ -582,7 +596,7 @@ impl fmt::Debug for SpannedDecl {
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct NameRangeDecl {
name: Interned<str>,
at: Box<(TypstFileId, Range<usize>)>,
at: Box<(TypstFileId, Range<usize>, Option<Range<usize>>)>,
}
impl NameRangeDecl {