-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
304 lines (270 loc) · 7.91 KB
/
index.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
'use strict';
const Disposable = require('atom').Disposable;
const CompositeDisposable = require('atom').CompositeDisposable;
const connect = require('./lib/client');
const ed = require('./lib/editor');
const diffFactory = require('./lib/diff');
const readFile = require('./lib/read-file');
const globalDebug = require('./lib/debug');
const createAnalyzer = require('./lib/analyzer');
const autocompleteProvider = require('./lib/analyzer/autocomplete');
const pkg = require('./package.json');
const debug = globalDebug('LiveStyle');
const EDITOR_ID = 'atom';
var analyzer = null;
module.exports.activate = function(state) {
setupLogger();
setupAnalyzer();
connect(pkg.config.websocketUrl, (err, client) => {
if (err) {
return console.error('Unable to setup LiveStyle connection:', err);
}
console.info('LiveStyle client connected');
client
.on('open', () => {
debug('connection opened');
clientId(client);
editorId(client);
})
.on('client-connect', () => editorId(client))
.on('identify-client', () => clientId(client))
.on('patcher-connect', () => {
let editor = atom.workspace.getActiveTextEditor();
if (editor) {
initialContent(client, editor);
}
})
.on('incoming-updates', data => {
let editor = ed.editorForUri(data.uri);
if (editor) {
sendEditorPayload(client, editor, 'apply-patch', {
patches: data.patches
});
}
})
.on('patch', data => {
let editor = ed.editorForUri(data.uri);
if (editor) {
updateContent(client, editor, data);
}
})
.on('request-files', data => {
let files = data.files || [];
debug('requested deps', files);
Promise.all(files.map(f => readFile(f.uri)))
.catch(err => {
console.error('Error fetching preprocessor dependencies:', err);
return [];
})
.then(files => {
files = files.filter(f => f && f.content !== null);
debug('respond with deps', files);
client.send('files', {token: data.token, files});
});
})
.on('request-unsaved-changes', data => {
let files = data.files || [];
files.forEach(uri => {
let editor = ed.editorForUri(uri);
if (editor) {
sendUnsavedChanges(client, editor);
}
});
})
// supress 'error' event since in Node.js, in most cases it means unhandled exception
.on('error', err => console.error(err));
// observe editor life cycle
let diff = diffFactory(client);
let refresh = () => scheduleRefreshFiles(client);
atom.workspace.observeTextEditors(editor => {
refresh();
debug('add callbacks to', editor.getPath());
// `editor.onDidChange()` might be invoked multiple times during
// a single change. Instead of sending diff on each `didChange` and
// generating unnecessary CPU load, postpone a single 'diff' request
// on next event loop cycle
// NB `editor.onDidStopChanging()` event is too slow for real-time
// editing
let diffScheduled = false;
let scheduleDiff = () => {
if (!diffScheduled) {
process.nextTick(() => {
debug('diff', editor.getPath());
diffScheduled = false;
diff(editor);
});
diffScheduled = true;
}
}
if (ed.syntax(editor)) {
initialContent(client, editor);
}
let callbacks = [
editor.onDidChange(() => {
if (ed.syntax(editor) && !ed.isLocked(editor)) {
debug('editor did change');
scheduleDiff();
}
}),
editor.onDidSave(refresh),
editor.observeGrammar(refresh)
];
editor.onDidDestroy(() => {
callbacks.forEach(c => c.dispose());
callbacks = null;
refresh();
});
});
atom.workspace.onDidChangeActivePaneItem(item => {
if (isEditor(item) && ed.syntax(item)) {
// when uses focuses on supported editor, force update
// its initial content
initialContent(client, item);
}
});
});
};
module.exports.config = {
debugMode: {
title: 'Debug Mode',
description: 'Makes excessive logging into DevTools console, helps in finding bugs in plugin',
type: 'boolean',
default: false
},
analyzer: require('./lib/analyzer/config')
};
module.exports.deactivate = function() {
// TODO close server
debug('deactivate');
globalDebug.disable();
if (analyzer) {
analyzer.dispose();
analyzer = null;
}
};
module.exports.getProvider = () => autocompleteProvider;
/**
* Updates content of given editor with patched content from LiveStyle
* @param {TextEditor} editor
* @param {Object} payload
*/
function updateContent(client, editor, payload) {
if (!payload) {
return;
}
ed.lock(editor);
// unlock after some timeout to ensure that `onDidChange` event didn't
// triggered 'calculate-diff' event
setTimeout(() => ed.unlock(editor), 10);
if (payload.ranges.length && payload.hash === ed.hash(editor)) {
// integrity check: if editor content didn't changed since last patch
// request (e.g. content hashes are match), apply incremental updates
let buf = editor.getBuffer();
editor.transact(() => {
if (editor.hasMultipleCursors()) {
// reset multiple selections into a single cursor pos
let pos = editor.getCursorBufferPosition();
editor.setCursorBufferPosition(pos, {autoscroll: false});
}
let opt = {undo: 'skip'};
payload.ranges.forEach(r => {
buf.setTextInRange([
buf.positionForCharacterIndex(r[0]),
buf.positionForCharacterIndex(r[1])
], r[2], opt);
});
// select last range
let lastRange = payload.ranges[payload.ranges.length - 1];
editor.setSelectedBufferRange([
buf.positionForCharacterIndex(lastRange[0]),
buf.positionForCharacterIndex(lastRange[0] + lastRange[2].length)
]);
});
} else {
// user changed content since last patch request: replace whole content
editor.setText(payload.content || '');
}
// update initial content for current view in LiveStyle cache
initialContent(client, editor);
}
function sendUnsavedChanges(client, editor) {
if (editor.isModified()) {
var previous = editor.getBuffer().cachedDiskContents || '';
debug('send unsaved changes for', ed.fileUri(editor), {previous});
sendEditorPayload(client, editor, 'calculate-diff', {previous});
}
}
function setupLogger() {
let key = `${pkg.name}.debugMode`;
let toggle = val => val ? globalDebug.enable() : globalDebug.disable();
toggle(atom.config.get(key));
atom.config.onDidChange(key, evt => toggle(evt.newValue));
}
function setupAnalyzer() {
if (!analyzer) {
let analyzerInstance = null;
analyzer = new CompositeDisposable();
analyzer.add(
atom.config.observe(`${pkg.name}.analyzer.enabled`, enabled => {
if (enabled && !analyzerInstance) {
analyzerInstance = createAnalyzer();
} else if (!enabled && analyzerInstance) {
analyzerInstance.dispose();
analyzerInstance = null;
}
}),
new Disposable(() => {
if (analyzerInstance) {
analyzerInstance.dispose();
analyzerInstance = null;
}
})
);
}
}
////////////////////////////////////////
function editorId(client) {
debug('send editor id');
client.send('editor-connect', {
id: EDITOR_ID,
title: 'Atom'
});
scheduleRefreshFiles(client);
}
function clientId(client) {
debug('send client id');
client.send('client-id', {id: EDITOR_ID});
}
function initialContent(client, editor) {
let syntax = ed.syntax(editor);
if (syntax) {
sendEditorPayload(client, editor, 'initial-content');
}
}
function sendEditorPayload(client, editor, message, data) {
ed.payload(editor, data)
.then(payload => {
debug('send', message, payload);
client.send(message, payload);
});
}
function refreshFiles(client) {
let files = ed.all().map(editor => ed.fileUri(editor)).filter(unique);
debug('send file list', files);
client.send('editor-files', {id: EDITOR_ID, files});
}
function isEditor(obj) {
return obj && 'getPath' in obj && 'getRootScopeDescriptor' in obj;
}
var _refreshFilesTimer = null;
function scheduleRefreshFiles(client) {
if (!_refreshFilesTimer) {
_refreshFilesTimer = setImmediate(() => {
refreshFiles(client);
_refreshFilesTimer = null;
});
}
}
function unique(value, i, array) {
return array.indexOf(value) === i;
}