branch: elpa/typst-ts-mode
commit 5f3e28bee0c5569d408c416915bdf2f6ab7619fd
Author: Meow King <mr.meowk...@anche.no>
Commit: Meow King <mr.meowk...@anche.no>

    feat: add utility function to modify language tags relationship
---
 justfile                            |   4 ++
 typst-ts-embedding-lang-settings.el | 103 +++++++++++++++++++++++++++++++-----
 typst-ts-mode.el                    |   8 ++-
 3 files changed, 100 insertions(+), 15 deletions(-)

diff --git a/justfile b/justfile
new file mode 100644
index 0000000000..16cd0d90d2
--- /dev/null
+++ b/justfile
@@ -0,0 +1,4 @@
+els-settings-test:
+    emacs --batch -l ./typst-ts-embedding-lang-settings.el \
+    -l ~/.emacs.d/.local/elpaca/repos/emacs-kotlin-ts-mode/kotlin-ts-mode.el \
+    --eval "(typst-ts-embedding-lang-settings-test)"
diff --git a/typst-ts-embedding-lang-settings.el 
b/typst-ts-embedding-lang-settings.el
index fdbf41a6a9..3a1097c8b2 100644
--- a/typst-ts-embedding-lang-settings.el
+++ b/typst-ts-embedding-lang-settings.el
@@ -566,6 +566,14 @@ languages in settings."
              '((comment constant number pair string)
                (escape-sequence)
                (bracket delimiter error))))
