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 18
/
Copy pathmain.js
176 lines (156 loc) · 5.09 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use babel'
import _ from 'underscore-plus'
import {CompositeDisposable, Disposable} from 'atom'
import SelectListView from 'atom-select-list'
import StatusBarItem from './status-bar-item'
import helpers from './helpers'
const LineEndingRegExp = /\r\n|\n/g
const LFRegExp = /(\A|[^\r])\n/g
const CRLFRegExp = /\r\n/g
let disposables = null
let modalPanel = null
let lineEndingListView = null
export function activate () {
disposables = new CompositeDisposable()
disposables.add(atom.commands.add('atom-text-editor', {
'line-ending-selector:show': (event) => {
if (!modalPanel) {
lineEndingListView = new SelectListView({
items: [{name: 'LF', value: '\n'}, {name: 'CRLF', value: '\r\n'}],
filterKeyForItem: (lineEnding) => lineEnding.name,
didConfirmSelection: (lineEnding) => {
setLineEnding(atom.workspace.getActiveTextEditor(), lineEnding.value)
modalPanel.hide()
},
didCancelSelection: () => {
modalPanel.hide()
},
elementForItem: (lineEnding) => {
const element = document.createElement('li')
element.textContent = lineEnding.name
return element
}
})
modalPanel = atom.workspace.addModalPanel({item: lineEndingListView})
disposables.add(new Disposable(() => {
lineEndingListView.destroy()
modalPanel.destroy()
modalPanel = null
}))
}
lineEndingListView.reset()
modalPanel.show()
lineEndingListView.focus()
},
'line-ending-selector:convert-to-LF': (event) => {
const editorElement = event.target.closest('atom-text-editor')
setLineEnding(editorElement.getModel(), '\n')
},
'line-ending-selector:convert-to-CRLF': (event) => {
const editorElement = event.target.closest('atom-text-editor')
setLineEnding(editorElement.getModel(), '\r\n')
}
}))
}
export function deactivate () {
disposables.dispose()
}
export function consumeStatusBar (statusBar) {
let statusBarItem = new StatusBarItem()
let currentBufferDisposable = null
let tooltipDisposable = null
const updateTile = _.debounce((buffer) => {
getLineEndings(buffer).then((lineEndings) => {
if (lineEndings.size === 0) {
let defaultLineEnding = getDefaultLineEnding()
buffer.setPreferredLineEnding(defaultLineEnding)
lineEndings = new Set().add(defaultLineEnding)
}
statusBarItem.setLineEndings(lineEndings)
})
}, 0)
disposables.add(atom.workspace.observeActiveTextEditor((editor) => {
if (currentBufferDisposable) currentBufferDisposable.dispose()
if (editor && editor.getBuffer) {
let buffer = editor.getBuffer()
updateTile(buffer)
currentBufferDisposable = buffer.onDidChange(({oldText, newText}) => {
if (!statusBarItem.hasLineEnding('\n')) {
if (newText.indexOf('\n') >= 0) {
updateTile(buffer)
}
} else if (!statusBarItem.hasLineEnding('\r\n')) {
if (newText.indexOf('\r\n') >= 0) {
updateTile(buffer)
}
} else if (oldText.indexOf('\n')) {
updateTile(buffer)
}
})
} else {
statusBarItem.setLineEndings(new Set())
currentBufferDisposable = null
}
if (tooltipDisposable) {
disposables.remove(tooltipDisposable)
tooltipDisposable.dispose()
}
tooltipDisposable = atom.tooltips.add(statusBarItem.element, {
title () {
return `File uses ${statusBarItem.description()} line endings`
}
})
disposables.add(tooltipDisposable)
}))
disposables.add(new Disposable(() => {
if (currentBufferDisposable) currentBufferDisposable.dispose()
}))
statusBarItem.onClick(() => {
const editor = atom.workspace.getActiveTextEditor()
atom.commands.dispatch(
atom.views.getView(editor),
'line-ending-selector:show'
)
})
let tile = statusBar.addRightTile({item: statusBarItem, priority: 200})
disposables.add(new Disposable(() => tile.destroy()))
}
function getDefaultLineEnding () {
switch (atom.config.get('line-ending-selector.defaultLineEnding')) {
case 'LF':
return '\n'
case 'CRLF':
return '\r\n'
case 'OS Default':
default:
return (helpers.getProcessPlatform() === 'win32') ? '\r\n' : '\n'
}
}
function getLineEndings (buffer) {
if (typeof buffer.find === 'function') {
return Promise.all([
buffer.find(LFRegExp),
buffer.find(CRLFRegExp)
]).then(([hasLF, hasCRLF]) => {
const result = new Set()
if (hasLF) result.add('\n')
if (hasCRLF) result.add('\r\n')
return result
})
} else {
return new Promise((resolve) => {
const result = new Set()
for (let i = 0; i < buffer.getLineCount() - 1; i++) {
result.add(buffer.lineEndingForRow(i))
}
resolve(result)
})
}
}
function setLineEnding (item, lineEnding) {
if (item && item.getBuffer) {
let buffer = item.getBuffer()
buffer.setPreferredLineEnding(lineEnding)
buffer.setText(buffer.getText().replace(LineEndingRegExp, lineEnding))
}
}