comparison src/cindent.c @ 26819:0d798c7e1865 v8.2.3938

patch 8.2.3938: line comment start is also found in a string Commit: https://github.com/vim/vim/commit/ba26367fea3b63df49d274f3d5cca0af38402add Author: Bram Moolenaar <Bram@vim.org> Date: Wed Dec 29 18:09:13 2021 +0000 patch 8.2.3938: line comment start is also found in a string Problem: Line comment start is also found in a string. Solution: Skip line comments in a string.
author Bram Moolenaar <Bram@vim.org>
date Wed, 29 Dec 2021 19:15:03 +0100
parents fc859aea8cec
children fb4c30606b4a
comparison
equal deleted inserted replaced
26818:be6a271eee6c 26819:0d798c7e1865
64 } 64 }
65 return retval; 65 return retval;
66 } 66 }
67 #endif 67 #endif
68 68
69 #if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
70
71 /* 69 /*
72 * Skip to the end of a "string" and a 'c' character. 70 * Skip to the end of a "string" and a 'c' character.
73 * If there is no string or character, return argument unmodified. 71 * If there is no string or character, return argument unmodified.
74 */ 72 */
75 static char_u * 73 static char_u *
136 --p; // backup from NUL 134 --p; // backup from NUL
137 return p; 135 return p;
138 } 136 }
139 137
140 /* 138 /*
139 * Return TRUE if "line[col]" is inside a C string.
140 */
141 int
142 is_pos_in_string(char_u *line, colnr_T col)
143 {
144 char_u *p;
145
146 for (p = line; *p && (colnr_T)(p - line) < col; ++p)
147 p = skip_string(p);
148 return !((colnr_T)(p - line) <= col);
149 }
150
151 #if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
152
153 /*
141 * Find the start of a comment, not knowing if we are in a comment right now. 154 * Find the start of a comment, not knowing if we are in a comment right now.
142 * Search starts at w_cursor.lnum and goes backwards. 155 * Search starts at w_cursor.lnum and goes backwards.
143 * Return NULL when not inside a comment. 156 * Return NULL when not inside a comment.
144 */ 157 */
145 static pos_T * 158 static pos_T *
150 163
151 pos_T * 164 pos_T *
152 find_start_comment(int ind_maxcomment) // XXX 165 find_start_comment(int ind_maxcomment) // XXX
153 { 166 {
154 pos_T *pos; 167 pos_T *pos;
155 char_u *line;
156 char_u *p;
157 int cur_maxcomment = ind_maxcomment; 168 int cur_maxcomment = ind_maxcomment;
158 169
159 for (;;) 170 for (;;)
160 { 171 {
161 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment); 172 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
162 if (pos == NULL) 173 if (pos == NULL)
163 break; 174 break;
164 175
165 // Check if the comment start we found is inside a string. 176 // Check if the comment start we found is inside a string.
166 // If it is then restrict the search to below this line and try again. 177 // If it is then restrict the search to below this line and try again.
167 line = ml_get(pos->lnum); 178 if (!is_pos_in_string(ml_get(pos->lnum), pos->col))
168 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
169 p = skip_string(p);
170 if ((colnr_T)(p - line) <= pos->col)
171 break; 179 break;
172 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; 180 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
173 if (cur_maxcomment <= 0) 181 if (cur_maxcomment <= 0)
174 { 182 {
175 pos = NULL; 183 pos = NULL;
186 */ 194 */
187 static pos_T * 195 static pos_T *
188 find_start_rawstring(int ind_maxcomment) // XXX 196 find_start_rawstring(int ind_maxcomment) // XXX
189 { 197 {
190 pos_T *pos; 198 pos_T *pos;
191 char_u *line;
192 char_u *p;
193 int cur_maxcomment = ind_maxcomment; 199 int cur_maxcomment = ind_maxcomment;
194 200
195 for (;;) 201 for (;;)
196 { 202 {
197 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment); 203 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
198 if (pos == NULL) 204 if (pos == NULL)
199 break; 205 break;
200 206
201 // Check if the raw string start we found is inside a string. 207 // Check if the raw string start we found is inside a string.
202 // If it is then restrict the search to below this line and try again. 208 // If it is then restrict the search to below this line and try again.
203 line = ml_get(pos->lnum); 209 if (!is_pos_in_string(ml_get(pos->lnum), pos->col))
204 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
205 p = skip_string(p);
206 if ((colnr_T)(p - line) <= pos->col)
207 break; 210 break;
208 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; 211 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
209 if (cur_maxcomment <= 0) 212 if (cur_maxcomment <= 0)
210 { 213 {
211 pos = NULL; 214 pos = NULL;