Folding RSpec content not depth

2018-04-16 14:36:43

Trying to fold content so the 'method definition' / 'explanatory' lines are all showing regardless of depth, and the content between them is folded.

Folding examples on the wikis and in tutorials all seem to fold by nested depth.

Example code:

# RSpec code example

# '...' represents many lines of content

...

context 'depth 0' do

...

context 'depth 1' do

...

it 'depth 2' do

...

end

end

...

end

Folding with syntax matching, foldlevel=1 will give:

# RSpec code example

# '...' represents many lines

...

context 'depth 0' do

...

+-- 66 lines: context 'depth 1' do {--------------------------

...

end

Trying to fold away the content ..., how do I get:

# RSpec code example

# '...' represents many lines of content

+-- MANY lines: ... {--------------------------

context 'depth 0' do

+-- MANY lines: ... {--------------------------

context 'depth 1' do

+-- MANY lines: ... {--------------------------

it 'depth 2' do

+-- MANY lines: ... {---

  • To show the lines ending in do, and fold away everything else:

    :set foldexpr=getline(v:lnum)=~'do$'?'=':getline(v:lnum-1)=~'do$'?'a1':getline(v:lnum+1)=~'do$'?'s1':'='

    Breakdown

    # using '#' as comments

    # this is ran on each line to work out its indentation level

    :set foldexpr=

    getline(v:lnum)=~'do$' # current line regex match lines that end in 'do'

    ? # if the previous statement is true do this

    '=' # same indent level as previous line

    : # else

    getline(v:lnum-1)=~'do$' # examine previous line, same matcher

    ? # if previous

    'a1' # one indent level deeper than previous line

    : # else

    getline(v:lnum+1)=~'do$'# examine next line, same matcher

    ? # if previous

    's1' # one indent level up from previous line

    : # else

    '=' # same indent level as previous line

    # if you want to exclude e.g. lines that are 'before do',

    # then change "getline(v:lnum)=~'do$'"

    # to "getline(v:lnum)=~'do$'&&getline(v:lnum)!~'before\\sdo$'"

    # giving :set

    2018-04-16 16:44:11