Skip to content

Commit

Permalink
implement using linear search
Browse files Browse the repository at this point in the history
  • Loading branch information
Markos-Th09 committed Oct 30, 2023
1 parent e89d672 commit 951a10b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 130 deletions.
66 changes: 0 additions & 66 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@ keywords.workspace = true
categories.workspace = true
license.workspace = true
readme = "README.md"

[dependencies]
phf = { version = "0.11.2", features = ["macros"] }
142 changes: 81 additions & 61 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Completion>) {
Expand All @@ -504,16 +505,35 @@ pub fn get_completions_for_prefix(mut prefix: &str) -> (usize, Vec<Completion>)
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(),
}],
)
});
}

Check warning on line 537 in core/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

core/src/lib.rs#L508-L537

Added lines #L508 - L537 were not covered by tests
if prefix.is_empty() {
return (0, vec![]);
Expand Down

0 comments on commit 951a10b

Please sign in to comment.