From 2394f25b92edaa0fa64bed856ccd87a339d2438e Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 30 Sep 2013 14:33:06 -0700 Subject: [PATCH] Add the leftPixel cache back in --- spec/editor-spec.coffee | 26 +++++++++++++++++++++++++- src/editor.coffee | 33 +++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 1ad1c527c..f6b6d8a59 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -2199,10 +2199,34 @@ describe "Editor", -> expect(editor.pixelPositionForBufferPosition([2,7])).toEqual top: 0, left: 0 describe "when the editor is attached and visible", -> - it "returns the top and left pixel positions", -> + beforeEach -> editor.attachToDom() + + it "returns the top and left pixel positions", -> expect(editor.pixelPositionForBufferPosition([2,7])).toEqual top: 40, left: 70 + it "caches the left position", -> + editor.renderedLines.css('font-size', '16px') + expect(editor.pixelPositionForBufferPosition([2,8])).toEqual top: 40, left: 80 + + # make characters smaller + editor.renderedLines.css('font-size', '15px') + + expect(editor.pixelPositionForBufferPosition([2,8])).toEqual top: 40, left: 80 + + it "breaks left position cache when line is changed", -> + editor.renderedLines.css('font-size', '16px') + expect(editor.pixelPositionForBufferPosition([2,8])).toEqual top: 40, left: 80 + + editor.setCursorBufferPosition([2, 8]) + editor.insertText("a") + + # make characters smaller + editor.renderedLines.css('font-size', '15px') + + expect(editor.pixelPositionForBufferPosition([2,8])).toEqual top: 40, left: 72 + + describe "when clicking in the gutter", -> beforeEach -> editor.attachToDom() diff --git a/src/editor.coffee b/src/editor.coffee index a1791d1a6..ba18118a9 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -94,8 +94,7 @@ class Editor extends View @pendingChanges = [] @newCursors = [] @newSelections = [] - @lineId = 0 - @pixelLeftCache = {} + @pixelLeftCache = new WeakMap() if editSession? @edit(editSession) @@ -1330,10 +1329,6 @@ class Editor extends View clearDirtyRanges: (intactRanges) -> renderedLines = @renderedLines[0] - killLine = (line) -> - next = line.nextSibling - renderedLines.removeChild(line) - next if intactRanges.length == 0 @renderedLines.empty() @@ -1341,13 +1336,19 @@ class Editor extends View domPosition = 0 for intactRange in intactRanges while intactRange.domStart > domPosition - currentLine = killLine(currentLine) + currentLine = @clearLine(currentLine) domPosition++ for i in [intactRange.start..intactRange.end] currentLine = currentLine.nextSibling domPosition++ while currentLine - currentLine = killLine(currentLine) + currentLine = @clearLine(currentLine) + + clearLine: (lineElement) -> + @pixelLeftCache.delete(lineElement) + next = lineElement.nextSibling + @renderedLines[0].removeChild(lineElement) + next fillDirtyRanges: (intactRanges, renderFrom, renderTo) -> renderedLines = @renderedLines[0] @@ -1427,9 +1428,9 @@ class Editor extends View htmlForScreenLine: (screenLine, screenRow) -> { tokens, text, lineEnding, fold, isSoftWrapped } = screenLine if fold - attributes = { class: 'fold line', 'fold-id': fold.id, 'line-id': @lineId++ } + attributes = { class: 'fold line', 'fold-id': fold.id } else - attributes = { class: 'line', 'line-id': @lineId++ } + attributes = { class: 'line' } invisibles = @invisibles if @showInvisibles eolInvisibles = @getEndOfLineInvisibles(screenLine) @@ -1519,11 +1520,19 @@ class Editor extends View @renderedLines[0].removeChild(lineElement) { top: row * @lineHeight, left } - positionLeftForLineAndColumn: (lineElement, column) -> - chars = lineElement.find('.character') + positionLeftForLineAndColumn: (line, column) -> + lineCache = @pixelLeftCache.get(line[0]) + @pixelLeftCache.set(line[0], lineCache = {}) unless lineCache? + + return lineCache[column] if lineCache[column]? + + chars = line.find('.character') left = 0 for i in [0...column] left += chars[i].offsetWidth if chars[i] + + lineCache[column] = left + left pixelOffsetForScreenPosition: (position) ->