]> git.eshelyaron.com Git - emacs.git/commitdiff
Add treesit-language-display-name
authorYuan Fu <casouri@gmail.com>
Tue, 24 Dec 2024 05:19:32 +0000 (21:19 -0800)
committerEshel Yaron <me@eshelyaron.com>
Fri, 27 Dec 2024 15:30:12 +0000 (16:30 +0100)
* lisp/treesit.el:
(treesit-language-display-name-alist): New variable.
(treesit-language-display-name): New function.
* doc/lispref/parsing.texi (Language Grammar): Add to manual.
* etc/NEWS: Add to NEWS.

(cherry picked from commit 251b4c8c39535fee9f6da89420483304274ac03e)

doc/lispref/parsing.texi
etc/NEWS
lisp/treesit.el

index 7f21c3864fcb641e1d8ccc8ceeba4ba8673096b1..fc56c20304b6a10475b832dcfd5c8548dda2370d 100644 (file)
@@ -160,6 +160,22 @@ grammar library loaded by Emacs for @var{language}.  If @var{language}
 is unavailable, this function returns @code{nil}.
 @end defun
 
+@vindex treesit-language-display-name-alist
+@defun treesit-language-display-name language
+This function translates @var{language} to an appropriate display name.
+For example, it translates @code{ruby} to ``Ruby'', @code{cpp} to
+``C++''.
+
+Most languages has ``regular'' names, and their display name is simply
+the symbol name with first letter capitalized.  For languages that has
+``irregular'' names, @var{treesit-language-display-name-alist} maps
+language symbols to their display names.
+
+If a major mode package uses a langauge with ``irregular'' name, they
+should add a mapping into @var{treesit-language-display-name-alist} on
+load.
+@end defun
+
 @heading Concrete syntax tree
 @cindex syntax tree, concrete
 
index fb69050d5e85087bb14dd17ef1703e82183125c8..4e563b603cf1769fc8f3175075916529a4c0f41e 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -832,6 +832,13 @@ The new function 'treesit-forward-sexp-list' uses 'sexp-list'
 to move across lists.  But to move across atoms inside the list
 it uses `forward-sexp-default-function'.
 
++++
+*** New function 'treesit-language-display-name'.
+New function that returns the display name given the language symbol.
+For example, 'cpp' is translated to "C++".  Also adds a new variable
+'treesit-language-display-name-alist' that the function uses to
+translate display names.
+
 +++
 ** New optional BUFFER argument for 'string-pixel-width'.
 If supplied, 'string-pixel-width' will use any face remappings from
index 3d6eb142ef496bb34bd9c7bd08a68b9ed9f675c7..49de28a9a6e87890a9b942dc4bc8b892ec95f0e4 100644 (file)
@@ -834,6 +834,36 @@ omitted, default END to BEG."
                return rng
                finally return nil))))
 
+;;; Language display name
+
+;; The entries are sorted by `sort-lines'.
+(defvar treesit-language-display-name-alist
+  '(
+    (charp . "C#")
+    (cmake . "CMake")
+    (cpp . "C++")
+    (gomod . "Go Mod")
+    (heex . "HEEx")
+    (json . "JSON")
+    (php . "PHP")
+    (tsx . "TSX")
+    )
+  "An alist mapping language symbols to their display names.
+
+Used by `treesit-language-display-name'.  If there's no mapping in this
+alist, `treesit-language-display-name' converts the symbol to display
+name by capitalizing the first letter.  So languages like Java,
+Javascript, Rust don't need an entry in this variable.")
+
+(defun treesit-language-display-name (language)
+  "Returns the display name (a string) of LANGUAGE.
+
+If LANGUAGE has an entry in `treesit-language-display-name-alist', use
+the display name in their.  Otherwise, capitalize the first letter of
+LANGUAGE and return the string."
+  (or (alist-get language treesit-language-display-name-alist)
+      (capitalize (symbol-name language))))
+
 ;;; Fontification
 
 (define-error 'treesit-font-lock-error