Ignore an empty last line when indenting/outdenting selected lines

This provides more intuitive behavior now that the cursor isn't visible when there's a selection.
This commit is contained in:
Nathan Sobo
2012-10-29 16:16:19 -06:00
parent db3d788664
commit aa20fbac6d
2 changed files with 24 additions and 4 deletions

View File

@@ -1413,6 +1413,15 @@ describe "EditSession", ->
expect(buffer.lineForRow(11)).toBe " return sort(Array.apply(this, arguments));"
expect(editSession.getSelectedBufferRange()).toEqual [[9, 1 + editSession.tabLength], [11, 15 + editSession.tabLength]]
it "does not indent the last row if the selection ends at column 0", ->
editSession.tabLength = 2
editSession.setSelectedBufferRange([[9,1], [11,0]])
editSession.indentSelectedRows()
expect(buffer.lineForRow(9)).toBe " };"
expect(buffer.lineForRow(10)).toBe ""
expect(buffer.lineForRow(11)).toBe " return sort(Array.apply(this, arguments));"
expect(editSession.getSelectedBufferRange()).toEqual [[9, 1 + editSession.tabLength], [11, 0]]
describe "when softTabs is disabled", ->
it "indents selected lines (that are not empty) and retains selection", ->
convertToHardTabs(buffer)
@@ -1470,8 +1479,19 @@ describe "EditSession", ->
expect(buffer.lineForRow(0)).toBe "var quicksort = function () {"
expect(buffer.lineForRow(1)).toBe "var sort = function(items) {"
expect(buffer.lineForRow(2)).toBe " if (items.length <= 1) return items;"
expect(buffer.lineForRow(3)).toBe " var pivot = items.shift(), current, left = [], right = [];"
expect(editSession.getSelectedBufferRange()).toEqual [[0, 1], [3, 15 - editSession.tabLength]]
it "does not outdent the last line of the selection if it ends at column 0", ->
editSession.setSelectedBufferRange([[0,1], [3,0]])
editSession.outdentSelectedRows()
expect(buffer.lineForRow(0)).toBe "var quicksort = function () {"
expect(buffer.lineForRow(1)).toBe "var sort = function(items) {"
expect(buffer.lineForRow(2)).toBe " if (items.length <= 1) return items;"
expect(buffer.lineForRow(3)).toBe " var pivot = items.shift(), current, left = [], right = [];"
expect(editSession.getSelectedBufferRange()).toEqual [[0, 1], [3, 0]]
describe ".toggleLineCommentsInSelection()", ->
it "toggles comments on the selected lines", ->
editSession.setSelectedBufferRange([[4, 5], [7, 5]])

View File

@@ -173,8 +173,8 @@ class Selection
@indentSelectedRows()
indentSelectedRows: ->
range = @getBufferRange()
for row in [range.start.row..range.end.row]
[start, end] = @getBufferRowRange()
for row in [start..end]
@editSession.buffer.insert([row, 0], @editSession.getTabText()) unless @editSession.buffer.lineLengthForRow(row) == 0
normalizeIndent: (text, options) ->
@@ -270,10 +270,10 @@ class Selection
@editSession.buffer.deleteRows(start, end)
outdentSelectedRows: ->
range = @getBufferRange()
[start, end] = @getBufferRowRange()
buffer = @editSession.buffer
leadingTabRegex = new RegExp("^ {1,#{@editSession.getTabLength()}}|\t")
for row in [range.start.row..range.end.row]
for row in [start..end]
if matchLength = buffer.lineForRow(row).match(leadingTabRegex)?[0].length
buffer.delete [[row, 0], [row, matchLength]]