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.
This commit is contained in:
Yuan Fu 2024-12-23 21:19:32 -08:00
parent 563e5868f6
commit 251b4c8c39
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
3 changed files with 53 additions and 0 deletions

View 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

View file

@ -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

View file

@ -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