Skip to content

Commit

Permalink
Add comments and clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
supersurviveur committed May 31, 2024
1 parent 40b4427 commit ed3f99d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 84 deletions.
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ wasm-pack build
```
Then you can run the extension by opening the project in VS Code and pressing `F5` to start a debug session.

### Tests
You can run tests with `cargo test`, and see [test coverage](https://doc.rust-lang.org/rustc/instrument-coverage.html) by running `coverage.sh`.

### Rendering mode
A few rendering modes are available :
- `nothing` : No rendering
Expand Down
1 change: 0 additions & 1 deletion typst-math-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod interface;
mod parser;
mod utils;

// use std::time::Instant;
use std::{collections::HashMap, ops::Range};

use crate::parser::parser::State;
Expand Down
22 changes: 22 additions & 0 deletions typst-math-rust/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ fn field_access_recursive(access: FieldAccess) -> Option<String> {
None
}

// Next functions are the blocks of the parser, each one match a specific expression and apply style

/// Parse a math ident block, symply add a symbol if it is in the symbols list
fn math_ident_block(parser: &mut InnerParser) {
let ident = unchecked_cast_expr::<MathIdent>(parser.expr);
parser.insert_result_symbol(
Expand All @@ -108,6 +111,9 @@ fn math_ident_block(parser: &mut InnerParser) {
("", ""),
);
}

/// Parse a field access block, create a string containing all fields sparated with a dot (alpha.alt), and check if it is in symbols list
/// Also check if the symbol starts with `sym.` and remove it if needed
fn field_access_block(parser: &mut InnerParser) {
let access = unchecked_cast_expr::<FieldAccess>(parser.expr);
if let Some(content) = field_access_recursive(access) {
Expand All @@ -131,6 +137,8 @@ fn field_access_block(parser: &mut InnerParser) {
);
}
}

/// Simply replace a linebreak with an arrow
fn linebreak_block(parser: &mut InnerParser) {
parser.insert_result(
parser.expr.range(),
Expand All @@ -145,6 +153,8 @@ fn linebreak_block(parser: &mut InnerParser) {
);
}

/// Parse a math attach block (subscript, superscript) \
/// Apply specific style and offset for each attachment, and compute specific style with rendering mode and current state
fn math_attach_block(parser: &mut InnerParser) {
let attachment = unchecked_cast_expr::<MathAttach>(parser.expr);
// Keep the current state to restore it after the attachment
Expand Down Expand Up @@ -205,6 +215,8 @@ fn math_attach_block(parser: &mut InnerParser) {
parser.state.is_attachment = state.is_attachment;
}

/// Parse a math block, check if it is a simple block (paren around a symbol) and propagate style if true \
/// Otherwise, continue over children and reset style
fn math_block(parser: &mut InnerParser) {
let children: Vec<LinkedNode> = parser.expr.children().collect();
// If we are in an attachment, check if the current math block is just paren around a symbol
Expand Down Expand Up @@ -281,6 +293,7 @@ fn math_block(parser: &mut InnerParser) {
}
}

/// Replace a shorthand with a specific style
fn shorthand_block(parser: &mut InnerParser) {
let short = unchecked_cast_expr::<Shorthand>(parser.expr);
let (color, decoration, content) = match short.get() {
Expand All @@ -303,6 +316,10 @@ fn shorthand_block(parser: &mut InnerParser) {
parser.offset,
);
}

/// Replace a text block with a specific style \
/// Some symbols are here instead of shorthand \
/// Also, if we are in an attachment, apply a specific style
fn text_block(parser: &mut InnerParser) {
let text = unchecked_cast_expr::<Text>(parser.expr);
if text.get().len() == 1 {
Expand Down Expand Up @@ -334,6 +351,9 @@ fn text_block(parser: &mut InnerParser) {
);
}
}

/// Same as text block, but for a string block (between quotes) \
/// Apply a specific style if we are in an attachment
fn str_block(parser: &mut InnerParser) {
let text = unchecked_cast_expr::<Str>(parser.expr);
if parser.state.is_attachment {
Expand All @@ -347,6 +367,8 @@ fn str_block(parser: &mut InnerParser) {
);
}
}

/// Parse a func call block, if it is a common func, apply style, else continue over args and callee
fn func_call_block(parser: &mut InnerParser) {
let func = unchecked_cast_expr::<FuncCall>(parser.expr);
let callee = parser.expr.find(func.callee().span()).unwrap();
Expand Down
34 changes: 25 additions & 9 deletions typst-math-rust/src/utils/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//! Stealed from https://github.com/typst/typst/blob/main/crates/typst/src/symbols/symbol.rs
//! and edited to be used in the frontend.


use phf::phf_map;
use std::fmt::Debug;
use typst_math_macros::symbols;
Expand Down Expand Up @@ -1130,20 +1129,37 @@ pub const BLACKBOLD_LETTERS: phf::Map<char, char> = phf_map! {
'9' => '𝟡',
};


#[cfg(test)]
mod tests {
use crate::utils::symbols::{get_category_by_name, Category};

#[test]
fn test_get_category_by_name() {
assert_eq!(get_category_by_name(&"leTter".to_string()), Category::Letter);
assert_eq!(get_category_by_name(&"coMparison".to_string()), Category::Comparison);
assert_eq!(get_category_by_name(&"keyword".to_string()), Category::Keyword);
assert_eq!(
get_category_by_name(&"leTter".to_string()),
Category::Letter
);
assert_eq!(
get_category_by_name(&"coMparison".to_string()),
Category::Comparison
);
assert_eq!(
get_category_by_name(&"keyword".to_string()),
Category::Keyword
);
assert_eq!(get_category_by_name(&"Set".to_string()), Category::Set);
assert_eq!(get_category_by_name(&"bigletter".to_string()), Category::BigLetter);
assert_eq!(get_category_by_name(&"Number".to_string()), Category::Number);
assert_eq!(
get_category_by_name(&"bigletter".to_string()),
Category::BigLetter
);
assert_eq!(
get_category_by_name(&"Number".to_string()),
Category::Number
);
assert_eq!(get_category_by_name(&"space".to_string()), Category::Space);
assert_eq!(get_category_by_name(&"doesn't exists".to_string()), Category::Default);
assert_eq!(
get_category_by_name(&"doesn't exists".to_string()),
Category::Default
);
}
}
}
100 changes: 27 additions & 73 deletions typst-math-rust/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
mod tests {
use typst_math_rust::parse_document;


#[test]
fn basic_symbol() {
let parsed = parse_document("$alpha$", -1, -1, 3, true, true, vec![], vec![]);
Expand Down Expand Up @@ -40,9 +39,27 @@ mod tests {
let parsed = parse_document("$x_alpha$", -1, -1, 3, true, true, vec![], vec![]);
assert_eq!(parsed.decorations[0].uuid, "bottom-alpha");

let parsed = parse_document("$x_alpha_alpha^alpha^alpha$", -1, -1, 3, true, true, vec![], vec![]);
let parsed = parse_document(
"$x_alpha_alpha^alpha^alpha$",
-1,
-1,
3,
true,
true,
vec![],
vec![],
);
assert_eq!(parsed.decorations.len(), 3);
let parsed = parse_document("$x_alpha_alpha^alpha^alpha$", -1, -1, 0, true, true, vec![], vec![]);
let parsed = parse_document(
"$x_alpha_alpha^alpha^alpha$",
-1,
-1,
0,
true,
true,
vec![],
vec![],
);
assert_eq!(parsed.decorations.len(), 1);
}

Expand Down Expand Up @@ -73,16 +90,7 @@ mod tests {
}
#[test]
fn test_functions() {
let parsed = parse_document(
"$arrow(x)$",
-1,
-1,
3,
true,
true,
vec![],
vec![],
);
let parsed = parse_document("$arrow(x)$", -1, -1, 3, true, true, vec![], vec![]);
assert_eq!(parsed.decorations.len(), 2);

// Check that not too many decorations are added
Expand Down Expand Up @@ -122,45 +130,18 @@ mod tests {
}
#[test]
fn test_field_access() {
let parsed = parse_document(
"$beta.alt$",
-1,
-1,
3,
true,
true,
vec![],
vec![],
);
let parsed = parse_document("$beta.alt$", -1, -1, 3, true, true, vec![], vec![]);
assert_eq!(parsed.decorations.len(), 1);
assert_eq!(parsed.decorations[0].symbol, "ϐ");
assert_eq!(parsed.decorations[0].uuid, "beta.alt");
let parsed = parse_document(
"$triangle.filled.b$",
-1,
-1,
3,
true,
true,
vec![],
vec![],
);
let parsed = parse_document("$triangle.filled.b$", -1, -1, 3, true, true, vec![], vec![]);
assert_eq!(parsed.decorations.len(), 1);
assert_eq!(parsed.decorations[0].symbol, "▼");
assert_eq!(parsed.decorations[0].uuid, "triangle.filled.b");
}
#[test]
fn test_text() {
let parsed = parse_document(
"$x^a x_a$",
-1,
-1,
3,
true,
true,
vec![],
vec![],
);
let parsed = parse_document("$x^a x_a$", -1, -1, 3, true, true, vec![], vec![]);
assert_eq!(parsed.decorations.len(), 2);
assert_eq!(parsed.decorations[0].symbol, "a");

Expand All @@ -179,42 +160,15 @@ mod tests {
}
#[test]
fn test_linebreak() {
let parsed = parse_document(
"$x$ \\ \\ \\ x",
-1,
-1,
3,
true,
true,
vec![],
vec![],
);
let parsed = parse_document("$x$ \\ \\ \\ x", -1, -1, 3, true, true, vec![], vec![]);
assert_eq!(parsed.decorations.len(), 1);
assert_eq!(parsed.decorations[0].symbol, "⮰");
}
#[test]
fn test_math_block() {
let parsed = parse_document(
"$x^(5+3-2)=6$",
-1,
-1,
3,
true,
true,
vec![],
vec![],
);
let parsed = parse_document("$x^(5+3-2)=6$", -1, -1, 3, true, true, vec![], vec![]);
assert_eq!(parsed.decorations.len(), 3);
let parsed = parse_document(
"$x^(alpha)$",
-1,
-1,
3,
true,
true,
vec![],
vec![],
);
let parsed = parse_document("$x^(alpha)$", -1, -1, 3, true, true, vec![], vec![]);
assert_eq!(parsed.decorations.len(), 2);
let parsed = parse_document(
"$x^(\"alpha\") x^(-\"alpha\") x^(-alpha)$",
Expand Down
1 change: 0 additions & 1 deletion typst-math-rust/tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1 + 1, 2);

}

0 comments on commit ed3f99d

Please sign in to comment.