Fold Python Doc Strings


#1

Does anyone know if there is anything out there similar to Sublime’s “Fold Python Docstring”?
https://packagecontrol.io/packages/Fold%20Python%20Docstrings


#2

this https://atom.io/packages/custom-folds
could … almost answer your need
by setting “”" as custom marker BUT it only seems to work with comment markers…
could ask the author a setting to enable non comment markers
but could imply a syntax-link to markers … lots of improvement possible :sweat_smile:


#3

I need something similar…I like well documented code, but it gets in the way.

It looks like if I just added custom fold points, that would work:

By default, a comment starting with <editor-fold identifies the start of a foldable region and another comment starting with marks the end of that region.


#4

Put the below code into your init.js file, located at ~/.atom/init.js. Then if your cursor is within a docstring, you can pull up the command palette and run fold-docstring.

const { CompositeDisposable, Point, Range } = require('atom');
atom.commands.add('atom-text-editor', 'fold-docstring', () => {
  var editor = atom.workspace.getActiveTextEditor();
  var currentPosition = editor.getCursorBufferPosition();
  var backwardRange = [0, currentPosition];
  var docstring_regex = new RegExp(/("""|''')/g);
  var foundStart = null;
  editor.backwardsScanInBufferRange(docstring_regex, backwardRange, function(result) {
    foundStart = result.range;
    return result.stop();
  });

  // now look for the end
  var numberOfLines = editor.getLineCount();
  var forwardRange = [foundStart.end, new Point(numberOfLines + 1, 0)];
  var foundEnd = null;
  editor.scanInBufferRange(docstring_regex, forwardRange, function(result) {
    foundEnd = result.range;
    return result.stop();
  });

  editor.setSelectedBufferRange([foundStart.start, foundEnd.end]);
  var selection = editor.selections[0];
  selection.fold();
})