Auto-indent works for multiple cursors

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-03-26 15:22:45 -07:00
parent d304786245
commit 65b0107811
4 changed files with 33 additions and 23 deletions

View File

@@ -591,6 +591,14 @@ describe "Editor", ->
expect(editor.buffer.lineForRow(2)).toEqual(" }")
expect(editor.getCursorBufferPosition().column).toBe 3
describe "when newlines are inserted for multiple cursors", ->
it "auto-indents the newline for each cursor", ->
editor.setCursorScreenPosition([1, 30])
editor.addCursorAtScreenPosition([4, 29])
editor.insertText("\n")
expect(editor.buffer.lineForRow(2)).toEqual(" ")
expect(editor.buffer.lineForRow(6)).toEqual(" ")
describe "when editing a line that spans multiple screen lines", ->
beforeEach ->
editor.setSoftWrap(true, 50)

View File

@@ -85,8 +85,11 @@ class Cursor extends View
getBufferRow: ->
@getBufferPosition().row
getCurrentBufferLine: ->
@editor.lineForBufferRow(@getBufferRow())
isOnEOL: ->
@getScreenColumn() == @editor.lineForBufferRow(@getBufferRow()).length
@getScreenColumn() == @getCurrentBufferLine().length
moveUp: ->
{ row, column } = @getScreenPosition()

View File

@@ -1,5 +1,4 @@
{View, $$} = require 'space-pen'
AceOutdentAdaptor = require 'ace-outdent-adaptor'
Buffer = require 'buffer'
CompositeCursor = require 'composite-cursor'
CompositeSelection = require 'composite-selection'
@@ -350,7 +349,6 @@ class Editor extends View
moveCursorRight: -> @compositeCursor.moveRight()
moveCursorLeft: -> @compositeCursor.moveLeft()
getCurrentScreenLine: -> @buffer.lineForRow(@getCursorScreenRow())
getCurrentBufferLine: -> @buffer.lineForRow(@getCursorBufferRow())
setCursorScreenPosition: (position) -> @compositeCursor.setScreenPosition(position)
getCursorScreenPosition: -> @getCursor().getScreenPosition()
@@ -381,27 +379,7 @@ class Editor extends View
lineForBufferRow: (row) -> @buffer.lineForRow(row)
insertText: (text) ->
{ text, shouldOutdent } = @autoIndentText(text)
@compositeSelection.insertText(text)
@autoOutdentText() if shouldOutdent
autoIndentText: (text) ->
if @autoIndent
row = @getCursorScreenPosition().row
state = @renderer.lineForRow(row).state
if text[0] == "\n"
indent = @buffer.mode.getNextLineIndent(state, @getCurrentBufferLine(), atom.tabText)
text = text[0] + indent + text[1..]
else if @buffer.mode.checkOutdent(state, @getCurrentBufferLine(), text)
shouldOutdent = true
{text, shouldOutdent}
autoOutdentText: ->
screenRow = @getCursorScreenPosition().row
bufferRow = @getCursorBufferPosition().row
state = @renderer.lineForRow(screenRow).state
@buffer.mode.autoOutdent(state, new AceOutdentAdaptor(@buffer, this), bufferRow)
cutSelection: -> @getSelection().cut()
copySelection: -> @getSelection().copy()

View File

@@ -1,4 +1,5 @@
Cursor = require 'cursor'
AceOutdentAdaptor = require 'ace-outdent-adaptor'
Range = require 'range'
{View, $$} = require 'space-pen'
@@ -78,7 +79,27 @@ class Selection extends View
@editor.buffer.getTextInRange @getBufferRange()
insertText: (text) ->
{ text, shouldOutdent } = @autoIndentText(text)
@editor.buffer.change(@getBufferRange(), text)
@autoOutdentText() if shouldOutdent
autoIndentText: (text) ->
if @editor.autoIndent
row = @cursor.getScreenPosition().row
state = @editor.renderer.lineForRow(row).state
if text[0] == "\n"
indent = @editor.buffer.mode.getNextLineIndent(state, @cursor.getCurrentBufferLine(), atom.tabText)
text = text[0] + indent + text[1..]
else if @editor.buffer.mode.checkOutdent(state, @cursor.getCurrentBufferLine(), text)
shouldOutdent = true
{text, shouldOutdent}
autoOutdentText: ->
screenRow = @cursor.getScreenPosition().row
bufferRow = @cursor.getBufferPosition().row
state = @editor.renderer.lineForRow(screenRow).state
@editor.buffer.mode.autoOutdent(state, new AceOutdentAdaptor(@editor.buffer, @editor), bufferRow)
backspace: ->
@selectLeft() if @isEmpty()