From 628701fd3a6eee975f2ac942da68b9f410ca39b2 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 31 Jan 2013 15:52:28 -0700 Subject: [PATCH] Finish converting cursors to use markers. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The model layer works at least… haven't tested the view. Will test out the view once I get the selection model working. --- spec/app/buffer-spec.coffee | 24 +++++++++++++++++++++ spec/app/display-buffer-spec.coffee | 21 +++++++++++++++++-- spec/app/edit-session-spec.coffee | 10 ++++----- src/app/buffer-change-operation.coffee | 8 +++---- src/app/buffer.coffee | 4 ++++ src/app/cursor.coffee | 10 ++++----- src/app/display-buffer.coffee | 7 +++++++ src/app/edit-session.coffee | 29 +++++++++++++++++++++++++- 8 files changed, 96 insertions(+), 17 deletions(-) diff --git a/spec/app/buffer-spec.coffee b/spec/app/buffer-spec.coffee index ca7dd8681..b72374fd6 100644 --- a/spec/app/buffer-spec.coffee +++ b/spec/app/buffer-spec.coffee @@ -727,6 +727,30 @@ describe 'Buffer', -> buffer.setMarkerHeadPosition(marker, [6, 2]) expect(observeHandler).not.toHaveBeenCalled() + describe "marker destruction", -> + marker = null + + beforeEach -> + marker = buffer.markRange([[4, 20], [4, 23]]) + + it "allows a marker to be destroyed", -> + buffer.destroyMarker(marker) + expect(buffer.getMarkerRange(marker)).toBeUndefined() + + it "does not restore invalidated markers that have been destroyed", -> + buffer.delete([[4, 15], [4, 25]]) + expect(buffer.getMarkerRange(marker)).toBeUndefined() + buffer.destroyMarker(marker) + buffer.undo() + expect(buffer.getMarkerRange(marker)).toBeUndefined() + + # even "stayValid" markers get destroyed properly + marker2 = buffer.markRange([[4, 20], [4, 23]], stayValid: true) + buffer.delete([[4, 15], [4, 25]]) + buffer.destroyMarker(marker2) + buffer.undo() + expect(buffer.getMarkerRange(marker2)).toBeUndefined() + describe "marker updates due to buffer changes", -> [marker1, marker2] = [] diff --git a/spec/app/display-buffer-spec.coffee b/spec/app/display-buffer-spec.coffee index 054978315..5c4366f0e 100644 --- a/spec/app/display-buffer-spec.coffee +++ b/spec/app/display-buffer-spec.coffee @@ -584,7 +584,7 @@ describe "DisplayBuffer", -> beforeEach -> displayBuffer.foldBufferRow(4) - describe "creation and manipulation", -> + describe "marker creation and manipulation", -> it "allows markers to be created in terms of both screen and buffer coordinates", -> marker1 = displayBuffer.markScreenRange([[5, 4], [5, 10]]) marker2 = displayBuffer.markBufferRange([[8, 4], [8, 10]]) @@ -603,7 +603,15 @@ describe "DisplayBuffer", -> expect(displayBuffer.isMarkerReversed(marker)).toBeTruthy() expect(displayBuffer.getMarkerBufferRange(marker)).toEqual [[5, 4], [8, 4]] - describe "observation", -> + it "clips screen positions before assigning them", -> + marker = displayBuffer.markScreenRange([[5, 4], [5, 10]]) + displayBuffer.setMarkerHeadScreenPosition(marker, [-5, -4]) + expect(displayBuffer.getMarkerBufferRange(marker)).toEqual [[0, 0], [8, 4]] + + displayBuffer.setMarkerTailScreenPosition(marker, [-5, -4]) + expect(displayBuffer.getMarkerBufferRange(marker)).toEqual [[0, 0], [0, 0]] + + describe "marker observation", -> observeHandler = null beforeEach -> @@ -662,3 +670,12 @@ describe "DisplayBuffer", -> displayBuffer.unfoldBufferRow(4) expect(observeHandler).not.toHaveBeenCalled() + + describe "marker destruction", -> + it "allows markers to be destroyed", -> + marker = displayBuffer.markScreenRange([[5, 4], [5, 10]]) + displayBuffer.destroyMarker(marker) + expect(displayBuffer.getMarkerBufferRange(marker)).toBeUndefined() + + + diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index e2892d68f..ba5b36e66 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -76,12 +76,12 @@ describe "EditSession", -> describe "when the cursor is on the first line", -> it "moves the cursor to the beginning of the line, but retains the goal column", -> - editSession.setCursorScreenPosition(row: 0, column: 4) + editSession.setCursorScreenPosition([0, 4]) editSession.moveCursorUp() - expect(editSession.getCursorScreenPosition()).toEqual(row: 0, column: 0) + expect(editSession.getCursorScreenPosition()).toEqual([0, 0]) editSession.moveCursorDown() - expect(editSession.getCursorScreenPosition()).toEqual(row: 1, column: 4) + expect(editSession.getCursorScreenPosition()).toEqual([1, 4]) it "merges cursors when they overlap", -> editSession.addCursorAtScreenPosition([1, 0]) @@ -185,9 +185,9 @@ describe "EditSession", -> describe "when the cursor is on the last column of a line", -> describe "when there is a subsequent line", -> it "wraps to the beginning of the next line", -> - editSession.setCursorScreenPosition(row: 0, column: buffer.lineForRow(0).length) + editSession.setCursorScreenPosition([0, buffer.lineForRow(0).length]) editSession.moveCursorRight() - expect(editSession.getCursorScreenPosition()).toEqual(row: 1, column: 0) + expect(editSession.getCursorScreenPosition()).toEqual [1, 0] describe "when the cursor is on the last line", -> it "remains in the same position", -> diff --git a/src/app/buffer-change-operation.coffee b/src/app/buffer-change-operation.coffee index bebcf384b..de64d0335 100644 --- a/src/app/buffer-change-operation.coffee +++ b/src/app/buffer-change-operation.coffee @@ -95,8 +95,8 @@ class BufferChangeOperation restoreMarkers: (markersToRestore) -> for [id, previousRange] in markersToRestore - if existingMarker = @buffer.validMarkers[id] - existingMarker.setRange(previousRange) - else - @buffer.validMarkers[id] = @buffer.invalidMarkers[id] + if validMarker = @buffer.validMarkers[id] + validMarker.setRange(previousRange) + else if invalidMarker = @buffer.invalidMarkers[id] + @buffer.validMarkers[id] = invalidMarker diff --git a/src/app/buffer.coffee b/src/app/buffer.coffee index a12f08908..6dab55088 100644 --- a/src/app/buffer.coffee +++ b/src/app/buffer.coffee @@ -283,6 +283,10 @@ class Buffer markPosition: (position, options) -> @markRange([position, position], _.defaults({noTail: true}, options)) + destroyMarker: (id) -> + delete @validMarkers[id] + delete @invalidMarkers[id] + getMarkerPosition: (args...) -> @getMarkerHeadPosition(args...) diff --git a/src/app/cursor.coffee b/src/app/cursor.coffee index 330b05464..b79a55574 100644 --- a/src/app/cursor.coffee +++ b/src/app/cursor.coffee @@ -12,10 +12,10 @@ class Cursor needsAutoscroll: null constructor: ({@editSession, @marker}) -> -# @editSession.observeMarkerHeadScreenPosition @marker, (screenPosition) -> -# @needsAutoscroll ?= @isLastCursor() -# @trigger 'moved', e -# @editSession.trigger 'cursor-moved', e + @editSession.observeMarkerHeadScreenPosition @marker, (screenPosition) => + @needsAutoscroll ?= @isLastCursor() + @trigger 'moved', screenPosition + @editSession.trigger 'cursor-moved', screenPosition @needsAutoscroll = true destroy: -> @@ -36,7 +36,7 @@ class Cursor @goalColumn = null @clearSelection() @needsAutoscroll = options.autoscroll ? @isLastCursor() - @editSession.setMarkerHeadBufferPosition(@marker, screenPosition, options) + @editSession.setMarkerHeadBufferPosition(@marker, bufferPosition, options) getBufferPosition: -> @editSession.getMarkerHeadBufferPosition(@marker) diff --git a/src/app/display-buffer.coffee b/src/app/display-buffer.coffee index 87c0aa87a..fcff6df24 100644 --- a/src/app/display-buffer.coffee +++ b/src/app/display-buffer.coffee @@ -312,6 +312,11 @@ class DisplayBuffer markBufferPosition: (bufferPosition) -> @buffer.markPosition(bufferPosition) + destroyMarker: (id) -> + @buffer.destroyMarker(id) + delete @markerScreenPositionObservers[id] + delete @markerScreenPositions[id] + getMarkerScreenRange: (id) -> @screenRangeForBufferRange(@getMarkerBufferRange(id)) @@ -328,6 +333,7 @@ class DisplayBuffer @screenPositionForBufferPosition(@getMarkerHeadBufferPosition(id)) setMarkerHeadScreenPosition: (id, screenPosition, options) -> + screenPosition = @clipScreenPosition(screenPosition, options) @setMarkerHeadBufferPosition(id, @bufferPositionForScreenPosition(screenPosition, options)) getMarkerHeadBufferPosition: (id) -> @@ -340,6 +346,7 @@ class DisplayBuffer @screenPositionForBufferPosition(@getMarkerTailBufferPosition(id)) setMarkerTailScreenPosition: (id, screenPosition, options) -> + screenPosition = @clipScreenPosition(screenPosition, options) @setMarkerTailBufferPosition(id, @bufferPositionForScreenPosition(screenPosition, options)) getMarkerTailBufferPosition: (id) -> diff --git a/src/app/edit-session.coffee b/src/app/edit-session.coffee index 7fdb33959..b3e1922b9 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -440,21 +440,48 @@ class EditSession markBufferPosition: (args...) -> @displayBuffer.markBufferPosition(args...) + destroyMarker: (args...) -> + @displayBuffer.destroyMarker(args...) + getMarkerBufferRange: (args...) -> @displayBuffer.getMarkerBufferRange(args...) getMarkerScreenRange: (args...) -> @displayBuffer.getMarkerScreenRange(args...) + getMarkerScreenPosition: (args...) -> + @displayBuffer.getMarkerScreenPosition(args...) + getMarkerBufferPosition: (args...) -> @displayBuffer.getMarkerBufferPosition(args...) + getMarkerHeadScreenPosition: (args...) -> + @displayBuffer.getMarkerHeadScreenPosition(args...) + + setMarkerHeadScreenPosition: (args...) -> + @displayBuffer.setMarkerHeadScreenPosition(args...) + getMarkerHeadBufferPosition: (args...) -> @displayBuffer.getMarkerHeadBufferPosition(args...) + setMarkerHeadBufferPosition: (args...) -> + @displayBuffer.setMarkerHeadBufferPosition(args...) + + getMarkerTailScreenPosition: (args...) -> + @displayBuffer.getMarkerTailScreenPosition(args...) + + setMarkerTailScreenPosition: (args...) -> + @displayBuffer.setMarkerTailScreenPosition(args...) + getMarkerTailBufferPosition: (args...) -> @displayBuffer.getMarkerTailBufferPosition(args...) + setMarkerTailBufferPosition: (args...) -> + @displayBuffer.setMarkerTailBufferPosition(args...) + + observeMarkerHeadScreenPosition: (args...) -> + @displayBuffer.observeMarkerHeadScreenPosition(args...) + addAnchor: (options={}) -> anchor = @buffer.addAnchor(_.extend({editSession: this}, options)) @anchors.push(anchor) @@ -495,7 +522,7 @@ class EditSession @addSelection(marker).cursor addCursorAtBufferPosition: (bufferPosition) -> - marker = @markBufferPosition(screenPosition, stayValid: true) + marker = @markBufferPosition(bufferPosition, stayValid: true) @addSelection(marker).cursor addCursor: (marker) ->