1

Usually I use incremental searching to find what I'm looking for. Also, in general I write very small methods that easily fit in one screen. So before now I have had little use for any sort of code-folding mechanism, which is good because of the general communal distaste for such things.

But one exception to this is with tests. Right now I use rspec and I'm finding it frustrating to navigate the tests. A test file I'm working on has about 500 lines so far and is structured like this:

Rspec.describe SomeClass do
  let(:some_common_variable) { create :the_thing }
  context 'with foos' do
    it 'does a thing' do
      # ... ~10 lines of arrange -> act -> assert stuff
    end
  end
  context 'with bars' do
    it 'does a thing differently' do
      # ... ~10 lines of arrange -> act -> assert stuff
    end
  end
end

So as you can see this makes some common-sense coding styles (like short methods) impossible. At the end of these tests I also have:

        end
      end
    end
  end
end

which makes it pretty tricky to figure out which "end" I want to insert a new test after -- especially because the matching do is not on the screen.

My question is: When I'm in the middle of some test, is there some way I can generate a breadcrumb that looks like this:

Someclass / with foos / it does a thing

Right now I'm using M-x occur with a regex I wrote that includes things like it, context, Rspec.describe, etc. to get an outline. Also, is there some code folding mode that makes it easy to quickly fold everything in an "it" block without folding everything else, but also have the ability to fold everything else with a different command.

Hut8
  • 658
  • 6
  • 11

2 Answers2

2

This solution doesn't address code folding, but does aim to address the issue of navigation and code context awareness.

imenu allows navigation of source files in all languages, including Ruby. But rspec doesn't support it out of the box. You can add support for imenu to rspec files with rspec mode (it's in melpa).

imenu by itself is OK, but I highly recommend accessing it via helm-imenu which is built into helm. That will give you not only an outline of all contexts/descriptions/specs, but provide incremental substring matching to quickly jump to any of them. Helm is available in melpa as well.

You'll no doubt continue using occur for more general cases, and I can again recommend using helm-occur, which provides the interactivity of helm when using occur to find matches.

R. P. Dillon
  • 415
  • 3
  • 11
1

Use bookmarks to identify and return to such positions.

If you use Bookmark+ then you can easily create and use autonamed or temporary (i.e., non-persistent) bookmarks, and these can be automatically highlighted in various ways (e.g., fringe marker or position or line), if you want.

Just hit C-x p RET to create an autonamed bookmark -- no need to provide a name. A bookmark moves automatically as you update text, and an autonamed bookmark is automatically renamed to reflect the current position when you jump to it.

Drew
  • 75,699
  • 9
  • 109
  • 225