[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 251b4c8c395 1/4: Add treesit-language-display-name
From: |
Yuan Fu |
Subject: |
master 251b4c8c395 1/4: Add treesit-language-display-name |
Date: |
Tue, 24 Dec 2024 17:11:29 -0500 (EST) |
branch: master
commit 251b4c8c39535fee9f6da89420483304274ac03e
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
Add treesit-language-display-name
* 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.
---
doc/lispref/parsing.texi | 16 ++++++++++++++++
etc/NEWS | 7 +++++++
lisp/treesit.el | 30 ++++++++++++++++++++++++++++++
3 files changed, 53 insertions(+)
diff --git a/doc/lispref/parsing.texi b/doc/lispref/parsing.texi
index 7f21c3864fc..fc56c20304b 100644
--- a/doc/lispref/parsing.texi
+++ b/doc/lispref/parsing.texi
@@ -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
diff --git a/etc/NEWS b/etc/NEWS
index 847cc39ce96..9754322b1bf 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -992,6 +992,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
diff --git a/lisp/treesit.el b/lisp/treesit.el
index e37ea85ebbe..be264b4292a 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -835,6 +835,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