From 1d1c613f5b7ac5d670cfa4fe0dda673eba8b3160 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 30 Jan 2013 22:13:50 -0700 Subject: [PATCH] Allow buffer marker head and tail positions to be manipulated --- spec/app/buffer-spec.coffee | 19 +++++++++++++++++++ src/app/buffer-marker.coffee | 19 ++++++++----------- src/app/buffer.coffee | 13 +++++++++++-- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/spec/app/buffer-spec.coffee b/spec/app/buffer-spec.coffee index 76747cc15..af485cb94 100644 --- a/spec/app/buffer-spec.coffee +++ b/spec/app/buffer-spec.coffee @@ -686,6 +686,25 @@ describe 'Buffer', -> expect(buffer.getMarkerHeadPosition(marker)).toEqual [4, 20] expect(buffer.getMarkerTailPosition(marker)).toEqual [4, 23] + describe "marker manipulation", -> + marker = null + beforeEach -> + marker = buffer.markRange([[4, 20], [4, 23]]) + + it "allows a markers head and tail positions to be changed", -> + buffer.setMarkerHeadPosition(marker, [5, 3]) + expect(buffer.getMarkerRange(marker)).toEqual [[4, 20], [5, 3]] + + buffer.setMarkerTailPosition(marker, [6, 3]) + expect(buffer.getMarkerRange(marker)).toEqual [[5, 3], [6, 3]] + expect(buffer.isMarkerReversed(marker)).toBeTruthy() + + it "clips head and tail positions to ensure they are in bounds", -> + buffer.setMarkerHeadPosition(marker, [-100, -5]) + expect(buffer.getMarkerRange(marker)).toEqual([[0, 0], [4, 20]]) + buffer.setMarkerTailPosition(marker, [Infinity, Infinity]) + expect(buffer.getMarkerRange(marker)).toEqual([[0, 0], [12, 2]]) + describe "marker updates due to buffer changes", -> [marker1, marker2] = [] diff --git a/src/app/buffer-marker.coffee b/src/app/buffer-marker.coffee index c0e8f0cf2..c97448887 100644 --- a/src/app/buffer-marker.coffee +++ b/src/app/buffer-marker.coffee @@ -33,6 +33,14 @@ class BufferMarker getTailPosition: -> @tailPosition + setHeadPosition: (headPosition, options={}) -> + @headPosition = Point.fromObject(headPosition) + @headPosition = @buffer.clipPosition(@headPosition) if options.clip ? true + + setTailPosition: (tailPosition, options={}) -> + @tailPosition = Point.fromObject(tailPosition) + @tailPosition = @buffer.clipPosition(@tailPosition) if options.clip ? true + getStartPosition: -> @getRange().start @@ -79,17 +87,6 @@ class BufferMarker [newRow, newColumn] - setTailPosition: (tailPosition, options={}) -> - @tailPosition = Point.fromObject(tailPosition) - @tailPosition = @buffer.clipPosition(@tailPosition) if options.clip ? true - - setHeadPosition: (headPosition, options={}) -> - @headPosition = Point.fromObject(headPosition) - @headPosition = @buffer.clipPosition(@headPosition) if options.clip ? true - - getPosition: -> - @position - invalidate: (preserve) -> delete @buffer.validMarkers[@id] @buffer.invalidMarkers[@id] = this diff --git a/src/app/buffer.coffee b/src/app/buffer.coffee index 824105ace..d588c9ce9 100644 --- a/src/app/buffer.coffee +++ b/src/app/buffer.coffee @@ -283,15 +283,24 @@ class Buffer markPosition: (position, options) -> @markRange([position, position], _.defaults({noTail: true}, options)) - getMarkerPosition: (id) -> - @getMarkerHeadPosition(id) + getMarkerPosition: (args...) -> + @getMarkerHeadPosition(args...) + + setMarkerPosition: (args...) -> + @setMarkerHeadPosition(args...) getMarkerHeadPosition: (id) -> @validMarkers[id]?.getHeadPosition() + setMarkerHeadPosition: (id, position, options) -> + @validMarkers[id]?.setHeadPosition(position) + getMarkerTailPosition: (id) -> @validMarkers[id]?.getTailPosition() + setMarkerTailPosition: (id, position, options) -> + @validMarkers[id]?.setTailPosition(position) + getMarkerRange: (id) -> @validMarkers[id]?.getRange()