-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
235 lines (203 loc) · 8.59 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
235
/*jslint vars: true, nomen: true, devel: false*/
/*global define, brackets, $*/
define(function (require, exports, module) {
"use strict";
// Brackets modules
var CommandManager = brackets.getModule("command/CommandManager"),
FileSystem = brackets.getModule('filesystem/FileSystem'),
DocumentManager = brackets.getModule('document/DocumentManager'),
EditorManager = brackets.getModule('editor/EditorManager'),
Widgets = brackets.getModule('widgets/Dialogs'),
Menus = brackets.getModule("command/Menus"),
AppInit = brackets.getModule("utils/AppInit"),
ExtensionUtils = brackets.getModule('utils/ExtensionUtils'),
NodeConnection = brackets.getModule('utils/NodeConnection'),
ThemeManager = brackets.getModule('view/ThemeManager'),
Mustache = brackets.getModule("thirdparty/mustache/mustache"),
Dialogs = brackets.getModule("widgets/Dialogs"),
Commands = brackets.getModule("command/Commands"),
FileUtils = brackets.getModule("file/FileUtils");
// Local modules
var SettingsDialog = require("src/SettingsDialog"),
Preferences = require("src/Preferences"),
Strings = require("strings");
// Start node
var node = new NodeConnection();
// Load CSS
ExtensionUtils.loadStyleSheet(module, "styles/gofmt.less");
var GFT_CMD_ID = "gofmt.runfmt",
GFT_SETTINGS_CMD_ID = "gofmt.settings",
running = false;
/** Sets the icon to its original state */
function endGoFmt() {
var icon = $('#gofmt-icon');
icon.addClass('easeOut');
icon.removeClass('on');
running = false;
}
/** Shows dialg with an error message */
function showErrorDialog(errorMessage, args) {
if (typeof args !== 'undefined') {
var key;
for (key in args) {
if (args.hasOwnProperty(key)) {
errorMessage = errorMessage.replace("_" + key + "_", args[key]);
}
}
}
var errorDialogTemplate = require("text!templates/error-dialog.html");
var compiledTemplate = Mustache.render(errorDialogTemplate, {
title: Strings.ERROR_TITLE,
error: errorMessage,
Strings: Strings
});
Dialogs.showModalDialogUsingTemplate(compiledTemplate).done(function (buttonId) {
if (buttonId === "settings") {
CommandManager.execute(GFT_SETTINGS_CMD_ID);
}
});
endGoFmt();
}
/** Adds colors and <br> tags to a gofmt error message */
function formatGoErrors(message) {
var colorTheme = ThemeManager.getCurrentTheme().dark ? "dark" : "light";
return ('<span class="go-errors ' + colorTheme + '">') +
message.replace(/(\S+)\.f\.tmp\:(\d*?\:\d*?)\:/g,
'<span class="file-name">$1</span><span class="line-number"> $2 </span>')
.replace(/\n/g, '<br>') + '</span>';
}
/** Calls gofmt to format file */
function formatFile(tmpFile, tmpFilePath, fileBody, callback) {
tmpFile.exists(function (err, exists) {
if (!exists) {
tmpFile.write(fileBody);
node.domains.gofmt.formatFile(tmpFilePath, Preferences.get('gofmtPath')).done(function (data) {
var index = data.indexOf('gofmt');
if (index !== -1 && index < 15) {
tmpFile.unlink();
} else if (data.match(/\.f\.tmp\:\d*?\:\d*?\:/)) {
showErrorDialog(formatGoErrors(data));
} else {
callback(data);
}
tmpFile.unlink();
});
} else {
tmpFile.unlink();
endGoFmt();
}
});
}
/** Calls goimports to automatically add/remove imports */
function autoImport(tmpFile, tmpFilePath, fileBody, callback) {
tmpFile.exists(function (err, exists) {
if (!exists) {
tmpFile.write(fileBody);
var goImports = Preferences.get('goimportsPath');
var goPath = Preferences.get('goPath');
node.domains.goimports.autoImports(tmpFilePath, goImports, goPath).done(function (data) {
var index = data.indexOf('goimports');
if (index !== -1 && index < 30) {
showErrorDialog(data);
} else if (data.match(/\.i\.tmp\:\d*?\:\d*?\:/)) {
showErrorDialog(data);
} else {
callback(data);
}
tmpFile.unlink();
});
} else {
tmpFile.unlink();
endGoFmt();
}
});
}
/** The main function, called when clicking the button or pressing the shortcut */
function startGoFmt() {
if (running) {
return;
}
running = true;
var editor = EditorManager.getFocusedEditor() || EditorManager.getActiveEditor(),
currentDocument = DocumentManager.getCurrentDocument();
if (currentDocument === null) {
showErrorDialog(Strings.ERROR_NO_CURRENT_FILE);
return;
}
var extension = FileUtils.getFileExtension(currentDocument.file._path);
if (extension !== "go") {
showErrorDialog(Strings.ERROR_NOT_GO_FILE, {language: currentDocument.language._name});
return;
}
if (!editor.document) {
endGoFmt();
return;
}
if (typeof node.domains.gofmt === "undefined" || typeof node.domains.goimports === "undefined") {
// Happens sometimes (when brackets has a critical problem)
endGoFmt();
return;
}
var cursorPos = editor.getCursorPos(),
fileBody = editor.document.getText(),
useAutoImport = Preferences.get('useGoImports');
var tmpFilePathFormat = currentDocument.file._path + '.f.tmp';
var tmpFileFormat = FileSystem.getFileForPath(tmpFilePathFormat);
var tmpFilePathImport = currentDocument.file._path + '.i.tmp';
var tmpFileImport;
if (useAutoImport) {
tmpFileImport = FileSystem.getFileForPath(tmpFilePathImport);
}
formatFile(tmpFileFormat, tmpFilePathFormat, fileBody, function (formatted) {
if (useAutoImport) {
autoImport(tmpFileImport, tmpFilePathImport, formatted, function (imported) {
editor.selectAllNoScroll();
editor.document.setText(imported);
editor.setCursorPos(cursorPos.line, cursorPos.ch, true);
endGoFmt();
});
} else {
editor.selectAllNoScroll();
editor.document.setText(formatted);
editor.setCursorPos(cursorPos.line, cursorPos.ch, true);
endGoFmt();
}
});
}
/** Styles the gofmt icon */
function handleIconClick() {
var icon = $('#gofmt-icon');
icon.removeClass('easeOut');
if (!icon.hasClass('on')) {
icon.addClass('on');
}
startGoFmt();
}
/** Adds the icon in the toolbar */
function initGoFmt() {
var icon = $("<a href='#' id='gofmt-icon'> </a>");
icon.attr("title", Strings.FORMAT_THIS_FILE);
icon.on("click", handleIconClick);
icon.appendTo($("#main-toolbar").find(".buttons"));
}
if (!node.domains.gofmt || !node.domains.goimports) {
node.connect(true).done(function () {
var gofmtPath = ExtensionUtils.getModulePath(module, 'node/gofmt.js');
var goimportsPath = ExtensionUtils.getModulePath(module, 'node/goimports.js');
node.loadDomains([gofmtPath, goimportsPath], true).done(function () {
AppInit.appReady(initGoFmt);
});
});
} else {
AppInit.appReady(initGoFmt);
}
// Register commands and add them to the menu.
CommandManager.register(Strings.FORMAT_THIS_FILE, GFT_CMD_ID, handleIconClick);
CommandManager.register(Strings.SETTINGS_CMD, GFT_SETTINGS_CMD_ID, SettingsDialog.show);
var editMenu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
var fileMenu = Menus.getMenu(Menus.AppMenuBar.FILE_MENU);
fileMenu.addMenuItem(GFT_SETTINGS_CMD_ID, [], Menus.AFTER, Commands.FILE_PROJECT_SETTINGS);
editMenu.addMenuItem(GFT_CMD_ID, [
{key: Preferences.get('gofmtShortcut')}
]);
});