unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#60529: [PATCH] Fontify C++ function definitions in c++-ts-mode
       [not found] <m1edsbgt82.fsf.ref@yahoo.es>
@ 2023-01-03 22:15 ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-01-04  6:50   ` Yuan Fu
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-01-03 22:15 UTC (permalink / raw)
  To: 60529

[-- Attachment #1: Type: text/plain, Size: 627 bytes --]

Tags: patch


Given the following valid C++ code:

void Foo::bar() {

}

c++-ts-mode should fontify bar with the font-lock-function-name-face
face.  The attached patch adds support for fontifying function
declarations whose identifier is "buried" inside qualified_identifier
nodes.

I've checked that the patch also fontifies bar in

void Foo::Bar::bar() {

}

but fontifies bar with font-lock-variable-name-face face in the
following cases:

Foo::Bar::Bar2 bar;
Foo::Bar bar;

I don't check for C++ mode in the code, as I think the impact of this
new check should be negligible for C code, but I'm open to feedback.

Thanks.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fontify-C-function-definitions-in-c-ts-mode.patch --]
[-- Type: text/patch, Size: 2140 bytes --]

From 6e687edc303ba5dba5f58479ad076c4ab9b610f6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= <mardani29@yahoo.es>
Date: Tue, 3 Jan 2023 22:08:13 +0100
Subject: [PATCH] Fontify C++ function definitions in c-ts-mode

* lisp/progmodes/c-ts-mode.el (c-ts-mode--declarator-identifier):
Teach the code how to extract the declarator of a node of type
"qualified_identifier".
(c-ts-mode--fontify-declarator): Consider the case where the
identifier in a function declarator is buried inside
"qualifier_identifier" nodes.
---
 lisp/progmodes/c-ts-mode.el | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index ba38845a084..30a14ecdfae 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -430,6 +430,9 @@ c-ts-mode--declarator-identifier
     ((or "function_declarator" "array_declarator" "init_declarator")
      (c-ts-mode--declarator-identifier
       (treesit-node-child-by-field-name node "declarator")))
+    ("qualified_identifier"
+     (c-ts-mode--declarator-identifier
+      (treesit-node-child-by-field-name node "name")))
     ;; Terminal case.
     ((or "identifier" "field_identifier")
      node)))
@@ -439,7 +442,14 @@ c-ts-mode--fontify-declarator
 For NODE, OVERRIDE, START, END, and ARGS, see
 `treesit-font-lock-rules'."
   (let* ((identifier (c-ts-mode--declarator-identifier node))
-         (face (pcase (treesit-node-type (treesit-node-parent identifier))
+         (qualified-root
+          (treesit-parent-while (treesit-node-parent identifier)
+                                (lambda (node)
+                                  (equal (treesit-node-type node)
+                                         "qualified_identifier"))))
+         (face (pcase (treesit-node-type (treesit-node-parent
+                                          (or qualified-root
+                                              identifier)))
                  ("function_declarator" 'font-lock-function-name-face)
                  (_ 'font-lock-variable-name-face))))
     (treesit-fontify-with-override
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* bug#60529: [PATCH] Fontify C++ function definitions in c++-ts-mode
  2023-01-03 22:15 ` bug#60529: [PATCH] Fontify C++ function definitions in c++-ts-mode Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-01-04  6:50   ` Yuan Fu
  0 siblings, 0 replies; 2+ messages in thread
From: Yuan Fu @ 2023-01-04  6:50 UTC (permalink / raw)
  To: Daniel Martín; +Cc: 60529-done


Daniel Martín <mardani29@yahoo.es> writes:

> Tags: patch
>
>
> Given the following valid C++ code:
>
> void Foo::bar() {
>
> }
>
> c++-ts-mode should fontify bar with the font-lock-function-name-face
> face.  The attached patch adds support for fontifying function
> declarations whose identifier is "buried" inside qualified_identifier
> nodes.
>
> I've checked that the patch also fontifies bar in
>
> void Foo::Bar::bar() {
>
> }
>
> but fontifies bar with font-lock-variable-name-face face in the
> following cases:
>
> Foo::Bar::Bar2 bar;
> Foo::Bar bar;
>
> I don't check for C++ mode in the code, as I think the impact of this
> new check should be negligible for C code, but I'm open to feedback.
>
> Thanks.

Makes sense. I applied the patch. Thanks!

Yuan





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-01-04  6:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <m1edsbgt82.fsf.ref@yahoo.es>
2023-01-03 22:15 ` bug#60529: [PATCH] Fontify C++ function definitions in c++-ts-mode Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-04  6:50   ` Yuan Fu

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).