mirror of
https://github.com/neovim/neovim.git
synced 2025-09-29 22:48:34 +00:00
vim-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.
ba26367fea
This commit is contained in:
@@ -42,8 +42,6 @@ static pos_T *ind_find_start_comment(void) // XXX
|
|||||||
pos_T *find_start_comment(int ind_maxcomment) // XXX
|
pos_T *find_start_comment(int ind_maxcomment) // XXX
|
||||||
{
|
{
|
||||||
pos_T *pos;
|
pos_T *pos;
|
||||||
char_u *line;
|
|
||||||
char_u *p;
|
|
||||||
int64_t cur_maxcomment = ind_maxcomment;
|
int64_t cur_maxcomment = ind_maxcomment;
|
||||||
|
|
||||||
for (;; ) {
|
for (;; ) {
|
||||||
@@ -55,11 +53,9 @@ pos_T *find_start_comment(int ind_maxcomment) // XXX
|
|||||||
* Check if the comment start we found is inside a string.
|
* 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.
|
* If it is then restrict the search to below this line and try again.
|
||||||
*/
|
*/
|
||||||
line = ml_get(pos->lnum);
|
if (!is_pos_in_string(ml_get(pos->lnum), pos->col)) {
|
||||||
for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
|
|
||||||
p = skip_string(p);
|
|
||||||
if ((colnr_T)(p - line) <= pos->col)
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
|
cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
|
||||||
if (cur_maxcomment <= 0) {
|
if (cur_maxcomment <= 0) {
|
||||||
pos = NULL;
|
pos = NULL;
|
||||||
@@ -110,8 +106,6 @@ static pos_T *ind_find_start_CORS(linenr_T *is_raw)
|
|||||||
static pos_T *find_start_rawstring(int ind_maxcomment) // XXX
|
static pos_T *find_start_rawstring(int ind_maxcomment) // XXX
|
||||||
{
|
{
|
||||||
pos_T *pos;
|
pos_T *pos;
|
||||||
char_u *line;
|
|
||||||
char_u *p;
|
|
||||||
long cur_maxcomment = ind_maxcomment;
|
long cur_maxcomment = ind_maxcomment;
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
@@ -124,11 +118,9 @@ static pos_T *find_start_rawstring(int ind_maxcomment) // XXX
|
|||||||
* Check if the raw string start we found is inside a string.
|
* 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 it is then restrict the search to below this line and try again.
|
||||||
*/
|
*/
|
||||||
line = ml_get(pos->lnum);
|
if (!is_pos_in_string(ml_get(pos->lnum), pos->col)) {
|
||||||
for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
|
|
||||||
p = skip_string(p);
|
|
||||||
if ((colnr_T)(p - line) <= pos->col)
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
|
cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
|
||||||
if (cur_maxcomment <= 0)
|
if (cur_maxcomment <= 0)
|
||||||
{
|
{
|
||||||
@@ -205,6 +197,16 @@ static char_u *skip_string(char_u *p)
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @returns true if "line[col]" is inside a C string.
|
||||||
|
int is_pos_in_string(char_u *line, colnr_T col)
|
||||||
|
{
|
||||||
|
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.
|
* Functions for C-indenting.
|
||||||
|
@@ -26,6 +26,7 @@
|
|||||||
#include "nvim/func_attr.h"
|
#include "nvim/func_attr.h"
|
||||||
#include "nvim/getchar.h"
|
#include "nvim/getchar.h"
|
||||||
#include "nvim/indent.h"
|
#include "nvim/indent.h"
|
||||||
|
#include "nvim/indent_c.h"
|
||||||
#include "nvim/main.h"
|
#include "nvim/main.h"
|
||||||
#include "nvim/mark.h"
|
#include "nvim/mark.h"
|
||||||
#include "nvim/mbyte.h"
|
#include "nvim/mbyte.h"
|
||||||
@@ -2313,11 +2314,8 @@ pos_T *findmatchlimit(oparg_T *oap, int initc, int flags, int64_t maxtravel)
|
|||||||
return (pos_T *)NULL; // never found it
|
return (pos_T *)NULL; // never found it
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/// Check if line[] contains a / / comment.
|
||||||
* Check if line[] contains a / / comment.
|
/// @returns MAXCOL if not, otherwise return the column.
|
||||||
* Return MAXCOL if not, otherwise return the column.
|
|
||||||
* TODO: skip strings.
|
|
||||||
*/
|
|
||||||
int check_linecomment(const char_u *line)
|
int check_linecomment(const char_u *line)
|
||||||
{
|
{
|
||||||
const char_u *p = line; // scan from start
|
const char_u *p = line; // scan from start
|
||||||
@@ -2338,7 +2336,8 @@ int check_linecomment(const char_u *line)
|
|||||||
in_str = true;
|
in_str = true;
|
||||||
}
|
}
|
||||||
} else if (!in_str && ((p - line) < 2
|
} else if (!in_str && ((p - line) < 2
|
||||||
|| (*(p - 1) != '\\' && *(p - 2) != '#'))) {
|
|| (*(p - 1) != '\\' && *(p - 2) != '#'))
|
||||||
|
&& !is_pos_in_string(line, (colnr_T)(p - line))) {
|
||||||
break; // found!
|
break; // found!
|
||||||
}
|
}
|
||||||
p++;
|
p++;
|
||||||
@@ -2348,9 +2347,11 @@ int check_linecomment(const char_u *line)
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
while ((p = vim_strchr(p, '/')) != NULL) {
|
while ((p = vim_strchr(p, '/')) != NULL) {
|
||||||
// accept a double /, unless it's preceded with * and followed by *,
|
// Accept a double /, unless it's preceded with * and followed by *,
|
||||||
// because * / / * is an end and start of a C comment
|
// because * / / * is an end and start of a C comment.
|
||||||
if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*')) {
|
// Only accept the position if it is not inside a string.
|
||||||
|
if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*')
|
||||||
|
&& !is_pos_in_string(line, (colnr_T)(p - line))) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
++p;
|
++p;
|
||||||
|
@@ -261,6 +261,21 @@ func Test_format_c_comment()
|
|||||||
END
|
END
|
||||||
call assert_equal(expected, getline(1, '$'))
|
call assert_equal(expected, getline(1, '$'))
|
||||||
|
|
||||||
|
" Using "o" does not repeat a comment in a string
|
||||||
|
%del
|
||||||
|
let text =<< trim END
|
||||||
|
nop;
|
||||||
|
val = " // This is not a comment";
|
||||||
|
END
|
||||||
|
call setline(1, text)
|
||||||
|
normal 2Gox
|
||||||
|
let expected =<< trim END
|
||||||
|
nop;
|
||||||
|
val = " // This is not a comment";
|
||||||
|
x
|
||||||
|
END
|
||||||
|
call assert_equal(expected, getline(1, '$'))
|
||||||
|
|
||||||
" Using CTRL-U after "o" fixes the indent
|
" Using CTRL-U after "o" fixes the indent
|
||||||
%del
|
%del
|
||||||
let text =<< trim END
|
let text =<< trim END
|
||||||
|
Reference in New Issue
Block a user