11

I'd like to set two methods for folding

  1. :set foldmethod=indent and retain all its features
  2. hiding comments with

    :set foldmethod=marker

    :set foldmarker=/*,*/

I found out that this is not possible. Is it possible to achieve the desired folding and set this in .vimrc or to use some script or plugin for this?

HaskellElephant
  • 9,819
  • 4
  • 38
  • 67
xralf
  • 3,312
  • 45
  • 129
  • 200

3 Answers3

8

It's not possible to have different foldmethod types in same buffer. How is Vim to know that there are some comments at same indent level as other text that you want to treat as being of a different (higher numbered) level?

I'm sure you can achieve what you want by setting foldmethod to 'expr'. This is most flexible way of doing folds in Vim but can get complicated (and/or slow) depending on what you want. I think it would work fairly easily for your use case, though.

First, somewhere in your vimrc or vimscripts you need to make sure that foldexpr is getting defined for the filetype in question.

set foldexpr=MyFoldLevel(v:lnum)
set foldmethod=expr
" and for last code example
let b:previous_level = 0

and you then have to flesh out your foldexpr function so that it assigns levels in a way that results in behavior you want. Something like the code below might come close to working in cases where each comment line has prefix symbol (i.e., not in your case), but I expect it needs some tweaks. h: fold-expr would be a good place to look for help:

function! MyFoldLevel(linenum)
   " assign levels based on spaces indented and tabstop of 4
   let level = indent(a:linenum) / 4
   if getline(a:linenum) =~ [put line-based comment prefix pattern here]
       let level = 20
   endif
endfunction

Would need to be modified to assign higher level for lines between comment start and end markers the way you want:

function! MyFoldLevel(linenum)
   let linetext = getline(a:linenum)
   if linetext =~ [put line-based comment prefix pattern here]
       let level = 20
   elseif linetext =~ '^\s*/\*'
       let level = 20
   elseif linetext =~ '^\s*\*/'
       let level = 21
   else
       if b:previous_level == 20
           let level = 20
       else
           "assuming code is space-indented with tabstop of 4
           let level = indent(a:linenum) / 4
       endif
   endif

   let b:previous_level = level
   return level

endfunction

I don't expect the foldmethod functions I've written would work exactly as written. But they do point the way to something that would work.

Note that use of level of '20' for comments is just arbitrary level that allows them to be folded while all (presumably lower-leveled) indented code could be visible. '21' for last line of comment section is just to differentiate it from the previous lines of comments that have level of 20, to know that next line should be treated as regular line of code.

Also, key ops like 'zc' and 'zo' will not work quite right on comments when they're set to level much higher than surrounding code. Would want to use direct command like :set foldlevel=21 to show all comment lines.

Not pretty, and I expect it could be simplified a little, but something like this is what I think is required for what you want.

Actually, thinking through this a little more, I think you would want the first line of any comment blocks to be at same level as if it were a non-comment line, only subsequent comment lines in same block would need to be of higher level to have them "fold" into the starting comment line. In the code I gave, if it works or comes close to working at all, I think vim would fold all the comment lines behind the preceding non-comment line, which isn't what you want, but I unfortunately don't have more time to devote to this little puzzle. . . I've done this sort of custom-folding quite a few times and generally always have a little bit of trial and error in getting exactly what I want.

Herbert Sitz
  • 21,858
  • 9
  • 50
  • 54
  • Is it possible to map 'zo' command to 'zo' command pressed 21 times? I noticed that zo pressed more times does not have side effects. Does it slow down vim much? – xralf May 12 '11 at 18:28
  • 1
    @xralf, maybe `zO` would be helpful to you - it opens all folds under the cursor recursively (likewise, `zC` closes all folds). – mattb May 10 '21 at 09:30
2

I have the same requests as yours, here is my not perfect solution

my maker pair is #<=== and #===> (or #region and #endregion as in pycharm)

