branch: elpa/typst-ts-mode commit 5ee0e336838df2ae70b535cdaef28886949144ae Author: Ziqi Yang <mr.ziqiy...@gmail.com> Commit: Ziqi Yang <mr.ziqiy...@gmail.com>
feat: math fontification support --- basic-syntax.typ | 9 +++++++++ highlight.compare.scm | 28 +++++++++++++-------------- typst-ts-mode.el | 52 +++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 67 insertions(+), 22 deletions(-) diff --git a/basic-syntax.typ b/basic-syntax.typ index 70704ff11c..5628099a0e 100644 --- a/basic-syntax.typ +++ b/basic-syntax.typ @@ -20,6 +20,9 @@ sudo rm -rf * <label> // label @reference // reference +Hello\nWorld // escape + + #(4.2) // number #"El Psy Kongaroo" // string #[El Psy Kongraoo] // content @@ -70,3 +73,9 @@ sudo rm -rf * ) #(a, c: b) // tagged #a.b // field + +$a$ // math +$ 1 + 1 = 2 $ +$ E = m * c^2 $ +$eq.not(0)$ +$ cal(A) := { x in RR | x "is natural" } $ diff --git a/highlight.compare.scm b/highlight.compare.scm index e88ab2506b..969f1bb231 100644 --- a/highlight.compare.scm +++ b/highlight.compare.scm @@ -30,9 +30,9 @@ (mul "*" @operator) ;; DONE code (div "/" @operator) ;; DONE code (cmp ["==" "<=" ">=" "!=" "<" ">"] @operator) ;; DONE code -(fraction "/" @operator) ;; TODO math -(fac "!" @operator) ;; TODO math -(attach ["^" "_"] @operator) ;; TODO math +(fraction "/" @operator) ;; DONE math +(fac "!" @operator) ;; DONE math +(attach ["^" "_"] @operator) ;; DONE math (wildcard) @operator ;; DONE code ; VALUE @@ -61,19 +61,19 @@ (strong "*" @operator) @markup.bold ;; DONE markup (item) @markup.list ;; DONE markup (term) @markup.list ;; DONE markup -(symbol) @constant.character ;; TODO math +(symbol) @constant.character ;; DONE math (shorthand) @constant.builtin ;; DONE common (quote) @markup.quote ;; DONE markup -(align) @operator ;; TODO math -(letter) @constant.character ;; TODO math +(align) @operator ;; DONE math +(letter) @constant.character ;; DONE math (linebreak) @constant.builtin ;; DONE markup -(math "$" @operator) -"#" @operator -"end" @operator +(math "$" @operator) ;; DONE math +"#" @operator ;; DONE code +"end" @operator ;; DONE code -(escape) @constant.character.escape -["(" ")" "{" "}"] @ponctuation.bracket -["," ";" ".." ":" "sep"] @ponctuation.delimiter -"assign" @ponctuation -(field "." @ponctuation) +(escape) @constant.character.escape ;; DONE markup +["(" ")" "{" "}"] @ponctuation.bracket ;; DONE +["," ";" ".." ":" "sep"] @ponctuation.delimiter ;; DONE +"assign" @ponctuation ;; DONE +(field "." @ponctuation) ;; DONE diff --git a/typst-ts-mode.el b/typst-ts-mode.el index b1a76137bf..cbb4dcf7cc 100644 --- a/typst-ts-mode.el +++ b/typst-ts-mode.el @@ -166,6 +166,11 @@ "Face for linebreak." :group 'typst-ts-faces) +(defface typst-ts-markup-escape-face + '((t :inherit escape-glyph)) + "Face for linebreak." + :group 'typst-ts-faces) + (defface typst-ts-markup-raw-indicator-face '((t :inherit shadow)) "Face for rawblock and rawspan indicator." @@ -221,6 +226,22 @@ "Face for reference." :group 'typst-ts-faces) +;; Code Faces =================================================================== + +(defface typst-ts-code-indicator-face + '((t :inherit shadow)) + "Face for code indicator #." + :group 'typst-ts-faces) + + +;; Math Faces =================================================================== + +(defface typst-ts-math-indicator-face + '((t :inherit shadow)) + "Face for math indicator $." + :group 'typst-ts-faces) + + (defvar typst-ts-mode-font-lock-rules '(;; Typst font locking :language typst @@ -245,6 +266,7 @@ (text) @typst-ts-markup-term-description-face) (quote) @typst-ts-markup-quote-face (linebreak) @typst-ts-markup-linebreak-face + (escape) @typst-ts-markup-escape-face (raw_span "`" @typst-ts-markup-rawspan-indicator-face (blob) @typst-ts-markup-rawspan-blob-face @@ -257,9 +279,12 @@ (label) @typst-ts-markup-label-face ;; TODO more precise highlight (upstream) (ref) @typst-ts-markup-reference-face) + ;; please note that some feature there also in the math mode :language typst :feature code - ((number) @font-lock-number-face + ("#" @typst-ts-code-indicator-face + ;; "end" @typst-ts-code-indicator-face ;; "end" is nothing but only a indicator + (number) @font-lock-number-face (string) @font-lock-string-face (content ["[" "]"] @font-lock-punctuation-face) (builtin) @font-lock-builtin-face @@ -268,6 +293,11 @@ (auto) @font-lock-constant-face (ident) @font-lock-variable-use-face + ["(" ")" "{" "}"] @font-lock-punctuation-face + ["," ";" ".." ":" "sep"] @font-lock-punctuation-face + "assign" @font-lock-punctuation-face + (field "." @font-lock-punctuation-face) + ;; operator (in ["in" "not"] @font-lock-keyword-face) (and "and" @font-lock-keyword-face) @@ -301,7 +331,18 @@ item: (field field: (ident) @font-lock-function-call-face)) (tagged field: (ident) @font-lock-variable-name-face) (field field: (ident) @font-lock-constant-face)) - )) + + :language typst + :feature math + ((math "$" @typst-ts-math-indicator-face) + + (fraction "/" @font-lock-operator-face) + (fac "!" @font-lock-operator-face) + (attach ["^" "_"] @font-lock-operator-face) + (align) @font-lock-operator-face + + (symbol) @font-lock-constant-face + (letter) @font-lock-constant-face))) (defun typst-ts-mode-comment-setup() "Setup comment related stuffs for typst-ts-mode." @@ -340,12 +381,7 @@ (apply #'treesit-font-lock-rules typst-ts-mode-font-lock-rules)) (setq-local treesit-font-lock-feature-list ;; TODO - '((comment common markup code) - ;; (markup code) - ;; (builtin) - ;; (operator ponctuation) - ;; (bracket delimiter error function operator property variable) - )) + '((comment common markup code math))) (treesit-major-mode-setup))