]> git.r.bdr.sh - rbdr/dotfiles/blame_incremental - atom/packages/relative-line-numbers/lib/line-number-view.coffee
Update vim plugins
[rbdr/dotfiles] / atom / packages / relative-line-numbers / lib / line-number-view.coffee
... / ...
CommitLineData
1{CompositeDisposable} = require 'atom'
2
3module.exports =
4class LineNumberView
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')
10
11 # Subscribe for when the line numbers should be updated.
12 @subscriptions.add(@editor.onDidChangeCursorPosition(@_update))
13 @subscriptions.add(@editor.onDidStopChanging(@_update))
14
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')
18 @_update()
19
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')
23 @_update()
24
25 # Dispose the subscriptions when the editor is destroyed.
26 @subscriptions.add @editor.onDidDestroy =>
27 @subscriptions.dispose()
28
29 @_update()
30
31 _spacer: (totalLines, currentIndex) ->
32 Array(totalLines.toString().length - currentIndex.toString().length + 1).join ' '
33
34 # Update the line numbers on the editor
35 _update: () =>
36 totalLines = @editor.getLineCount()
37 currentLineNumber = @editor.getCursorScreenPosition().row + 1
38 lineNumberElements = @editorView.rootElement?.querySelectorAll('.line-number')
39
40 index = @_index(totalLines, currentLineNumber)
41
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>"
50
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
57 '•'
58 else
59 currentLineNumber
60 else
61 lineNumber