From c70206fc9c779e1deeadf5ee36e5e22ff7012a49 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 26 Jan 2012 13:23:59 -0800 Subject: [PATCH] Introduce Selection object that contains the cursor All cursor operations will be proxied through the selection, so that the selection can be cleared if necessary. --- spec/atom/editor-spec.coffee | 8 +++--- src/atom/cursor.coffee | 4 +-- src/atom/editor.coffee | 31 +++++++++++---------- src/atom/selection.coffee | 52 ++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 src/atom/selection.coffee diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index a99591ba5..5423a73d0 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -33,8 +33,8 @@ describe "Editor", -> editor.attachToDom() editor.setPosition(row: 2, column: 2) - expect(editor.cursor.position().top).toBe(2 * editor.lineHeight) - expect(editor.cursor.position().left).toBe(2 * editor.charWidth) + expect(editor.getCursor().position().top).toBe(2 * editor.lineHeight) + expect(editor.getCursor().position().left).toBe(2 * editor.charWidth) describe "when the arrow keys are pressed", -> it "moves the cursor by a single row/column", -> @@ -204,8 +204,8 @@ describe "Editor", -> editor.attachToDom() - expect(editor.cursor.position().top).toBe(2 * editor.lineHeight) - expect(editor.cursor.position().left).toBe(2 * editor.charWidth) + expect(editor.getCursor().position().top).toBe(2 * editor.lineHeight) + expect(editor.getCursor().position().left).toBe(2 * editor.charWidth) it "is focused", -> editor.attachToDom() diff --git a/src/atom/cursor.coffee b/src/atom/cursor.coffee index fa5667d75..a3d7d746d 100644 --- a/src/atom/cursor.coffee +++ b/src/atom/cursor.coffee @@ -17,7 +17,7 @@ class Cursor extends Template setPosition: (point) -> @point = @editor.clipPosition(point) @goalColumn = null - @updateAbsolutePosition() + @updateScreenPosition() getPosition: -> @point @@ -78,7 +78,7 @@ class Cursor extends Template @setPosition({row, column}) - updateAbsolutePosition: -> + updateScreenPosition: -> position = @editor.pixelPositionFromPoint(@point) @css(position) diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index ff2995c71..d61a050d4 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -1,6 +1,6 @@ Template = require 'template' Buffer = require 'buffer' -Cursor = require 'cursor' +Selection = require 'selection' $ = require 'jquery' $$ = require 'template/builder' _ = require 'underscore' @@ -14,20 +14,17 @@ class Editor extends Template viewProperties: buffer: null - cursor: null + selection: null scrollMargin: 2 initialize: () -> requireStylesheet 'editor.css' - @bindKeys() - @attachCursor() + @selection = Selection.build(this) + @append(@selection) @handleEvents() @setBuffer(new Buffer) - attachCursor: -> - @cursor = Cursor.build(this).appendTo(this) - bindKeys: -> atom.bindKeys '*', right: 'move-right' @@ -84,7 +81,7 @@ class Editor extends Template @updateLineElement(curRow) curRow++ - @cursor.bufferChanged(e) + @selection.bufferChanged(e) updateLineElement: (row) -> line = @buffer.getLine(row) @@ -116,7 +113,7 @@ class Editor extends Template @charWidth = fragment.width() @lineHeight = fragment.outerHeight() fragment.remove() - @cursor.updateAbsolutePosition() + @selection.updateScreenPosition() scrollBottom: (newValue) -> if newValue? @@ -126,10 +123,12 @@ class Editor extends Template getCurrentLine: -> @buffer.getLine(@getPosition().row) - moveUp: -> @cursor.moveUp() - moveDown: -> @cursor.moveDown() - moveRight: -> @cursor.moveRight() - moveLeft: -> @cursor.moveLeft() - setPosition: (point) -> @cursor.setPosition(point) - getPosition: -> @cursor.getPosition() - setColumn: (column)-> @cursor.setColumn column + getCursor: -> @selection.cursor + moveUp: -> @selection.moveCursorUp() + moveDown: -> @selection.moveCursorDown() + moveRight: -> @selection.moveCursorRight() + moveLeft: -> @selection.moveCursorLeft() + setPosition: (point) -> @selection.setCursorPosition(point) + getPosition: -> @selection.getCursorPosition() + setColumn: (column) -> @selection.setCursorColumn(column) + diff --git a/src/atom/selection.coffee b/src/atom/selection.coffee new file mode 100644 index 000000000..5a9cc80cc --- /dev/null +++ b/src/atom/selection.coffee @@ -0,0 +1,52 @@ +Template = require 'template' +Cursor = require 'cursor' + +module.exports = +class Selection extends Template + content: -> + @div() + + viewProperties: + initialize: (editor) -> + @editor = editor + @cursor = Cursor.build(editor).appendTo(this) + + bufferChanged: (e) -> + @cursor.setPosition(e.postRange.end) + + updateScreenPosition: -> + @cursor.updateScreenPosition() + + setCursorPosition: (point) -> + @cursor.setPosition(point) + + getCursorPosition: -> + @cursor.getPosition() + + setCursorColumn: (column) -> + @cursor.setColumn(column) + + getCursorColumn: -> + @cursor.getColumn() + + getCursorRow: -> + @cursor.getRow() + + moveCursorUp: -> + @cursor.moveUp() + + moveCursorDown: -> + @cursor.moveDown() + + moveCursorLeft: -> + @cursor.moveLeft() + + moveCursorRight: -> + @cursor.moveRight() + + moveCursorToLineEnd: -> + @cursor.moveToLineEnd() + + moveCursorToLineStart: -> + @cursor.moveToLineStart() +