Fold "it" blocks within Mocha tests
Created by smashwilson
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.document) 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 => {
return ['it', 'beforeEach', 'afterEach'].some(text => startsWith(node, text))
}
const frontier = [languageMode.document.rootNode]
const itNodes = []
while (frontier.length) {
const current = frontier.pop()
if (current.type === 'call_expression' && shouldFoldCall(current)) {
if (editor.isFoldableAtBufferRow(current.startPosition.row)) {
editor.foldBufferRow(current.startPosition.row)
}
} else {
frontier.push(...current.children)
}
}
})
Fold a file containing Mocha tests at its “it”, “beforeEach”, and “afterEach” blocks. Requires Tree Sitter grammars to be enabled in your core settings.