From 026e116f1cff71dc094c87a48f56d8c4f254cfd7 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Mon, 23 Dec 2024 21:19:32 -0800 Subject: [PATCH] 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. (cherry picked from commit 251b4c8c39535fee9f6da89420483304274ac03e) --- 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 fb69050d5e8..4e563b603cf 100644 --- 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 diff --git a/lisp/treesit.el b/lisp/treesit.el index 3d6eb142ef4..49de28a9a6e 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -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 -- 2.39.5