Modifies sort function and adds a unit test

This commit is contained in:
Robby 2017-08-06 23:22:13 -05:00
parent 15296ff369
commit 4312777508
2 changed files with 23 additions and 4 deletions

View file

@ -438,10 +438,19 @@ class Ex
optionProcessor() optionProcessor()
sort: ({ range }) => sort: ({ range }) =>
range = [[range[0], 0], [range[1] + 1, 0]]
editor = atom.workspace.getActiveTextEditor() editor = atom.workspace.getActiveTextEditor()
text = editor.getTextInBufferRange(range) sortingRange = [[]]
sortedText = _.sortBy(text.split(/\n/)).join('\n') isMultiLine = range[1] - range[0] > 1
editor.buffer.setTextInRange(range, sortedText) if isMultiLine
sortingRange = [[range[0], 0], [range[1] + 1, 0]]
else
sortingRange = [[0, 0], [editor.getLastBufferRow(), 0]]
textLines = []
for lineIndex in [sortingRange[0][0]..sortingRange[1][0] - 1]
textLines.push(editor.lineTextForBufferRow(lineIndex))
sortedText = _.sortBy(textLines).join('\n') + '\n'
editor.buffer.setTextInRange(sortingRange, sortedText)
module.exports = Ex module.exports = Ex

View file

@ -983,3 +983,13 @@ describe "the commands", ->
expect(calls.length).toEqual 2 expect(calls.length).toEqual 2
expect(calls[0].args[0].range).toEqual [0, 2] expect(calls[0].args[0].range).toEqual [0, 2]
expect(calls[1].args[0].range).toEqual [3, 3] expect(calls[1].args[0].range).toEqual [3, 3]
desctibe ':sort', ->
beforeEach ->
editor.setText('ghi\nabc\njkl\ndef\n')
editor.setCursorBufferPosition([0, 0])
it "sorts entire file if range is not multi-line", ->
openEx()
submitNormalModeInputText('sort')
expect(atom.getText()).toEqual('abc\ndef\nghi\njkl\n')