From 09e0464e875fccecd6f2f686d462ad2c4a3b5ecd Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 15 Jul 2024 20:01:51 +0200 Subject: [PATCH] Fix duplicate completions (#4563) --- crates/typst-ide/src/complete.rs | 27 +++++++++---------------- crates/typst/src/foundations/methods.rs | 21 +------------------ crates/typst/src/foundations/mod.rs | 2 +- 3 files changed, 12 insertions(+), 38 deletions(-) diff --git a/crates/typst-ide/src/complete.rs b/crates/typst-ide/src/complete.rs index f6c96d001..d534f55cc 100644 --- a/crates/typst-ide/src/complete.rs +++ b/crates/typst-ide/src/complete.rs @@ -5,8 +5,8 @@ use ecow::{eco_format, EcoString}; use if_chain::if_chain; use serde::{Deserialize, Serialize}; use typst::foundations::{ - fields_on, format_str, mutable_methods_on, repr, AutoValue, CastInfo, Func, Label, - NoneValue, Repr, Scope, StyleChain, Styles, Type, Value, + fields_on, format_str, repr, AutoValue, CastInfo, Func, Label, NoneValue, Repr, + Scope, StyleChain, Styles, Type, Value, }; use typst::model::Document; use typst::syntax::{ @@ -396,19 +396,6 @@ fn field_access_completions( } } - for &(method, args) in mutable_methods_on(value.ty()) { - ctx.completions.push(Completion { - kind: CompletionKind::Func, - label: method.into(), - apply: Some(if args { - eco_format!("{method}(${{}})") - } else { - eco_format!("{method}()${{}}") - }), - detail: None, - }) - } - for &field in fields_on(value.ty()) { // Complete the field name along with its value. Notes: // 1. No parentheses since function fields cannot currently be called @@ -1394,7 +1381,7 @@ mod tests { } #[test] - fn test_whitespace_in_autocomplete() { + fn test_autocomplete_whitespace() { //Check that extra space before '.' is handled correctly. test("#() .", 5, &[], &["insert", "remove", "len", "all"]); test("#{() .}", 6, &["insert", "remove", "len", "all"], &["foo"]); @@ -1404,10 +1391,16 @@ mod tests { } #[test] - fn test_before_window_char_boundary() { + fn test_autocomplete_before_window_char_boundary() { // Check that the `before_window` doesn't slice into invalid byte // boundaries. let s = "😀😀 #text(font: \"\")"; test(s, s.len() - 2, &[], &[]); } + + #[test] + fn test_autocomplete_mutable_method() { + let s = "#{ let x = (1, 2, 3); x. }"; + test(s, s.len() - 2, &["at", "push", "pop"], &[]); + } } diff --git a/crates/typst/src/foundations/methods.rs b/crates/typst/src/foundations/methods.rs index 287a49c69..945b7c507 100644 --- a/crates/typst/src/foundations/methods.rs +++ b/crates/typst/src/foundations/methods.rs @@ -1,28 +1,9 @@ //! Handles special built-in methods on values. use crate::diag::{At, SourceResult}; -use crate::foundations::{Args, Array, Dict, Str, Type, Value}; +use crate::foundations::{Args, Str, Type, Value}; use crate::syntax::Span; -/// List the available methods for a type and whether they take arguments. -pub fn mutable_methods_on(ty: Type) -> &'static [(&'static str, bool)] { - if ty == Type::of::() { - &[ - ("first", false), - ("last", false), - ("at", true), - ("pop", false), - ("push", true), - ("insert", true), - ("remove", true), - ] - } else if ty == Type::of::() { - &[("at", true), ("insert", true), ("remove", true)] - } else { - &[] - } -} - /// Whether a specific method is mutating. pub(crate) fn is_mutating_method(method: &str) -> bool { matches!(method, "push" | "pop" | "insert" | "remove") diff --git a/crates/typst/src/foundations/mod.rs b/crates/typst/src/foundations/mod.rs index b7783dda9..f9dfff4f5 100644 --- a/crates/typst/src/foundations/mod.rs +++ b/crates/typst/src/foundations/mod.rs @@ -49,7 +49,7 @@ pub use self::float::*; pub use self::func::*; pub use self::int::*; pub use self::label::*; -pub use self::methods::*; +pub(crate) use self::methods::*; pub use self::module::*; pub use self::none::*; pub use self::plugin::*;