-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
234 lines (202 loc) · 6.61 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
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
/*
* Copyright (c) 2014 Drew Fyock
*/
define(function(require, exports, module) {
'use strict';
/* Globals */
var COMMAND_ID = 'steelbisondev.fixmyjs',
COMMAND_SAVE_ID = COMMAND_ID + '.autosave',
COMMAND_TIMESTAMP = COMMAND_ID + '-timeStamp',
CONTEXTUAL_COMMAND_ID = COMMAND_ID + 'Contextual';
var Menus = brackets.getModule('command/Menus'),
AppInit = brackets.getModule('utils/AppInit'),
Commands = brackets.getModule('command/Commands'),
Editor = brackets.getModule('editor/Editor').Editor,
FileSystem = brackets.getModule('filesystem/FileSystem'),
EditorManager = brackets.getModule('editor/EditorManager'),
CommandManager = brackets.getModule('command/CommandManager'),
ProjectManager = brackets.getModule('project/ProjectManager'),
DocumentManager = brackets.getModule('document/DocumentManager'),
PreferencesManager = brackets.getModule('preferences/PreferencesManager');
/* thirdparty */
require('thirdparty/fixmyjs/lib/legacy');
var fixmyjs = fixMyJS;
require('thirdparty/jshint/dist/jshint');
var jshint = JSHINT;
/* Variables */
var fixmyjsOnSave,
settingsFileName = '.fixmyjsrc',
menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU),
settings = JSON.parse(require('text!settings.json')),
debugPreferences = PreferencesManager.getExtensionPrefs('debug'),
fixmyjsPreferences = PreferencesManager.getExtensionPrefs(COMMAND_ID),
windowsCommand = {
key: 'Alt-Shift-L',
platform: 'win'
},
macCommand = {
key: 'Alt-Shift-L',
platform: 'mac'
},
command = [windowsCommand, macCommand];
// Brackets debug mode
var DEBUG_MODE = debugPreferences.get('showErrorsInStatusBar');
fixmyjsOnSave = fixmyjsPreferences.get('on_save') || false;
if (!fixmyjsOnSave) {
fixmyjsPreferences.set('on_save', false);
fixmyjsPreferences.save();
}
function __debug(msg, code) {
if (DEBUG_MODE) {
var m = '[brackets-fixmyjs] :: ' + msg;
if (typeof msg !== 'string') {
m = msg;
}
if (code === 0) {
console.log(m);
} else {
console.error(m);
}
}
}
/**
*
* @param {String} unformattedText
* @param {String} indentChar
* @param {String} indentSize
*/
function _fixmyjs(unformattedText, indentChar, indentSize) {
var options = {
'indent_size': indentSize,
'indent_char': indentChar
};
var data = unformattedText;
var opts = $.extend(options, settings);
try {
if (opts.legacy) {
jshint(data, opts);
return fixmyjs(jshint.data(), data, opts).run();
} else {
return fixmyjs.fix(data, opts);
}
} catch (err) {
throw err;
}
}
function batchUpdate(formattedText, isSelection) {
var editor = EditorManager.getCurrentFullEditor();
var cursor = editor.getCursorPos();
var scroll = editor.getScrollPos();
var doc = DocumentManager.getCurrentDocument();
var selection = editor.getSelection();
doc.batchOperation(function() {
if (isSelection) {
doc.replaceRange(formattedText, selection.start, selection.end);
} else {
doc.setText(formattedText);
}
editor.setCursorPos(cursor);
editor.setScrollPos(scroll.x, scroll.y);
});
}
/**
* Format
*/
function format(autoSave) {
var indentChar, indentSize, formattedText;
var unformattedText, isSelection = false;
var useTabs = Editor.getUseTabChar();
__debug(settings, 0);
if (useTabs) {
indentChar = '\t';
indentSize = 1;
} else {
indentChar = ' ';
indentSize = Editor.getSpaceUnits();
}
var editor = EditorManager.getCurrentFullEditor();
var selectedText = editor.getSelectedText();
if (selectedText.length > 0) {
isSelection = true;
unformattedText = selectedText;
} else {
unformattedText = DocumentManager.getCurrentDocument().getText();
}
var doc = DocumentManager.getCurrentDocument();
var language = doc.getLanguage();
var fileType = language._id;
switch (fileType) {
case 'javascript':
case 'json':
formattedText = _fixmyjs(unformattedText, indentChar, indentSize);
batchUpdate(formattedText, isSelection);
break;
default:
if (!autoSave) {
alert('Could not determine file type');
}
return;
}
}
function onSave(event, doc) {
if ((event.timeStamp - localStorage.getItem(COMMAND_TIMESTAMP)) > 1000) {
format(true);
localStorage.setItem(COMMAND_TIMESTAMP, event.timeStamp);
CommandManager.execute(Commands.FILE_SAVE, {
doc: doc
});
}
}
function loadConfig() {
var settingsFile = FileSystem.getFileForPath(ProjectManager.getProjectRoot().fullPath + settingsFileName);
settingsFile.read(function(err, content) {
if (content) {
try {
settings = JSON.parse(content);
__debug('settings loaded' + settings, 0);
} catch (e) {
__debug('error parsing ' + settingsFile + '. Details: ' + e);
return;
}
}
});
}
function toggle(command, fromCheckbox) {
var newValue = (typeof fromCheckbox === 'undefined') ? fixmyjsOnSave : fromCheckbox;
$(DocumentManager)[newValue ? 'on' : 'off']('documentSaved', onSave);
command.setChecked(newValue);
fixmyjsPreferences.set('on_save', newValue);
fixmyjsPreferences.save();
}
/**
* File menu
*/
CommandManager.register('FixMyJS', COMMAND_ID, format);
var commandOnSave = CommandManager.register('FixMyJS on save', COMMAND_SAVE_ID, function() {
toggle(this, !this.getChecked());
if (this.getChecked()) {
localStorage.setItem(COMMAND_TIMESTAMP, 0);
}
});
/**
* Contextual menu
*/
CommandManager.register('FixMyJS', CONTEXTUAL_COMMAND_ID, format);
var contextMenu = Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU);
contextMenu.addMenuItem(CONTEXTUAL_COMMAND_ID);
toggle(commandOnSave);
menu.addMenuDivider();
menu.addMenuItem(COMMAND_ID, command);
menu.addMenuItem(COMMAND_SAVE_ID);
AppInit.appReady(function() {
$(DocumentManager).on('documentRefreshed.fixmyjs', function(e, document) {
if (document.file.fullPath === ProjectManager.getProjectRoot().fullPath + settingsFileName) {
loadConfig();
}
});
$(ProjectManager).on('projectOpen.fixmyjs', function() {
loadConfig();
});
loadConfig();
});
});