From 4c2683a378d7a3089d1c435369ffb9276dd313a4 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 13 May 2015 11:14:21 +0200 Subject: [PATCH] :art: Clarify `@updateTilesState` --- src/text-editor-presenter.coffee | 59 +++++++++++++++++--------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/src/text-editor-presenter.coffee b/src/text-editor-presenter.coffee index 28d677734..37b5daee1 100644 --- a/src/text-editor-presenter.coffee +++ b/src/text-editor-presenter.coffee @@ -300,43 +300,48 @@ class TextEditorPresenter @state.content.backgroundColor = if @model.isMini() then null else @backgroundColor @state.content.placeholderText = if @model.isEmpty() then @model.getPlaceholderText() else null + # REFACTOR: This should be a @field rather than a function. + linesPerTile: -> + linesPerTile = Math.floor(@height / @lineHeight / @tileCount) + Math.max(1, linesPerTile) + + tileForRow: (row) -> + row - (row % @linesPerTile()) + + getVisibleTileRange: -> + startTileRow = Math.max(0, @tileForRow(@startRow) - @tileOverdrawMargin) + endTileRow = Math.min( + @tileForRow(@model.getScreenLineCount()), + @tileForRow(@endRow) + @tileOverdrawMargin + ) + + [startTileRow..endTileRow] + updateTilesState: -> return unless @startRow? and @endRow? and @lineHeight? - linesPerTile = Math.floor(@height / @lineHeight / @tileCount) - linesPerTile = Math.max(1, linesPerTile) - - startIndex = Math.max( - 0, Math.floor(@startRow / linesPerTile) - @tileOverdrawMargin - ) - endIndex = Math.min( - Math.ceil(@model.getScreenLineCount() / linesPerTile), - Math.ceil(@endRow / linesPerTile) + @tileOverdrawMargin - ) - visibleTiles = {} - for index in [startIndex...endIndex] - startRow = Math.floor(index * linesPerTile) - endRow = Math.ceil(Math.min(@model.getScreenLineCount(), (index + 1) * linesPerTile)) + for startRow in @getVisibleTileRange() by @linesPerTile() + endRow = Math.min(@model.getScreenLineCount(), startRow + @linesPerTile()) - isNewTile = not @state.content.tiles.hasOwnProperty(index) - tile = @state.content.tiles[index] ?= {} - tile.top = (index * linesPerTile * @lineHeight) - @scrollTop - tile.height = linesPerTile * @lineHeight + isNewTile = not @state.content.tiles.hasOwnProperty(startRow) + tile = @state.content.tiles[startRow] ?= {} + tile.top = startRow * @lineHeight - @scrollTop + tile.height = @linesPerTile() * @lineHeight tile.newlyCreated = isNewTile tile.display = "block" @updateLinesState(tile, startRow, endRow) - visibleTiles[index] = true + visibleTiles[startRow] = true - for index, tile of @state.content.tiles - continue if visibleTiles.hasOwnProperty(index) + for id, tile of @state.content.tiles + continue if visibleTiles.hasOwnProperty(id) - if index is @scrollingTile + if id is @scrollingTileId tile.display = "none" else - delete @state.content.tiles[index] + delete @state.content.tiles[id] updateLinesState: (tileState, startRow, endRow) -> @@ -767,8 +772,8 @@ class TextEditorPresenter didStopScrolling: -> @state.content.scrollingVertically = false - if @scrollingTile? - @scrollingTile = null + if @scrollingTileId? + @scrollingTileId = null @shouldUpdateTilesState = true @shouldUpdateLineNumbersState = true @shouldUpdateCustomGutterDecorationState = true @@ -935,8 +940,8 @@ class TextEditorPresenter @emitDidUpdateState() setScrollingTileId: (tileId) -> - if @scrollingTile isnt tileId - @scrollingTile = tileId + if @scrollingTileId isnt tileId + @scrollingTileId = tileId @didStartScrolling() setBaseCharacterWidth: (baseCharacterWidth) ->