Subject: bug#75774: 30.0.92; `treesit-thing-settings' not set properly for typescript-ts-mode

From: Noel Errenil <[email-masked]>
Date: Thu, 23 Jan 2025 14:44:49 +1100

From: [email-masked]
To: [email-masked]
Subject: 30.0.92; treesit-thing-settings not set properly for typescript-ts-mode
X-Debbugs-Cc:
--text follows this line--

While using the shipped typescript-ts-mode to edit a typescript file, I
found I cannot use `C-M-f' to forward sexpressions as I usually do with
other major modes (e.g. c-ts-mode, rust-mode, etc). For example, given
the following code snippet

import { Chain } from './common'
           ^

If I put my point under the opening `{' and press `C-M-f', the point
will move immediately after `Chain',  instead of at the closing `}'.

Here is another example:

constructor(url: string) {
        console.log('SuiTransport', url)

If I put the pointer under the first `(' after the word `constructor',
and then press `C-M-f', the pointer will move to the `:' after
`url'. However, forwarding sexp works for the parentheses after `log'.

I tried to search through the Emacs manual and found the most likely
 relevant variable `treesit-thing-settings`, which has a default,
 local-to-buffer value for typescript-ts-mode as below:

((typescript
  (sexp
   "\\(?:ar\\(?:guments\\|ray\\)\\|e\\(?:scape\\|xpression\\)\\|f\\(?:alse\\|unction\\)\\|identifier\\|nu\\(?:ll\\|mber\\)\\|pa\\(?:ir\\|ttern\\)\\|regex\\|s\\(?:tring\\|uper\\)\\|t\\(?:emplate\\|his\\|rue\\)\\|undefined\\)")
  (sentence
   "\\(?:break_statement\\|continue_statement\\|d\\(?:\\(?:ebugger\\|o\\)_statement\\)\\|e\\(?:\\(?:mpty\\|xpression\\)_statement\\)\\|for_\\(?:\\(?:in_\\)?statement\\)\\|i\\(?:\\(?:f\\|mport\\)_statement\\)\\|l\\(?:abeled_statement\\|exical_declaration\\)\\|property_signature\\|return_statement\\|switch_statement\\|t\\(?:\\(?:hrow\\|ry\\)_statement\\)\\|variable_declaration\\|w\\(?:\\(?:hile\\|ith\\)_statement\\)\\)")
  (text "\\(?:comment\\|template_string\\)")))

This does explain the behavior I have experienced so far: since
`arguments', `identifier' are all recognized as sexp, the pointer
moves to and stops after them, which is really not intuitive compared to the
more common Emacs's view of sexp: expressions with balanced structures.

In contrast, the c-ts-mode has set `treesit-thing-settings' to the
following value:

(setq treesit-thing-settings
      '((typescript . ((sexp . (not "[](),[{}]"))
                      (sentence
      . "\\(import_statement\\|expression_statement\\|declaration\\)")))))

I think this makes more sense. Considering that typescript's syntax is
predominantly C-like and it's very common to move between `{}', `()',
`[]', `""',
`''', May I ask if it is possible to make the default value of
`treesit-thing-settings' for typescript-ts-mode to act more like, (if
not exactly the same as), to that of the traditional
`forward-sexp-function'?

Best regards,
Errlin

In GNU Emacs 30.0.92 (build 2, x86_64-pc-linux-gnu)
Repository revision: 80cddfaaea3058fe70fc73ba3d267ad1c884bd3d
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12401004
System Description: Arch Linux

Configured using:
 'configure --sysconfdir=/etc --prefix=/usr --libexecdir=/usr/lib
 --localstatedir=/var --with-tree-sitter --with-harfbuzz
 --with-libsystemd --with-modules --with-jpeg --with-png --with-rsvg
 --without-cairo --without-gpm --without-sound --disable-build-details
 --with-x-toolkit=no --without-toolkit-scroll-bars --without-gsettings
 'CFLAGS=-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions
 -Wp,-D_FORTIFY_SOURCE=3 -Wformat -Werror=format-security
 -fstack-clash-protection -fcf-protection -fno-omit-frame-pointer
 -mno-omit-leaf-frame-pointer -g
 -ffile-prefix-map=/home/noel/projects/pkgbuilds/app-editors/emacs/src=/usr/src/debug/emacs
 -flto=auto' 'LDFLAGS=-Wl,-O1 -Wl,--sort-common -Wl,--as-needed
 -Wl,-z,relro -Wl,-z,now -Wl,-z,pack-relative-relocs -flto=auto''

Configured features:
ACL DBUS FREETYPE GIF GLIB GMP GNUTLS HARFBUZZ JPEG LCMS2 LIBOTF
LIBSYSTEMD LIBXML2 M17N_FLT MODULES NATIVE_COMP NOTIFY INOTIFY OLDXMENU
PDUMPER PNG RSVG SECCOMP SQLITE3 THREADS TIFF TREE_SITTER WEBP X11 XDBE
XFT XIM XINPUT2 XPM ZLIB

Important settings:
  value of $LC_TIME: en_AU.UTF-8
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: TypeScript

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  minibuffer-regexp-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message mailcap yank-media puny dired
dired-loaddefs rfc822 mml mml-sec password-cache epa derived epg rfc6068
epg-config gnus-util mm-decode mm-bodies mm-encode mail-parse rfc2231
mailabbrev gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums
mm-util mail-prsvr mail-utils time-date cl-extra pp cl-print thingatpt
help-fns radix-tree help-mode compile text-property-search comint
ansi-osc ansi-color ring comp-run comp-common rx cl-seq
typescript-ts-mode js c-ts-common json subr-x map byte-opt gv bytecomp
byte-compile imenu cc-mode cc-fonts cc-guess cc-menus cc-cmds cc-styles
cc-align cc-engine cc-vars cc-defs treesit vc-git diff-mode
track-changes easy-mmode vc-dispatcher cl-loaddefs cl-lib rmc iso-transl
tooltip cconv eldoc paren electric uniquify ediff-hook vc-hooks
lisp-float-type elisp-mode mwheel term/x-win x-win term/common-win x-dnd
touch-screen tool-bar dnd fontset image regexp-opt fringe tabulated-list
replace newcomment text-mode lisp-mode prog-mode register page tab-bar
menu-bar rfn-eshadow isearch easymenu timer select scroll-bar mouse
jit-lock font-lock syntax font-core term/tty-colors frame minibuffer
nadvice seq simple cl-generic indonesian philippine cham georgian
utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao korean
japanese eucjp-ms cp51932 hebrew greek romanian slovak czech european
ethiopic indian cyrillic chinese composite emoji-zwj charscript charprop
case-table epa-hook jka-cmpr-hook help abbrev obarray oclosure
cl-preloaded button loaddefs theme-loaddefs faces cus-face macroexp
files window text-properties overlay sha1 md5 base64 format env
code-pages mule custom widget keymap hashtable-print-readable backquote
threads dbusbind inotify lcms2 dynamic-setting font-render-setting
xinput2 x multi-tty move-toolbar make-network-process native-compile
emacs)

Memory information:
((conses 16 132999 13414) (symbols 48 10422 0) (strings 32 32345 2933)
 (string-bytes 1 1113270) (vectors 16 16978)
 (vector-slots 8 213223 8287) (floats 8 70 2) (intervals 56 503 4)
 (buffers 992 13))