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

@@ -307,6 +307,36 @@ describe "EditSession", ->
editSession.moveCursorToEndOfWord()
expect(editSession.getCursorBufferPosition()).toEqual endPosition
describe ".getCurrentParagraphBufferRange()", ->
it "returns the buffer range of the current paragraph, delimited by blank lines or the beginning / end of the file", ->
buffer.setText """
I am the first paragraph,
bordered by the beginning of
the file
#{' '}
I am the second paragraph
with blank lines above and below
me.
I am the last paragraph,
bordered by the end of the file.
"""
# in a paragraph
editSession.setCursorBufferPosition([1, 7])
expect(editSession.getCurrentParagraphBufferRange()).toEqual [[0, 0], [2, 8]]
editSession.setCursorBufferPosition([7, 1])
expect(editSession.getCurrentParagraphBufferRange()).toEqual [[5, 0], [7, 3]]
editSession.setCursorBufferPosition([9, 10])
expect(editSession.getCurrentParagraphBufferRange()).toEqual [[9, 0], [10, 32]]
# between paragraphs
editSession.setCursorBufferPosition([3, 1])
expect(editSession.getCurrentParagraphBufferRange()).toBeUndefined()
describe "selection", ->
selection = null

View File

@@ -175,6 +175,23 @@ class Cursor
getCurrentLineBufferRange: (options) ->
@editSession.bufferRangeForBufferRow(@getBufferRow(), options)
getCurrentParagraphBufferRange: ->
row = @getBufferRow()
return unless /\w/.test(@editSession.lineForBufferRow(row))
startRow = row
while startRow > 0
break unless /\w/.test(@editSession.lineForBufferRow(startRow - 1))
startRow--
endRow = row
lastRow = @editSession.getLastBufferRow()
while endRow < lastRow
break unless /\w/.test(@editSession.lineForBufferRow(endRow + 1))
endRow++
new Range([startRow, 0], [endRow, @editSession.lineLengthForBufferRow(endRow)])
getCurrentWordPrefix: ->
@editSession.getTextInBufferRange([@getBeginningOfCurrentWordBufferPosition(), @getBufferPosition()])

View File

@@ -139,6 +139,7 @@ class EditSession
getLastBufferRow: -> @buffer.getLastRow()
bufferRangeForBufferRow: (row, options) -> @buffer.rangeForRow(row, options)
lineForBufferRow: (row) -> @buffer.lineForRow(row)
lineLengthForBufferRow: (row) -> @buffer.lineLengthForRow(row)
scanInRange: (args...) -> @buffer.scanInRange(args...)
backwardsScanInRange: (args...) -> @buffer.backwardsScanInRange(args...)
@@ -489,6 +490,9 @@ class EditSession
getTextInBufferRange: (range) ->
@buffer.getTextInRange(range)
getCurrentParagraphBufferRange: ->
@getCursor().getCurrentParagraphBufferRange()
moveCursorUp: (lineCount) ->
@moveCursors (cursor) -> cursor.moveUp(lineCount)

View File

@@ -206,6 +206,7 @@ class Editor extends View
getCursorScreenRow: -> @activeEditSession.getCursorScreenRow()
setCursorBufferPosition: (position, options) -> @activeEditSession.setCursorBufferPosition(position, options)
getCursorBufferPosition: -> @activeEditSession.getCursorBufferPosition()
getCurrentParagraphBufferRange: -> @activeEditSession.getCurrentParagraphBufferRange()
getSelection: (index) -> @activeEditSession.getSelection(index)
getSelections: -> @activeEditSession.getSelections()

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.
"""