From 363e85dbd92b30395bb6f7273e99bf11e2436206 Mon Sep 17 00:00:00 2001 From: MarkosTh09 Date: Thu, 26 Oct 2023 13:59:52 +0400 Subject: [PATCH 1/3] add completion for greek letters --- Cargo.lock | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ core/Cargo.toml | 1 + core/src/lib.rs | 39 +++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 5188f501..319ab8b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -165,6 +165,9 @@ dependencies = [ [[package]] name = "fend-core" version = "1.3.1" +dependencies = [ + "phf", +] [[package]] name = "fend-wasm" @@ -408,6 +411,48 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" +[[package]] +name = "phf" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" +dependencies = [ + "phf_macros", + "phf_shared", +] + +[[package]] +name = "phf_generator" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0" +dependencies = [ + "phf_shared", + "rand", +] + +[[package]] +name = "phf_macros" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "phf_shared" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" +dependencies = [ + "siphasher", +] + [[package]] name = "pkg-config" version = "0.3.27" @@ -442,6 +487,21 @@ dependencies = [ "nibble_vec", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + [[package]] name = "redox_syscall" version = "0.3.5" @@ -599,6 +659,12 @@ dependencies = [ "serde", ] +[[package]] +name = "siphasher" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" + [[package]] name = "smallvec" version = "1.11.1" diff --git a/core/Cargo.toml b/core/Cargo.toml index c98281a7..44ad4689 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -11,3 +11,4 @@ license.workspace = true readme = "README.md" [dependencies] +phf = { version = "0.11.2", features = ["macros"] } diff --git a/core/src/lib.rs b/core/src/lib.rs index 57c4279c..3fac1608 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -44,6 +44,7 @@ use std::{collections::HashMap, fmt, io}; use error::FendError; pub(crate) use eval::Attrs; pub use interrupt::Interrupt; +use phf::phf_map; use serialize::{deserialize_string, deserialize_usize, serialize_string, serialize_usize}; /// This contains the result of a computation. @@ -444,6 +445,33 @@ impl Completion { } } +static GREEK_LETTERS: phf::Map<&'static str, &'static str> = phf_map! { + "\\alpha" => "α", + "\\beta" => "β", + "\\gamma" => "γ", + "\\delta" => "δ", + "\\epsilon" => "ε", + "\\zeta" => "ζ", + "\\eta" => "η", + "\\theta" => "θ", + "\\iota" => "ι", + "\\kappa" => "κ", + "\\lambda" => "λ", + "\\mu" => "μ", + "\\nu" => "ν", + "\\xi" => "ξ", + "\\omicron" => "ο", + "\\pi" => "π", + "\\rho" => "ρ", + "\\sigma" => "σ", + "\\tau" => "τ", + "\\upsilon" => "υ", + "\\phi" => "φ", + "\\chi" => "χ", + "\\psi" => "ψ", + "\\omega" => "ω", +}; + #[must_use] pub fn get_completions_for_prefix(mut prefix: &str) -> (usize, Vec) { let mut prepend = ""; @@ -452,6 +480,17 @@ pub fn get_completions_for_prefix(mut prefix: &str) -> (usize, Vec) prepend = a; prefix = b; } + if prefix.starts_with('\\') { + return GREEK_LETTERS.get(prefix).map_or((0, vec![]), |&letter| { + ( + 0, + vec![Completion { + display: prefix.to_owned(), + insert: letter.to_owned(), + }], + ) + }); + } if prefix.is_empty() { return (0, vec![]); } From e89d672c254fe675b4b9ea04dd92674ded3935bf Mon Sep 17 00:00:00 2001 From: MarkosTh09 Date: Thu, 26 Oct 2023 14:09:06 +0400 Subject: [PATCH 2/3] add capital letters --- core/src/lib.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/core/src/lib.rs b/core/src/lib.rs index 3fac1608..f3e00aea 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -470,6 +470,30 @@ static GREEK_LETTERS: phf::Map<&'static str, &'static str> = phf_map! { "\\chi" => "χ", "\\psi" => "ψ", "\\omega" => "ω", + "\\Alpha" => "Α", + "\\Beta" => "Β", + "\\Gamma" => "Γ", + "\\Delta" => "Δ", + "\\Epsilon" => "Ε", + "\\Zeta" => "Ζ", + "\\Eta" => "Η", + "\\Theta" => "Θ", + "\\Iota" => "Ι", + "\\Kappa" => "Κ", + "\\Lambda" => "Λ", + "\\Mu" => "Μ", + "\\Nu" => "Ν", + "\\Xi" => "Ξ", + "\\Omicron" => "Ο", + "\\Pi" => "Π", + "\\Rho" => "Ρ", + "\\Sigma" => "Σ", + "\\Tau" => "Τ", + "\\Upsilon" => "Υ", + "\\Phi" => "Φ", + "\\Chi" => "Χ", + "\\Psi" => "Ψ", + "\\Omega" => "Ω", }; #[must_use] From 951a10bd0cf845cd1f713478bbcb4e6785b82258 Mon Sep 17 00:00:00 2001 From: MarkosTh09 Date: Mon, 30 Oct 2023 11:15:26 +0400 Subject: [PATCH 3/3] implement using linear search --- Cargo.lock | 66 ---------------------- core/Cargo.toml | 3 - core/src/lib.rs | 142 +++++++++++++++++++++++++++--------------------- 3 files changed, 81 insertions(+), 130 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 319ab8b4..5188f501 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -165,9 +165,6 @@ dependencies = [ [[package]] name = "fend-core" version = "1.3.1" -dependencies = [ - "phf", -] [[package]] name = "fend-wasm" @@ -411,48 +408,6 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" -[[package]] -name = "phf" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" -dependencies = [ - "phf_macros", - "phf_shared", -] - -[[package]] -name = "phf_generator" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0" -dependencies = [ - "phf_shared", - "rand", -] - -[[package]] -name = "phf_macros" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b" -dependencies = [ - "phf_generator", - "phf_shared", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "phf_shared" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" -dependencies = [ - "siphasher", -] - [[package]] name = "pkg-config" version = "0.3.27" @@ -487,21 +442,6 @@ dependencies = [ "nibble_vec", ] -[[package]] -name = "rand" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" -dependencies = [ - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" - [[package]] name = "redox_syscall" version = "0.3.5" @@ -659,12 +599,6 @@ dependencies = [ "serde", ] -[[package]] -name = "siphasher" -version = "0.3.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" - [[package]] name = "smallvec" version = "1.11.1" diff --git a/core/Cargo.toml b/core/Cargo.toml index 44ad4689..74c67dfd 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -9,6 +9,3 @@ keywords.workspace = true categories.workspace = true license.workspace = true readme = "README.md" - -[dependencies] -phf = { version = "0.11.2", features = ["macros"] } diff --git a/core/src/lib.rs b/core/src/lib.rs index f3e00aea..7daa741e 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -44,7 +44,6 @@ use std::{collections::HashMap, fmt, io}; use error::FendError; pub(crate) use eval::Attrs; pub use interrupt::Interrupt; -use phf::phf_map; use serialize::{deserialize_string, deserialize_usize, serialize_string, serialize_usize}; /// This contains the result of a computation. @@ -445,56 +444,58 @@ impl Completion { } } -static GREEK_LETTERS: phf::Map<&'static str, &'static str> = phf_map! { - "\\alpha" => "α", - "\\beta" => "β", - "\\gamma" => "γ", - "\\delta" => "δ", - "\\epsilon" => "ε", - "\\zeta" => "ζ", - "\\eta" => "η", - "\\theta" => "θ", - "\\iota" => "ι", - "\\kappa" => "κ", - "\\lambda" => "λ", - "\\mu" => "μ", - "\\nu" => "ν", - "\\xi" => "ξ", - "\\omicron" => "ο", - "\\pi" => "π", - "\\rho" => "ρ", - "\\sigma" => "σ", - "\\tau" => "τ", - "\\upsilon" => "υ", - "\\phi" => "φ", - "\\chi" => "χ", - "\\psi" => "ψ", - "\\omega" => "ω", - "\\Alpha" => "Α", - "\\Beta" => "Β", - "\\Gamma" => "Γ", - "\\Delta" => "Δ", - "\\Epsilon" => "Ε", - "\\Zeta" => "Ζ", - "\\Eta" => "Η", - "\\Theta" => "Θ", - "\\Iota" => "Ι", - "\\Kappa" => "Κ", - "\\Lambda" => "Λ", - "\\Mu" => "Μ", - "\\Nu" => "Ν", - "\\Xi" => "Ξ", - "\\Omicron" => "Ο", - "\\Pi" => "Π", - "\\Rho" => "Ρ", - "\\Sigma" => "Σ", - "\\Tau" => "Τ", - "\\Upsilon" => "Υ", - "\\Phi" => "Φ", - "\\Chi" => "Χ", - "\\Psi" => "Ψ", - "\\Omega" => "Ω", -}; +static GREEK_LOWERCASE_LETTERS: [(&str, &str); 24] = [ + ("alpha", "α"), + ("beta", "β"), + ("gamma", "γ"), + ("delta", "δ"), + ("epsilon", "ε"), + ("zeta", "ζ"), + ("eta", "η"), + ("theta", "θ"), + ("iota", "ι"), + ("kappa", "κ"), + ("lambda", "λ"), + ("mu", "μ"), + ("nu", "ν"), + ("xi", "ξ"), + ("omicron", "ο"), + ("pi", "π"), + ("rho", "ρ"), + ("sigma", "σ"), + ("tau", "τ"), + ("upsilon", "υ"), + ("phi", "φ"), + ("chi", "χ"), + ("psi", "ψ"), + ("omega", "ω"), +]; +static GREEK_UPPERCASE_LETTERS: [(&str, &str); 24] = [ + ("Alpha", "Α"), + ("Beta", "Β"), + ("Gamma", "Γ"), + ("Delta", "Δ"), + ("Epsilon", "Ε"), + ("Zeta", "Ζ"), + ("Eta", "Η"), + ("Theta", "Θ"), + ("Iota", "Ι"), + ("Kappa", "Κ"), + ("Lambda", "Λ"), + ("Mu", "Μ"), + ("Nu", "Ν"), + ("Xi", "Ξ"), + ("Omicron", "Ο"), + ("Pi", "Π"), + ("Rho", "Ρ"), + ("Sigma", "Σ"), + ("Tau", "Τ"), + ("Upsilon", "Υ"), + ("Phi", "Φ"), + ("Chi", "Χ"), + ("Psi", "Ψ"), + ("Omega", "Ω"), +]; #[must_use] pub fn get_completions_for_prefix(mut prefix: &str) -> (usize, Vec) { @@ -504,16 +505,35 @@ pub fn get_completions_for_prefix(mut prefix: &str) -> (usize, Vec) prepend = a; prefix = b; } - if prefix.starts_with('\\') { - return GREEK_LETTERS.get(prefix).map_or((0, vec![]), |&letter| { - ( - 0, - vec![Completion { - display: prefix.to_owned(), - insert: letter.to_owned(), - }], - ) - }); + if let Some(letter) = prefix.strip_prefix('\\') { + if letter.starts_with(|c: char| !c.is_ascii_alphabetic()) { + return (0, vec![]); + } else if letter.starts_with(|c: char| c.is_ascii_uppercase()) { + return GREEK_UPPERCASE_LETTERS + .iter() + .find(|l| l.0 == letter) + .map_or((0, vec![]), |l| { + ( + 0, + vec![Completion { + display: prefix.to_string(), + insert: l.1.to_string(), + }], + ) + }); + } + return GREEK_LOWERCASE_LETTERS + .iter() + .find(|l| l.0 == letter) + .map_or((0, vec![]), |l| { + ( + 0, + vec![Completion { + display: prefix.to_string(), + insert: l.1.to_string(), + }], + ) + }); } if prefix.is_empty() { return (0, vec![]);