changeset 6691:c4cb95fb4c58 v7.4.670

updated for version 7.4.670 Problem: Using 'cindent' for Javascript is less than perfect. Solution: Improve indenting of continuation lines. (Hirohito Higashi)
author Bram Moolenaar <bram@vim.org>
date Fri, 20 Mar 2015 19:06:06 +0100
parents f861a3682c95
children c8ea399de9be
files src/misc1.c src/testdir/test3.in src/testdir/test3.ok src/version.c
diffstat 4 files changed, 365 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/misc1.c
+++ b/src/misc1.c
@@ -6670,20 +6670,43 @@ find_match_char(c, ind_maxparen)	    /* 
     pos_T	cursor_save;
     pos_T	*trypos;
     static pos_T pos_copy;
+    int		ind_maxp_wk;
 
     cursor_save = curwin->w_cursor;
-    if ((trypos = findmatchlimit(NULL, c, 0, ind_maxparen)) != NULL)
+    ind_maxp_wk = ind_maxparen;
+retry:
+    if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
     {
 	/* check if the ( is in a // comment */
 	if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
+	{
+	    ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
+	    if (ind_maxp_wk > 0)
+	    {
+		curwin->w_cursor = *trypos;
+		curwin->w_cursor.col = 0;	/* XXX */
+		goto retry;
+	    }
 	    trypos = NULL;
+	}
 	else
 	{
+	    pos_T	*trypos_wk;
+
 	    pos_copy = *trypos;	    /* copy trypos, findmatch will change it */
 	    trypos = &pos_copy;
 	    curwin->w_cursor = *trypos;
-	    if (ind_find_start_comment() != NULL) /* XXX */
+	    if ((trypos_wk = ind_find_start_comment()) != NULL) /* XXX */
+	    {
+		ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
+			- trypos_wk->lnum);
+		if (ind_maxp_wk > 0)
+		{
+		    curwin->w_cursor = *trypos_wk;
+		    goto retry;
+		}
 		trypos = NULL;
+	    }
 	}
     }
     curwin->w_cursor = cursor_save;
@@ -7024,7 +7047,7 @@ get_c_indent()
 #define LOOKFOR_CPP_BASECLASS	9
 #define LOOKFOR_ENUM_OR_INIT	10
 #define LOOKFOR_JS_KEY		11
-#define LOOKFOR_NO_COMMA	12
+#define LOOKFOR_COMMA	12
 
     int		whilelevel;
     linenr_T	lnum;
@@ -7842,7 +7865,8 @@ get_c_indent()
 		    else
 		    {
 			if (lookfor != LOOKFOR_TERM
-					  && lookfor != LOOKFOR_CPP_BASECLASS)
+					&& lookfor != LOOKFOR_CPP_BASECLASS
+					&& lookfor != LOOKFOR_COMMA)
 			{
 			    amount = scope_amount;
 			    if (theline[0] == '{')
@@ -8134,23 +8158,31 @@ get_c_indent()
 		    amount = get_indent();
 		    break;
 		}
-		if (lookfor == LOOKFOR_NO_COMMA)
+		if (lookfor == LOOKFOR_COMMA)
 		{
-		    if (terminated != ',')
+		    if (tryposBrace != NULL && tryposBrace->lnum
+						    >= curwin->w_cursor.lnum)
+			break;
+		    if (terminated == ',')
 			/* line below current line is the one that starts a
 			 * (possibly broken) line ending in a comma. */
 			break;
-		    amount = get_indent();
-		    if (curwin->w_cursor.lnum - 1 == ourscope)
-			/* line above is start of the scope, thus current line
-			 * is the one that stars a (possibly broken) line
-			 * ending in a comma. */
-			break;
+		    else
+		    {
+			amount = get_indent();
+			if (curwin->w_cursor.lnum - 1 == ourscope)
+			    /* line above is start of the scope, thus current
+			     * line is the one that stars a (possibly broken)
+			     * line ending in a comma. */
+			    break;
+		    }
 		}
 
 		if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
 							&& terminated == ','))
 		{
+		    if (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '[')
+			amount += ind_continuation;
 		    /*
 		     * if we're in the middle of a paren thing,
 		     * go back to the line that starts it so
@@ -8389,7 +8421,10 @@ get_c_indent()
 			     *	    100 +
 			     * ->	    here;
 			     */
+			    l = ml_get_curline();
 			    amount = cur_amount;
+			    if (*skipwhite(l) == ']' || l[STRLEN(l) - 1] == ']')
+				break;
 
 			    /*
 			     * If previous line ends in ',', check whether we
@@ -8418,8 +8453,9 @@ get_c_indent()
 				     *        5,
 				     *     6,
 				     */
-				    lookfor = LOOKFOR_NO_COMMA;
-				    amount = get_indent();	    /* XXX */
+				    if (cin_iscomment(skipwhite(l)))
+					break;
+				    lookfor = LOOKFOR_COMMA;
 				    trypos = find_match_char('[',
 						      curbuf->b_ind_maxparen);
 				    if (trypos != NULL)
@@ -8449,7 +8485,8 @@ get_c_indent()
 				    cont_amount = cin_get_equal_amount(
 						       curwin->w_cursor.lnum);
 				if (lookfor != LOOKFOR_TERM
-						 && lookfor != LOOKFOR_JS_KEY)
+						&& lookfor != LOOKFOR_JS_KEY
+						&& lookfor != LOOKFOR_COMMA)
 				    lookfor = LOOKFOR_UNTERM;
 			    }
 			}
--- a/src/testdir/test3.in
+++ b/src/testdir/test3.in
@@ -2065,6 +2065,164 @@ return true;
 JSEND
 
 STARTTEST
+:set cino=j1,J1,+2
+/^JSSTART
+=/^JSEND
+ENDTEST
+
+JSSTART
+// Results of JavaScript indent
+// 1
+(function(){
+var a = [
+'a',
+'b',
+'c',
+'d',
+'e',
+'f',
+'g',
+'h',
+'i'
+];
+}())
+
+// 2
+(function(){
+var a = [
+0 +
+5 *
+9 *
+'a',
+'b',
+0 +
+5 *
+9 *
+'c',
+'d',
+'e',
+'f',
+'g',
+'h',
+'i'
+];
+}())
+
+// 3
+(function(){
+var a = [
+0 +
+// comment 1
+5 *
+/* comment 2 */
+9 *
+'a',
+'b',
+0 +
+5 *
+9 *
+'c',
+'d',
+'e',
+'f',
+'g',
+'h',
+'i'
+];
+}())
+
+// 4
+{
+var a = [
+0,
+1
+];
+var b;
+var c;
+}
+
+// 5
+{
+var a = [
+[
+0
+],
+2,
+3
+];
+}
+
+// 6
+{
+var a = [
+[
+0,
+1
+],
+2,
+3
+];
+}
+
+// 7
+{
+var a = [
+// [
+0,
+// 1
+// ],
+2,
+3
+];
+}
+
+// 8
+var x = [
+(function(){
+var a,
+b,
+c,
+d,
+e,
+f,
+g,
+h,
+i;
+})
+];
+
+// 9
+var a = [
+0 +
+5 *
+9 *
+'a',
+'b',
+0 +
+5 *
+9 *
+'c',
+'d',
+'e',
+'f',
+'g',
+'h',
+'i'
+];
+
+// 10
+var a,
+b,
+c,
+d,
+e,
+f,
+g,
+h,
+i;
+JSEND
+
+STARTTEST
 :g/^STARTTEST/.,/^ENDTEST/d
 :1;/start of AUTO/,$wq! test.out
 ENDTEST
--- a/src/testdir/test3.ok
+++ b/src/testdir/test3.ok
@@ -1832,3 +1832,156 @@ JSSTART
 })(jQuery);
 JSEND
 
+
+JSSTART
+// Results of JavaScript indent
+// 1
+(function(){
+	var a = [
+	  'a',
+	  'b',
+	  'c',
+	  'd',
+	  'e',
+	  'f',
+	  'g',
+	  'h',
+	  'i'
+	];
+}())
+
+// 2
+(function(){
+	var a = [
+	  0 +
+		5 *
+		9 *
+		'a',
+	  'b',
+	  0 +
+		5 *
+		9 *
+		'c',
+	  'd',
+	  'e',
+	  'f',
+	  'g',
+	  'h',
+	  'i'
+	];
+}())
+
+// 3
+(function(){
+	var a = [
+	  0 +
+		// comment 1
+		5 *
+		/* comment 2 */
+		9 *
+		'a',
+	  'b',
+	  0 +
+		5 *
+		9 *
+		'c',
+	  'd',
+	  'e',
+	  'f',
+	  'g',
+	  'h',
+	  'i'
+	];
+}())
+
+// 4
+{
+	var a = [
+	  0,
+	  1
+	];
+	var b;
+	var c;
+}
+
+// 5
+{
+	var a = [
+	  [
+		0
+	  ],
+	  2,
+	  3
+	];
+}
+
+// 6
+{
+	var a = [
+	  [
+		0,
+		1
+	  ],
+	  2,
+	  3
+	];
+}
+
+// 7
+{
+	var a = [
+	  // [
+	  0,
+	  // 1
+	  // ],
+	  2,
+	  3
+	];
+}
+
+// 8
+var x = [
+  (function(){
+	  var a,
+	  b,
+	  c,
+	  d,
+	  e,
+	  f,
+	  g,
+	  h,
+	  i;
+  })
+];
+
+// 9
+var a = [
+  0 +
+  5 *
+  9 *
+  'a',
+  'b',
+  0 +
+  5 *
+  9 *
+  'c',
+  'd',
+  'e',
+  'f',
+  'g',
+  'h',
+  'i'
+];
+
+// 10
+var a,
+	b,
+	c,
+	d,
+	e,
+	f,
+	g,
+	h,
+	i;
+JSEND
+
--- a/src/version.c
+++ b/src/version.c
@@ -742,6 +742,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    670,
+/**/
     669,
 /**/
     668,