Mercurial > vim
comparison src/misc1.c @ 2297:5ffe000a9ecf vim73
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sun, 11 Jul 2010 17:23:02 +0200 |
parents | 4dda2bd944e1 |
children | 488be8cbe19c |
comparison
equal
deleted
inserted
replaced
2296:eb7be7b075a6 | 2297:5ffe000a9ecf |
---|---|
5048 continue; | 5048 continue; |
5049 | 5049 |
5050 curwin->w_cursor = cursor_save; | 5050 curwin->w_cursor = cursor_save; |
5051 if (cin_isterminated(line, TRUE, FALSE) | 5051 if (cin_isterminated(line, TRUE, FALSE) |
5052 || cin_isscopedecl(line) | 5052 || cin_isscopedecl(line) |
5053 || cin_iscase(line) | 5053 || cin_iscase(line, TRUE) |
5054 || (cin_islabel_skip(&line) && cin_nocode(line))) | 5054 || (cin_islabel_skip(&line) && cin_nocode(line))) |
5055 return TRUE; | 5055 return TRUE; |
5056 return FALSE; | 5056 return FALSE; |
5057 } | 5057 } |
5058 curwin->w_cursor = cursor_save; | 5058 curwin->w_cursor = cursor_save; |
5087 | 5087 |
5088 /* | 5088 /* |
5089 * Recognize a switch label: "case .*:" or "default:". | 5089 * Recognize a switch label: "case .*:" or "default:". |
5090 */ | 5090 */ |
5091 int | 5091 int |
5092 cin_iscase(s) | 5092 cin_iscase(s, strict) |
5093 char_u *s; | 5093 char_u *s; |
5094 int strict; /* Allow relaxed check of case statement for JS */ | |
5094 { | 5095 { |
5095 s = cin_skipcomment(s); | 5096 s = cin_skipcomment(s); |
5096 if (STRNCMP(s, "case", 4) == 0 && !vim_isIDc(s[4])) | 5097 if (STRNCMP(s, "case", 4) == 0 && !vim_isIDc(s[4])) |
5097 { | 5098 { |
5098 for (s += 4; *s; ++s) | 5099 for (s += 4; *s; ++s) |
5104 ++s; | 5105 ++s; |
5105 else | 5106 else |
5106 return TRUE; | 5107 return TRUE; |
5107 } | 5108 } |
5108 if (*s == '\'' && s[1] && s[2] == '\'') | 5109 if (*s == '\'' && s[1] && s[2] == '\'') |
5109 s += 2; /* skip over '.' */ | 5110 s += 2; /* skip over ':' */ |
5110 else if (*s == '/' && (s[1] == '*' || s[1] == '/')) | 5111 else if (*s == '/' && (s[1] == '*' || s[1] == '/')) |
5111 return FALSE; /* stop at comment */ | 5112 return FALSE; /* stop at comment */ |
5112 else if (*s == '"') | 5113 else if (*s == '"') |
5113 return FALSE; /* stop at string */ | 5114 { |
5115 /* JS etc. */ | |
5116 if (strict) | |
5117 return FALSE; /* stop at string */ | |
5118 else | |
5119 return TRUE; | |
5120 } | |
5114 } | 5121 } |
5115 return FALSE; | 5122 return FALSE; |
5116 } | 5123 } |
5117 | 5124 |
5118 if (cin_isdefault(s)) | 5125 if (cin_isdefault(s)) |
5167 { | 5174 { |
5168 if (*l == ':') | 5175 if (*l == ':') |
5169 { | 5176 { |
5170 if (l[1] == ':') /* skip over "::" for C++ */ | 5177 if (l[1] == ':') /* skip over "::" for C++ */ |
5171 ++l; | 5178 ++l; |
5172 else if (!cin_iscase(l + 1)) | 5179 else if (!cin_iscase(l + 1, FALSE)) |
5173 break; | 5180 break; |
5174 } | 5181 } |
5175 else if (*l == '\'' && l[1] && l[2] == '\'') | 5182 else if (*l == '\'' && l[1] && l[2] == '\'') |
5176 l += 2; /* skip over 'x' */ | 5183 l += 2; /* skip over 'x' */ |
5177 } | 5184 } |
5225 | 5232 |
5226 cursor_save = curwin->w_cursor; | 5233 cursor_save = curwin->w_cursor; |
5227 curwin->w_cursor.lnum = lnum; | 5234 curwin->w_cursor.lnum = lnum; |
5228 l = ml_get_curline(); | 5235 l = ml_get_curline(); |
5229 /* XXX */ | 5236 /* XXX */ |
5230 if (cin_iscase(l) || cin_isscopedecl(l) || cin_islabel(ind_maxcomment)) | 5237 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) |
5238 || cin_islabel(ind_maxcomment)) | |
5231 { | 5239 { |
5232 amount = get_indent_nolabel(lnum); | 5240 amount = get_indent_nolabel(lnum); |
5233 l = after_label(ml_get_curline()); | 5241 l = after_label(ml_get_curline()); |
5234 if (l == NULL) /* just in case */ | 5242 if (l == NULL) /* just in case */ |
5235 l = ml_get_curline(); | 5243 l = ml_get_curline(); |
6170 | 6178 |
6171 /* | 6179 /* |
6172 * handle braces for java code | 6180 * handle braces for java code |
6173 */ | 6181 */ |
6174 int ind_java = 0; | 6182 int ind_java = 0; |
6183 | |
6184 /* | |
6185 * not to confuse JS object properties with labels | |
6186 */ | |
6187 int ind_js = 0; | |
6175 | 6188 |
6176 /* | 6189 /* |
6177 * handle blocked cases correctly | 6190 * handle blocked cases correctly |
6178 */ | 6191 */ |
6179 int ind_keep_case_label = 0; | 6192 int ind_keep_case_label = 0; |
6284 case ')': ind_maxparen = n; break; | 6297 case ')': ind_maxparen = n; break; |
6285 case '*': ind_maxcomment = n; break; | 6298 case '*': ind_maxcomment = n; break; |
6286 case 'g': ind_scopedecl = n; break; | 6299 case 'g': ind_scopedecl = n; break; |
6287 case 'h': ind_scopedecl_code = n; break; | 6300 case 'h': ind_scopedecl_code = n; break; |
6288 case 'j': ind_java = n; break; | 6301 case 'j': ind_java = n; break; |
6302 case 'J': ind_js = n; break; | |
6289 case 'l': ind_keep_case_label = n; break; | 6303 case 'l': ind_keep_case_label = n; break; |
6290 case '#': ind_hash_comment = n; break; | 6304 case '#': ind_hash_comment = n; break; |
6291 } | 6305 } |
6292 if (*options == ',') | 6306 if (*options == ',') |
6293 ++options; | 6307 ++options; |
6294 } | 6308 } |
6295 | 6309 |
6296 /* remember where the cursor was when we started */ | 6310 /* remember where the cursor was when we started */ |
6297 cur_curpos = curwin->w_cursor; | 6311 cur_curpos = curwin->w_cursor; |
6312 | |
6313 /* if we are at line 1 0 is fine, right? */ | |
6314 if (cur_curpos.lnum == 1) | |
6315 return 0; | |
6298 | 6316 |
6299 /* Get a copy of the current contents of the line. | 6317 /* Get a copy of the current contents of the line. |
6300 * This is required, because only the most recent line obtained with | 6318 * This is required, because only the most recent line obtained with |
6301 * ml_get is valid! */ | 6319 * ml_get is valid! */ |
6302 linecopy = vim_strsave(ml_get(cur_curpos.lnum)); | 6320 linecopy = vim_strsave(ml_get(cur_curpos.lnum)); |
6328 { | 6346 { |
6329 amount = 0; | 6347 amount = 0; |
6330 } | 6348 } |
6331 | 6349 |
6332 /* | 6350 /* |
6333 * Is it a non-case label? Then that goes at the left margin too. | 6351 * Is it a non-case label? Then that goes at the left margin too unless JS flag is set. |
6334 */ | 6352 */ |
6335 else if (cin_islabel(ind_maxcomment)) /* XXX */ | 6353 else if (!ind_js && cin_islabel(ind_maxcomment)) /* XXX */ |
6336 { | 6354 { |
6337 amount = 0; | 6355 amount = 0; |
6338 } | 6356 } |
6339 | 6357 |
6340 /* | 6358 /* |
6781 * It could have been something like | 6799 * It could have been something like |
6782 * case 1: if (asdf && | 6800 * case 1: if (asdf && |
6783 * ldfd) { | 6801 * ldfd) { |
6784 * } | 6802 * } |
6785 */ | 6803 */ |
6786 if (ind_keep_case_label && cin_iscase(skipwhite(ml_get_curline()))) | 6804 if ((ind_keep_case_label |
6805 && cin_iscase(skipwhite(ml_get_curline()), FALSE))) | |
6787 amount = get_indent(); | 6806 amount = get_indent(); |
6788 else | 6807 else |
6789 amount = skip_label(lnum, &l, ind_maxcomment); | 6808 amount = skip_label(lnum, &l, ind_maxcomment); |
6790 | 6809 |
6791 start_brace = BRACE_AT_END; | 6810 start_brace = BRACE_AT_END; |
6859 } | 6878 } |
6860 } | 6879 } |
6861 | 6880 |
6862 lookfor_break = FALSE; | 6881 lookfor_break = FALSE; |
6863 | 6882 |
6864 if (cin_iscase(theline)) /* it's a switch() label */ | 6883 if (cin_iscase(theline, FALSE)) /* it's a switch() label */ |
6865 { | 6884 { |
6866 lookfor = LOOKFOR_CASE; /* find a previous switch() label */ | 6885 lookfor = LOOKFOR_CASE; /* find a previous switch() label */ |
6867 amount += ind_case; | 6886 amount += ind_case; |
6868 } | 6887 } |
6869 else if (cin_isscopedecl(theline)) /* private:, ... */ | 6888 else if (cin_isscopedecl(theline)) /* private:, ... */ |
6920 /* nothing found (abuse ind_maxparen as limit) | 6939 /* nothing found (abuse ind_maxparen as limit) |
6921 * assume terminated line (i.e. a variable | 6940 * assume terminated line (i.e. a variable |
6922 * initialization) */ | 6941 * initialization) */ |
6923 if (cont_amount > 0) | 6942 if (cont_amount > 0) |
6924 amount = cont_amount; | 6943 amount = cont_amount; |
6925 else | 6944 else if (!ind_js) |
6926 amount += ind_continuation; | 6945 amount += ind_continuation; |
6927 break; | 6946 break; |
6928 } | 6947 } |
6929 | 6948 |
6930 l = ml_get_curline(); | 6949 l = ml_get_curline(); |
7044 | 7063 |
7045 /* | 7064 /* |
7046 * If this is a switch() label, may line up relative to that. | 7065 * If this is a switch() label, may line up relative to that. |
7047 * If this is a C++ scope declaration, do the same. | 7066 * If this is a C++ scope declaration, do the same. |
7048 */ | 7067 */ |
7049 iscase = cin_iscase(l); | 7068 iscase = cin_iscase(l, FALSE); |
7050 if (iscase || cin_isscopedecl(l)) | 7069 if (iscase || cin_isscopedecl(l)) |
7051 { | 7070 { |
7052 /* we are only looking for cpp base class | 7071 /* we are only looking for cpp base class |
7053 * declaration/initialization any longer */ | 7072 * declaration/initialization any longer */ |
7054 if (lookfor == LOOKFOR_CPP_BASECLASS) | 7073 if (lookfor == LOOKFOR_CPP_BASECLASS) |
7168 } | 7187 } |
7169 | 7188 |
7170 /* | 7189 /* |
7171 * Ignore jump labels with nothing after them. | 7190 * Ignore jump labels with nothing after them. |
7172 */ | 7191 */ |
7173 if (cin_islabel(ind_maxcomment)) | 7192 if (!ind_js && cin_islabel(ind_maxcomment)) |
7174 { | 7193 { |
7175 l = after_label(ml_get_curline()); | 7194 l = after_label(ml_get_curline()); |
7176 if (l == NULL || cin_nocode(l)) | 7195 if (l == NULL || cin_nocode(l)) |
7177 continue; | 7196 continue; |
7178 } | 7197 } |
7279 * case xx: if ( asdf && | 7298 * case xx: if ( asdf && |
7280 * asdf) | 7299 * asdf) |
7281 */ | 7300 */ |
7282 curwin->w_cursor = *trypos; | 7301 curwin->w_cursor = *trypos; |
7283 l = ml_get_curline(); | 7302 l = ml_get_curline(); |
7284 if (cin_iscase(l) || cin_isscopedecl(l)) | 7303 if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
7285 { | 7304 { |
7286 ++curwin->w_cursor.lnum; | 7305 ++curwin->w_cursor.lnum; |
7287 curwin->w_cursor.col = 0; | 7306 curwin->w_cursor.col = 0; |
7288 continue; | 7307 continue; |
7289 } | 7308 } |
7310 | 7329 |
7311 /* | 7330 /* |
7312 * Get indent and pointer to text for current line, | 7331 * Get indent and pointer to text for current line, |
7313 * ignoring any jump label. XXX | 7332 * ignoring any jump label. XXX |
7314 */ | 7333 */ |
7315 cur_amount = skip_label(curwin->w_cursor.lnum, | 7334 if (!ind_js) |
7335 cur_amount = skip_label(curwin->w_cursor.lnum, | |
7316 &l, ind_maxcomment); | 7336 &l, ind_maxcomment); |
7317 | 7337 else |
7338 cur_amount = get_indent(); | |
7318 /* | 7339 /* |
7319 * If this is just above the line we are indenting, and it | 7340 * If this is just above the line we are indenting, and it |
7320 * starts with a '{', line it up with this line. | 7341 * starts with a '{', line it up with this line. |
7321 * while (not) | 7342 * while (not) |
7322 * -> { | 7343 * -> { |
7638 * case xx: if ( asdf && | 7659 * case xx: if ( asdf && |
7639 * asdf) | 7660 * asdf) |
7640 */ | 7661 */ |
7641 curwin->w_cursor = *trypos; | 7662 curwin->w_cursor = *trypos; |
7642 l = ml_get_curline(); | 7663 l = ml_get_curline(); |
7643 if (cin_iscase(l) || cin_isscopedecl(l)) | 7664 if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
7644 { | 7665 { |
7645 ++curwin->w_cursor.lnum; | 7666 ++curwin->w_cursor.lnum; |
7646 curwin->w_cursor.col = 0; | 7667 curwin->w_cursor.col = 0; |
7647 continue; | 7668 continue; |
7648 } | 7669 } |
7655 * } | 7676 * } |
7656 * case 2: | 7677 * case 2: |
7657 * stat; | 7678 * stat; |
7658 * } | 7679 * } |
7659 */ | 7680 */ |
7660 iscase = (ind_keep_case_label && cin_iscase(l)); | 7681 iscase = (ind_keep_case_label && cin_iscase(l, FALSE)); |
7661 | 7682 |
7662 /* | 7683 /* |
7663 * Get indent and pointer to text for current line, | 7684 * Get indent and pointer to text for current line, |
7664 * ignoring any jump label. | 7685 * ignoring any jump label. |
7665 */ | 7686 */ |