about summary refs log tree commit diff
path: root/tools/emacs-pkgs/treecrumbs
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@tvl.su>2024-03-10T10·20+0300
committerclbot <clbot@tvl.fyi>2024-03-10T19·35+0000
commit3b7e546dfc0fbd85c4e7ca88a489a8e2ee4f1d74 (patch)
tree3be0fe363f3bbd0dc731ca8a14c144b9ff3ebd25 /tools/emacs-pkgs/treecrumbs
parent470f7dfa45ce9c597ba3edbc0e75d5b959aa938c (diff)
feat(treecrumbs): initial support for C++ r/7670
Supports namespaces, functions, fields (including trailing return type
syntax) and so on.

One notable issue about this is that the tree-sitter parser for C++
returns the node *following* point if point is on whitespace, which
means that at the top-level of a namespace the crumbs will often show
the *next* function.

I'm against adding workarounds for stuff like that, so I'll just keep
it as is.

Change-Id: If7e71525c4e86e128157dd4eb17c130297ed1e0a
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11109
Reviewed-by: tazjin <tazjin@tvl.su>
Autosubmit: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Diffstat (limited to 'tools/emacs-pkgs/treecrumbs')
-rw-r--r--tools/emacs-pkgs/treecrumbs/treecrumbs.el42
1 files changed, 34 insertions, 8 deletions
diff --git a/tools/emacs-pkgs/treecrumbs/treecrumbs.el b/tools/emacs-pkgs/treecrumbs/treecrumbs.el
index 81538de770f2..a01e5490fa7f 100644
--- a/tools/emacs-pkgs/treecrumbs/treecrumbs.el
+++ b/tools/emacs-pkgs/treecrumbs/treecrumbs.el
@@ -90,6 +90,32 @@ The defined languages are stored in `treecrumbs-languages'."
   ("flow_pair" . ((_) key: (_) @key))
   ("flow_sequence" . "[]"))
 
+(define-treecrumbs-language cpp
+  ;; In C++ files, crumbs are generated from namespaces and
+  ;; identifier declarations.
+  ("namespace_definition" . ([(namespace_definition
+                               name: (namespace_identifier) @key)
+                              (namespace_definition
+                               "namespace" @key
+                               !name)]))
+
+  ("function_definition" . ((function_definition
+                             declarator:
+                             (function_declarator
+                              declarator: (_) @key))))
+
+  ("class_specifier" . ((class_specifier
+                         name: (type_identifier) @key)))
+
+  ("struct_specifier" . ((struct_specifier
+                          name: (type_identifier) @key)))
+
+  ("field_declaration" . ((field_declaration
+                           declarator: (_) @key)))
+
+  ("init_declarator" . ((init_declarator
+                         declarator: (_) @key))))
+
 (defvar-local treecrumbs--current-crumbs nil
   "Current crumbs to display in the header line. Only updated when
 the node under point changes.")
@@ -110,14 +136,14 @@ is undefined, it directly updates the buffer-local
        (when-let ((query (cdr (assoc (treesit-node-type parent) lang))))
 
          (setq-local treecrumbs--current-crumbs
-               (concat treecrumbs--current-crumbs
-                       (if (string-empty-p treecrumbs--current-crumbs) ""
-                         " < ")
-
-                       (if (stringp query)
-                           query
-                         (substring-no-properties
-                          (treesit-node-text (cdar (treesit-query-capture parent query))))))))
+                     (concat treecrumbs--current-crumbs
+                             (if (string-empty-p treecrumbs--current-crumbs) ""
+                               " < ")
+
+                             (if (stringp query)
+                                 query
+                               (substring-no-properties
+                                (treesit-node-text (cdar (treesit-query-capture parent query))))))))
        t))))