From 0efbd65a242a96eca09c22697caa47249806bbe5 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 21 Mar 2012 14:25:28 -0700 Subject: [PATCH] Add Buffer.characterIndexForPosition --- spec/atom/buffer-spec.coffee | 8 ++++++++ src/atom/buffer.coffee | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/spec/atom/buffer-spec.coffee b/spec/atom/buffer-spec.coffee index d52d897ba..892507281 100644 --- a/spec/atom/buffer-spec.coffee +++ b/spec/atom/buffer-spec.coffee @@ -203,3 +203,11 @@ describe 'Buffer', -> lineLength = buffer.lineForRow(2).length range = [[2,10], [4,10]] expect(buffer.getTextInRange(range)).toBe "ems.length <= 1) return items;\n var pivot = items.shift(), current, left = [], right = [];\n while(" + + describe ".characterIndexForPosition(position)", -> + it "returns the total number of charachters that precede the given position", -> + expect(buffer.characterIndexForPosition([0, 0])).toBe 0 + expect(buffer.characterIndexForPosition([0, 1])).toBe 1 + expect(buffer.characterIndexForPosition([0, 29])).toBe 29 + expect(buffer.characterIndexForPosition([1, 0])).toBe 30 + expect(buffer.characterIndexForPosition([2, 0])).toBe 61 \ No newline at end of file diff --git a/src/atom/buffer.coffee b/src/atom/buffer.coffee index 1a477241f..84ed96644 100644 --- a/src/atom/buffer.coffee +++ b/src/atom/buffer.coffee @@ -1,5 +1,6 @@ _ = require 'underscore' fs = require 'fs' +Point = require 'point' Range = require 'range' EventEmitter = require 'event-emitter' UndoManager = require 'undo-manager' @@ -59,6 +60,13 @@ class Buffer lastLine: -> @lineForRow(@lastRow()) + characterIndexForPosition: (position) -> + position = Point.fromObject(position) + + index = 0 + index += @getLineLength(row) + 1 for row in [0...position.row] + index + position.column + deleteRow: (row) -> range = null if row == @lastRow()