changeset 28767:7bbd859b2a48 v8.2.4908

patch 8.2.4908: no text formatting for // comment after a statement Commit: https://github.com/vim/vim/commit/48a8a833033e10fc1eba96f2fc8dd19c2408eddf Author: Bram Moolenaar <Bram@vim.org> Date: Sat May 7 15:43:52 2022 +0100 patch 8.2.4908: no text formatting for // comment after a statement Problem: No text formatting for // comment after a statement. Solution: format a comment when the 'c' flag is in 'formatoptions'.
author Bram Moolenaar <Bram@vim.org>
date Sat, 07 May 2022 16:45:02 +0200
parents f708e1986aef
children c8ffba1b1c9c
files src/testdir/test_textformat.vim src/textformat.c src/version.c
diffstat 3 files changed, 35 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/testdir/test_textformat.vim
+++ b/src/testdir/test_textformat.vim
@@ -342,6 +342,18 @@ func Test_format_c_comment()
   END
   call assert_equal(expected, getline(1, '$'))
 
+  " typing comment text auto-wraps
+  %del
+  call setline(1, text)
+  exe "normal! 2GA blah more text blah.\<Esc>"
+  let expected =<< trim END
+      {
+         val = val;      // This is a comment
+                         // blah more text
+                         // blah.
+  END
+  call assert_equal(expected, getline(1, '$'))
+
   bwipe!
 endfunc
 
--- a/src/textformat.c
+++ b/src/textformat.c
@@ -104,7 +104,27 @@ internal_format(
 
 	// Don't break until after the comment leader
 	if (do_comments)
-	    leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
+	{
+	    char_u *line = ml_get_curline();
+
+	    leader_len = get_leader_len(line, NULL, FALSE, TRUE);
+#ifdef FEAT_CINDENT
+	    if (leader_len == 0 && curbuf->b_p_cin)
+	    {
+		int		comment_start;
+
+		// Check for a line comment after code.
+		comment_start = check_linecomment(line);
+		if (comment_start != MAXCOL)
+		{
+		    leader_len = get_leader_len(
+				      line + comment_start, NULL, FALSE, TRUE);
+		    if (leader_len != 0)
+			leader_len += comment_start;
+		}
+	    }
+#endif
+	}
 	else
 	    leader_len = 0;
 
--- a/src/version.c
+++ b/src/version.c
@@ -747,6 +747,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    4908,
+/**/
     4907,
 /**/
     4906,