Vim: language independent fall back to syntax folding with foldmethod=expr












1















My goal is to extend the foldmethod=syntax with an additional custom defined rule of my own.



My approach in doing so is to use foldmethod=expr and define this rule in my foldexpr (e.g. add a fold for '///' comments). After that the resources I found usually fall back to something similar to indentation folding. So is there a good way to fall back to syntax folding after the custom rules, without reproducing the complete grammar for whatever language I am using?



My attempt so far is this, which is not a very satisfying approximation of syntax folding:



function! GetFold(lnum)
let this_line=getline(a:lnum)
let pprev_i=indent(a:lnum - 2)/&shiftwidth
let prev_i=indent(a:lnum - 1)/&shiftwidth
let this_i=indent(a:lnum)/&shiftwidth
" comments
if this_line =~? '^s*///.*'
return this_i + 1
endif
" like indent folding, but includes the closing bracket line to the fold
if empty(this_line)
if prev_i < pprev_i
return prev_i
endif
return -1
endif
if this_i < prev_i
return prev_i
endif
return this_i
endfunction









share|improve this question



























    1















    My goal is to extend the foldmethod=syntax with an additional custom defined rule of my own.



    My approach in doing so is to use foldmethod=expr and define this rule in my foldexpr (e.g. add a fold for '///' comments). After that the resources I found usually fall back to something similar to indentation folding. So is there a good way to fall back to syntax folding after the custom rules, without reproducing the complete grammar for whatever language I am using?



    My attempt so far is this, which is not a very satisfying approximation of syntax folding:



    function! GetFold(lnum)
    let this_line=getline(a:lnum)
    let pprev_i=indent(a:lnum - 2)/&shiftwidth
    let prev_i=indent(a:lnum - 1)/&shiftwidth
    let this_i=indent(a:lnum)/&shiftwidth
    " comments
    if this_line =~? '^s*///.*'
    return this_i + 1
    endif
    " like indent folding, but includes the closing bracket line to the fold
    if empty(this_line)
    if prev_i < pprev_i
    return prev_i
    endif
    return -1
    endif
    if this_i < prev_i
    return prev_i
    endif
    return this_i
    endfunction









    share|improve this question

























      1












      1








      1


      0






      My goal is to extend the foldmethod=syntax with an additional custom defined rule of my own.



      My approach in doing so is to use foldmethod=expr and define this rule in my foldexpr (e.g. add a fold for '///' comments). After that the resources I found usually fall back to something similar to indentation folding. So is there a good way to fall back to syntax folding after the custom rules, without reproducing the complete grammar for whatever language I am using?



      My attempt so far is this, which is not a very satisfying approximation of syntax folding:



      function! GetFold(lnum)
      let this_line=getline(a:lnum)
      let pprev_i=indent(a:lnum - 2)/&shiftwidth
      let prev_i=indent(a:lnum - 1)/&shiftwidth
      let this_i=indent(a:lnum)/&shiftwidth
      " comments
      if this_line =~? '^s*///.*'
      return this_i + 1
      endif
      " like indent folding, but includes the closing bracket line to the fold
      if empty(this_line)
      if prev_i < pprev_i
      return prev_i
      endif
      return -1
      endif
      if this_i < prev_i
      return prev_i
      endif
      return this_i
      endfunction









      share|improve this question














      My goal is to extend the foldmethod=syntax with an additional custom defined rule of my own.



      My approach in doing so is to use foldmethod=expr and define this rule in my foldexpr (e.g. add a fold for '///' comments). After that the resources I found usually fall back to something similar to indentation folding. So is there a good way to fall back to syntax folding after the custom rules, without reproducing the complete grammar for whatever language I am using?



      My attempt so far is this, which is not a very satisfying approximation of syntax folding:



      function! GetFold(lnum)
      let this_line=getline(a:lnum)
      let pprev_i=indent(a:lnum - 2)/&shiftwidth
      let prev_i=indent(a:lnum - 1)/&shiftwidth
      let this_i=indent(a:lnum)/&shiftwidth
      " comments
      if this_line =~? '^s*///.*'
      return this_i + 1
      endif
      " like indent folding, but includes the closing bracket line to the fold
      if empty(this_line)
      if prev_i < pprev_i
      return prev_i
      endif
      return -1
      endif
      if this_i < prev_i
      return prev_i
      endif
      return this_i
      endfunction






      vim syntax folding






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 11:20









      hakononakanihakononakani

      1559




      1559
























          2 Answers
          2






          active

          oldest

          votes


















          1














          The solution is to just use set fold=syntax and add a syntax region for the comments to your .vimrc. There you can use the fold keyword to mark the region as foldable (see :h syn-fold for more information):



           syn region myFold start="///" end="///" transparent fold  


          (Note, take also a look at :h syn-transparent it is quite useful here)






          share|improve this answer

































            0














            No, there isn't a way to make Vim "fall back"; only one 'foldmethod' can be active at a time, and there's no API to evaluate another fold method "as if".



            You could temporarily switch to syntax folding, store the generated folds, and then use that information in your fold information, extended with what your algorithm determines. (Or you could keep the same buffer in a window split, have syntax folding enabled there, and query it from the; this would save the complete re-creation of the folds, but you'd probably need to synchronize the cursor positions.)



            This is cumbersome and possibly slow; I wouldn't recommend it.






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021305%2fvim-language-independent-fall-back-to-syntax-folding-with-foldmethod-expr%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              The solution is to just use set fold=syntax and add a syntax region for the comments to your .vimrc. There you can use the fold keyword to mark the region as foldable (see :h syn-fold for more information):



               syn region myFold start="///" end="///" transparent fold  


              (Note, take also a look at :h syn-transparent it is quite useful here)






              share|improve this answer






























                1














                The solution is to just use set fold=syntax and add a syntax region for the comments to your .vimrc. There you can use the fold keyword to mark the region as foldable (see :h syn-fold for more information):



                 syn region myFold start="///" end="///" transparent fold  


                (Note, take also a look at :h syn-transparent it is quite useful here)






                share|improve this answer




























                  1












                  1








                  1







                  The solution is to just use set fold=syntax and add a syntax region for the comments to your .vimrc. There you can use the fold keyword to mark the region as foldable (see :h syn-fold for more information):



                   syn region myFold start="///" end="///" transparent fold  


                  (Note, take also a look at :h syn-transparent it is quite useful here)






                  share|improve this answer















                  The solution is to just use set fold=syntax and add a syntax region for the comments to your .vimrc. There you can use the fold keyword to mark the region as foldable (see :h syn-fold for more information):



                   syn region myFold start="///" end="///" transparent fold  


                  (Note, take also a look at :h syn-transparent it is quite useful here)







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 3 at 15:15

























                  answered Jan 3 at 11:37









                  Doktor OSwaldoDoktor OSwaldo

                  3,9381128




                  3,9381128

























                      0














                      No, there isn't a way to make Vim "fall back"; only one 'foldmethod' can be active at a time, and there's no API to evaluate another fold method "as if".



                      You could temporarily switch to syntax folding, store the generated folds, and then use that information in your fold information, extended with what your algorithm determines. (Or you could keep the same buffer in a window split, have syntax folding enabled there, and query it from the; this would save the complete re-creation of the folds, but you'd probably need to synchronize the cursor positions.)



                      This is cumbersome and possibly slow; I wouldn't recommend it.






                      share|improve this answer




























                        0














                        No, there isn't a way to make Vim "fall back"; only one 'foldmethod' can be active at a time, and there's no API to evaluate another fold method "as if".



                        You could temporarily switch to syntax folding, store the generated folds, and then use that information in your fold information, extended with what your algorithm determines. (Or you could keep the same buffer in a window split, have syntax folding enabled there, and query it from the; this would save the complete re-creation of the folds, but you'd probably need to synchronize the cursor positions.)



                        This is cumbersome and possibly slow; I wouldn't recommend it.






                        share|improve this answer


























                          0












                          0








                          0







                          No, there isn't a way to make Vim "fall back"; only one 'foldmethod' can be active at a time, and there's no API to evaluate another fold method "as if".



                          You could temporarily switch to syntax folding, store the generated folds, and then use that information in your fold information, extended with what your algorithm determines. (Or you could keep the same buffer in a window split, have syntax folding enabled there, and query it from the; this would save the complete re-creation of the folds, but you'd probably need to synchronize the cursor positions.)



                          This is cumbersome and possibly slow; I wouldn't recommend it.






                          share|improve this answer













                          No, there isn't a way to make Vim "fall back"; only one 'foldmethod' can be active at a time, and there's no API to evaluate another fold method "as if".



                          You could temporarily switch to syntax folding, store the generated folds, and then use that information in your fold information, extended with what your algorithm determines. (Or you could keep the same buffer in a window split, have syntax folding enabled there, and query it from the; this would save the complete re-creation of the folds, but you'd probably need to synchronize the cursor positions.)



                          This is cumbersome and possibly slow; I wouldn't recommend it.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 9 at 17:03









                          Ingo KarkatIngo Karkat

                          134k14152202




                          134k14152202






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021305%2fvim-language-independent-fall-back-to-syntax-folding-with-foldmethod-expr%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Monofisismo

                              Angular Downloading a file using contenturl with Basic Authentication

                              Olmecas