+    (kotlin . (:feature
+               kotlin-ts-mode
+               :font-lock kotlin-ts-mode--treesit-settings
+               :indentation kotlin-ts-mode--treesit-indent-rules
+               :ts-feature-list
+               '((comment number string definition)
+                 (keyword builtin type constant variable)
+                 (escape-sequence function property))))
     (python . (:feature
                python
                :font-lock python--treesit-settings
@@ -759,24 +767,91 @@ Use this function as one notifier of 
`treesit-parser-notifiers'."
           (add-to-list 'typst-ts-els--include-languages lang))
         ))))
 
+;;; Utilities functions for changing language tag relationship (change two maps
+;;; synchronizely) 
=============================================================
+
+;;;###autoload
+(defun typst-ts-els--add-lang-tags-relationship (lang tags)
+  "Add or modify language tags relationship.
+This function will make changes to `typst-ts-els-lang-tags-map' and
+`typst-ts-els-tag-lang-map'.
+LANG: either a symbol or string.
+TAGS: either a string or a list of strings."
+  (let ((lang (if (symbolp lang)
+                  lang
+                (if (stringp lang)
+                    (intern lang)
+                  (error "LANG should be either symbol or string"))))
+        (tags (if (stringp tags)
+                  (list tags)
+                (if (and (listp tags)
+                         (stringp (nth 0 tags)))
+                    tags
+                  (error "Tags should be either a string or a list of 
strings"))))
+        (original-tags (gethash lang typst-ts-els-lang-tags-map))
+        temp-lang)
+    (dolist (tag tags)
+      (setq temp-lang (gethash tag typst-ts-els-tag-lang-map))
+      (when (and temp-lang (not (eq temp-lang lang)))
+        (puthash temp-lang
+                 (remove tag (gethash temp-lang typst-ts-els-lang-tags-map))
+                 typst-ts-els-lang-tags-map))
+      
+      (puthash tag lang typst-ts-els-tag-lang-map))
+    (puthash lang (seq-uniq (append tags original-tags)) 
typst-ts-els-lang-tags-map)))
+
+;;;###autoload
+(defun typst-ts-els--lang-name-remap (lang newlang)
+  "Remap language name to a new language name.
+This function will remap lang to newlang for `typst-ts-els-lang-tags-map' and
+`typst-ts-els-tag-lang-map'.
+LANG and NEWLANG: either a symbol or string."
+  (let ((lang (if (symbolp lang)
+                  lang
+                (if (stringp lang)
+                    (intern lang)
+                  (error "LANG should be either symbol or string"))))
+        (newlang (if (symbolp newlang)
+                     newlang
+                   (if (stringp newlang)
+                       (intern newlang)
+                     (error "NEWLANG should be either symbol or string"))))
+        lang-tags newlang-tags)
+    (unless (eq lang newlang)
+      (setq lang-tags (gethash lang typst-ts-els-lang-tags-map))
+      (setq newlang-tags (gethash newlang typst-ts-els-lang-tags-map))
+      
+      (dolist (tag lang-tags)
+        (puthash tag newlang typst-ts-els-tag-lang-map))
+      
+      (puthash newlang (append newlang-tags lang-tags) 
typst-ts-els-lang-tags-map)
+      (remhash lang typst-ts-els-lang-tags-map))))
+
 ;;; Test Utilities 
=============================================================
 
 (defun typst-ts-embedding-lang-settings-test ()
   "Test typst-ts-embedding-lang-settings."
-  (setq-local treesit-font-lock-feature-list
-              '((comment common)
-                (markup-basic code-basic math-basic)
-                (markup-standard code-standard math-standard)
-                (markup-extended code-extended math-extended)))
-  (dolist (setting-entry typst-ts-embedding-lang-settings)
-    (let ((language (car setting-entry))
-          (config (cdr setting-entry)))
-      (message "Testing %s ..."  language)
-      (unless (treesit-ready-p language t)
-        (message "!!! Doesn't have %s dynamic library file." language))
-      (typst-ts-els-merge-settings config)))
-  (message "No setting problem found!"))
-
+  (let ((treesit-font-lock-feature-list
+         '((comment common)
+           (markup-basic code-basic math-basic)
+           (markup-standard code-standard math-standard)
+           (markup-extended code-extended math-extended)))
+        missing-dylibs err-msgs)
+    (dolist (setting-entry typst-ts-embedding-lang-settings)
+      (let ((language (car setting-entry))
+            (config (cdr setting-entry)))
+        (message "Testing %s ..."  language)
+        (unless (treesit-ready-p language t)
+          (setq missing-dylibs (list (symbol-name language))))
+        (condition-case err
+            (typst-ts-els-merge-settings config)
+          (error
+           (setq err-msgs (list (error-message-string err)))))))
+    (message "---------  Missing Tree Sitter Dynamic libraries -------------")
+    (message " (This also could be an error of settings entry key name) ")
+    (message "%s" (string-join missing-dylibs " "))
+    (message "---------  Error Messages ------------------------------------")
+    (message "%s" (string-join err-msgs "\n"))))
 
 (provide 'typst-ts-embedding-lang-settings)
 
diff --git a/typst-ts-mode.el b/typst-ts-mode.el
index 532f5a4646..fe7d026c54 100644
--- a/typst-ts-mode.el
+++ b/typst-ts-mode.el
@@ -1002,7 +1002,11 @@ See `treesit-language-at-point-function'."
   (cl-loop for lang in langs
            when (treesit-ready-p lang)
            nconc
-           (typst-ts-els--treesit-range-rules lang)))
+           (condition-case err
+               (typst-ts-els--treesit-range-rules lang)
+             (error
+              (message "%s" (error-message-string err))
+              nil))))
 
 ;;;###autoload
 (define-derived-mode typst-ts-mode text-mode "Typst"
@@ -1012,6 +1016,8 @@ See `treesit-language-at-point-function'."
   :after-hook
   ;; it seems like the following code only works in this place (after-hook)
   (when typst-ts-mode-highlight-raw-blocks-at-startup
+    ;; since currently local parsers haven't created, we cannot only load
+    ;; those necessary parsers 
     (cl-loop for setting in typst-ts-embedding-lang-settings
              for lang = (car setting)
              for config = (cdr setting)

Reply via email to