This repository has been archived by the owner on Jan 23, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
67 lines (56 loc) · 1.82 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
/** @babel */
import {CompositeDisposable} from 'atom';
import fixmyjs from 'fixmyjs';
import jshintCli from 'jshint/src/cli';
function init(editor) {
const file = editor.getURI();
const config = file ? jshintCli.getConfig(file) : {};
const selectedText = editor.getSelectedText();
const text = selectedText || editor.getText();
let retText = '';
try {
if (atom.config.get('fixmyjs.legacy')) {
const jshint = require('jshint').JSHINT;
jshint(text, config);
retText = fixmyjs(jshint.data(), text).run();
} else {
retText = fixmyjs.fix(text, config);
}
} catch (err) {
console.error(err);
atom.notifications.addError('FixMyJS', {detail: err.message});
return;
}
const cursorPosition = editor.getCursorBufferPosition();
const line = atom.views.getView(editor).getFirstVisibleScreenRow() +
editor.getVerticalScrollMargin();
if (selectedText) {
editor.setTextInBufferRange(editor.getSelectedBufferRange(), retText);
} else {
editor.setText(retText);
editor.getBuffer().setTextViaDiff(retText);
}
editor.setCursorBufferPosition(cursorPosition);
if (editor.getScreenLineCount() > line) {
editor.scrollToScreenPosition([line, 0]);
}
}
export const config = {
legacy: {
type: 'boolean',
default: true,
description: 'Legacy mode uses the last stable version of the module which uses JSHint to detect errors in your code and fix them. It does not include all of the fixes the current version of FixMyJS exposes, but does do a much better job of preserving source formatting.'
}
};
export function deactivate() {
this.subscriptions.dispose();
}
export function activate() {
this.subscriptions = new CompositeDisposable();
this.subscriptions.add(atom.commands.add('atom-workspace', 'FixMyJS', () => {
const editor = atom.workspace.getActiveTextEditor();
if (editor) {
init(editor);
}
}));
}