aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/indent_c.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/indent_c.c')
-rw-r--r--src/nvim/indent_c.c1464
1 files changed, 823 insertions, 641 deletions
diff --git a/src/nvim/indent_c.c b/src/nvim/indent_c.c
index 0f0cab33ea..3c74b4bd8d 100644
--- a/src/nvim/indent_c.c
+++ b/src/nvim/indent_c.c
@@ -1,12 +1,9 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
-// uncrustify:off
-
#include <assert.h>
#include <inttypes.h>
-#include "nvim/vim.h"
#include "nvim/ascii.h"
#include "nvim/charset.h"
#include "nvim/cursor.h"
@@ -19,11 +16,12 @@
#include "nvim/option.h"
#include "nvim/search.h"
#include "nvim/strings.h"
+#include "nvim/vim.h"
// Find result cache for cpp_baseclass
typedef struct {
- int found;
- lpos_T lpos;
+ int found;
+ lpos_T lpos;
} cpp_baseclass_cache_T;
#ifdef INCLUDE_GENERATED_DECLARATIONS
@@ -41,25 +39,22 @@ static pos_T *ind_find_start_comment(void) // XXX
pos_T *find_start_comment(int ind_maxcomment) // XXX
{
- pos_T *pos;
- char_u *line;
- char_u *p;
+ pos_T *pos;
int64_t cur_maxcomment = ind_maxcomment;
- for (;; ) {
+ for (;;) {
pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
- if (pos == NULL)
+ if (pos == NULL) {
break;
+ }
/*
* Check if the comment start we found is inside a string.
* If it is then restrict the search to below this line and try again.
*/
- line = ml_get(pos->lnum);
- for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
- p = skip_string(p);
- if ((colnr_T)(p - line) <= pos->col)
+ if (!is_pos_in_string(ml_get(pos->lnum), pos->col)) {
break;
+ }
cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
if (cur_maxcomment <= 0) {
pos = NULL;
@@ -109,41 +104,34 @@ static pos_T *ind_find_start_CORS(linenr_T *is_raw)
*/
static pos_T *find_start_rawstring(int ind_maxcomment) // XXX
{
- pos_T *pos;
- char_u *line;
- char_u *p;
- long cur_maxcomment = ind_maxcomment;
+ pos_T *pos;
+ long cur_maxcomment = ind_maxcomment;
- for (;;)
- {
- pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
- if (pos == NULL)
- break;
+ for (;;) {
+ pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
+ if (pos == NULL) {
+ break;
+ }
- /*
- * Check if the raw string start we found is inside a string.
- * If it is then restrict the search to below this line and try again.
- */
- line = ml_get(pos->lnum);
- for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
- p = skip_string(p);
- if ((colnr_T)(p - line) <= pos->col)
- break;
- cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
- if (cur_maxcomment <= 0)
- {
- pos = NULL;
- break;
- }
+ // Check if the raw string start we found is inside a string.
+ // If it is then restrict the search to below this line and try again.
+ if (!is_pos_in_string(ml_get(pos->lnum), pos->col)) {
+ break;
+ }
+ cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
+ if (cur_maxcomment <= 0) {
+ pos = NULL;
+ break;
}
- return pos;
+ }
+ return pos;
}
/*
* Skip to the end of a "string" and a 'c' character.
* If there is no string or character, return argument unmodified.
*/
-static char_u *skip_string(char_u *p)
+static const char_u *skip_string(const char_u *p)
{
int i;
@@ -152,17 +140,17 @@ static char_u *skip_string(char_u *p)
*/
for (;; p++) {
if (p[0] == '\'') { // 'c' or '\n' or '\000'
- if (!p[1]) { // ' at end of line
+ if (p[1] == NUL) { // ' at end of line
break;
}
i = 2;
- if (p[1] == '\\') { // '\n' or '\000'
+ if (p[1] == '\\' && p[2] != NUL) { // '\n' or '\000'
i++;
while (ascii_isdigit(p[i - 1])) { // '\000'
i++;
}
}
- if (p[i] == '\'') { // check for trailing '
+ if (p[i - 1] != NUL && p[i] == '\'') { // check for trailing '
p += i;
continue;
}
@@ -178,24 +166,24 @@ static char_u *skip_string(char_u *p)
continue; // continue for another string
}
} else if (p[0] == 'R' && p[1] == '"') {
- // Raw string: R"[delim](...)[delim]"
- char_u *delim = p + 2;
- char_u *paren = vim_strchr(delim, '(');
-
- if (paren != NULL) {
- const ptrdiff_t delim_len = paren - delim;
-
- for (p += 3; *p; ++p)
- if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
- && p[delim_len + 1] == '"')
- {
- p += delim_len + 1;
- break;
- }
- if (p[0] == '"') {
- continue; // continue for another string
- }
+ // Raw string: R"[delim](...)[delim]"
+ const char *delim = (char *)p + 2;
+ const char *paren = vim_strchr((char *)delim, '(');
+
+ if (paren != NULL) {
+ const ptrdiff_t delim_len = paren - delim;
+
+ for (p += 3; *p; p++) {
+ if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
+ && p[delim_len + 1] == '"') {
+ p += delim_len + 1;
+ break;
+ }
}
+ if (p[0] == '"') {
+ continue; // continue for another string
+ }
+ }
}
break; // no string found
}
@@ -205,6 +193,16 @@ static char_u *skip_string(char_u *p)
return p;
}
+/// @returns true if "line[col]" is inside a C string.
+int is_pos_in_string(const char_u *line, colnr_T col)
+{
+ const char_u *p;
+
+ for (p = line; *p && (colnr_T)(p - line) < col; p++) {
+ p = skip_string(p);
+ }
+ return !((colnr_T)(p - line) <= col);
+}
/*
* Functions for C-indenting.
@@ -214,20 +212,19 @@ static char_u *skip_string(char_u *p)
* Below "XXX" means that this function may unlock the current line.
*/
-
/*
* Return true if the string "line" starts with a word from 'cinwords'.
*/
-bool cin_is_cinword(char_u *line)
+bool cin_is_cinword(const char_u *line)
{
bool retval = false;
size_t cinw_len = STRLEN(curbuf->b_p_cinw) + 1;
char_u *cinw_buf = xmalloc(cinw_len);
- line = skipwhite(line);
+ line = (char_u *)skipwhite((char *)line);
- for (char_u *cinw = curbuf->b_p_cinw; *cinw; ) {
- size_t len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
+ for (char_u *cinw = curbuf->b_p_cinw; *cinw;) {
+ size_t len = copy_option_part((char **)&cinw, (char *)cinw_buf, cinw_len, ",");
if (STRNCMP(line, cinw_buf, len) == 0
&& (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1]))) {
retval = true;
@@ -240,18 +237,16 @@ bool cin_is_cinword(char_u *line)
return retval;
}
-
-
/*
* Skip over white space and C comments within the line.
* Also skip over Perl/shell comments if desired.
*/
-static char_u *cin_skipcomment(char_u *s)
+static const char_u *cin_skipcomment(const char_u *s)
{
while (*s) {
- char_u *prev_s = s;
+ const char_u *prev_s = s;
- s = skipwhite(s);
+ s = (char_u *)skipwhite((char *)s);
/* Perl/shell # comment comment continues until eol. Require a space
* before # to avoid recognizing $#array. */
@@ -267,8 +262,9 @@ static char_u *cin_skipcomment(char_u *s)
s += STRLEN(s);
break;
}
- if (*s != '*')
+ if (*s != '*') {
break;
+ }
for (++s; *s; s++) { // skip slash-star comment
if (s[0] == '*' && s[1] == '/') {
s += 2;
@@ -283,7 +279,7 @@ static char_u *cin_skipcomment(char_u *s)
* Return TRUE if there is no code at *s. White space and comments are
* not considered code.
*/
-static int cin_nocode(char_u *s)
+static int cin_nocode(const char_u *s)
{
return *cin_skipcomment(s) == NUL;
}
@@ -294,27 +290,28 @@ static int cin_nocode(char_u *s)
static pos_T *find_line_comment(void) // XXX
{
static pos_T pos;
- char_u *line;
- char_u *p;
+ char_u *line;
+ char_u *p;
pos = curwin->w_cursor;
while (--pos.lnum > 0) {
line = ml_get(pos.lnum);
- p = skipwhite(line);
+ p = (char_u *)skipwhite((char *)line);
if (cin_islinecomment(p)) {
pos.col = (int)(p - line);
return &pos;
}
- if (*p != NUL)
+ if (*p != NUL) {
break;
+ }
}
return NULL;
}
/// Checks if `text` starts with "key:".
-static bool cin_has_js_key(char_u *text)
+static bool cin_has_js_key(const char_u *text)
{
- char_u *s = skipwhite(text);
+ const char_u *s = (char_u *)skipwhite((char *)text);
char_u quote = 0;
if (*s == '\'' || *s == '"') {
@@ -322,8 +319,8 @@ static bool cin_has_js_key(char_u *text)
quote = *s;
++s;
}
- if (!vim_isIDc(*s)) { // need at least one ID character
- return FALSE;
+ if (!vim_isIDc(*s)) { // need at least one ID character
+ return false;
}
while (vim_isIDc(*s)) {
@@ -341,15 +338,16 @@ static bool cin_has_js_key(char_u *text)
/// Checks if string matches "label:"; move to character after ':' if true.
/// "*s" must point to the start of the label, if there is one.
-static bool cin_islabel_skip(char_u **s)
+static bool cin_islabel_skip(const char_u **s)
FUNC_ATTR_NONNULL_ALL
{
if (!vim_isIDc(**s)) { // need at least one ID character
return false;
}
- while (vim_isIDc(**s))
+ while (vim_isIDc(**s)) {
(*s)++;
+ }
*s = cin_skipcomment(*s);
@@ -361,7 +359,7 @@ static bool cin_islabel_skip(char_u **s)
// Note: curwin->w_cursor must be where we are looking for the label.
bool cin_islabel(void) // XXX
{
- char_u *s = cin_skipcomment(get_cursor_line_ptr());
+ const char_u *s = cin_skipcomment(get_cursor_line_ptr());
// Exclude "default" from labels, since it should be indented
// like a switch label. Same for C++ scope declarations.
@@ -380,8 +378,8 @@ bool cin_islabel(void) // XXX
* label.
*/
pos_T cursor_save;
- pos_T *trypos;
- char_u *line;
+ pos_T *trypos;
+ const char_u *line;
cursor_save = curwin->w_cursor;
while (curwin->w_cursor.lnum > 1) {
@@ -424,15 +422,16 @@ bool cin_islabel(void) // XXX
*/
static int cin_isinit(void)
{
- char_u *s;
- static char *skip[] = {"static", "public", "protected", "private"};
+ const char_u *s;
+ static char *skip[] = { "static", "public", "protected", "private" };
s = cin_skipcomment(get_cursor_line_ptr());
- if (cin_starts_with(s, "typedef"))
+ if (cin_starts_with(s, "typedef")) {
s = cin_skipcomment(s + 7);
+ }
- for (;; ) {
+ for (;;) {
int i, l;
for (i = 0; i < (int)ARRAY_SIZE(skip); ++i) {
@@ -443,26 +442,26 @@ static int cin_isinit(void)
break;
}
}
- if (l != 0)
+ if (l != 0) {
break;
+ }
}
- if (cin_starts_with(s, "enum"))
- return TRUE;
+ if (cin_starts_with(s, "enum")) {
+ return true;
+ }
- if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
- return TRUE;
+ if (cin_ends_in(s, (char_u *)"=", (char_u *)"{")) {
+ return true;
+ }
return FALSE;
}
-/*
- * Recognize a switch label: "case .*:" or "default:".
- */
-bool cin_iscase(
- char_u *s,
- bool strict // Allow relaxed check of case statement for JS
-)
+/// Recognize a switch label: "case .*:" or "default:".
+///
+/// @param strict Allow relaxed check of case statement for JS
+bool cin_iscase(const char_u *s, bool strict)
{
s = cin_skipcomment(s);
if (cin_starts_with(s, "case")) {
@@ -503,50 +502,61 @@ bool cin_iscase(
/*
* Recognize a "default" switch label.
*/
-static int cin_isdefault(char_u *s)
+static int cin_isdefault(const char_u *s)
{
return STRNCMP(s, "default", 7) == 0
&& *(s = cin_skipcomment(s + 7)) == ':'
&& s[1] != ':';
}
-/*
- * Recognize a "public/private/protected" scope declaration label.
- */
-bool cin_isscopedecl(char_u *s)
+/// Recognize a scope declaration label set in 'cinscopedecls'.
+bool cin_isscopedecl(const char_u *p)
{
- int i;
+ const char_u *s = cin_skipcomment(p);
- s = cin_skipcomment(s);
- if (STRNCMP(s, "public", 6) == 0) {
- i = 6;
- } else if (STRNCMP(s, "protected", 9) == 0) {
- i = 9;
- } else if (STRNCMP(s, "private", 7) == 0) {
- i = 7;
- } else {
- return false;
+ const size_t cinsd_len = STRLEN(curbuf->b_p_cinsd) + 1;
+ char_u *cinsd_buf = xmalloc(cinsd_len);
+
+ bool found = false;
+
+ for (char_u *cinsd = curbuf->b_p_cinsd; *cinsd;) {
+ const size_t len = copy_option_part((char **)&cinsd, (char *)cinsd_buf, cinsd_len, ",");
+ if (STRNCMP(s, cinsd_buf, len) == 0) {
+ const char_u *skip = cin_skipcomment(s + len);
+ if (*skip == ':' && skip[1] != ':') {
+ found = true;
+ break;
+ }
+ }
}
- return *(s = cin_skipcomment(s + i)) == ':' && s[1] != ':';
+
+ xfree(cinsd_buf);
+
+ return found;
}
// Maximum number of lines to search back for a "namespace" line.
#define FIND_NAMESPACE_LIM 20
// Recognize a "namespace" scope declaration.
-static bool cin_is_cpp_namespace(char_u *s)
+static bool cin_is_cpp_namespace(const char_u *s)
{
- char_u *p;
+ const char_u *p;
bool has_name = false;
bool has_name_start = false;
s = cin_skipcomment(s);
+
+ if (STRNCMP(s, "inline", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6]))) {
+ s = cin_skipcomment((char_u *)skipwhite((char *)s + 6));
+ }
+
if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9]))) {
- p = cin_skipcomment(skipwhite(s + 9));
+ p = cin_skipcomment((char_u *)skipwhite((char *)s + 9));
while (*p != NUL) {
if (ascii_iswhite(*p)) {
has_name = true; // found end of a name
- p = cin_skipcomment(skipwhite(p));
+ p = cin_skipcomment((char_u *)skipwhite((char *)p));
} else if (*p == '{') {
break;
} else if (vim_iswordc(*p)) {
@@ -573,10 +583,10 @@ static bool cin_is_cpp_namespace(char_u *s)
/*
* Return a pointer to the first non-empty non-comment character after a ':'.
* Return NULL if not found.
- * case 234: a = b;
- * ^
+ * case 234: a = b;
+ * ^
*/
-static char_u *after_label(char_u *l)
+static const char_u *after_label(const char_u *l)
{
for (; *l; ++l) {
if (*l == ':') {
@@ -589,11 +599,13 @@ static char_u *after_label(char_u *l)
l += 2; // skip over 'x'
}
}
- if (*l == NUL)
+ if (*l == NUL) {
return NULL;
+ }
l = cin_skipcomment(l + 1);
- if (*l == NUL)
+ if (*l == NUL) {
return NULL;
+ }
return l;
}
@@ -603,15 +615,16 @@ static char_u *after_label(char_u *l)
*/
static int get_indent_nolabel(linenr_T lnum) // XXX
{
- char_u *l;
+ const char_u *l;
pos_T fp;
colnr_T col;
- char_u *p;
+ const char_u *p;
l = ml_get(lnum);
p = after_label(l);
- if (p == NULL)
+ if (p == NULL) {
return 0;
+ }
fp.col = (colnr_T)(p - l);
fp.lnum = lnum;
@@ -622,12 +635,12 @@ static int get_indent_nolabel(linenr_T lnum) // XXX
/*
* Find indent for line "lnum", ignoring any case or jump label.
* Also return a pointer to the text (after the label) in "pp".
- * label: if (asdf && asdfasdf)
- * ^
+ * label: if (asdf && asdfasdf)
+ * ^
*/
-static int skip_label(linenr_T lnum, char_u **pp)
+static int skip_label(linenr_T lnum, const char_u **pp)
{
- char_u *l;
+ const char_u *l;
int amount;
pos_T cursor_save;
@@ -653,44 +666,45 @@ static int skip_label(linenr_T lnum, char_u **pp)
/*
* Return the indent of the first variable name after a type in a declaration.
- * int a, indent of "a"
- * static struct foo b, indent of "b"
- * enum bla c, indent of "c"
+ * int a, indent of "a"
+ * static struct foo b, indent of "b"
+ * enum bla c, indent of "c"
* Returns zero when it doesn't look like a declaration.
*/
static int cin_first_id_amount(void)
{
- char_u *line, *p, *s;
+ char_u *line, *p, *s;
int len;
pos_T fp;
colnr_T col;
line = get_cursor_line_ptr();
- p = skipwhite(line);
+ p = (char_u *)skipwhite((char *)line);
len = (int)(skiptowhite(p) - p);
if (len == 6 && STRNCMP(p, "static", 6) == 0) {
- p = skipwhite(p + 6);
+ p = (char_u *)skipwhite((char *)p + 6);
len = (int)(skiptowhite(p) - p);
}
- if (len == 6 && STRNCMP(p, "struct", 6) == 0)
- p = skipwhite(p + 6);
- else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
- p = skipwhite(p + 4);
- else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
- || (len == 6 && STRNCMP(p, "signed", 6) == 0)) {
- s = skipwhite(p + len);
+ if (len == 6 && STRNCMP(p, "struct", 6) == 0) {
+ p = (char_u *)skipwhite((char *)p + 6);
+ } else if (len == 4 && STRNCMP(p, "enum", 4) == 0) {
+ p = (char_u *)skipwhite((char *)p + 4);
+ } else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
+ || (len == 6 && STRNCMP(p, "signed", 6) == 0)) {
+ s = (char_u *)skipwhite((char *)p + len);
if ((STRNCMP(s, "int", 3) == 0 && ascii_iswhite(s[3]))
|| (STRNCMP(s, "long", 4) == 0 && ascii_iswhite(s[4]))
|| (STRNCMP(s, "short", 5) == 0 && ascii_iswhite(s[5]))
- || (STRNCMP(s, "char", 4) == 0 && ascii_iswhite(s[4])))
+ || (STRNCMP(s, "char", 4) == 0 && ascii_iswhite(s[4]))) {
p = s;
+ }
}
- for (len = 0; vim_isIDc(p[len]); ++len)
- ;
- if (len == 0 || !ascii_iswhite(p[len]) || cin_nocode(p))
+ for (len = 0; vim_isIDc(p[len]); len++) {}
+ if (len == 0 || !ascii_iswhite(p[len]) || cin_nocode(p)) {
return 0;
+ }
- p = skipwhite(p + len);
+ p = (char_u *)skipwhite((char *)p + len);
fp.lnum = curwin->w_cursor.lnum;
fp.col = (colnr_T)(p - line);
getvcol(curwin, &fp, &col, NULL, NULL);
@@ -703,36 +717,39 @@ static int cin_first_id_amount(void)
* Return zero if no (useful) equal sign found.
* Return -1 if the line above "lnum" ends in a backslash.
* foo = "asdf\
- * asdf\
- * here";
+ * asdf\
+ * here";
*/
static int cin_get_equal_amount(linenr_T lnum)
{
- char_u *line;
- char_u *s;
+ const char_u *line;
+ const char_u *s;
colnr_T col;
pos_T fp;
if (lnum > 1) {
line = ml_get(lnum - 1);
- if (*line != NUL && line[STRLEN(line) - 1] == '\\')
+ if (*line != NUL && line[STRLEN(line) - 1] == '\\') {
return -1;
+ }
}
line = s = ml_get(lnum);
- while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL) {
+ while (*s != NUL && vim_strchr("=;{}\"'", *s) == NULL) {
if (cin_iscomment(s)) { // ignore comments
s = cin_skipcomment(s);
} else {
s++;
}
}
- if (*s != '=')
+ if (*s != '=') {
return 0;
+ }
- s = skipwhite(s + 1);
- if (cin_nocode(s))
+ s = (char_u *)skipwhite((char *)s + 1);
+ if (cin_nocode(s)) {
return 0;
+ }
if (*s == '"') { // nice alignment for continued strings
s++;
@@ -747,10 +764,11 @@ static int cin_get_equal_amount(linenr_T lnum)
/*
* Recognize a preprocessor statement: Any line that starts with '#'.
*/
-static int cin_ispreproc(char_u *s)
+static int cin_ispreproc(const char_u *s)
{
- if (*skipwhite(s) == '#')
- return TRUE;
+ if (*skipwhite((char *)s) == '#') {
+ return true;
+ }
return FALSE;
}
@@ -758,9 +776,9 @@ static int cin_ispreproc(char_u *s)
/// continuation line of a preprocessor statement. Decrease "*lnump" to the
/// start and return the line in "*pp".
/// Put the amount of indent in "*amount".
-static int cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
+static int cin_ispreproc_cont(const char_u **pp, linenr_T *lnump, int *amount)
{
- char_u *line = *pp;
+ const char_u *line = *pp;
linenr_T lnum = *lnump;
int retval = false;
int candidate_amount = *amount;
@@ -769,17 +787,19 @@ static int cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
candidate_amount = get_indent_lnum(lnum);
}
- for (;; ) {
+ for (;;) {
if (cin_ispreproc(line)) {
retval = TRUE;
*lnump = lnum;
break;
}
- if (lnum == 1)
+ if (lnum == 1) {
break;
+ }
line = ml_get(--lnum);
- if (*line == NUL || line[STRLEN(line) - 1] != '\\')
+ if (*line == NUL || line[STRLEN(line) - 1] != '\\') {
break;
+ }
}
if (lnum != *lnump) {
@@ -794,7 +814,7 @@ static int cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
/*
* Recognize the start of a C or C++ comment.
*/
-static int cin_iscomment(char_u *p)
+static int cin_iscomment(const char_u *p)
{
return p[0] == '/' && (p[1] == '*' || p[1] == '/');
}
@@ -802,26 +822,23 @@ static int cin_iscomment(char_u *p)
/*
* Recognize the start of a "//" comment.
*/
-static int cin_islinecomment(char_u *p)
+static int cin_islinecomment(const char_u *p)
{
return p[0] == '/' && p[1] == '/';
}
-/*
- * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
- * '}'.
- * Don't consider "} else" a terminated line.
- * If a line begins with an "else", only consider it terminated if no unmatched
- * opening braces follow (handle "else { foo();" correctly).
- * Return the character terminating the line (ending char's have precedence if
- * both apply in order to determine initializations).
- */
-static char_u
-cin_isterminated (
- char_u *s,
- int incl_open, // include '{' at the end as terminator
- int incl_comma // recognize a trailing comma
-)
+/// Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
+/// '}'.
+/// Don't consider "} else" a terminated line.
+/// If a line begins with an "else", only consider it terminated if no unmatched
+/// opening braces follow (handle "else { foo();" correctly).
+///
+/// @param incl_open include '{' at the end as terminator
+/// @param incl_comma recognize a trailing comma
+///
+/// @return the character terminating the line (ending char's have precedence if
+/// both apply in order to determine initializations).
+static char_u cin_isterminated(const char_u *s, int incl_open, int incl_comma)
{
char_u found_start = 0;
unsigned n_open = 0;
@@ -829,30 +846,35 @@ cin_isterminated (
s = cin_skipcomment(s);
- if (*s == '{' || (*s == '}' && !cin_iselse(s)))
+ if (*s == '{' || (*s == '}' && !cin_iselse(s))) {
found_start = *s;
+ }
- if (!found_start)
+ if (!found_start) {
is_else = cin_iselse(s);
+ }
while (*s) {
// skip over comments, "" strings and 'c'haracters
s = skip_string(cin_skipcomment(s));
- if (*s == '}' && n_open > 0)
- --n_open;
+ if (*s == '}' && n_open > 0) {
+ n_open--;
+ }
if ((!is_else || n_open == 0)
&& (*s == ';' || *s == '}' || (incl_comma && *s == ','))
- && cin_nocode(s + 1))
+ && cin_nocode(s + 1)) {
return *s;
- else if (*s == '{') {
- if (incl_open && cin_nocode(s + 1))
+ } else if (*s == '{') {
+ if (incl_open && cin_nocode(s + 1)) {
return *s;
- else
- ++n_open;
+ } else {
+ n_open++;
+ }
}
- if (*s)
+ if (*s) {
s++;
+ }
}
return found_start;
}
@@ -867,19 +889,20 @@ cin_isterminated (
/// lines here.
/// @param[in] first_lnum Where to start looking.
/// @param[in] min_lnum The line before which we will not be looking.
-static int cin_isfuncdecl(char_u **sp, linenr_T first_lnum, linenr_T min_lnum)
+static int cin_isfuncdecl(const char_u **sp, linenr_T first_lnum, linenr_T min_lnum)
{
- char_u *s;
+ const char_u *s;
linenr_T lnum = first_lnum;
linenr_T save_lnum = curwin->w_cursor.lnum;
int retval = false;
- pos_T *trypos;
- int just_started = TRUE;
+ pos_T *trypos;
+ int just_started = true;
- if (sp == NULL)
+ if (sp == NULL) {
s = ml_get(lnum);
- else
+ } else {
s = *sp;
+ }
curwin->w_cursor.lnum = lnum;
if (find_last_paren(s, '(', ')')
@@ -929,8 +952,9 @@ static int cin_isfuncdecl(char_u **sp, linenr_T first_lnum, linenr_T min_lnum)
// defined(y)
lnum = first_lnum - 1;
s = ml_get(lnum);
- if (*s == NUL || s[STRLEN(s) - 1] != '\\')
- retval = TRUE;
+ if (*s == NUL || s[STRLEN(s) - 1] != '\\') {
+ retval = true;
+ }
goto done;
}
if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s)) {
@@ -940,20 +964,24 @@ static int cin_isfuncdecl(char_u **sp, linenr_T first_lnum, linenr_T min_lnum)
* At the end: check for ',' in the next line, for this style:
* func(arg1
* , arg2) */
- for (;; ) {
- if (lnum >= curbuf->b_ml.ml_line_count)
+ for (;;) {
+ if (lnum >= curbuf->b_ml.ml_line_count) {
break;
+ }
s = ml_get(++lnum);
- if (!cin_ispreproc(s))
+ if (!cin_ispreproc(s)) {
break;
+ }
}
- if (lnum >= curbuf->b_ml.ml_line_count)
+ if (lnum >= curbuf->b_ml.ml_line_count) {
break;
- /* Require a comma at end of the line or a comma or ')' at the
- * start of next line. */
- s = skipwhite(s);
- if (!just_started && (!comma && *s != ',' && *s != ')'))
+ }
+ // Require a comma at end of the line or a comma or ')' at the
+ // start of next line.
+ s = (char_u *)skipwhite((char *)s);
+ if (!just_started && (!comma && *s != ',' && *s != ')')) {
break;
+ }
just_started = false;
} else if (cin_iscomment(s)) { // ignore comments
s = cin_skipcomment(s);
@@ -964,18 +992,19 @@ static int cin_isfuncdecl(char_u **sp, linenr_T first_lnum, linenr_T min_lnum)
}
done:
- if (lnum != first_lnum && sp != NULL)
+ if (lnum != first_lnum && sp != NULL) {
*sp = ml_get(first_lnum);
+ }
return retval;
}
-static int cin_isif(char_u *p)
+static int cin_isif(const char_u *p)
{
return STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]);
}
-static int cin_iselse(char_u *p)
+static int cin_iselse(const char_u *p)
{
if (*p == '}') { // accept "} else"
p = cin_skipcomment(p + 1);
@@ -983,7 +1012,7 @@ static int cin_iselse(char_u *p)
return STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]);
}
-static int cin_isdo(char_u *p)
+static int cin_isdo(const char_u *p)
{
return STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]);
}
@@ -993,11 +1022,11 @@ static int cin_isdo(char_u *p)
* We only accept a "while (condition) ;", with only white space between the
* ')' and ';'. The condition may be spread over several lines.
*/
-static int cin_iswhileofdo(char_u *p, linenr_T lnum) // XXX
+static int cin_iswhileofdo(const char_u *p, linenr_T lnum) // XXX
{
pos_T cursor_save;
- pos_T *trypos;
- int retval = FALSE;
+ pos_T *trypos;
+ int retval = false;
p = cin_skipcomment(p);
if (*p == '}') { // accept "} while (cond);"
@@ -1012,10 +1041,10 @@ static int cin_iswhileofdo(char_u *p, linenr_T lnum) // XXX
p++;
curwin->w_cursor.col++;
}
- if ((trypos = findmatchlimit(NULL, 0, 0,
- curbuf->b_ind_maxparen)) != NULL
- && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
- retval = TRUE;
+ if ((trypos = findmatchlimit(NULL, 0, 0, curbuf->b_ind_maxparen)) != NULL
+ && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';') {
+ retval = true;
+ }
curwin->w_cursor = cursor_save;
}
return retval;
@@ -1027,28 +1056,33 @@ static int cin_iswhileofdo(char_u *p, linenr_T lnum) // XXX
* Otherwise return !0 and update "*poffset" to point to the place where the
* string was found.
*/
-static int cin_is_if_for_while_before_offset(char_u *line, int *poffset)
+static int cin_is_if_for_while_before_offset(const char_u *line, int *poffset)
{
int offset = *poffset;
- if (offset-- < 2)
+ if (offset-- < 2) {
return 0;
- while (offset > 2 && ascii_iswhite(line[offset]))
- --offset;
+ }
+ while (offset > 2 && ascii_iswhite(line[offset])) {
+ offset--;
+ }
offset -= 1;
- if (!STRNCMP(line + offset, "if", 2))
+ if (!STRNCMP(line + offset, "if", 2)) {
goto probablyFound;
+ }
if (offset >= 1) {
offset -= 1;
- if (!STRNCMP(line + offset, "for", 3))
+ if (!STRNCMP(line + offset, "for", 3)) {
goto probablyFound;
+ }
if (offset >= 2) {
offset -= 2;
- if (!STRNCMP(line + offset, "while", 5))
+ if (!STRNCMP(line + offset, "while", 5)) {
goto probablyFound;
+ }
}
}
return 0;
@@ -1066,15 +1100,15 @@ probablyFound:
* do
* nothing;
* while (foo
- * && bar); <-- here
+ * && bar); <-- here
* Adjust the cursor to the line with "while".
*/
static int cin_iswhileofdo_end(int terminated)
{
- char_u *line;
- char_u *p;
- char_u *s;
- pos_T *trypos;
+ const char_u *line;
+ const char_u *p;
+ const char_u *s;
+ pos_T *trypos;
int i;
if (terminated != ';') { // there must be a ';' at the end
@@ -1085,7 +1119,7 @@ static int cin_iswhileofdo_end(int terminated)
while (*p != NUL) {
p = cin_skipcomment(p);
if (*p == ')') {
- s = skipwhite(p + 1);
+ s = (char_u *)skipwhite((char *)p + 1);
if (*s == ';' && cin_nocode(s + 1)) {
/* Found ");" at end of the line, now check there is "while"
* before the matching '('. XXX */
@@ -1108,13 +1142,14 @@ static int cin_iswhileofdo_end(int terminated)
p = line + i;
}
}
- if (*p != NUL)
- ++p;
+ if (*p != NUL) {
+ p++;
+ }
}
return FALSE;
}
-static int cin_isbreak(char_u *p)
+static int cin_isbreak(const char_u *p)
{
return STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]);
}
@@ -1124,20 +1159,21 @@ static int cin_isbreak(char_u *p)
* constructor-initialization. eg:
*
* class MyClass :
- * baseClass <-- here
+ * baseClass <-- here
* class MyClass : public baseClass,
- * anotherBaseClass <-- here (should probably lineup ??)
+ * anotherBaseClass <-- here (should probably lineup ??)
* MyClass::MyClass(...) :
- * baseClass(...) <-- here (constructor-initialization)
+ * baseClass(...) <-- here (constructor-initialization)
*
* This is a lot of guessing. Watch out for "cond ? func() : foo".
*/
-static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) {
+static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached)
+{
lpos_T *pos = &cached->lpos; // find position
- char_u *s;
+ const char_u *s;
int class_or_struct, lookfor_ctor_init, cpp_base_class;
linenr_T lnum = curwin->w_cursor.lnum;
- char_u *line = get_cursor_line_ptr();
+ const char_u *line = get_cursor_line_ptr();
if (pos->lnum <= lnum) {
return cached->found; // Use the cached result
@@ -1145,51 +1181,56 @@ static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) {
pos->col = 0;
- s = skipwhite(line);
+ s = (char_u *)skipwhite((char *)line);
if (*s == '#') { // skip #define FOO x ? (x) : x
return false;
}
s = cin_skipcomment(s);
- if (*s == NUL)
- return FALSE;
+ if (*s == NUL) {
+ return false;
+ }
cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
/* Search for a line starting with '#', empty, ending in ';' or containing
* '{' or '}' and start below it. This handles the following situations:
- * a = cond ?
- * func() :
- * asdf;
- * func::foo()
- * : something
- * {}
- * Foo::Foo (int one, int two)
- * : something(4),
- * somethingelse(3)
- * {}
+ * a = cond ?
+ * func() :
+ * asdf;
+ * func::foo()
+ * : something
+ * {}
+ * Foo::Foo (int one, int two)
+ * : something(4),
+ * somethingelse(3)
+ * {}
*/
while (lnum > 1) {
line = ml_get(lnum - 1);
- s = skipwhite(line);
- if (*s == '#' || *s == NUL)
+ s = (char_u *)skipwhite((char *)line);
+ if (*s == '#' || *s == NUL) {
break;
+ }
while (*s != NUL) {
s = cin_skipcomment(s);
if (*s == '{' || *s == '}'
- || (*s == ';' && cin_nocode(s + 1)))
+ || (*s == ';' && cin_nocode(s + 1))) {
break;
- if (*s != NUL)
- ++s;
+ }
+ if (*s != NUL) {
+ s++;
+ }
}
- if (*s != NUL)
+ if (*s != NUL) {
break;
- --lnum;
+ }
+ lnum--;
}
pos->lnum = lnum;
line = ml_get(lnum);
s = line;
- for (;; ) {
+ for (;;) {
if (*s == NUL) {
if (lnum == curwin->w_cursor.lnum) {
break;
@@ -1204,13 +1245,14 @@ static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) {
break;
}
s = cin_skipcomment(line);
- if (*s == NUL)
+ if (*s == NUL) {
continue;
+ }
}
- if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
+ if (s[0] == '"' || (s[0] == 'R' && s[1] == '"')) {
s = skip_string(s) + 1;
- else if (s[0] == ':') {
+ } else if (s[0] == ':') {
if (s[1] == ':') {
/* skip double colon. It can't be a constructor
* initialization any more */
@@ -1223,17 +1265,19 @@ static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) {
lookfor_ctor_init = class_or_struct = false;
pos->col = 0;
s = cin_skipcomment(s + 1);
- } else
+ } else {
s = cin_skipcomment(s + 1);
+ }
} else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
|| (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6]))) {
class_or_struct = TRUE;
lookfor_ctor_init = FALSE;
- if (*s == 'c')
+ if (*s == 'c') {
s = cin_skipcomment(s + 5);
- else
+ } else {
s = cin_skipcomment(s + 6);
+ }
} else {
if (s[0] == '{' || s[0] == '}' || s[0] == ';') {
cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
@@ -1279,7 +1323,7 @@ static int get_baseclass_amount(int col)
{
int amount;
colnr_T vcol;
- pos_T *trypos;
+ pos_T *trypos;
if (col == 0) {
amount = get_indent();
@@ -1295,8 +1339,9 @@ static int get_baseclass_amount(int col)
getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
amount = (int)vcol;
}
- if (amount < curbuf->b_ind_cpp_baseclass)
+ if (amount < curbuf->b_ind_cpp_baseclass) {
amount = curbuf->b_ind_cpp_baseclass;
+ }
return amount;
}
@@ -1305,23 +1350,26 @@ static int get_baseclass_amount(int col)
* white space and comments. Skip strings and comments.
* Ignore "ignore" after "find" if it's not NULL.
*/
-static int cin_ends_in(char_u *s, char_u *find, char_u *ignore)
+static int cin_ends_in(const char_u *s, const char_u *find, const char_u *ignore)
{
- char_u *p = s;
- char_u *r;
+ const char_u *p = s;
+ const char_u *r;
int len = (int)STRLEN(find);
while (*p != NUL) {
p = cin_skipcomment(p);
if (STRNCMP(p, find, len) == 0) {
- r = skipwhite(p + len);
- if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
- r = skipwhite(r + STRLEN(ignore));
- if (cin_nocode(r))
- return TRUE;
+ r = (char_u *)skipwhite((char *)p + len);
+ if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0) {
+ r = (char_u *)skipwhite((char *)r + STRLEN(ignore));
+ }
+ if (cin_nocode(r)) {
+ return true;
+ }
+ }
+ if (*p != NUL) {
+ p++;
}
- if (*p != NUL)
- ++p;
}
return FALSE;
}
@@ -1329,7 +1377,7 @@ static int cin_ends_in(char_u *s, char_u *find, char_u *ignore)
/*
* Return TRUE when "s" starts with "word" and then a non-ID character.
*/
-static int cin_starts_with(char_u *s, char *word)
+static int cin_starts_with(const char_u *s, const char *word)
{
int l = (int)STRLEN(word);
@@ -1337,17 +1385,17 @@ static int cin_starts_with(char_u *s, char *word)
}
/// Recognize a `extern "C"` or `extern "C++"` linkage specifications.
-static int cin_is_cpp_extern_c(char_u *s)
+static int cin_is_cpp_extern_c(const char_u *s)
{
- char_u *p;
- int has_string_literal = false;
+ const char_u *p;
+ int has_string_literal = false;
s = cin_skipcomment(s);
if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6]))) {
- p = cin_skipcomment(skipwhite(s + 6));
+ p = cin_skipcomment((char_u *)skipwhite((char *)s + 6));
while (*p != NUL) {
if (ascii_iswhite(*p)) {
- p = cin_skipcomment(skipwhite(p));
+ p = cin_skipcomment((char_u *)skipwhite((char *)p));
} else if (*p == '{') {
break;
} else if (p[0] == '"' && p[1] == 'C' && p[2] == '"') {
@@ -1372,16 +1420,15 @@ static int cin_is_cpp_extern_c(char_u *s)
return false;
}
-
/*
* Skip strings, chars and comments until at or past "trypos".
* Return the column found.
*/
static int cin_skip2pos(pos_T *trypos)
{
- char_u *line;
- char_u *p;
- char_u *new_p;
+ const char_u *line;
+ const char_u *p;
+ const char_u *new_p;
p = line = ml_get(trypos->lnum);
while (*p && (colnr_T)(p - line) < trypos->col) {
@@ -1411,8 +1458,8 @@ static int cin_skip2pos(pos_T *trypos)
static pos_T *find_start_brace(void) // XXX
{
pos_T cursor_save;
- pos_T *trypos;
- pos_T *pos;
+ pos_T *trypos;
+ pos_T *pos;
static pos_T pos_copy;
cursor_save = curwin->w_cursor;
@@ -1427,7 +1474,7 @@ static pos_T *find_start_brace(void) // XXX
break;
}
if (pos != NULL) {
- curwin->w_cursor.lnum = pos->lnum;
+ curwin->w_cursor = *pos;
}
}
curwin->w_cursor = cursor_save;
@@ -1441,10 +1488,10 @@ static pos_T *find_match_paren(int ind_maxparen)
return find_match_char('(', ind_maxparen);
}
-static pos_T * find_match_char(char_u c, int ind_maxparen)
+static pos_T *find_match_char(char_u c, int ind_maxparen)
{
pos_T cursor_save;
- pos_T *trypos;
+ pos_T *trypos;
static pos_T pos_copy;
int ind_maxp_wk;
@@ -1454,7 +1501,7 @@ 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);
+ ind_maxp_wk = ind_maxparen - (cursor_save.lnum - trypos->lnum);
if (ind_maxp_wk > 0) {
curwin->w_cursor = *trypos;
curwin->w_cursor.col = 0; // XXX
@@ -1468,8 +1515,7 @@ retry:
trypos = &pos_copy;
curwin->w_cursor = *trypos;
if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) { // XXX
- ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
- - trypos_wk->lnum);
+ ind_maxp_wk = ind_maxparen - (cursor_save.lnum - trypos_wk->lnum);
if (ind_maxp_wk > 0) {
curwin->w_cursor = *trypos_wk;
goto retry;
@@ -1504,7 +1550,6 @@ static pos_T *find_match_paren_after_brace(int ind_maxparen)
return trypos;
}
-
/*
* Return ind_maxparen corrected for the difference in line number between the
* cursor position and "startpos". This makes sure that searching for a
@@ -1515,8 +1560,9 @@ static int corr_ind_maxparen(pos_T *startpos)
{
long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
- if (n > 0 && n < curbuf->b_ind_maxparen / 2)
+ if (n > 0 && n < curbuf->b_ind_maxparen / 2) {
return curbuf->b_ind_maxparen - (int)n;
+ }
return curbuf->b_ind_maxparen;
}
@@ -1524,7 +1570,7 @@ static int corr_ind_maxparen(pos_T *startpos)
* Set w_cursor.col to the column number of the last unmatched ')' or '{' in
* line "l". "l" must point to the start of the line.
*/
-static int find_last_paren(char_u *l, int start, int end)
+static int find_last_paren(const char_u *l, int start, int end)
{
int i;
int retval = FALSE;
@@ -1555,8 +1601,8 @@ static int find_last_paren(char_u *l, int start, int end)
*/
void parse_cino(buf_T *buf)
{
- char_u *p;
- char_u *l;
+ char_u *p;
+ char_u *l;
int divider;
int fraction = 0;
int sw = get_sw_value(buf);
@@ -1694,13 +1740,13 @@ void parse_cino(buf_T *buf)
// Handle C #pragma directives
buf->b_ind_pragma = 0;
- for (p = buf->b_p_cino; *p; ) {
+ for (p = buf->b_p_cino; *p;) {
l = p++;
if (*p == '-') {
p++;
}
char_u *digits_start = p; // remember where the digits start
- int n = getdigits_int(&p, true, 0);
+ int n = getdigits_int((char **)&p, true, 0);
divider = 0;
if (*p == '.') { // ".5s" means a fraction.
fraction = atoi((char *)++p);
@@ -1718,57 +1764,134 @@ void parse_cino(buf_T *buf)
n = sw; // just "s" is one 'shiftwidth'.
} else {
n *= sw;
- if (divider)
+ if (divider) {
n += (sw * fraction + divider / 2) / divider;
+ }
}
++p;
}
- if (l[1] == '-')
+ if (l[1] == '-') {
n = -n;
+ }
/* When adding an entry here, also update the default 'cinoptions' in
* doc/indent.txt, and add explanation for it! */
switch (*l) {
- case '>': buf->b_ind_level = n; break;
- case 'e': buf->b_ind_open_imag = n; break;
- case 'n': buf->b_ind_no_brace = n; break;
- case 'f': buf->b_ind_first_open = n; break;
- case '{': buf->b_ind_open_extra = n; break;
- case '}': buf->b_ind_close_extra = n; break;
- case '^': buf->b_ind_open_left_imag = n; break;
- case 'L': buf->b_ind_jump_label = n; break;
- case ':': buf->b_ind_case = n; break;
- case '=': buf->b_ind_case_code = n; break;
- case 'b': buf->b_ind_case_break = n; break;
- case 'p': buf->b_ind_param = n; break;
- case 't': buf->b_ind_func_type = n; break;
- case '/': buf->b_ind_comment = n; break;
- case 'c': buf->b_ind_in_comment = n; break;
- case 'C': buf->b_ind_in_comment2 = n; break;
- case 'i': buf->b_ind_cpp_baseclass = n; break;
- case '+': buf->b_ind_continuation = n; break;
- case '(': buf->b_ind_unclosed = n; break;
- case 'u': buf->b_ind_unclosed2 = n; break;
- case 'U': buf->b_ind_unclosed_noignore = n; break;
- case 'W': buf->b_ind_unclosed_wrapped = n; break;
- case 'w': buf->b_ind_unclosed_whiteok = n; break;
- case 'm': buf->b_ind_matching_paren = n; break;
- case 'M': buf->b_ind_paren_prev = n; break;
- case ')': buf->b_ind_maxparen = n; break;
- case '*': buf->b_ind_maxcomment = n; break;
- case 'g': buf->b_ind_scopedecl = n; break;
- case 'h': buf->b_ind_scopedecl_code = n; break;
- case 'j': buf->b_ind_java = n; break;
- case 'J': buf->b_ind_js = n; break;
- case 'l': buf->b_ind_keep_case_label = n; break;
- case '#': buf->b_ind_hash_comment = n; break;
- case 'N': buf->b_ind_cpp_namespace = n; break;
- case 'k': buf->b_ind_if_for_while = n; break;
- case 'E': buf->b_ind_cpp_extern_c = n; break;
- case 'P': buf->b_ind_pragma = n; break;
- }
- if (*p == ',')
- ++p;
+ case '>':
+ buf->b_ind_level = n;
+ break;
+ case 'e':
+ buf->b_ind_open_imag = n;
+ break;
+ case 'n':
+ buf->b_ind_no_brace = n;
+ break;
+ case 'f':
+ buf->b_ind_first_open = n;
+ break;
+ case '{':
+ buf->b_ind_open_extra = n;
+ break;
+ case '}':
+ buf->b_ind_close_extra = n;
+ break;
+ case '^':
+ buf->b_ind_open_left_imag = n;
+ break;
+ case 'L':
+ buf->b_ind_jump_label = n;
+ break;
+ case ':':
+ buf->b_ind_case = n;
+ break;
+ case '=':
+ buf->b_ind_case_code = n;
+ break;
+ case 'b':
+ buf->b_ind_case_break = n;
+ break;
+ case 'p':
+ buf->b_ind_param = n;
+ break;
+ case 't':
+ buf->b_ind_func_type = n;
+ break;
+ case '/':
+ buf->b_ind_comment = n;
+ break;
+ case 'c':
+ buf->b_ind_in_comment = n;
+ break;
+ case 'C':
+ buf->b_ind_in_comment2 = n;
+ break;
+ case 'i':
+ buf->b_ind_cpp_baseclass = n;
+ break;
+ case '+':
+ buf->b_ind_continuation = n;
+ break;
+ case '(':
+ buf->b_ind_unclosed = n;
+ break;
+ case 'u':
+ buf->b_ind_unclosed2 = n;
+ break;
+ case 'U':
+ buf->b_ind_unclosed_noignore = n;
+ break;
+ case 'W':
+ buf->b_ind_unclosed_wrapped = n;
+ break;
+ case 'w':
+ buf->b_ind_unclosed_whiteok = n;
+ break;
+ case 'm':
+ buf->b_ind_matching_paren = n;
+ break;
+ case 'M':
+ buf->b_ind_paren_prev = n;
+ break;
+ case ')':
+ buf->b_ind_maxparen = n;
+ break;
+ case '*':
+ buf->b_ind_maxcomment = n;
+ break;
+ case 'g':
+ buf->b_ind_scopedecl = n;
+ break;
+ case 'h':
+ buf->b_ind_scopedecl_code = n;
+ break;
+ case 'j':
+ buf->b_ind_java = n;
+ break;
+ case 'J':
+ buf->b_ind_js = n;
+ break;
+ case 'l':
+ buf->b_ind_keep_case_label = n;
+ break;
+ case '#':
+ buf->b_ind_hash_comment = n;
+ break;
+ case 'N':
+ buf->b_ind_cpp_namespace = n;
+ break;
+ case 'k':
+ buf->b_ind_if_for_while = n;
+ break;
+ case 'E':
+ buf->b_ind_cpp_extern_c = n;
+ break;
+ case 'P':
+ buf->b_ind_pragma = n;
+ break;
+ }
+ if (*p == ',') {
+ p++;
+ }
}
}
@@ -1783,21 +1906,21 @@ int get_c_indent(void)
int scope_amount;
int cur_amount = MAXCOL;
colnr_T col;
- char_u *theline;
- char_u *linecopy;
- pos_T *trypos;
- pos_T *comment_pos;
- pos_T *tryposBrace = NULL;
- pos_T tryposCopy;
+ char_u *theline;
+ char_u *linecopy;
+ pos_T *trypos;
+ pos_T *comment_pos;
+ pos_T *tryposBrace = NULL;
+ pos_T tryposCopy;
pos_T our_paren_pos;
- char_u *start;
+ char_u *start;
int start_brace;
#define BRACE_IN_COL0 1 // '{' is in column 0
#define BRACE_AT_START 2 // '{' is at start of line
#define BRACE_AT_END 3 // '{' is at end of line
linenr_T ourscope;
- char_u *l;
- char_u *look;
+ const char_u *l;
+ const char_u *look;
char_u terminated;
int lookfor;
#define LOOKFOR_INITIAL 0
@@ -1848,12 +1971,13 @@ int get_c_indent(void)
* For unknown reasons the cursor might be past the end of the line, thus
* check for that.
*/
- if ((State & INSERT)
+ if ((State & MODE_INSERT)
&& curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
- && linecopy[curwin->w_cursor.col] == ')')
+ && linecopy[curwin->w_cursor.col] == ')') {
linecopy[curwin->w_cursor.col] = NUL;
+ }
- theline = skipwhite(linecopy);
+ theline = (char_u *)skipwhite((char *)linecopy);
// move the cursor to the start of the line
@@ -1880,20 +2004,17 @@ int get_c_indent(void)
// #defines and so on go at the left when included in 'cinkeys',
// exluding pragmas when customized in 'cinoptions'
if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', true))) {
- const char_u *const directive = skipwhite(theline + 1);
+ const char_u *const directive = (char_u *)skipwhite((char *)theline + 1);
if (curbuf->b_ind_pragma == 0 || STRNCMP(directive, "pragma", 6) != 0) {
amount = curbuf->b_ind_hash_comment;
goto theend;
}
}
- /*
- * Is it a non-case label? Then that goes at the left margin too unless:
- * - JS flag is set.
- * - 'L' item has a positive value.
- */
- if (original_line_islabel && !curbuf->b_ind_js
- && curbuf->b_ind_jump_label < 0) {
+ // Is it a non-case label? Then that goes at the left margin too unless:
+ // - JS flag is set.
+ // - 'L' item has a positive value.
+ if (original_line_islabel && !curbuf->b_ind_js && curbuf->b_ind_jump_label < 0) {
amount = 0;
goto theend;
}
@@ -1901,12 +2022,25 @@ int 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
- // find how indented the line beginning the comment is
- getvcol(curwin, trypos, &col, NULL, NULL);
- amount = col;
- goto theend;
+ if (cin_islinecomment(theline)) {
+ 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
@@ -1915,10 +2049,10 @@ int get_c_indent(void)
if (!cin_iscomment(theline) && comment_pos != NULL) { // XXX
int lead_start_len = 2;
int lead_middle_len = 1;
- char_u lead_start[COM_MAX_LEN]; // start-comment string
- char_u lead_middle[COM_MAX_LEN]; // middle-comment string
- char_u lead_end[COM_MAX_LEN]; // end-comment string
- char_u *p;
+ char lead_start[COM_MAX_LEN]; // start-comment string
+ char lead_middle[COM_MAX_LEN]; // middle-comment string
+ char lead_end[COM_MAX_LEN]; // end-comment string
+ char_u *p;
int start_align = 0;
int start_off = 0;
int done = FALSE;
@@ -1941,15 +2075,16 @@ int get_c_indent(void)
} else if (*p == COM_LEFT || *p == COM_RIGHT) {
align = *p++;
} else if (ascii_isdigit(*p) || *p == '-') {
- off = getdigits_int(&p, true, 0);
+ off = getdigits_int((char **)&p, true, 0);
} else {
p++;
}
}
- if (*p == ':')
- ++p;
- (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
+ if (*p == ':') {
+ p++;
+ }
+ (void)copy_option_part((char **)&p, lead_end, COM_MAX_LEN, ",");
if (what == COM_START) {
STRCPY(lead_start, lead_end);
lead_start_len = (int)STRLEN(lead_start);
@@ -1969,25 +2104,24 @@ int get_c_indent(void)
* line, use the indent of that line plus offset. If
* the middle comment string matches in the previous
* line, use the indent of that line. XXX */
- look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
- if (STRNCMP(look, lead_start, lead_start_len) == 0)
+ look = (char_u *)skipwhite((char *)ml_get(curwin->w_cursor.lnum - 1));
+ if (STRNCMP(look, lead_start, lead_start_len) == 0) {
amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
- else if (STRNCMP(look, lead_middle,
- lead_middle_len) == 0) {
+ } else if (STRNCMP(look, lead_middle, lead_middle_len) == 0) {
amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
break;
} else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
- lead_start, lead_start_len) != 0) {
- /* If the start comment string doesn't match with the
- * start of the comment, skip this entry. XXX */
+ lead_start, lead_start_len) != 0) {
+ // If the start comment string doesn't match with the
+ // start of the comment, skip this entry. XXX
continue;
}
}
- if (start_off != 0)
+ if (start_off != 0) {
amount += start_off;
- else if (start_align == COM_RIGHT)
- amount += vim_strsize(lead_start)
- - vim_strsize(lead_middle);
+ } else if (start_align == COM_RIGHT) {
+ amount += vim_strsize(lead_start) - vim_strsize(lead_middle);
+ }
break;
}
@@ -2012,18 +2146,16 @@ int get_c_indent(void)
* asterisk in the comment opener; otherwise, line up
* with the first character of the comment text.
*/
- if (done)
- ;
- else if (theline[0] == '*')
+ if (done) {
+ // skip
+ } else if (theline[0] == '*') {
amount += 1;
- else {
- /*
- * If we are more than one line away from the comment opener, take
- * the indent of the previous non-empty line. If 'cino' has "CO"
- * and we are just below the comment opener and there are any
- * white characters after it line up with the text after it;
- * otherwise, add the amount specified by "c" in 'cino'
- */
+ } else {
+ // If we are more than one line away from the comment opener, take
+ // the indent of the previous non-empty line. If 'cino' has "CO"
+ // and we are just below the comment opener and there are any
+ // white characters after it line up with the text after it;
+ // otherwise, add the amount specified by "c" in 'cino'
amount = -1;
for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; lnum--) {
if (linewhite(lnum)) { // skip blank lines
@@ -2037,20 +2169,21 @@ int get_c_indent(void)
start = ml_get(comment_pos->lnum);
look = start + comment_pos->col + 2; // skip / and *
if (*look != NUL) { // if something after it
- comment_pos->col = (colnr_T)(skipwhite(look) - start);
+ comment_pos->col = (colnr_T)((char_u *)skipwhite((char *)look) - start);
}
}
getvcol(curwin, comment_pos, &col, NULL, NULL);
amount = col;
- if (curbuf->b_ind_in_comment2 || *look == NUL)
+ if (curbuf->b_ind_in_comment2 || *look == NUL) {
amount += curbuf->b_ind_in_comment;
+ }
}
}
- goto theend;
+ goto theend;
}
// Are we looking at a ']' that has a match?
- if (*skipwhite(theline) == ']'
- && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL) {
+ if (*skipwhite((char *)theline) == ']'
+ && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL) {
// align with the line containing the '['.
amount = get_indent_lnum(trypos->lnum);
goto theend;
@@ -2058,18 +2191,19 @@ int get_c_indent(void)
// Are we inside parentheses or braces?
// XXX
if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
- && curbuf->b_ind_java == 0)
- || (tryposBrace = find_start_brace()) != NULL
- || trypos != NULL) {
+ && curbuf->b_ind_java == 0)
+ || (tryposBrace = find_start_brace()) != NULL
+ || trypos != NULL) {
if (trypos != NULL && tryposBrace != NULL) {
/* Both an unmatched '(' and '{' is found. Use the one which is
* closer to the current cursor position, set the other to NULL. */
if (trypos->lnum != tryposBrace->lnum
? trypos->lnum < tryposBrace->lnum
- : trypos->col < tryposBrace->col)
+ : trypos->col < tryposBrace->col) {
trypos = NULL;
- else
+ } else {
tryposBrace = NULL;
+ }
}
if (trypos != NULL) {
@@ -2083,8 +2217,8 @@ int get_c_indent(void)
amount = get_indent_lnum(curwin->w_cursor.lnum - 1); // XXX
} else {
amount = -1;
- for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum) {
- l = skipwhite(ml_get(lnum));
+ for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; lnum--) {
+ l = (char_u *)skipwhite((char *)ml_get(lnum));
if (cin_nocode(l)) { // skip comment lines
continue;
}
@@ -2100,16 +2234,16 @@ int get_c_indent(void)
}
// XXX
- if ((trypos = find_match_paren(
- corr_ind_maxparen(&cur_curpos))) != NULL
+ if ((trypos = find_match_paren(corr_ind_maxparen(&cur_curpos))) != NULL
&& trypos->lnum == our_paren_pos.lnum
&& trypos->col == our_paren_pos.col) {
amount = get_indent_lnum(lnum); // XXX
if (theline[0] == ')') {
if (our_paren_pos.lnum != lnum
- && cur_amount > amount)
+ && cur_amount > amount) {
cur_amount = amount;
+ }
amount = -1;
}
break;
@@ -2132,7 +2266,7 @@ int get_c_indent(void)
pos_T cursor_save = curwin->w_cursor;
pos_T outermost;
- char_u *line;
+ char_u *line;
trypos = &our_paren_pos;
do {
@@ -2152,10 +2286,10 @@ int get_c_indent(void)
}
amount = skip_label(our_paren_pos.lnum, &look);
- look = skipwhite(look);
+ look = (char_u *)skipwhite((char *)look);
if (*look == '(') {
linenr_T save_lnum = curwin->w_cursor.lnum;
- char_u *line;
+ char_u *line;
int look_col;
/* Ignore a '(' in front of the line that has a match before
@@ -2165,11 +2299,12 @@ int get_c_indent(void)
look_col = (int)(look - line);
curwin->w_cursor.col = look_col + 1;
if ((trypos = findmatchlimit(NULL, ')', 0,
- curbuf->b_ind_maxparen))
+ curbuf->b_ind_maxparen))
!= NULL
&& trypos->lnum == our_paren_pos.lnum
- && trypos->col < our_paren_pos.col)
+ && trypos->col < our_paren_pos.col) {
ignore_paren_col = trypos->col + 1;
+ }
curwin->w_cursor.lnum = save_lnum;
look = ml_get(our_paren_pos.lnum) + look_col;
@@ -2198,24 +2333,28 @@ int get_c_indent(void)
for (col = 0; col < our_paren_pos.col; ++col) {
switch (l[col]) {
case '(':
- case '{': ++n;
+ case '{':
+ n++;
break;
case ')':
- case '}': if (n > 1)
- --n;
+ case '}':
+ if (n > 1) {
+ n--;
+ }
break;
}
}
our_paren_pos.col = 0;
amount += n * curbuf->b_ind_unclosed_wrapped;
- } else if (curbuf->b_ind_unclosed_whiteok)
+ } else if (curbuf->b_ind_unclosed_whiteok) {
our_paren_pos.col++;
- else {
+ } else {
col = our_paren_pos.col + 1;
- while (ascii_iswhite(l[col]))
+ while (ascii_iswhite(l[col])) {
col++;
+ }
if (l[col] != NUL) { // In case of trailing space
our_paren_pos.col = col;
} else {
@@ -2230,8 +2369,9 @@ int get_c_indent(void)
*/
if (our_paren_pos.col > 0) {
getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
- if (cur_amount > (int)col)
+ if (cur_amount > (int)col) {
cur_amount = col;
+ }
}
}
@@ -2240,8 +2380,9 @@ int get_c_indent(void)
} else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
|| (!curbuf->b_ind_unclosed_noignore
&& *look == '(' && ignore_paren_col == 0)) {
- if (cur_amount != MAXCOL)
+ if (cur_amount != MAXCOL) {
amount = cur_amount;
+ }
} else {
/* Add b_ind_unclosed2 for each '(' before our matching one,
* but ignore (void) before the line (ignore_paren_col). */
@@ -2249,10 +2390,12 @@ int get_c_indent(void)
while ((int)our_paren_pos.col > ignore_paren_col) {
--our_paren_pos.col;
switch (*ml_get_pos(&our_paren_pos)) {
- case '(': amount += curbuf->b_ind_unclosed2;
+ case '(':
+ amount += curbuf->b_ind_unclosed2;
col = our_paren_pos.col;
break;
- case ')': amount -= curbuf->b_ind_unclosed2;
+ case ')':
+ amount -= curbuf->b_ind_unclosed2;
col = MAXCOL;
break;
}
@@ -2260,9 +2403,9 @@ int get_c_indent(void)
/* Use b_ind_unclosed once, when the first '(' is not inside
* braces */
- if (col == MAXCOL)
+ if (col == MAXCOL) {
amount += curbuf->b_ind_unclosed;
- else {
+ } else {
curwin->w_cursor.lnum = our_paren_pos.lnum;
curwin->w_cursor.col = col;
if (find_match_paren_after_brace(curbuf->b_ind_maxparen)) {
@@ -2279,12 +2422,13 @@ int get_c_indent(void)
* For a line starting with ')' use the minimum of the two
* positions, to avoid giving it more indent than the previous
* lines:
- * func_long_name( if (x
- * arg && yy
- * ) ^ not here ) ^ not here
+ * func_long_name( if (x
+ * arg && yy
+ * ) ^ not here ) ^ not here
*/
- if (cur_amount < amount)
+ if (cur_amount < amount) {
amount = cur_amount;
+ }
}
}
@@ -2309,14 +2453,15 @@ int get_c_indent(void)
* otherwise, check out the indentation of the line as
* a whole and then add the "imaginary indent" to that.
*/
- look = skipwhite(start);
+ look = (char_u *)skipwhite((char *)start);
if (*look == '{') {
getvcol(curwin, trypos, &col, NULL, NULL);
amount = col;
- if (*start == '{')
+ if (*start == '{') {
start_brace = BRACE_IN_COL0;
- else
+ } else {
start_brace = BRACE_AT_START;
+ }
} else {
// That opening brace might have been on a continuation
// line. If so, find the start of the line.
@@ -2331,11 +2476,11 @@ int get_c_indent(void)
}
// It could have been something like
- // case 1: if (asdf &&
- // ldfd) {
- // }
+ // case 1: if (asdf &&
+ // ldfd) {
+ // }
if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
- && cin_iscase(skipwhite(get_cursor_line_ptr()), false)) {
+ && cin_iscase((char_u *)skipwhite((char *)get_cursor_line_ptr()), false)) {
amount = get_indent();
} else if (curbuf->b_ind_js) {
amount = get_indent_lnum(lnum);
@@ -2402,7 +2547,7 @@ int get_c_indent(void)
if (start_brace == BRACE_AT_END) { // '{' is at end of line
amount += curbuf->b_ind_open_imag;
- l = skipwhite(get_cursor_line_ptr());
+ l = (char_u *)skipwhite((char *)get_cursor_line_ptr());
if (cin_is_cpp_namespace(l)) {
amount += curbuf->b_ind_cpp_namespace;
} else if (cin_is_cpp_extern_c(l)) {
@@ -2411,8 +2556,9 @@ int get_c_indent(void)
} else {
// Compensate for adding b_ind_open_extra later.
amount -= curbuf->b_ind_open_extra;
- if (amount < 0)
+ if (amount < 0) {
amount = 0;
+ }
}
}
@@ -2444,7 +2590,7 @@ int get_c_indent(void)
// the usual amount relative to the conditional
// that opens the block.
curwin->w_cursor = cur_curpos;
- for (;; ) {
+ for (;;) {
curwin->w_cursor.lnum--;
curwin->w_cursor.col = 0;
@@ -2468,10 +2614,11 @@ int get_c_indent(void)
/* nothing found (abuse curbuf->b_ind_maxparen as
* limit) assume terminated line (i.e. a variable
* initialization) */
- if (cont_amount > 0)
+ if (cont_amount > 0) {
amount = cont_amount;
- else if (!curbuf->b_ind_js)
+ } else if (!curbuf->b_ind_js) {
amount += ind_continuation;
+ }
break;
}
@@ -2495,8 +2642,9 @@ int get_c_indent(void)
continue;
}
- if (cin_nocode(l))
+ if (cin_nocode(l)) {
continue;
+ }
terminated = cin_isterminated(l, FALSE, TRUE);
@@ -2514,14 +2662,16 @@ int get_c_indent(void)
* declaration is split over multiple lines:
* cin_isfuncdecl returns FALSE then.
*/
- if (terminated == ',')
+ if (terminated == ',') {
break;
+ }
/* if it is an enum declaration or an assignment,
* we are done.
*/
- if (terminated != ';' && cin_isinit())
+ if (terminated != ';' && cin_isinit()) {
break;
+ }
// nothing useful found
if (terminated == 0 || terminated == '{') {
@@ -2535,12 +2685,13 @@ int get_c_indent(void)
// will take us back to the start of the line.
// XXX
trypos = NULL;
- if (find_last_paren(l, '(', ')'))
- trypos = find_match_paren(
- curbuf->b_ind_maxparen);
+ if (find_last_paren(l, '(', ')')) {
+ trypos = find_match_paren(curbuf->b_ind_maxparen);
+ }
- if (trypos == NULL && find_last_paren(l, '{', '}'))
+ if (trypos == NULL && find_last_paren(l, '{', '}')) {
trypos = find_start_brace();
+ }
if (trypos != NULL) {
curwin->w_cursor.lnum = trypos->lnum + 1;
@@ -2554,15 +2705,17 @@ int get_c_indent(void)
* int a,
* b;
*/
- if (cont_amount > 0)
+ if (cont_amount > 0) {
amount = cont_amount;
- else
+ } else {
amount += ind_continuation;
+ }
} else if (lookfor == LOOKFOR_UNTERM) {
- if (cont_amount > 0)
+ if (cont_amount > 0) {
amount = cont_amount;
- else
+ } else {
amount += ind_continuation;
+ }
} else {
if (lookfor != LOOKFOR_TERM
&& lookfor != LOOKFOR_CPP_BASECLASS
@@ -2579,13 +2732,15 @@ int get_c_indent(void)
* Looking for C++ namespace, need to look further
* back.
*/
- if (curwin->w_cursor.lnum == ourscope)
+ if (curwin->w_cursor.lnum == ourscope) {
continue;
+ }
if (curwin->w_cursor.lnum == 0
|| curwin->w_cursor.lnum
- < ourscope - FIND_NAMESPACE_LIM)
+ < ourscope - FIND_NAMESPACE_LIM) {
break;
+ }
l = get_cursor_line_ptr();
@@ -2613,8 +2768,9 @@ int get_c_indent(void)
break;
}
- if (cin_nocode(l))
+ if (cin_nocode(l)) {
continue;
+ }
}
}
break;
@@ -2639,25 +2795,25 @@ int get_c_indent(void)
if (iscase || cin_isscopedecl(l)) {
/* we are only looking for cpp base class
* declaration/initialization any longer */
- if (lookfor == LOOKFOR_CPP_BASECLASS)
+ if (lookfor == LOOKFOR_CPP_BASECLASS) {
break;
+ }
/* When looking for a "do" we are not interested in
* labels. */
- if (whilelevel > 0)
+ if (whilelevel > 0) {
continue;
+ }
- /*
- * case xx:
- * c = 99 + <- this indent plus continuation
- **-> here;
- */
- if (lookfor == LOOKFOR_UNTERM
- || lookfor == LOOKFOR_ENUM_OR_INIT) {
- if (cont_amount > 0)
+ // case xx:
+ // c = 99 + <- this indent plus continuation
+ // -> here;
+ if (lookfor == LOOKFOR_UNTERM || lookfor == LOOKFOR_ENUM_OR_INIT) {
+ if (cont_amount > 0) {
amount = cont_amount;
- else
+ } else {
amount += ind_continuation;
+ }
break;
}
@@ -2680,40 +2836,39 @@ int get_c_indent(void)
n = get_indent_nolabel(curwin->w_cursor.lnum); // XXX
- /*
- * case xx: if (cond) <- line up with this if
- * y = y + 1;
- * -> s = 99;
- *
- * case xx:
- * if (cond) <- line up with this line
- * y = y + 1;
- * -> s = 99;
- */
+ // case xx: if (cond) <- line up with this if
+ // y = y + 1;
+ // -> s = 99;
+ //
+ // case xx:
+ // if (cond) <- line up with this line
+ // y = y + 1;
+ // -> s = 99;
if (lookfor == LOOKFOR_TERM) {
- if (n)
+ if (n) {
amount = n;
+ }
- if (!lookfor_break)
+ if (!lookfor_break) {
break;
+ }
}
- /*
- * case xx: x = x + 1; <- line up with this x
- * -> y = y + 1;
- *
- * case xx: if (cond) <- line up with this if
- * -> y = y + 1;
- */
+ // case xx: x = x + 1; <- line up with this x
+ // -> y = y + 1;
+ //
+ // case xx: if (cond) <- line up with this if
+ // -> y = y + 1;
if (n) {
amount = n;
l = after_label(get_cursor_line_ptr());
if (l != NULL && cin_is_cinword(l)) {
- if (theline[0] == '{')
+ if (theline[0] == '{') {
amount += curbuf->b_ind_open_extra;
- else
+ } else {
amount += curbuf->b_ind_level
+ curbuf->b_ind_no_brace;
+ }
}
break;
}
@@ -2722,8 +2877,8 @@ int get_c_indent(void)
* Try to get the indent of a statement before the switch
* label. If nothing is found, line up relative to the
* switch label.
- * break; <- may line up with this line
- * case xx:
+ * break; <- may line up with this line
+ * case xx:
* -> y = 1;
*/
scope_amount = get_indent() + (iscase // XXX
@@ -2752,8 +2907,9 @@ int get_c_indent(void)
*/
if (!curbuf->b_ind_js && cin_islabel()) {
l = after_label(get_cursor_line_ptr());
- if (l == NULL || cin_nocode(l))
+ if (l == NULL || cin_nocode(l)) {
continue;
+ }
}
/*
@@ -2778,10 +2934,11 @@ int get_c_indent(void)
}
if (n) {
if (lookfor == LOOKFOR_UNTERM) {
- if (cont_amount > 0)
+ if (cont_amount > 0) {
amount = cont_amount;
- else
+ } else {
amount += ind_continuation;
+ }
} else if (theline[0] == '{') {
// Need to find start of the declaration.
lookfor = LOOKFOR_UNTERM;
@@ -2796,10 +2953,11 @@ int get_c_indent(void)
/* only look, whether there is a cpp base class
* declaration or initialization before the opening brace.
*/
- if (cin_isterminated(l, TRUE, FALSE))
+ if (cin_isterminated(l, true, false)) {
break;
- else
+ } else {
continue;
+ }
}
/*
@@ -2808,7 +2966,7 @@ int get_c_indent(void)
* there is another unterminated statement behind, eg:
* 123,
* sizeof
- * here
+ * here
* Otherwise check whether it is an enumeration or structure
* initialisation (not indented) or a variable declaration
* (indented).
@@ -2816,7 +2974,7 @@ int get_c_indent(void)
terminated = cin_isterminated(l, FALSE, TRUE);
if (js_cur_has_key) {
- js_cur_has_key = false; // only check the first line
+ js_cur_has_key = false; // only check the first line
if (curbuf->b_ind_js && terminated == ',') {
// For Javascript we might be inside an object:
// key: something, <- align with this
@@ -2855,13 +3013,13 @@ int get_c_indent(void)
if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
&& terminated == ',')) {
if (lookfor != LOOKFOR_ENUM_OR_INIT
- && (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '[')) {
+ && (*skipwhite((char *)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 we can get the right prevailing indent
- // if ( foo &&
- // bar )
+ // if ( foo &&
+ // bar )
// Position the cursor over the rightmost paren, so that
// matching it will take us back to the start of the line.
@@ -2877,15 +3035,16 @@ int get_c_indent(void)
// If we are looking for ',', we also look for matching
// braces.
if (trypos == NULL && terminated == ','
- && find_last_paren(l, '{', '}'))
+ && find_last_paren(l, '{', '}')) {
trypos = find_start_brace();
+ }
if (trypos != NULL) {
/*
* Check if we are on a case label now. This is
* handled above.
* case xx: if ( asdf &&
- * asdf)
+ * asdf)
*/
curwin->w_cursor = *trypos;
l = get_cursor_line_ptr();
@@ -2900,22 +3059,23 @@ int get_c_indent(void)
* Skip over continuation lines to find the one to get the
* indent from
* char *usethis = "bla\
- * bla",
+ * bla",
* here;
*/
if (terminated == ',') {
while (curwin->w_cursor.lnum > 1) {
l = ml_get(curwin->w_cursor.lnum - 1);
- if (*l == NUL || l[STRLEN(l) - 1] != '\\')
+ if (*l == NUL || l[STRLEN(l) - 1] != '\\') {
break;
- --curwin->w_cursor.lnum;
+ }
+ curwin->w_cursor.lnum--;
curwin->w_cursor.col = 0;
}
}
/*
* Get indent and pointer to text for current line,
- * ignoring any jump label. XXX
+ * ignoring any jump label. XXX
*/
if (curbuf->b_ind_js) {
cur_amount = get_indent();
@@ -2925,9 +3085,9 @@ int get_c_indent(void)
/*
* If this is just above the line we are indenting, and it
* starts with a '{', line it up with this line.
- * while (not)
- * -> {
- * }
+ * while (not)
+ * -> {
+ * }
*/
if (terminated != ',' && lookfor != LOOKFOR_TERM
&& theline[0] == '{') {
@@ -2936,11 +3096,12 @@ int get_c_indent(void)
* Only add b_ind_open_extra when the current line
* doesn't start with a '{', which must have a match
* in the same line (scope is the same). Probably:
- * { 1, 2 },
- * -> { 3, 4 }
+ * { 1, 2 },
+ * -> { 3, 4 }
*/
- if (*skipwhite(l) != '{')
+ if (*skipwhite((char *)l) != '{') {
amount += curbuf->b_ind_open_extra;
+ }
if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js) {
/* have to look back, whether it is a cpp base
@@ -2955,39 +3116,39 @@ int get_c_indent(void)
* Check if we are after an "if", "while", etc.
* Also allow " } else".
*/
- if (cin_is_cinword(l) || cin_iselse(skipwhite(l))) {
- /*
- * Found an unterminated line after an if (), line up
- * with the last one.
- * if (cond)
- * 100 +
- * -> here;
- */
+ if (cin_is_cinword(l) || cin_iselse((char_u *)skipwhite((char *)l))) {
+ // Found an unterminated line after an if (), line up
+ // with the last one.
+ // if (cond)
+ // 100 +
+ // -> here;
if (lookfor == LOOKFOR_UNTERM
|| lookfor == LOOKFOR_ENUM_OR_INIT) {
- if (cont_amount > 0)
+ if (cont_amount > 0) {
amount = cont_amount;
- else
+ } else {
amount += ind_continuation;
+ }
break;
}
/*
* If this is just above the line we are indenting, we
* are finished.
- * while (not)
- * -> here;
+ * while (not)
+ * -> here;
* Otherwise this indent can be used when the line
* before this is terminated.
- * yyy;
- * if (stat)
- * while (not)
- * xxx;
- * -> here;
+ * yyy;
+ * if (stat)
+ * while (not)
+ * xxx;
+ * -> here;
*/
amount = cur_amount;
- if (theline[0] == '{')
+ if (theline[0] == '{') {
amount += curbuf->b_ind_open_extra;
+ }
if (lookfor != LOOKFOR_TERM) {
amount += curbuf->b_ind_level
+ curbuf->b_ind_no_brace;
@@ -2998,14 +3159,15 @@ int get_c_indent(void)
* Special trick: when expecting the while () after a
* do, line up with the while()
* do
- * x = 1;
+ * x = 1;
* -> here
*/
- l = skipwhite(get_cursor_line_ptr());
+ l = (char_u *)skipwhite((char *)get_cursor_line_ptr());
if (cin_isdo(l)) {
- if (whilelevel == 0)
+ if (whilelevel == 0) {
break;
- --whilelevel;
+ }
+ whilelevel--;
}
/*
@@ -3018,14 +3180,16 @@ int get_c_indent(void)
/* If we're looking at "} else", let's make sure we
* find the opening brace of the enclosing scope,
* not the one from "if () {". */
- if (*l == '}')
+ if (*l == '}') {
curwin->w_cursor.col =
(colnr_T)(l - get_cursor_line_ptr()) + 1;
+ }
if ((trypos = find_start_brace()) == NULL
|| find_match(LOOKFOR_IF, trypos->lnum)
- == FAIL)
+ == FAIL) {
break;
+ }
}
}
/*
@@ -3039,8 +3203,8 @@ int get_c_indent(void)
* Found two unterminated lines on a row, line up with
* the last one.
* c = 99 +
- * 100 +
- * -> here;
+ * 100 +
+ * -> here;
*/
if (lookfor == LOOKFOR_UNTERM) {
// When line ends in a comma add extra indent
@@ -3057,8 +3221,9 @@ int get_c_indent(void)
* opening brace or we are looking just for
* enumerations/initializations. */
if (terminated == ',') {
- if (curbuf->b_ind_cpp_baseclass == 0)
+ if (curbuf->b_ind_cpp_baseclass == 0) {
break;
+ }
lookfor = LOOKFOR_CPP_BASECLASS;
continue;
@@ -3072,15 +3237,15 @@ int get_c_indent(void)
} else {
// Found first unterminated line on a row, may
// line up with this line, remember its indent
- // 100 + // NOLINT(whitespace/tab)
- // -> here; // NOLINT(whitespace/tab)
+ // 100 + // NOLINT(whitespace/tab)
+ // -> here; // NOLINT(whitespace/tab)
l = get_cursor_line_ptr();
amount = cur_amount;
n = (int)STRLEN(l);
if (terminated == ','
- && (*skipwhite(l) == ']'
- || (n >=2 && l[n - 2] == ']'))) {
+ && (*skipwhite((char *)l) == ']'
+ || (n >= 2 && l[n - 2] == ']'))) {
break;
}
@@ -3106,7 +3271,7 @@ int get_c_indent(void)
// 4 *
// 5,
// 6,
- if (cin_iscomment(skipwhite(l))) {
+ if (cin_iscomment((char_u *)skipwhite((char *)l))) {
break;
}
lookfor = LOOKFOR_COMMA;
@@ -3128,7 +3293,7 @@ int get_c_indent(void)
&& *l != NUL
&& l[STRLEN(l) - 1] == '\\') {
// XXX
- cont_amount = cin_get_equal_amount( curwin->w_cursor.lnum);
+ cont_amount = cin_get_equal_amount(curwin->w_cursor.lnum);
}
if (lookfor != LOOKFOR_TERM
&& lookfor != LOOKFOR_JS_KEY
@@ -3148,16 +3313,17 @@ int get_c_indent(void)
/*
* Found an unterminated line after a while ();, line up
* with the last one.
- * while (cond);
- * 100 + <- line up with this one
- * -> here;
+ * while (cond);
+ * 100 + <- line up with this one
+ * -> here;
*/
if (lookfor == LOOKFOR_UNTERM
|| lookfor == LOOKFOR_ENUM_OR_INIT) {
- if (cont_amount > 0)
+ if (cont_amount > 0) {
amount = cont_amount;
- else
+ } else {
amount += ind_continuation;
+ }
break;
}
@@ -3183,7 +3349,7 @@ int get_c_indent(void)
* may be lined up with the case label.
*/
if (lookfor == LOOKFOR_NOBREAK
- && cin_isbreak(skipwhite(get_cursor_line_ptr()))) {
+ && cin_isbreak((char_u *)skipwhite((char *)get_cursor_line_ptr()))) {
lookfor = LOOKFOR_ANY;
continue;
}
@@ -3203,35 +3369,37 @@ int get_c_indent(void)
/*
* Found a terminated line above an unterminated line. Add
* the amount for a continuation line.
- * x = 1;
- * y = foo +
- * -> here;
+ * x = 1;
+ * y = foo +
+ * -> here;
* or
- * int x = 1;
- * int foo,
- * -> here;
+ * int x = 1;
+ * int foo,
+ * -> here;
*/
if (lookfor == LOOKFOR_UNTERM
|| lookfor == LOOKFOR_ENUM_OR_INIT) {
- if (cont_amount > 0)
+ if (cont_amount > 0) {
amount = cont_amount;
- else
+ } else {
amount += ind_continuation;
+ }
break;
}
/*
* Found a terminated line above a terminated line or "if"
* etc. line. Use the amount of the line below us.
- * x = 1; x = 1;
- * if (asdf) y = 2;
- * while (asdf) ->here;
- * here;
+ * x = 1; x = 1;
+ * if (asdf) y = 2;
+ * while (asdf) ->here;
+ * here;
* ->foo;
*/
if (lookfor == LOOKFOR_TERM) {
- if (!lookfor_break && whilelevel == 0)
+ if (!lookfor_break && whilelevel == 0) {
break;
+ }
}
/*
* First line above the one we're indenting is terminated.
@@ -3244,20 +3412,17 @@ int get_c_indent(void)
* that matching it will take us back to the start of
* the line. Helps for:
* func(asdr,
- * asdfasdf);
+ * asdfasdf);
* here;
*/
term_again:
l = get_cursor_line_ptr();
if (find_last_paren(l, '(', ')')
- && (trypos = find_match_paren(
- curbuf->b_ind_maxparen)) != NULL) {
- /*
- * Check if we are on a case label now. This is
- * handled above.
- * case xx: if ( asdf &&
- * asdf)
- */
+ && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) {
+ // Check if we are on a case label now. This is
+ // handled above.
+ // case xx: if ( asdf &&
+ // asdf)
curwin->w_cursor = *trypos;
l = get_cursor_line_ptr();
if (cin_iscase(l, false) || cin_isscopedecl(l)) {
@@ -3270,10 +3435,10 @@ term_again:
/* When aligning with the case statement, don't align
* with a statement after it.
* case 1: { <-- don't use this { position
- * stat;
+ * stat;
* }
* case 2:
- * stat;
+ * stat;
* }
*/
iscase = curbuf->b_ind_keep_case_label && cin_iscase(l, false);
@@ -3284,19 +3449,21 @@ term_again:
*/
amount = skip_label(curwin->w_cursor.lnum, &l);
- if (theline[0] == '{')
+ if (theline[0] == '{') {
amount += curbuf->b_ind_open_extra;
+ }
// See remark above: "Only add b_ind_open_extra.."
- l = skipwhite(l);
- if (*l == '{')
+ l = (char_u *)skipwhite((char *)l);
+ if (*l == '{') {
amount -= curbuf->b_ind_open_extra;
+ }
lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
/*
* When a terminated line starts with "else" skip to
* the matching "if":
* else 3;
- * indent this;
+ * indent this;
* Need to use the scope of this "else". XXX
* If whilelevel != 0 continue looking for a "do {".
*/
@@ -3306,8 +3473,9 @@ term_again:
&& whilelevel == 0) {
if ((trypos = find_start_brace()) == NULL
|| find_match(LOOKFOR_IF, trypos->lnum)
- == FAIL)
+ == FAIL) {
break;
+ }
continue;
}
@@ -3322,9 +3490,10 @@ term_again:
// if not "else {" check for terminated again
// but skip block for "} else {"
l = cin_skipcomment(get_cursor_line_ptr());
- if (*l == '}' || !cin_iselse(l))
+ if (*l == '}' || !cin_iselse(l)) {
goto term_again;
- ++curwin->w_cursor.lnum;
+ }
+ curwin->w_cursor.lnum++;
curwin->w_cursor.col = 0;
}
}
@@ -3357,8 +3526,8 @@ term_again:
// of a function
if (theline[0] == '{') {
- amount = curbuf->b_ind_first_open;
- goto theend;
+ amount = curbuf->b_ind_first_open;
+ goto theend;
}
/*
* If the NEXT line is a function declaration, the current
@@ -3368,14 +3537,13 @@ term_again:
* contains { or }: "void f() {\n if (1)"
*/
if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
- && !cin_nocode(theline)
- && vim_strchr(theline, '{') == NULL
- && vim_strchr(theline, '}') == NULL
- && !cin_ends_in(theline, (char_u *)":", NULL)
- && !cin_ends_in(theline, (char_u *)",", NULL)
- && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
- cur_curpos.lnum + 1)
- && !cin_isterminated(theline, false, true)) {
+ && !cin_nocode(theline)
+ && vim_strchr((char *)theline, '{') == NULL
+ && vim_strchr((char *)theline, '}') == NULL
+ && !cin_ends_in(theline, (char_u *)":", NULL)
+ && !cin_ends_in(theline, (char_u *)",", NULL)
+ && cin_isfuncdecl(NULL, cur_curpos.lnum + 1, cur_curpos.lnum + 1)
+ && !cin_isterminated(theline, false, true)) {
amount = curbuf->b_ind_func_type;
goto theend;
}
@@ -3418,8 +3586,9 @@ term_again:
continue;
}
- if (cin_nocode(l))
+ if (cin_nocode(l)) {
continue;
+ }
/*
* If the previous line ends in ',', use one level of
@@ -3444,23 +3613,26 @@ term_again:
/* For a line ending in ',' that is a continuation line go
* back to the first line with a backslash:
* char *foo = "bla\
- * bla",
+ * bla",
* here;
*/
while (n == 0 && curwin->w_cursor.lnum > 1) {
l = ml_get(curwin->w_cursor.lnum - 1);
- if (*l == NUL || l[STRLEN(l) - 1] != '\\')
+ if (*l == NUL || l[STRLEN(l) - 1] != '\\') {
break;
- --curwin->w_cursor.lnum;
+ }
+ curwin->w_cursor.lnum--;
curwin->w_cursor.col = 0;
}
amount = get_indent(); // XXX
- if (amount == 0)
+ if (amount == 0) {
amount = cin_first_id_amount();
- if (amount == 0)
+ }
+ if (amount == 0) {
amount = ind_continuation;
+ }
break;
}
@@ -3477,17 +3649,18 @@ term_again:
* Finding the closing '}' of a previous function. Put
* current line at the left margin. For when 'cino' has "fs".
*/
- if (*skipwhite(l) == '}')
+ if (*skipwhite((char *)l) == '}') {
break;
+ }
- /* (matching {)
- * If the previous line ends on '};' (maybe followed by
- * comments) align at column 0. For example:
- * char *string_array[] = { "foo",
- * / * x * / "b};ar" }; / * foobar * /
- */
- if (cin_ends_in(l, (char_u *)"};", NULL))
+ // (matching {)
+ // If the previous line ends on '};' (maybe followed by
+ // comments) align at column 0. For example:
+ // char *string_array[] = { "foo",
+ // / * x * / "b};ar" }; / * foobar * /
+ if (cin_ends_in(l, (char_u *)"};", NULL)) {
break;
+ }
// If the previous line ends on '[' we are probably in an
// array constant:
@@ -3503,7 +3676,7 @@ term_again:
* line ending in '}', e.g. before an #endif. Don't increase
* indent then.
*/
- if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1)) {
+ if (*(look = (char_u *)skipwhite((char *)l)) == ';' && cin_nocode(look + 1)) {
pos_T curpos_save = curwin->w_cursor;
while (curwin->w_cursor.lnum > 1) {
@@ -3514,8 +3687,9 @@ term_again:
}
}
if (curwin->w_cursor.lnum > 0
- && cin_ends_in(look, (char_u *)"}", NULL))
+ && cin_ends_in(look, (char_u *)"}", NULL)) {
break;
+ }
curwin->w_cursor = curpos_save;
}
@@ -3540,8 +3714,9 @@ term_again:
if (cin_ends_in(l, (char_u *)";", NULL)) {
l = ml_get(curwin->w_cursor.lnum - 1);
if (cin_ends_in(l, (char_u *)",", NULL)
- || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
+ || (*l != NUL && l[STRLEN(l) - 1] == '\\')) {
break;
+ }
l = get_cursor_line_ptr();
}
@@ -3554,8 +3729,9 @@ term_again:
*/
(void)find_last_paren(l, '(', ')');
- if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
+ if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) {
curwin->w_cursor = *trypos;
+ }
amount = get_indent(); // XXX
break;
}
@@ -3565,26 +3741,27 @@ term_again:
amount += curbuf->b_ind_comment;
}
- /* add extra indent if the previous line ended in a backslash:
- * "asdfasdf\
- * here";
- * char *foo = "asdf\
- * here";
- */
+ // add extra indent if the previous line ended in a backslash:
+ // "asdfasdf{backslash}
+ // here";
+ // char *foo = "asdf{backslash}
+ // here";
if (cur_curpos.lnum > 1) {
l = ml_get(cur_curpos.lnum - 1);
if (*l != NUL && l[STRLEN(l) - 1] == '\\') {
cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
- if (cur_amount > 0)
+ if (cur_amount > 0) {
amount = cur_amount;
- else if (cur_amount == 0)
+ } else if (cur_amount == 0) {
amount += ind_continuation;
+ }
}
}
theend:
- if (amount < 0)
+ if (amount < 0) {
amount = 0;
+ }
laterend:
// put the cursor back where it belongs
@@ -3597,9 +3774,9 @@ laterend:
static int find_match(int lookfor, linenr_T ourscope)
{
- char_u *look;
- pos_T *theirscope;
- char_u *mightbeif;
+ const char_u *look;
+ pos_T *theirscope;
+ const char_u *mightbeif;
int elselevel;
int whilelevel;
@@ -3639,16 +3816,18 @@ static int find_match(int lookfor, linenr_T ourscope)
* back than the one enclosing the else, we're
* out of luck too.
*/
- if (theirscope->lnum < ourscope)
+ if (theirscope->lnum < ourscope) {
break;
+ }
/*
* and if they're enclosed in a *deeper* brace,
* then we can ignore it because it's in a
* different scope...
*/
- if (theirscope->lnum > ourscope)
+ if (theirscope->lnum > ourscope) {
continue;
+ }
/*
* if it was an "else" (that's not an "else if")
@@ -3658,8 +3837,9 @@ static int find_match(int lookfor, linenr_T ourscope)
look = cin_skipcomment(get_cursor_line_ptr());
if (cin_iselse(look)) {
mightbeif = cin_skipcomment(look + 4);
- if (!cin_isif(mightbeif))
- ++elselevel;
+ if (!cin_isif(mightbeif)) {
+ elselevel++; // NOLINT(readability/braces)
+ }
continue;
}
@@ -3680,8 +3860,9 @@ static int find_match(int lookfor, linenr_T ourscope)
* When looking for an "if" ignore "while"s that
* get in the way.
*/
- if (elselevel == 0 && lookfor == LOOKFOR_IF)
+ if (elselevel == 0 && lookfor == LOOKFOR_IF) {
whilelevel = 0;
+ }
}
// If it's a "do" decrement whilelevel
@@ -3706,8 +3887,9 @@ static int find_match(int lookfor, linenr_T ourscope)
*/
void do_c_expr_indent(void)
{
- if (*curbuf->b_p_inde != NUL)
+ if (*curbuf->b_p_inde != NUL) {
fixthisline(get_expr_indent);
- else
+ } else {
fixthisline(get_c_indent);
+ }
}