let b:inBlock=0
let b:lastLineNum=0
let b:lastLevel=0
let b:lastGoodLine=0
let b:lastGoodBlock=0
let b:startFoldingMark='^\s*.\?#<==*\|^\s*.\?#region'
let b:endFoldingMark='^\s*.\?#=*=>\|^\s*.\?#endregion'
function! MyFold(linenum)
    let linetext = getline(a:linenum)
    let level     = indent(a:linenum)   / &shiftwidth
    "the first line have 0 fold level
    if (a:linenum == 1)
        if linetext =~ b:startFoldingMark
            let b:inBlock = 1
            let b:lastLineNum=a:linenum
            let b:lastGoodLine=0
            let b:lastGoodBlock=0
            let b:lastLevel=level
            return level
        endif
        let b:inBlock=0
        let b:lastInBlock=0
        let b:lastLineNum=a:linenum
        let b:lastGoodLine=0
        let b:lastGoodBlock=b:inBlock
        let b:lastLevel=level + b:inBlock
        return level + b:inBlock
    endif

    " not calculate from the mid of text
    if ((b:lastLineNum+1) != a:linenum)
        let level     = indent(a:linenum)   / &shiftwidth
        let lastGoodNum = a:linenum-1
        while (lastGoodNum>1 && getline(lastGoodNum) =~? '\v^\s*$' )
            let lastGoodNum -= 1
        endwhile
        if  (foldlevel(lastGoodNum)==-1)
            let b:inBlock=b:lastGoodBlock
        else
            let lastlevel = indent(lastGoodNum)   / &shiftwidth
            let lastlinetext = getline(lastGoodNum)
            let lastlinelevel = foldlevel(lastGoodNum)
            if lastlinetext =~ b:startFoldingMark
                let b:inBlock = lastlinelevel - lastlevel + 1
            elseif lastlinetext =~ b:endFoldingMark
                let b:inBlock = lastlinelevel - lastlevel - 1
            else
                let b:inBlock = lastlinelevel - lastlevel
            endif
        endif
    endif

    "blank lines have undefined fold level
    if getline(a:linenum) =~? '\v^\s*$'
        let b:lastLineNum=a:linenum
        let b:lastLevel=-1
        return -1
    endif

    "if next line is a start of new marker block, inBlock ++
    if linetext =~ b:startFoldingMark
        let b:lastLineNum=a:linenum
        if (b:lastLevel != -1)
            let b:lastGoodLine=a:linenum
            let b:lastGoodBlock=b:inBlock
        endif
        let b:lastLevel=level + b:inBlock - 1
        return level + b:inBlock - 1
    "if next line is an end of new marker block, inBlock -
    elseif linetext =~ b:endFoldingMark
        let b:inBlock = b:inBlock - 1
        let b:lastLineNum=a:linenum
        let b:lastGoodLine=a:linenum
        let b:lastGoodBlock=b:inBlock
        let b:lastLevel=level + b:inBlock + 1
        return level + b:inBlock + 1
    endif

    let b:lastLineNum=a:linenum
    if (b:lastLevel != -1)
        let b:lastGoodLine=a:linenum
        let b:lastGoodBlock=b:inBlock
    endif
    let b:lastLevel=level + b:inBlock
    return level+b:inBlock
endfunction

now, i can keep all the features when using indent fold method, and i can fold each #<=, #=> marker block, also, the lines' indent folding relations are still kept in each block.

In this function, i avoid using "a1", "s1" and "=" level, which will cause iteration for this function and may be slow for large files. However, when you update lines, the calculation of fold level may be incorrect (because vim may not update all fold level from beginning, and thus have incorrect inBlock value)

you can use zx to update fold levels manually.

see more at https://github.com/Fmajor/configs

Major F
  • 21
  • 2
1

Syntax-based folding may be a better way to get what you want than the expr-based method I suggested in different answer to your question. Check :h fold-syn for more info. I think there may be some good solutions already out there for c-based folding. Don't know how good it is, but here is a c-syntax file with support for syntax-based folding: http://www.vim.org/scripts/script.php?script_id=234 and another: http://www.vim.org/scripts/script.php?script_id=925

The solutions above are entirely syntax-based, don't involve using indents to determine fold levels. But you could modify syntax-based folding to do the main folding by indented regions if you wanted. If you indent based on syntactical elements the result might be the same anyway.

Here's a tip that shows how to just fold c-style comments (not the actual code) http://vim.wikia.com/wiki/Fold_C-style_comments

Herbert Sitz
  • 21,858
  • 9
  • 50
  • 54