diff src/cindent.c @ 26516:9596c652420b v8.2.3787

patch 8.2.3787: no proper formatting of a C line comment after a statement Commit: https://github.com/vim/vim/commit/6e371ecb27227ff8fedd8561d0f3880a17576848 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Dec 12 14:16:39 2021 +0000 patch 8.2.3787: no proper formatting of a C line comment after a statement Problem: No proper formatting of a C line comment after a statement. Solution: Find the start of the line comment, insert the comment leader and indent the comment properly.
author Bram Moolenaar <Bram@vim.org>
date Sun, 12 Dec 2021 15:30:03 +0100
parents 8e92f01f7226
children 1095fd0dc362
line wrap: on
line diff
--- a/src/cindent.c
+++ b/src/cindent.c
@@ -2144,13 +2144,30 @@ get_c_indent(void)
 
     // If we're inside a "//" comment and there is a "//" comment in a
     // previous line, lineup with that one.
-    if (cin_islinecomment(theline)
-	    && (trypos = find_line_comment()) != NULL) // XXX
+    if (cin_islinecomment(theline))
     {
-	// find how indented the line beginning the comment is
-	getvcol(curwin, trypos, &col, NULL, NULL);
-	amount = col;
-	goto theend;
+	pos_T	linecomment_pos;
+
+	trypos = find_line_comment(); // XXX
+	if (trypos == NULL && curwin->w_cursor.lnum > 1)
+	{
+	    // There may be a statement before the comment, search from the end
+	    // of the line for a comment start.
+	    linecomment_pos.col =
+			  check_linecomment(ml_get(curwin->w_cursor.lnum - 1));
+	    if (linecomment_pos.col != MAXCOL)
+	    {
+	        trypos = &linecomment_pos;
+	        trypos->lnum = curwin->w_cursor.lnum - 1;
+	    }
+	}
+	if (trypos  != NULL)
+	{
+	    // find how indented the line beginning the comment is
+	    getvcol(curwin, trypos, &col, NULL, NULL);
+	    amount = col;
+	    goto theend;
+	}
     }
 
     // If we're inside a comment and not looking at the start of the