Skip to content

Instantly share code, notes, and snippets.

@h-east
Last active August 29, 2015 14:18
Show Gist options
  • Save h-east/69f70b011c8a5b6d80a3 to your computer and use it in GitHub Desktop.
Save h-east/69f70b011c8a5b6d80a3 to your computer and use it in GitHub Desktop.
Patch for https://github.com/vim-jp/issues/issues/20 (Contain debug code)
diff -r 39e174e02dec src/misc1.c
--- a/src/misc1.c Thu Apr 09 22:08:22 2015 +0200
+++ b/src/misc1.c Sat Apr 11 08:26:08 2015 +0900
@@ -26,6 +26,11 @@
static garray_T ga_users;
#endif
+static FILE *sFp = NULL;
+
+#define VIM_DEBUG_LOG(fmt, ...) \
+ fprintf(sFp, "%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__)
+
/*
* Count the size (in window cells) of the indent in the current line.
*/
@@ -5409,7 +5414,7 @@
static int cin_is_if_for_while_before_offset __ARGS((char_u *line, int *poffset));
static int cin_iswhileofdo_end __ARGS((int terminated));
static int cin_isbreak __ARGS((char_u *));
-static int cin_is_cpp_baseclass __ARGS((colnr_T *col));
+static int cin_is_cpp_baseclass __ARGS((lfpos_T *pos));
static int get_baseclass_amount __ARGS((int col));
static int cin_ends_in __ARGS((char_u *, char_u *, char_u *));
static int cin_starts_with __ARGS((char_u *s, char *word));
@@ -6372,15 +6377,22 @@
* This is a lot of guessing. Watch out for "cond ? func() : foo".
*/
static int
-cin_is_cpp_baseclass(col)
- colnr_T *col; /* return: column to align with */
+cin_is_cpp_baseclass(pos)
+ lfpos_T *pos; /* return: position to align with */
{
char_u *s;
int class_or_struct, lookfor_ctor_init, cpp_base_class;
linenr_T lnum = curwin->w_cursor.lnum;
char_u *line = ml_get_curline();
- *col = 0;
+ if (pos->lnum <= lnum)
+ {
+ VIM_DEBUG_LOG("Use a cache. %d, %d, %d\n",
+ pos->lnum, pos->col, pos->found);
+ return pos->found; /* Use a cache */
+ }
+
+ pos->col = 0;
s = skipwhite(line);
if (*s == '#') /* skip #define FOO x ? (x) : x */
@@ -6424,7 +6436,9 @@
--lnum;
}
+ pos->lnum = lnum;
line = ml_get(lnum);
+ VIM_DEBUG_LOG("lnum:%d, s:%c. \"%s\"\n", pos->lnum, *s, line);
s = cin_skipcomment(line);
for (;;)
{
@@ -6439,6 +6453,7 @@
continue;
}
+ //VIM_DEBUG_LOG("loop s[0]:%c\n", s[0]);
if (s[0] == '"')
s = skip_string(s) + 1;
else if (s[0] == ':')
@@ -6452,11 +6467,12 @@
}
else if (lookfor_ctor_init || class_or_struct)
{
+ VIM_DEBUG_LOG("loop cpp_base_class is TRUE\n");
/* we have something found, that looks like the start of
* cpp-base-class-declaration or constructor-initialization */
cpp_base_class = TRUE;
lookfor_ctor_init = class_or_struct = FALSE;
- *col = 0;
+ pos->col = 0;
s = cin_skipcomment(s + 1);
}
else
@@ -6497,24 +6513,27 @@
class_or_struct = FALSE;
lookfor_ctor_init = FALSE;
}
- else if (*col == 0)
+ else if (pos->col == 0)
{
/* it can't be a constructor-initialization any more */
lookfor_ctor_init = FALSE;
/* the first statement starts here: lineup with this one... */
if (cpp_base_class)
- *col = (colnr_T)(s - line);
+ pos->col = (colnr_T)(s - line);
}
/* When the line ends in a comma don't align with it. */
if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
- *col = 0;
+ pos->col = 0;
s = cin_skipcomment(s + 1);
}
}
+ pos->found = cpp_base_class;
+ if (cpp_base_class)
+ pos->lnum = lnum;
return cpp_base_class;
}
@@ -7059,10 +7078,22 @@
int original_line_islabel;
int added_to_amount = 0;
int js_cur_has_key = 0;
+ /* find cache */
+ struct {
+ lfpos_T cpp_baseclass;
+ } fc = {
+ { MAXLNUM, 0, FALSE },
+ };
/* make a copy, value is changed below */
int ind_continuation = curbuf->b_ind_continuation;
+ if (sFp == NULL) {
+ sFp = fopen("./dbg_indent.txt", "w");
+ if (sFp == NULL)
+ exit(1);
+ }
+
/* remember where the cursor was when we started */
cur_curpos = curwin->w_cursor;
@@ -7733,11 +7764,15 @@
* that opens the block.
*/
curwin->w_cursor = cur_curpos;
+
+ VIM_DEBUG_LOG("w_cursor %d,%d\n", curwin->w_cursor.lnum,
+ curwin->w_cursor.col);
for (;;)
{
curwin->w_cursor.lnum--;
curwin->w_cursor.col = 0;
+ VIM_DEBUG_LOG("lnum:%d\n", curwin->w_cursor.lnum);
/*
* If we went all the way back to the start of our scope, line
* up with it.
@@ -8089,7 +8124,12 @@
n = FALSE;
if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
{
- n = cin_is_cpp_baseclass(&col);
+ n = cin_is_cpp_baseclass(&fc.cpp_baseclass);
+ VIM_DEBUG_LOG("is_cpp_baseclass:%s (%d, %d, %d)\n",
+ n ? "TRUE" : "FALSE",
+ fc.cpp_baseclass.lnum,
+ fc.cpp_baseclass.col,
+ fc.cpp_baseclass.found);
l = ml_get_curline();
}
if (n)
@@ -8110,7 +8150,7 @@
}
else
/* XXX */
- amount = get_baseclass_amount(col);
+ amount = get_baseclass_amount(fc.cpp_baseclass.col);
break;
}
else if (lookfor == LOOKFOR_CPP_BASECLASS)
@@ -8780,13 +8820,13 @@
n = FALSE;
if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
{
- n = cin_is_cpp_baseclass(&col);
+ n = cin_is_cpp_baseclass(&fc.cpp_baseclass);
l = ml_get_curline();
}
if (n)
{
/* XXX */
- amount = get_baseclass_amount(col);
+ amount = get_baseclass_amount(fc.cpp_baseclass.col);
break;
}
diff -r 39e174e02dec src/structs.h
--- a/src/structs.h Thu Apr 09 22:08:22 2015 +0200
+++ b/src/structs.h Sat Apr 11 08:26:08 2015 +0900
@@ -1982,6 +1982,16 @@
#define MAXPOSMATCH 8
/*
+ * Same as lpos_T, but with additional field found.
+ */
+typedef struct
+{
+ linenr_T lnum; /* line number */
+ colnr_T col; /* column number */
+ int found; /* find result: FALSE/TRUE */
+} lfpos_T;
+
+/*
* Same as lpos_T, but with additional field len.
*/
typedef struct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment