-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.js
190 lines (152 loc) · 6.03 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
/**
* Interactive Linter Copyright (c) 2015 Miguel Castillo.
*
* Licensed under MIT
*/
define(function (require, exports, module) {
"use strict";
var _ = brackets.getModule("thirdparty/lodash"),
EditorManager = brackets.getModule("editor/EditorManager"),
CodeInspection = brackets.getModule("language/CodeInspection"),
AppInit = brackets.getModule("utils/AppInit"),
KeyBindingManager = brackets.getModule("command/KeyBindingManager"),
Commands = brackets.getModule("command/Commands"),
CommandManager = brackets.getModule("command/CommandManager"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
preferences = PreferencesManager.getExtensionPrefs("interactive-linter"),
linterManager = require("linterManager"),
pluginManager = require("pluginManager");
require("lintIndicator");
require("lintPanel");
require("linterSettings");
var bracketsLinterEnabled = true;
var CMD_SHOW_LINE_DETAILS = "MiguelCastillo.interactive-linter.showLineDetails";
var SHORTCUT_KEY = "Ctrl-Shift-E";
var linter;
ExtensionUtils.loadStyleSheet(module, "style.less");
KeyBindingManager.addBinding(CMD_SHOW_LINE_DETAILS, SHORTCUT_KEY);
CommandManager.register("Show Line Details", CMD_SHOW_LINE_DETAILS, handleToggleLineDetails);
AppInit.appReady(appReady);
function addGutter(editor) {
var cm = editor._codeMirror;
var gutters = cm.getOption("gutters").slice(0);
if (gutters.indexOf("interactive-linter-gutter") === -1) {
gutters.unshift("interactive-linter-gutter");
cm.setOption("gutters", gutters);
}
}
/**
* Show line details
*/
function gutterClick(cm, lineIndex, gutterId) {
if (gutterId === "interactive-linter-gutter") {
linter.reporter.toggleLineDetails(lineIndex);
}
}
function activateEditor(editor) {
editor._codeMirror.on("gutterClick", gutterClick);
editor.on("editorChange", handleDocumentChange);
}
function deactivateEditor(editor) {
editor._codeMirror.off("gutterClick", gutterClick);
editor.off("editorChange", handleDocumentChange);
}
/**
* Toggles open/close the inline details widget.
*/
function handleToggleLineDetails() {
if (linter) {
linter.reporter.toggleLineDetails();
}
}
/**
* Function to cause a lint operation
*/
function handleDocumentChange() {
if (linter && linter.canProcess()) {
linter.lint();
setTimeout(disableBracketsIndicator);
}
else {
linter.clear();
setTimeout(enableBracketsIndicator);
$(linterManager).triggerHandler("linterNotFound");
}
}
/**
* Function to disable the native linter indicator in Brackets. This is
* because interactive linter has its own indicator and we don't want any
* conflicts.
*/
function disableBracketsIndicator() {
CodeInspection.toggleEnabled(false, true);
var command = CommandManager.get(Commands.VIEW_TOGGLE_INSPECTION);
bracketsLinterEnabled = command.getChecked();
command.setChecked(false);
command.setEnabled(false);
$("#status-inspection").hide();
$("#interactive-linter-lint-indicator").show();
}
/**
* Function that enables the native linter indicator when interactive
* linter is not active.
*/
function enableBracketsIndicator() {
var command = CommandManager.get(Commands.VIEW_TOGGLE_INSPECTION);
command.setChecked(bracketsLinterEnabled);
command.setEnabled(true);
$("#status-inspection").show();
$("#interactive-linter-lint-indicator").hide();
CodeInspection.toggleEnabled(true, true);
}
/**
* Function that gets called when there is a new document that needs
* interactive linter registered on.
*/
function setDocument(evt, currentEditor, previousEditor) {
if (previousEditor) {
deactivateEditor(previousEditor);
}
if (currentEditor) {
linter = currentEditor.__linter || (currentEditor.__linter = linterManager.createLintRunner(currentEditor));
// If a linter was successfully created, then we are safe to bind event handlers for editor changes.
activateEditor(currentEditor);
addGutter(currentEditor);
handleDocumentChange();
}
}
/**
* Function that gets called when Brackets is loaded and ready
*/
function appReady() {
// Removes the default Brackets JSLint linter
CodeInspection.register("javascript", {
name: "interactive-linter-remove-jslint",
scanFile: $.noop
});
// Load up plugins and wait til they are done loading before we
// register any handlers into Brackets
pluginManager().done(function(plugins) {
for (var iPlugin in plugins) {
linterManager.registerLinter(plugins[iPlugin]);
}
EditorManager.on("activeEditorChange.interactive-linter", setDocument);
setDocument(null, EditorManager.getActiveEditor());
// If the linters change, then make sure to rebind the document with the new linter
var lastLinters;
preferences.on("change", function() {
var editor = EditorManager.getActiveEditor();
if (!editor) {
return;
}
var language = editor.document.getLanguage().getId();
var linters = preferences.get(language);
if (linters && !_.isEqual(linters, lastLinters)) {
lastLinters = linters.slice(0);
handleDocumentChange();
}
});
});
}
});