Last active
April 29, 2020 17:45
-
-
Save smashwilson/79b018bc7c9c1732a698b0d2a188986f to your computer and use it in GitHub Desktop.
Init script blurb to fold mocha, jasmine, or minitest tests down to the "it" blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
atom.commands.add('atom-text-editor', 'me:fold-to-it', event => { | |
const editor = event.target.closest('atom-text-editor').getModel() | |
const languageMode = editor.getBuffer().getLanguageMode() | |
if (!languageMode.tree) return | |
editor.unfoldAll() | |
const startsWith = (node, text) => { | |
const startPosition = new Point(node.startPosition.row, node.startPosition.column) | |
const endPosition = startPosition.traverse([0, text.length]) | |
if (node.endPosition.row === endPosition.row && node.endPosition.column < endPosition.column) { | |
return false | |
} | |
return editor.getTextInRange([startPosition, endPosition]) === text | |
} | |
const shouldFoldCall = node => { | |
if (node.type === 'call_expression') { | |
return ['it', 'beforeEach', 'afterEach'].some(text => startsWith(node, text)) | |
} else if (node.type === 'method_call') { | |
return ['test', 'fixtures', 'setup', 'teardown'].some(text => startsWith(node, text)) | |
} else { | |
return false | |
} | |
} | |
const frontier = [languageMode.tree.rootNode] | |
const itNodes = [] | |
while (frontier.length) { | |
const current = frontier.pop() | |
if (shouldFoldCall(current)) { | |
if (editor.isFoldableAtBufferRow(current.startPosition.row)) { | |
editor.foldBufferRow(current.startPosition.row) | |
} | |
} else { | |
frontier.push(...current.children) | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment