unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#71225: 30.0.50; [PATCH] c-ts-common-comment-indent-new-line does not behave like c/c++-mode in comments
@ 2024-05-27 14:09 Vincenzo Pupillo
  2024-05-28 18:15 ` Andrea Corallo
  0 siblings, 1 reply; 5+ messages in thread
From: Vincenzo Pupillo @ 2024-05-27 14:09 UTC (permalink / raw)
  To: 71225

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

Hi, 
the c-ts-common-comment-indent-new-line does not behave like c/c++-mode in comments. 
M-j also behaves differently in different places.

// M-j wrong behavior.
int test(int a)
{
  /** (1)
  */
  int a;

  /* (1)
  */
  int b;
  // test (1)

  int c; // blah blah
         // (3)
}

/* (2)
 */

/** (2)
*/

// (2)

If you place the cursor at the positions indicated by (1) and (2), press M-j the result is different and is different from what happens with c/c++-mode. 

As a bonus, the attached patch also allows single-line comments placed at the end of a line to be indented as in c-mode (as in "(3)")

Thank you.
V.


[-- Attachment #2: 0001-tree-sitter-comment-indent-new-line-behave-more-like.patch --]
[-- Type: text/x-patch, Size: 2191 bytes --]

From 6416150bffa8b147758cc4aa131a0681684b56c4 Mon Sep 17 00:00:00 2001
From: Vincenzo Pupillo <v.pupillo@gmail.com>
Date: Tue, 26 May 2024 21:07:50 +0200
Subject: [PATCH] tree-sitter comment-indent-new-line behave more like the
 standard one.

* lisp/progmodes/c-ts-common.el:
  (c-ts-common-comment-indent-new-line): Single line comment and
  block comment now behave more like the c-indent-new-comment-line.
---
 lisp/progmodes/c-ts-common.el | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/lisp/progmodes/c-ts-common.el b/lisp/progmodes/c-ts-common.el
index b1520db22e9..f027fc28c04 100644
--- a/lisp/progmodes/c-ts-common.el
+++ b/lisp/progmodes/c-ts-common.el
@@ -303,20 +303,31 @@ c-ts-common-comment-indent-new-line
    ;; Or //! (used in rust).
    ((save-excursion
       (beginning-of-line)
-      (looking-at (rx "//" (group (* (any "/!")) (* " ")))))
-    (let ((whitespaces (match-string 1)))
+      (re-search-forward
+       (rx "//" (group (* (any "/!")) (* " ")))
+       (line-end-position)
+       t nil))
+    (let ((offset (- (match-beginning 0) (line-beginning-position)))
+          (whitespaces (match-string 1)))
       (if soft (insert-and-inherit ?\n) (newline 1))
       (delete-region (line-beginning-position) (point))
-      (insert "//" whitespaces)))
+      (insert (make-string offset ?\s) "//" whitespaces)))
 
    ;; Line starts with /* or /**.
    ((save-excursion
       (beginning-of-line)
-      (looking-at (rx "/*" (group (? "*") (* " ")))))
-    (let ((whitespace-and-star-len (length (match-string 1))))
+      (re-search-forward
+       (rx "/*" (group (? "*") (* " ")))
+       (line-end-position)
+       t nil))
+    (let ((offset (- (match-beginning 0) (line-beginning-position)))
+          (whitespace-and-star-len (length (match-string 1))))
       (if soft (insert-and-inherit ?\n) (newline 1))
       (delete-region (line-beginning-position) (point))
-      (insert " *" (make-string whitespace-and-star-len ?\s))))
+      (insert
+       (make-string offset ?\s)
+       " *"
+       (make-string whitespace-and-star-len ?\s))))
 
    ;; Line starts with *.
    ((save-excursion
-- 
2.45.1


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

end of thread, other threads:[~2024-06-02  8:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-27 14:09 bug#71225: 30.0.50; [PATCH] c-ts-common-comment-indent-new-line does not behave like c/c++-mode in comments Vincenzo Pupillo
2024-05-28 18:15 ` Andrea Corallo
2024-05-28 18:30   ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-06-02  8:11     ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-06-02  8:30       ` Eli Zaretskii

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).