Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Reflow current selection, falling back to current paragraph if nothin…
Browse files Browse the repository at this point in the history
…g is selected
  • Loading branch information
Matt Swanson committed Mar 26, 2014
1 parent 8982c8b commit 9f71d13
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 70 deletions.
4 changes: 2 additions & 2 deletions README.md
@@ -1,6 +1,6 @@
# Autoflow package

Format the current paragraph to have lines no longer than 80 characters using
`cmd-alt-q`
Format the current selection to have lines no longer than 80 characters using
`cmd-alt-q`. If nothing is selected, the current paragraph will be reflowed.

This packages uses the config value of `editor.preferredLineLength` when set.
6 changes: 2 additions & 4 deletions keymaps/autoflow.cson
@@ -1,7 +1,5 @@
'.platform-darwin .editor':
'alt-cmd-q': 'autoflow:reflow-paragraph'
'alt-cmd-shift-q': 'autoflow:reflow-selection'
'alt-cmd-q': 'autoflow:reflow-selection'

'.platform-win32 .editor':
'alt-ctrl-q': 'autoflow:reflow-paragraph'
'alt-ctrl-shift-q': 'autoflow:reflow-selection'
'alt-ctrl-q': 'autoflow:reflow-selection'
13 changes: 6 additions & 7 deletions lib/autoflow.coffee
Expand Up @@ -2,17 +2,16 @@ module.exports =
activate: ->
atom.workspaceView.eachEditorView (editorView) =>
return unless editorView.attached and editorView.getPane()?
editorView.command 'autoflow:reflow-paragraph', =>
@reflowParagraph(editorView.editor)
editorView.command 'autoflow:reflow-selection', =>
@reflowSelection(editorView.editor)

reflowParagraph: (editor) ->
if range = editor.getCurrentParagraphBufferRange()
editor.getBuffer().change(range, @reflow(editor.getTextInRange(range), {wrapColumn: @getPreferredLineLength()}))

reflowSelection: (editor) ->
if range = editor.getSelectedBufferRange()
range = editor.getSelectedBufferRange()

if range.isEmpty()
range = editor.getCurrentParagraphBufferRange()

if range
editor.getBuffer().change(range, @reflow(editor.getTextInRange(range), {wrapColumn: @getPreferredLineLength()}))

reflow: (text, {wrapColumn}) ->
Expand Down
4 changes: 0 additions & 4 deletions menus/autoflow.cson
Expand Up @@ -2,10 +2,6 @@
{
'label': 'Edit'
'submenu': [
{
'label': 'Reflow Paragraph'
'command': 'autoflow:reflow-paragraph'
},
{
'label': 'Reflow Selection'
'command': 'autoflow:reflow-selection'
Expand Down
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -2,9 +2,8 @@
"name": "autoflow",
"version": "0.15.0",
"main": "./lib/autoflow",
"description": "Format the current paragraph to have lines no longer than 80 characters.\n\nThis packages uses the config value of `editor.preferredLineLength` when set.",
"description": "Format the current selection to have lines no longer than 80 characters.\n\nThis packages uses the config value of `editor.preferredLineLength` when set.",
"activationEvents": {
"autoflow:reflow-paragraph": ".editor",
"autoflow:reflow-selection": ".editor"
},
"repository": "https://github.com/atom/autoflow",
Expand Down
84 changes: 33 additions & 51 deletions spec/autoflow-spec.coffee
Expand Up @@ -3,7 +3,7 @@
describe "Autoflow package", ->
[autoflow, editor, editorView] = []

describe "autoflow:reflow-paragraph", ->
describe "autoflow:reflow-selection", ->
beforeEach ->
atom.workspaceView = new WorkspaceView
atom.workspaceView.openSync()
Expand All @@ -16,12 +16,39 @@ describe "Autoflow package", ->

activationPromise = atom.packages.activatePackage('autoflow')

editorView.trigger 'autoflow:reflow-paragraph' # Trigger the activation event
editorView.trigger 'autoflow:reflow-selection' # Trigger the activation event

waitsForPromise ->
activationPromise

it "rearranges line breaks in the current paragraph to ensure lines are shorter than config.editor.preferredLineLength", ->
it "rrearranges line breaks in the current selection to ensure lines are shorter than config.editor.preferredLineLength", ->
editor.setText """
This is the first paragraph and it is longer than the preferred line length so it should be reflowed.
This is a short paragraph.
Another long paragraph, it should also be reflowed with the use of this single command.
"""

editor.selectAll()
editorView.trigger 'autoflow:reflow-selection'

expect(editor.getText()).toBe """
This is the first paragraph
and it is longer than the
preferred line length so it
should be reflowed.
This is a short paragraph.
Another long paragraph, it
should also be reflowed with
the use of this single
command.
"""


it "reflows the current paragraph if nothing is selected", ->
editor.setText """
This is a preceding paragraph, which shouldn't be modified by a reflow of the following paragraph.
Expand All @@ -35,7 +62,7 @@ describe "Autoflow package", ->
"""

editor.setCursorBufferPosition([3, 5])
editorView.trigger 'autoflow:reflow-paragraph'
editorView.trigger 'autoflow:reflow-selection'

expect(editor.getText()).toBe """
This is a preceding paragraph, which shouldn't be modified by a reflow of the following paragraph.
Expand All @@ -54,60 +81,15 @@ describe "Autoflow package", ->
it "allows for single words that exceed the preferred wrap column length", ->
editor.setText("this-is-a-super-long-word-that-shouldn't-break-autoflow and these are some smaller words")

editor.setCursorBufferPosition([0, 4])
editorView.trigger 'autoflow:reflow-paragraph'
editor.selectAll()
editorView.trigger 'autoflow:reflow-selection'

expect(editor.getText()).toBe """
this-is-a-super-long-word-that-shouldn't-break-autoflow
and these are some smaller
words
"""

describe "autoflow:reflow-selection", ->
beforeEach ->
atom.workspaceView = new WorkspaceView
atom.workspaceView.openSync()
atom.workspaceView.attachToDom()

editorView = atom.workspaceView.getActiveView()
{editor} = editorView

atom.config.set('editor.preferredLineLength', 30)

activationPromise = atom.packages.activatePackage('autoflow')

editorView.trigger 'autoflow:reflow-selection' # Trigger the activation event

waitsForPromise ->
activationPromise

it "reflows the current selection", ->
editor.setText """
This is the first paragraph and it is longer than the preferred line length so it should be reflowed.
This is a short paragraph.
Another long paragraph, it should also be reflowed with the use of this single command.
"""

editor.selectAll()
editorView.trigger 'autoflow:reflow-selection'

expect(editor.getText()).toBe """
This is the first paragraph
and it is longer than the
preferred line length so it
should be reflowed.
This is a short paragraph.
Another long paragraph, it
should also be reflowed with
the use of this single
command.
"""


describe "reflowing text", ->
beforeEach ->
atom.workspaceView = new WorkspaceView
Expand Down

0 comments on commit 9f71d13

Please sign in to comment.