From 43127775084196b1bdbd3038e272a4abaffeefab Mon Sep 17 00:00:00 2001 From: Robby Date: Sun, 6 Aug 2017 23:22:13 -0500 Subject: [PATCH] Modifies sort function and adds a unit test --- lib/ex.coffee | 17 +++++++++++++---- spec/ex-commands-spec.coffee | 10 ++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/ex.coffee b/lib/ex.coffee index 16f74f9..46ab42b 100644 --- a/lib/ex.coffee +++ b/lib/ex.coffee @@ -438,10 +438,19 @@ class Ex optionProcessor() sort: ({ range }) => - range = [[range[0], 0], [range[1] + 1, 0]] editor = atom.workspace.getActiveTextEditor() - text = editor.getTextInBufferRange(range) - sortedText = _.sortBy(text.split(/\n/)).join('\n') - editor.buffer.setTextInRange(range, sortedText) + sortingRange = [[]] + isMultiLine = range[1] - range[0] > 1 + 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 diff --git a/spec/ex-commands-spec.coffee b/spec/ex-commands-spec.coffee index 4e3d839..2d9facc 100644 --- a/spec/ex-commands-spec.coffee +++ b/spec/ex-commands-spec.coffee @@ -983,3 +983,13 @@ describe "the commands", -> expect(calls.length).toEqual 2 expect(calls[0].args[0].range).toEqual [0, 2] 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')