This repository has been archived by the owner on Jan 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.js
executable file
·50 lines (40 loc) · 1.86 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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets */
// When code is pasted in the editor, re-indent the changed lines.
define(function (require, exports, module) {
"use strict";
var DocumentManger = brackets.getModule("document/DocumentManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
prefs = PreferencesManager.getExtensionPrefs("brackets-paste-and-indent");
// Define the `enabled` preference, default is `true`.
prefs.definePreference("enabled", "boolean", "true");
// Re-indent the editor in between specific lines. These are batched into
// one update.
function reindentLines(codeMirror, lineFrom, lineTo) {
codeMirror.operation(function () {
codeMirror.eachLine(lineFrom, lineTo, function (lineHandle) {
codeMirror.indentLine(lineHandle.lineNo(), "smart");
});
});
}
// When the Brackets document changes, attach an event listener for paste
// events on its internal codeMirror object.
$(DocumentManger).on("currentDocumentChange", function () {
var editor = EditorManager.getCurrentFullEditor();
if (!editor) {
return;
}
var codeMirror = editor._codeMirror;
// Listen for change events. If this change is not a 'paste', or the
// extension is disabled, return early.
codeMirror.on("change", function (codeMirror, change) {
if (!prefs.get("enabled") || change.origin !== "paste") {
return;
}
var lineFrom = change.from.line,
lineTo = change.from.line + change.text.length;
reindentLines(codeMirror, lineFrom, lineTo);
});
});
});