This repository has been archived by the owner on Jan 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
126 lines (103 loc) · 3.74 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
define(function (require, exports, module) {
'use strict';
var CommandManager = brackets.getModule("command/CommandManager"),
Commands = brackets.getModule('command/Commands'),
EditorManager = brackets.getModule("editor/EditorManager"),
DocumentManager = brackets.getModule("document/DocumentManager"),
PanelManager = brackets.getModule("view/PanelManager"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
NodeConnection = brackets.getModule("utils/NodeConnection"),
Menus = brackets.getModule("command/Menus"),
node = new NodeConnection(),
domainPath = ExtensionUtils.getModulePath(module) + "domain",
tidyPath = ExtensionUtils.getModulePath(module) + "python-tidy.py";
var panel,
panelHTML = require('text!pythontidy-panel.html'),
autosave = (localStorage["pythontidy.autosave"] === "true");
var COMMAND_ID = "com.josecols.PythonTidy",
COMMAND_NAME = "Python Tidy";
function _processStdout(formattedText, document) {
var editor = EditorManager.getCurrentFullEditor();
var cursor = editor.getCursorPos();
var scroll = editor.getScrollPos();
formattedText = JSON.parse(JSON.stringify(formattedText).replace(/\\r/g, ''));
if (document.getText() != formattedText) {
document.setText(formattedText);
CommandManager.execute(Commands.FILE_SAVE, {
doc: document
});
editor.setCursorPos(cursor);
editor.setScrollPos(scroll.x, scroll.y);
}
}
function _processStderror(error) {
error = JSON.stringify(error);
error = error.replace(/\\n/g, '<br>').replace(/\"/g, '').replace(/\\t/g, '').replace(/\\r/g, '');
return panelHTML.replace("{{content}}", error);
}
function _processPanel(panel) {
panel.show();
$('.pythontidy-panel .close').on('click', function () {
panel.hide();
});
}
function tidy() {
var document = DocumentManager.getCurrentDocument(),
directory = document.file._parentPath,
file = document.file._path,
language = document.language._name,
cmd = '';
if ('python' === language.toLowerCase()) {
node.connect(true).fail(function (error) {
console.error("Python Tidy cannot connect to node: ", error);
}).then(function () {
return node.loadDomains([domainPath], true).fail(function (error) {
console.error("Python Tidy cannot register domain: ", error);
});
}).then(function () {
cmd = 'python "' + tidyPath + '" "' + file + '"';
}).then(function () {
node.domains["python-tidy.execute"].exec(directory, cmd)
.fail(function (error) {
panel = PanelManager.createBottomPanel("pythontidy-panel", $(_processStderror(error)));
_processPanel(panel);
})
.then(function (formattedText) {
_processStdout(formattedText, document);
});
}).done();
}
}
$(DocumentManager).on("documentSaved", function (event, document) {
if (autosave) {
var fileExtension = document.file.name.split(".").pop().toLowerCase();
if (fileExtension === "py" || fileExtension === "python") {
tidy();
}
}
});
CommandManager.register(COMMAND_NAME, COMMAND_ID, tidy);
CommandManager.register(COMMAND_NAME + " on Save", COMMAND_ID + ".autosave", function () {
this.setChecked(!this.getChecked());
});
var autosaveItem = CommandManager.get(COMMAND_ID + ".autosave");
autosaveItem.setChecked(autosave);
$(autosaveItem).on('checkedStateChange', function () {
autosave = autosaveItem.getChecked();
localStorage["pythontidy.autosave"] = autosave;
});
var windowsCommand = {
key: "Ctrl-P",
platform: "win"
};
var macCommand = {
key: "Cmd-P",
platform: "mac"
};
var command = [windowsCommand, macCommand],
menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
menu.addMenuDivider();
menu.addMenuItem(COMMAND_ID, command);
menu.addMenuItem(autosaveItem);
ExtensionUtils.loadStyleSheet(module, "python-tidy.css");
});