aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Edmund Lazo <jan.lazo@mail.utoronto.ca>2020-11-02 01:37:05 -0500
committerGitHub <noreply@github.com>2020-11-02 01:37:05 -0500
commit6224ec3d4a672a9beb7522a34e61fa3b335bf070 (patch)
treeffa272383fa6ad9a6deb7dc3b76ec14103a50a40
parent90f3a8ba291fb0583ab6b0bd33129fdd45df1dab (diff)
parent4b65e4aeab16c1700a3c01a643e439a5000d932e (diff)
downloadrneovim-6224ec3d4a672a9beb7522a34e61fa3b335bf070.tar.gz
rneovim-6224ec3d4a672a9beb7522a34e61fa3b335bf070.tar.bz2
rneovim-6224ec3d4a672a9beb7522a34e61fa3b335bf070.zip
Merge pull request #13204 from janlazo/vim-8.2.1938
vim-patch:8.1.{2030,2063},8.2.{864,1086,1938}
-rw-r--r--runtime/doc/indent.txt8
-rw-r--r--src/nvim/buffer_defs.h1
-rw-r--r--src/nvim/indent.c17
-rw-r--r--src/nvim/indent_c.c16
-rw-r--r--src/nvim/testdir/test_cindent.vim36
5 files changed, 65 insertions, 13 deletions
diff --git a/runtime/doc/indent.txt b/runtime/doc/indent.txt
index 1df0331239..f2278f8453 100644
--- a/runtime/doc/indent.txt
+++ b/runtime/doc/indent.txt
@@ -566,9 +566,15 @@ The examples below assume a 'shiftwidth' of 4.
with "#" does not work.
+ PN When N is non-zero recognize C pragmas, and indent them like any
+ other code; does not concern other preprocessor directives.
+ When N is zero (default): don't recognize C pragmas, treating
+ them like every other preprocessor directive.
+
+
The defaults, spelled out in full, are:
cinoptions=>s,e0,n0,f0,{0,}0,^0,L-1,:s,=s,l0,b0,gs,hs,N0,E0,ps,ts,is,+s,
- c3,C0,/0,(2s,us,U0,w0,W0,k0,m0,j0,J0,)20,*70,#0
+ c3,C0,/0,(2s,us,U0,w0,W0,k0,m0,j0,J0,)20,*70,#0,P0
Vim puts a line in column 1 if:
- It starts with '#' (preprocessor directives), if 'cinkeys' contains '#0'.
diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h
index 9d4a6ecbea..4efc341875 100644
--- a/src/nvim/buffer_defs.h
+++ b/src/nvim/buffer_defs.h
@@ -795,6 +795,7 @@ struct file_buffer {
int b_ind_cpp_namespace;
int b_ind_if_for_while;
int b_ind_cpp_extern_c;
+ int b_ind_pragma;
linenr_T b_no_eol_lnum; /* non-zero lnum when last line of next binary
* write should not have an end-of-line */
diff --git a/src/nvim/indent.c b/src/nvim/indent.c
index bb0fdfec01..9e6693afdf 100644
--- a/src/nvim/indent.c
+++ b/src/nvim/indent.c
@@ -295,13 +295,17 @@ int set_indent(int size, int flags)
// Replace the line (unless undo fails).
if (!(flags & SIN_UNDO) || (u_savesub(curwin->w_cursor.lnum) == OK)) {
+ const colnr_T old_offset = (colnr_T)(p - oldline);
+ const colnr_T new_offset = (colnr_T)(s - newline);
+
+ // this may free "newline"
ml_replace(curwin->w_cursor.lnum, newline, false);
if (!(flags & SIN_NOMARK)) {
extmark_splice_cols(curbuf,
(int)curwin->w_cursor.lnum-1,
skipcols,
- (int)(p-oldline) - skipcols,
- (int)(s-newline) - skipcols,
+ old_offset - skipcols,
+ new_offset - skipcols,
kExtmarkUndo);
}
@@ -311,15 +315,14 @@ int set_indent(int size, int flags)
// Correct saved cursor position if it is in this line.
if (saved_cursor.lnum == curwin->w_cursor.lnum) {
- if (saved_cursor.col >= (colnr_T)(p - oldline)) {
+ if (saved_cursor.col >= old_offset) {
// Cursor was after the indent, adjust for the number of
// bytes added/removed.
- saved_cursor.col += ind_len - (colnr_T)(p - oldline);
-
- } else if (saved_cursor.col >= (colnr_T)(s - newline)) {
+ saved_cursor.col += ind_len - old_offset;
+ } else if (saved_cursor.col >= new_offset) {
// Cursor was in the indent, and is now after it, put it back
// at the start of the indent (replacing spaces with TAB).
- saved_cursor.col = (colnr_T)(s - newline);
+ saved_cursor.col = new_offset;
}
}
retval = true;
diff --git a/src/nvim/indent_c.c b/src/nvim/indent_c.c
index bb443161ef..9298e57411 100644
--- a/src/nvim/indent_c.c
+++ b/src/nvim/indent_c.c
@@ -1676,6 +1676,9 @@ void parse_cino(buf_T *buf)
// Handle C++ extern "C" or "C++"
buf->b_ind_cpp_extern_c = 0;
+ // Handle C #pragma directives
+ buf->b_ind_pragma = 0;
+
for (p = buf->b_p_cino; *p; ) {
l = p++;
if (*p == '-') {
@@ -1747,6 +1750,7 @@ void parse_cino(buf_T *buf)
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;
@@ -1858,12 +1862,14 @@ int get_c_indent(void)
goto laterend;
}
- /*
- * #defines and so on always go at the left when included in 'cinkeys'.
- */
+ // #defines and so on go at the left when included in 'cinkeys',
+ // exluding pragmas when customized in 'cinoptions'
if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', true))) {
- amount = curbuf->b_ind_hash_comment;
- goto theend;
+ const char_u *const directive = skipwhite(theline + 1);
+ if (curbuf->b_ind_pragma == 0 || STRNCMP(directive, "pragma", 6) != 0) {
+ amount = curbuf->b_ind_hash_comment;
+ goto theend;
+ }
}
/*
diff --git a/src/nvim/testdir/test_cindent.vim b/src/nvim/testdir/test_cindent.vim
index debc9da46d..b6c2d1467e 100644
--- a/src/nvim/testdir/test_cindent.vim
+++ b/src/nvim/testdir/test_cindent.vim
@@ -127,4 +127,40 @@ func Test_cindent_case()
bwipe!
endfunc
+func Test_cindent_pragma()
+ new
+ setl cindent ts=4 sw=4
+ setl cino=Ps
+
+ let code =<< trim [CODE]
+ {
+ #pragma omp parallel
+ {
+ #pragma omp task
+ foo();
+ # pragma omp taskwait
+ }
+ }
+ [CODE]
+
+ call append(0, code)
+ normal gg
+ normal =G
+
+ let expected =<< trim [CODE]
+ {
+ #pragma omp parallel
+ {
+ #pragma omp task
+ foo();
+ # pragma omp taskwait
+ }
+ }
+
+ [CODE]
+
+ call assert_equal(expected, getline(1, '$'))
+ enew! | close
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab