mirror of
https://github.com/atom/atom.git
synced 2026-02-18 10:31:54 -05:00
Add autoflow package w/ autoflow:reflow-paragraph command
This commit is contained in:
1
src/packages/autoflow/index.coffee
Normal file
1
src/packages/autoflow/index.coffee
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require './lib/autoflow'
|
||||
31
src/packages/autoflow/lib/autoflow.coffee
Normal file
31
src/packages/autoflow/lib/autoflow.coffee
Normal 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
|
||||
41
src/packages/autoflow/spec/autoflow-spec.coffee
Normal file
41
src/packages/autoflow/spec/autoflow-spec.coffee
Normal 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.
|
||||
|
||||
"""
|
||||
Reference in New Issue
Block a user