dev: add Definition::file_id (#1626)

This commit is contained in:
Myriad-Dreamin 2025-04-04 16:08:38 +08:00 committed by GitHub
parent ec5be5d2b0
commit 04c013f3a4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 21 additions and 17 deletions

View file

@ -43,6 +43,11 @@ impl Definition {
Some((fid, range)) 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. /// The range of the name of the definition.
pub fn name_range(&self, ctx: &SharedContext) -> Option<Range<usize>> { pub fn name_range(&self, ctx: &SharedContext) -> Option<Range<usize>> {
self.decl.name_range(ctx) self.decl.name_range(ctx)

View file

@ -47,7 +47,7 @@ impl StatefulRequest for PrepareRenameRequest {
let origin_selection_range = ctx.to_lsp_range(syntax.node().range(), &source); let origin_selection_range = ctx.to_lsp_range(syntax.node().range(), &source);
let def = ctx.def_of_syntax(&source, doc, syntax.clone())?; let def = ctx.def_of_syntax(&source, doc, syntax.clone())?;
let (name, range) = prepare_renaming(ctx, &syntax, &def)?; let (name, range) = prepare_renaming(&syntax, &def)?;
Some(PrepareRenameResponse::RangeWithPlaceholder { Some(PrepareRenameResponse::RangeWithPlaceholder {
range: range.unwrap_or(origin_selection_range), range: range.unwrap_or(origin_selection_range),
@ -57,12 +57,11 @@ impl StatefulRequest for PrepareRenameRequest {
} }
pub(crate) fn prepare_renaming( pub(crate) fn prepare_renaming(
ctx: &mut LocalContext,
deref_target: &SyntaxClass, deref_target: &SyntaxClass,
def: &Definition, def: &Definition,
) -> Option<(String, Option<LspRange>)> { ) -> Option<(String, Option<LspRange>)> {
let name = def.name().clone(); let name = def.name().clone();
let (def_fid, _name_range) = def.location(ctx.shared()).clone()?; let def_fid = def.file_id()?;
if WorkspaceResolver::is_package_file(def_fid) { if WorkspaceResolver::is_package_file(def_fid) {
crate::log_debug_ct!( crate::log_debug_ct!(

View file

@ -44,7 +44,7 @@ impl StatefulRequest for RenameRequest {
let def = ctx.def_of_syntax(&source, doc, syntax.clone())?; let def = ctx.def_of_syntax(&source, doc, syntax.clone())?;
prepare_renaming(ctx, &syntax, &def)?; prepare_renaming(&syntax, &def)?;
match syntax { match syntax {
// todo: abs path // todo: abs path
@ -56,7 +56,7 @@ impl StatefulRequest for RenameRequest {
self.new_name self.new_name
}; };
let def_fid = def.location(ctx.shared())?.0; let def_fid = def.file_id()?;
// todo: rename in untitled files // todo: rename in untitled files
let old_path = ctx.path_for_id(def_fid).ok()?.to_err().ok()?; let old_path = ctx.path_for_id(def_fid).ok()?.to_err().ok()?;

View file

@ -438,6 +438,7 @@ impl Decl {
} }
} }
/// Gets file location of the declaration.
pub fn file_id(&self) -> Option<TypstFileId> { pub fn file_id(&self) -> Option<TypstFileId> {
match self { match self {
Self::Module(ModuleDecl { fid, .. }) => Some(*fid), Self::Module(ModuleDecl { fid, .. }) => Some(*fid),
@ -446,16 +447,16 @@ impl Decl {
} }
} }
// todo: name range /// Gets name range of the declaration.
/// The range of the name of the definition.
pub fn name_range(&self, ctx: &SharedContext) -> Option<Range<usize>> { pub fn name_range(&self, ctx: &SharedContext) -> Option<Range<usize>> {
if !self.is_def() {
return None;
}
if let Decl::BibEntry(decl) = self { if let Decl::BibEntry(decl) = self {
return Some(decl.at.1.clone()); return Some(decl.at.1.clone());
} }
if !self.is_def() {
return None;
}
let span = self.span(); let span = self.span();
if let Some(range) = span.range() { if let Some(range) = span.range() {
return Some(range.clone()); return Some(range.clone());

View file

@ -536,7 +536,6 @@ impl InsTy {
Value::Func(func) => func.span(), Value::Func(func) => func.span(),
Value::Args(args) => args.span, Value::Args(args) => args.span,
Value::Content(content) => content.span(), Value::Content(content) => content.span(),
// todo: module might have file id
_ => return None, _ => return None,
}) })
}) })
@ -966,9 +965,9 @@ impl SigTy {
pub fn matches<'a>( pub fn matches<'a>(
&'a self, &'a self,
args: &'a SigTy, args: &'a SigTy,
withs: Option<&'a Vec<Interned<crate::analysis::SigTy>>>, with: Option<&'a Vec<Interned<crate::analysis::SigTy>>>,
) -> impl Iterator<Item = (&'a Ty, &'a Ty)> + 'a { ) -> impl Iterator<Item = (&'a Ty, &'a Ty)> + 'a {
let with_len = withs let with_len = with
.map(|w| w.iter().map(|w| w.positional_params().len()).sum::<usize>()) .map(|w| w.iter().map(|w| w.positional_params().len()).sum::<usize>())
.unwrap_or(0); .unwrap_or(0);
@ -985,7 +984,7 @@ impl SigTy {
0 0
}; };
let arg_pos = withs let arg_pos = with
.into_iter() .into_iter()
.flat_map(|w| w.iter().rev().map(|w| w.positional_params())) .flat_map(|w| w.iter().rev().map(|w| w.positional_params()))
.flatten() .flatten()
@ -995,7 +994,7 @@ impl SigTy {
let arg_stream = arg_pos.chain(arg_rest.into_iter().cycle()).take(max_len); let arg_stream = arg_pos.chain(arg_rest.into_iter().cycle()).take(max_len);
let pos = sig_stream.zip(arg_stream); let pos = sig_stream.zip(arg_stream);
let common_ifaces = withs let common_ifaces = with
.map(|args_all| args_all.iter().rev()) .map(|args_all| args_all.iter().rev())
.into_iter() .into_iter()
.flatten() .flatten()
@ -1445,9 +1444,9 @@ mod tests {
fn matches( fn matches(
sig: Interned<SigTy>, sig: Interned<SigTy>,
args: Interned<SigTy>, args: Interned<SigTy>,
withs: Option<Vec<Interned<ArgsTy>>>, with: Option<Vec<Interned<ArgsTy>>>,
) -> String { ) -> String {
let res = sig.matches(&args, withs.as_ref()).collect::<Vec<_>>(); let res = sig.matches(&args, with.as_ref()).collect::<Vec<_>>();
format!("{res:?}") format!("{res:?}")
} }