Subject: [PATCH] Add < and > to the syntax table in c++-ts-mode

From: Daniel Martín <[email-masked]>
Date: Tue, 13 Dec 2022 22:39:36 +0100

Tags: patch

Given the following C++ code (where "|" represents the point):

|Expected<int> getFoo();

If you press C-M-d when c++-ts-mode is enabled, point moves to:

Expected<int> getFoo(|);

In c++-mode, point moves (correctly) to:

Expected<|int> getFoo();

Also, show-paren-mode does not highlight any <> pairs in c++-ts-mode.

My proposed fix is to create a syntax table for c++-ts-mode and add "<"
and ">" to it as open/close delimiters.

The attached patch implements the proposed solution.  Thanks.

>From 868322dae6a881792c5601615c8d11b4181e7424 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= <[email-masked]>
Date: Tue, 13 Dec 2022 22:28:13 +0100
Subject: [PATCH] Add < and > to the syntax table in c++-ts-mode

* lisp/progmodes/c-ts-mode.el (c++-ts-mode--syntax-table): Add a
specific syntax table for C++.  Consider "<" and ">" open/close
delimiters (C++ templates).
(c++-ts-mode): Use the new syntax table.
---
 lisp/progmodes/c-ts-mode.el | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 8407be3b6b..31c22dc321 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -83,6 +83,14 @@ c-ts-mode--syntax-table
     table)
   "Syntax table for `c-ts-mode'.")
 
+(defvar c++-ts-mode--syntax-table
+  (let ((table (make-syntax-table c-ts-mode--syntax-table)))
+    ;; Template delimiters.
+    (modify-syntax-entry ?<  "("     table)
+    (modify-syntax-entry ?>  ")"     table)
+    table)
+  "Syntax table for `c++-ts-mode'.")
+
 (defun c-ts-mode--indent-styles (mode)
   "Indent rules supported by `c-ts-mode'.
 MODE is either `c' or `cpp'."
@@ -615,6 +623,7 @@ c-ts-mode
 (define-derived-mode c++-ts-mode c-ts-base-mode "C++"
   "Major mode for editing C++, powered by tree-sitter."
   :group 'c++
+  :syntax-table c++-ts-mode--syntax-table
 
   (unless (treesit-ready-p 'cpp)
     (error "Tree-sitter for C++ isn't available"))
-- 
2.34.1