This repository has been archived by the owner on Oct 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
go-to-line-view.js
105 lines (91 loc) · 2.92 KB
/
go-to-line-view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use babel'
import { Point, TextEditor } from 'atom'
class GoToLineView {
constructor () {
this.miniEditor = new TextEditor({ mini: true })
this.miniEditor.element.addEventListener('blur', this.close.bind(this))
this.message = document.createElement('div')
this.message.classList.add('message')
this.element = document.createElement('div')
this.element.classList.add('go-to-line')
this.element.appendChild(this.miniEditor.element)
this.element.appendChild(this.message)
this.panel = atom.workspace.addModalPanel({
item: this,
visible: false
})
atom.commands.add('atom-text-editor', 'go-to-line:toggle', () => {
this.toggle()
return false
})
atom.commands.add(this.miniEditor.element, 'core:confirm', () => {
this.navigate()
})
atom.commands.add(this.miniEditor.element, 'core:cancel', () => {
this.close()
})
this.miniEditor.onWillInsertText((arg) => {
if (arg.text.match(/[^0-9:]/)) {
arg.cancel()
}
})
this.miniEditor.onDidChange(() => {
this.navigate({keepOpen: true})
})
}
toggle () {
this.panel.isVisible() ? this.close() : this.open()
}
close () {
if (!this.panel.isVisible()) return
this.miniEditor.setText('')
this.panel.hide()
if (this.miniEditor.element.hasFocus()) {
this.restoreFocus()
}
}
navigate (options = {}) {
const lineNumber = this.miniEditor.getText()
const editor = atom.workspace.getActiveTextEditor()
if (!options.keepOpen) {
this.close()
}
if (!editor || !lineNumber.length) return
const currentRow = editor.getCursorBufferPosition().row
const rowLineNumber = lineNumber.split(/:+/)[0] || ''
const row = rowLineNumber.length > 0 ? parseInt(rowLineNumber) - 1 : currentRow
const columnLineNumber = lineNumber.split(/:+/)[1] || ''
const column = columnLineNumber.length > 0 ? parseInt(columnLineNumber) - 1 : -1
const position = new Point(row, column)
editor.setCursorBufferPosition(position)
editor.unfoldBufferRow(row)
if (column < 0) {
editor.moveToFirstCharacterOfLine()
}
editor.scrollToBufferPosition(position, {
center: true
})
}
storeFocusedElement () {
this.previouslyFocusedElement = document.activeElement
return this.previouslyFocusedElement
}
restoreFocus () {
if (this.previouslyFocusedElement && this.previouslyFocusedElement.parentElement) {
return this.previouslyFocusedElement.focus()
}
atom.views.getView(atom.workspace).focus()
}
open () {
if (this.panel.isVisible() || !atom.workspace.getActiveTextEditor()) return
this.storeFocusedElement()
this.panel.show()
this.message.textContent = 'Enter a <row> or <row>:<column> to go there. Examples: "3" for row 3 or "2:7" for row 2 and column 7'
this.miniEditor.element.focus()
}
}
export default {
activate () {
return new GoToLineView()
}
}