mirror of
https://github.com/Myriad-Dreamin/tinymist.git
synced 2025-08-03 17:58:17 +00:00
fix: correct name_range
impl (#1623)
This commit is contained in:
parent
d7424abed8
commit
bceb93b944
6 changed files with 46 additions and 10 deletions
|
@ -39,9 +39,8 @@ impl Definition {
|
|||
// todo: cache
|
||||
pub(crate) fn location(&self, ctx: &SharedContext) -> Option<(TypstFileId, Range<usize>)> {
|
||||
let fid = self.decl.file_id()?;
|
||||
let span = self.decl.span();
|
||||
let range = (!span.is_detached()).then(|| ctx.source_by_id(fid).ok()?.range(span));
|
||||
Some((fid, range.flatten().unwrap_or_default()))
|
||||
let range = self.decl.name_range(ctx).unwrap_or_default();
|
||||
Some((fid, range))
|
||||
}
|
||||
|
||||
/// The range of the name of the definition.
|
||||
|
@ -155,7 +154,7 @@ 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.range.clone());
|
||||
let decl = Decl::bib_entry(key.into(), entry.file_id, entry.name_range.clone());
|
||||
Some(Definition::new(decl.into(), None))
|
||||
}
|
||||
|
||||
|
|
15
crates/tinymist-query/src/fixtures/goto_definition/bib.typ
Normal file
15
crates/tinymist-query/src/fixtures/goto_definition/bib.typ
Normal file
|
@ -0,0 +1,15 @@
|
|||
/// path: references.bib
|
||||
@article{Russell:1908,
|
||||
Author = {Bertand Russell},
|
||||
Journal = {American Journal of Mathematics},
|
||||
Pages = {222--262},
|
||||
Title = {Mathematical logic based on the theory of types},
|
||||
Volume = 30,
|
||||
Year = 1908}
|
||||
|
||||
-----
|
||||
/// compile: true
|
||||
|
||||
/* position after */@Russell:1908
|
||||
|
||||
#bibliography("references.bib")
|
|
@ -0,0 +1,13 @@
|
|||
---
|
||||
source: crates/tinymist-query/src/goto_definition.rs
|
||||
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
|
||||
input_file: crates/tinymist-query/src/fixtures/goto_definition/bib.typ
|
||||
---
|
||||
[
|
||||
{
|
||||
"originSelectionRange": "2:20:2:33",
|
||||
"targetRange": "0:9:0:21",
|
||||
"targetSelectionRange": "0:9:0:21",
|
||||
"targetUri": "references.bib"
|
||||
}
|
||||
]
|
|
@ -34,9 +34,9 @@ impl StatefulRequest for GotoDefinitionRequest {
|
|||
|
||||
let def = ctx.def_of_syntax(&source, doc, syntax)?;
|
||||
|
||||
let (fid, def_range) = def.location(ctx.shared())?;
|
||||
let (fid, name_range) = def.location(ctx.shared())?;
|
||||
let uri = ctx.uri_for_id(fid).ok()?;
|
||||
let range = ctx.to_lsp_range_(def_range, fid)?;
|
||||
let range = ctx.to_lsp_range_(name_range, fid)?;
|
||||
|
||||
let res = Some(GotoDefinitionResponse::Link(vec![LocationLink {
|
||||
origin_selection_range: Some(origin_selection_range),
|
||||
|
|
|
@ -62,7 +62,7 @@ pub(crate) fn prepare_renaming(
|
|||
def: &Definition,
|
||||
) -> Option<(String, Option<LspRange>)> {
|
||||
let name = def.name().clone();
|
||||
let (def_fid, _def_range) = def.location(ctx.shared()).clone()?;
|
||||
let (def_fid, _name_range) = def.location(ctx.shared()).clone()?;
|
||||
|
||||
if WorkspaceResolver::is_package_file(def_fid) {
|
||||
crate::log_debug_ct!(
|
||||
|
|
|
@ -408,6 +408,7 @@ impl Decl {
|
|||
matches!(
|
||||
self,
|
||||
Self::Func(..)
|
||||
| Self::BibEntry(..)
|
||||
| Self::Closure(..)
|
||||
| Self::Var(..)
|
||||
| Self::Label(..)
|
||||
|
@ -440,6 +441,7 @@ impl Decl {
|
|||
pub fn file_id(&self) -> Option<TypstFileId> {
|
||||
match self {
|
||||
Self::Module(ModuleDecl { fid, .. }) => Some(*fid),
|
||||
Self::BibEntry(NameRangeDecl { at, .. }) => Some(at.0),
|
||||
that => that.span().id(),
|
||||
}
|
||||
}
|
||||
|
@ -450,10 +452,17 @@ impl Decl {
|
|||
if !self.is_def() {
|
||||
return None;
|
||||
}
|
||||
if let Decl::BibEntry(decl) = self {
|
||||
return Some(decl.at.1.clone());
|
||||
}
|
||||
|
||||
let fid = self.file_id()?;
|
||||
let src = ctx.source_by_id(fid).ok()?;
|
||||
src.range(self.span())
|
||||
let span = self.span();
|
||||
if let Some(range) = span.range() {
|
||||
return Some(range.clone());
|
||||
}
|
||||
|
||||
let src = ctx.source_by_id(self.file_id()?).ok()?;
|
||||
src.range(span)
|
||||
}
|
||||
|
||||
pub fn as_def(this: &Interned<Self>, val: Option<Ty>) -> Interned<RefExpr> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue