Add autoflow package w/ autoflow:reflow-paragraph command

This commit is contained in:
Nathan Sobo
2013-01-10 16:54:09 -07:00
parent 2086c2b353
commit b0fe034c9a
7 changed files with 125 additions and 0 deletions

View File

@@ -0,0 +1 @@
module.exports = require './lib/autoflow'

View File

@@ -0,0 +1,31 @@
module.exports =
activate: (rootView) ->
rootView.command 'autoflow:reflow-paragraph', '.editor', (e) =>
@reflowParagraph(e.currentTargetView())
reflowParagraph: (editor) ->
if range = editor.getCurrentParagraphBufferRange()
editor.getBuffer().change(range, @reflow(editor.getTextInRange(range)))
reflow: (text) ->
wrapColumn = config.get('editor.preferredLineLength') ? 80
lines = []
currentLine = []
currentLineLength = 0
for segment in @segmentText(text.replace(/\n/g, ' '))
if /\w/.test(segment) and currentLineLength + segment.length > wrapColumn
lines.push(currentLine.join(''))
currentLine = []
currentLineLength = 0
currentLine.push(segment)
currentLineLength += segment.length
lines.push(currentLine.join(''))
lines.join('\n').replace(/\s+\n/g, '\n')
segmentText: (text) ->
segments = []
re = /[\s]+|[^\s]+/g
segments.push(match[0]) while match = re.exec(text)
segments

View File

@@ -0,0 +1,41 @@
RootView = require 'root-view'
describe "Autoflow package", ->
editor = null
beforeEach ->
rootView = new RootView
atom.loadPackage 'autoflow'
editor = rootView.getActiveEditor()
describe "autoflow:reflow-paragraph", ->
it "rearranges line breaks in the current paragraph to ensure lines are shorter than config.editor.preferredLineLength", ->
config.set('editor.preferredLineLength', 30)
editor.setText """
This is a preceding paragraph, which shouldn't be modified by a reflow of the following paragraph.
The quick brown fox jumps over the lazy
dog. The preceding sentence contains every letter
in the entire English alphabet, which has absolutely no relevance
to this test.
This is a following paragraph, which shouldn't be modified by a reflow of the preciding paragraph.
"""
editor.setCursorBufferPosition([3, 5])
editor.trigger 'autoflow:reflow-paragraph'
expect(editor.getText()).toBe """
This is a preceding paragraph, which shouldn't be modified by a reflow of the following paragraph.
The quick brown fox jumps over
the lazy dog. The preceding
sentence contains every letter
in the entire English
alphabet, which has absolutely
no relevance to this test.
This is a following paragraph, which shouldn't be modified by a reflow of the preciding paragraph.
"""