]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.0864: pragmas are indented all the way to the left v8.2.0864
authorBram Moolenaar <Bram@vim.org>
Sun, 31 May 2020 15:49:30 +0000 (17:49 +0200)
committerBram Moolenaar <Bram@vim.org>
Sun, 31 May 2020 15:49:30 +0000 (17:49 +0200)
Problem:    Pragmas are indented all the way to the left.
Solution:   Add an option to indent progmas like normal code. (Max Rumpf,
            closes #5468)

runtime/doc/indent.txt
src/cindent.c
src/structs.h
src/testdir/test_cindent.vim
src/version.c

index 02702fa5b6e9815a64c7304653c7afb9415d3d98..c4785c4b227204c2c5bdab98ccd4cdc6794e23ec 100644 (file)
@@ -570,9 +570,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'.
index 3dc7b1635485e1bfed1e78f15398be92390be4ea..aeecc4b95c7bda2f28da67c652aacd937aa498f5 100644 (file)
@@ -1845,6 +1845,9 @@ 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++;
@@ -1920,6 +1923,7 @@ 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;
@@ -2116,11 +2120,16 @@ 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;
+       char_u *directive = skipwhite(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:
index f67aba33ffe19daa3e3f95f6687ae3e13daf5347..9968e9205d35fde6e88679e3db325c8ab5dcceaf 100644 (file)
@@ -2803,6 +2803,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;
 #endif
 
     linenr_T   b_no_eol_lnum;  // non-zero lnum when last line of next binary
index 9258192897b729f2f758da73692e4a44ee846fc6..dc8c51dbfe44cf99c8f32ee66c0c0d38e7dc3f16 100644 (file)
@@ -5272,4 +5272,40 @@ func Test_cindent_change_multline()
   close!
 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
index e6caf8d96b6675849685c279f02478daf7702499..6f77139a346f2d0e0ab82038ec554ce9082ace6d 100644 (file)
@@ -746,6 +746,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    864,
 /**/
     863,
 /**/