From 235f2e49aedabd6df48dc9d03d9c2a3378f0da77 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> Date: Tue, 14 May 2024 20:08:39 +0800 Subject: [PATCH] fix: filter unsettable params when making a set rule (#287) --- crates/tinymist-query/src/signature_help.rs | 54 ++++++++++++++++----- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/crates/tinymist-query/src/signature_help.rs b/crates/tinymist-query/src/signature_help.rs index 9b4bac21..733d82e0 100644 --- a/crates/tinymist-query/src/signature_help.rs +++ b/crates/tinymist-query/src/signature_help.rs @@ -1,4 +1,7 @@ +use once_cell::sync::OnceCell; + use crate::{ + adt::interner::Interned, analysis::{analyze_dyn_signature, find_definition, Ty}, prelude::*, syntax::{get_check_target, get_deref_target, CheckTarget, ParamTarget}, @@ -25,7 +28,13 @@ impl SemanticRequest for SignatureHelpRequest { let cursor = ctx.to_typst_pos(self.position, &source)? + 1; let ast_node = LinkedNode::new(source.root()).leaf_at(cursor)?; - let CheckTarget::Param { callee, target, .. } = get_check_target(ast_node)? else { + let CheckTarget::Param { + callee, + target, + is_set, + .. + } = get_check_target(ast_node)? + else { return None; }; @@ -63,16 +72,7 @@ impl SemanticRequest for SignatureHelpRequest { named.sort_by_key(|x| &x.name); - let active_parameter = match &target { - ParamTarget::Positional { positional, .. } => Some((*positional) + param_shift), - ParamTarget::Named(name) => { - let name = name.get().clone().into_text(); - named - .iter() - .position(|x| x.name.as_ref() == name.as_ref()) - .map(|i| pos.len() + i) - } - }; + let mut active_parameter = None; let mut label = def_link.name.clone(); let mut params = Vec::new(); @@ -88,7 +88,32 @@ impl SemanticRequest for SignatureHelpRequest { let rest = rest .iter() .map(|x| (x, type_sig.as_ref().and_then(|sig| sig.rest_param()))); - for (param, ty) in pos.chain(named).chain(rest) { + + let mut real_offset = 0; + let focus_name = OnceCell::new(); + for (i, (param, ty)) in pos.chain(named).chain(rest).enumerate() { + if is_set && !param.settable { + continue; + } + + match &target { + ParamTarget::Positional { .. } if is_set => {} + ParamTarget::Positional { positional, .. } => { + if (*positional) + param_shift == i { + active_parameter = Some(real_offset); + } + } + ParamTarget::Named(name) => { + let focus_name = focus_name + .get_or_init(|| Interned::new_str(&name.get().clone().into_text())); + if focus_name == ¶m.name { + active_parameter = Some(real_offset); + } + } + } + + real_offset += 1; + if !params.is_empty() { label.push_str(", "); } @@ -125,6 +150,11 @@ impl SemanticRequest for SignatureHelpRequest { label.push_str(ret_ty.describe().as_deref().unwrap_or("any")); } + if matches!(target, ParamTarget::Positional { .. }) { + active_parameter = + active_parameter.map(|x| x.min(sig.primary().pos.len().saturating_sub(1))); + } + trace!("got signature info {label} {params:?}"); Some(SignatureHelp {