1 {CompositeDisposable} = require 'atom'
5 constructor: (@editor) ->
6 @subscriptions = new CompositeDisposable()
7 @editorView = atom.views.getView(@editor)
8 @trueNumberCurrentLine = atom.config.get('relative-line-numbers.trueNumberCurrentLine')
9 @showNormalLineNumbers = atom.config.get('relative-line-numbers.showNormalLineNumbers')
11 # Subscribe for when the line numbers should be updated.
12 @subscriptions.add(@editor.onDidChangeCursorPosition(@_update))
13 @subscriptions.add(@editor.onDidStopChanging(@_update))
15 # Subscribe to when the true number on current line config is modified.
16 @subscriptions.add atom.config.onDidChange 'relative-line-numbers.trueNumberCurrentLine', =>
17 @trueNumberCurrentLine = atom.config.get('relative-line-numbers.trueNumberCurrentLine')
20 # Subscribe to when the show normal line numbers config is modified.
21 @subscriptions.add atom.config.onDidChange 'relative-line-numbers.showNormalLineNumbers', =>
22 @showNormalLineNumbers = atom.config.get('relative-line-numbers.showNormalLineNumbers')
25 # Dispose the subscriptions when the editor is destroyed.
26 @subscriptions.add @editor.onDidDestroy =>
27 @subscriptions.dispose()
31 _spacer: (totalLines, currentIndex) ->
32 Array(totalLines.toString().length - currentIndex.toString().length + 1).join ' '
34 # Update the line numbers on the editor
36 totalLines = @editor.getLineCount()
37 currentLineNumber = @editor.getCursorScreenPosition().row + 1
38 lineNumberElements = @editorView.rootElement?.querySelectorAll('.line-number')
40 index = @_index(totalLines, currentLineNumber)
42 for lineNumberElement in lineNumberElements
43 row = lineNumberElement.getAttribute('data-buffer-row')
44 relative = index[row] or = 0
45 normalLineNumbers = ''
46 if @showNormalLineNumbers
47 humanRow = parseInt(row) + 1
48 normalLineNumbers = humanRow + @_spacer(totalLines, humanRow) + " "
49 lineNumberElement.innerHTML = "#{normalLineNumbers}#{relative}<div class=\"icon-right\"></div>"
51 # Return a lookup array with the relative line numbers
52 _index: (totalLines, currentLineNumber) ->
53 for line in [0...totalLines]
54 lineNumber = (Math.abs(currentLineNumber - (line + 1)))
55 if @trueNumberCurrentLine and lineNumber == 0
56 if @showNormalLineNumbers