mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-09-20 15:37:46 +03:00
55 lines
1.5 KiB
CoffeeScript
55 lines
1.5 KiB
CoffeeScript
{Point} = require 'text-buffer'
|
|
|
|
module.exports =
|
|
class FakeLinesYardstick
|
|
constructor: (@model, @lineTopIndex) ->
|
|
@characterWidthsByScope = {}
|
|
|
|
getScopedCharacterWidth: (scopeNames, char) ->
|
|
@getScopedCharacterWidths(scopeNames)[char]
|
|
|
|
getScopedCharacterWidths: (scopeNames) ->
|
|
scope = @characterWidthsByScope
|
|
for scopeName in scopeNames
|
|
scope[scopeName] ?= {}
|
|
scope = scope[scopeName]
|
|
scope.characterWidths ?= {}
|
|
scope.characterWidths
|
|
|
|
setScopedCharacterWidth: (scopeNames, character, width) ->
|
|
@getScopedCharacterWidths(scopeNames)[character] = width
|
|
|
|
pixelPositionForScreenPosition: (screenPosition) ->
|
|
screenPosition = Point.fromObject(screenPosition)
|
|
|
|
targetRow = screenPosition.row
|
|
targetColumn = screenPosition.column
|
|
baseCharacterWidth = @model.getDefaultCharWidth()
|
|
|
|
top = @lineTopIndex.pixelPositionAfterBlocksForRow(targetRow)
|
|
left = 0
|
|
column = 0
|
|
|
|
iterator = @model.tokenizedLineForScreenRow(targetRow).getTokenIterator()
|
|
while iterator.next()
|
|
characterWidths = @getScopedCharacterWidths(iterator.getScopes())
|
|
|
|
valueIndex = 0
|
|
text = iterator.getText()
|
|
while valueIndex < text.length
|
|
if iterator.isPairedCharacter()
|
|
char = text
|
|
charLength = 2
|
|
valueIndex += 2
|
|
else
|
|
char = text[valueIndex]
|
|
charLength = 1
|
|
valueIndex++
|
|
|
|
break if column is targetColumn
|
|
|
|
left += characterWidths[char] ? baseCharacterWidth unless char is '\0'
|
|
column += charLength
|
|
|
|
{top, left}
|