Merge pull request #7225 from lpommers/add-remove-cursor-when-there-are-multiple-cursors-if-cmd-clicking-on-it

Add remove cursor when there are multiple cursors and cmd clicking on it
This commit is contained in:
Max Brunsfeld 2015-06-12 10:41:17 -07:00
commit c43233892b
4 changed files with 44 additions and 7 deletions

View File

@ -1569,11 +1569,28 @@ describe "TextEditorComponent", ->
expect(editor.getSelectedScreenRange()).toEqual [[3, 4], [5, 6]]
describe "when the command key is held down", ->
it "adds a cursor at the nearest screen position", ->
editor.setCursorScreenPosition([3, 4])
linesNode.dispatchEvent(buildMouseEvent('mousedown', clientCoordinatesForScreenPosition([5, 6]), metaKey: true))
nextAnimationFrame()
expect(editor.getSelectedScreenRanges()).toEqual [[[3, 4], [3, 4]], [[5, 6], [5, 6]]]
describe "the current cursor position and screen position do not match", ->
it "adds a cursor at the nearest screen position", ->
editor.setCursorScreenPosition([3, 4])
linesNode.dispatchEvent(buildMouseEvent('mousedown', clientCoordinatesForScreenPosition([5, 6]), metaKey: true))
nextAnimationFrame()
expect(editor.getSelectedScreenRanges()).toEqual [[[3, 4], [3, 4]], [[5, 6], [5, 6]]]
describe "when there are multiple cursors, and one of the cursor's screen position is the same as the mouse click screen position", ->
it "removes a cursor at the mouse screen position", ->
editor.setCursorScreenPosition([3, 4])
editor.addCursorAtScreenPosition([5, 2])
editor.addCursorAtScreenPosition([7, 5])
linesNode.dispatchEvent(buildMouseEvent('mousedown', clientCoordinatesForScreenPosition([3, 4]), metaKey: true))
nextAnimationFrame()
expect(editor.getSelectedScreenRanges()).toEqual [[[5, 2], [5, 2]], [[7, 5], [7, 5]]]
describe "when there is a single cursor and the click occurs at the cursor's screen position", ->
it "neither adds a new cursor nor removes the current cursor", ->
editor.setCursorScreenPosition([3, 4])
linesNode.dispatchEvent(buildMouseEvent('mousedown', clientCoordinatesForScreenPosition([3, 4]), metaKey: true))
nextAnimationFrame()
expect(editor.getSelectedScreenRanges()).toEqual [[[3, 4], [3, 4]]]
describe "when a non-folded line is double-clicked", ->
describe "when no modifier keys are held down", ->

View File

@ -916,6 +916,12 @@ describe "TextEditor", ->
editor.setCursorBufferPosition([3, 1])
expect(editor.getCurrentParagraphBufferRange()).toBeUndefined()
describe "getCursorAtScreenPosition(screenPosition)", ->
it "returns the cursor at the given screenPosition", ->
cursor1 = editor.addCursorAtScreenPosition([0, 2])
cursor2 = editor.getCursorAtScreenPosition(cursor1.getScreenPosition())
expect(cursor2).toBe cursor1
describe "::getCursorScreenPositions()", ->
it "returns the cursor positions in the order they were added", ->
editor.foldBufferRow(4)
@ -935,7 +941,7 @@ describe "TextEditor", ->
it "returns the existing cursor", ->
cursor1 = editor.addCursorAtScreenPosition([0, 2])
cursor2 = editor.addCursorAtScreenPosition([0, 2])
expect(cursor2.marker).toBe cursor1.marker
expect(cursor2).toBe cursor1
describe "addCursorAtBufferPosition(bufferPosition)", ->
describe "when a cursor already exists at the position", ->

View File

@ -392,7 +392,11 @@ class TextEditorComponent
if shiftKey
@editor.selectToScreenPosition(screenPosition)
else if metaKey or (ctrlKey and process.platform isnt 'darwin')
@editor.addCursorAtScreenPosition(screenPosition)
cursorAtScreenPosition = @editor.getCursorAtScreenPosition(screenPosition)
if cursorAtScreenPosition and @editor.hasMultipleCursors()
cursorAtScreenPosition.destroy()
else
@editor.addCursorAtScreenPosition(screenPosition)
else
@editor.setCursorScreenPosition(screenPosition)
when 2

View File

@ -1565,6 +1565,16 @@ class TextEditor extends Model
setCursorBufferPosition: (position, options) ->
@moveCursors (cursor) -> cursor.setBufferPosition(position, options)
# Essential: Get a {Cursor} at given screen coordinates {Point}
#
# * `position` A {Point} or {Array} of `[row, column]`
#
# Returns the first matched {Cursor} or undefined
getCursorAtScreenPosition: (position) ->
for cursor in @cursors
return cursor if cursor.getScreenPosition().isEqual(position)
undefined
# Essential: Get the position of the most recently added cursor in screen
# coordinates.
#