From 3b7e546dfc0fbd85c4e7ca88a489a8e2ee4f1d74 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 10 Mar 2024 13:20:40 +0300 Subject: [PATCH] feat(treecrumbs): initial support for C++ 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 Autosubmit: tazjin Tested-by: BuildkiteCI --- tools/emacs-pkgs/treecrumbs/treecrumbs.el | 40 +++++++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/tools/emacs-pkgs/treecrumbs/treecrumbs.el b/tools/emacs-pkgs/treecrumbs/treecrumbs.el index 81538de77..a01e5490f 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) "" - " < ") + (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)))))))) + (if (stringp query) + query + (substring-no-properties + (treesit-node-text (cdar (treesit-query-capture parent query)))))))) t))))