forked from scriptin/brackets-indent-softwraps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·73 lines (65 loc) · 2.38 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
define(function (require, exports, module) {
"use strict";
var ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
AppInit = brackets.getModule("utils/AppInit"),
EditorManager = brackets.getModule("editor/EditorManager"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager");
ExtensionUtils.loadStyleSheet(module, "main.css");
var DEFAULT_GUTTER_WIDTH = 15;
function indentSoftWraps(editor) {
if (!editor) return;
editor._codeMirror.on("renderLine", function (cm, line, elt) {
var firstNonSpace = line.text.search(/\S/);
if (firstNonSpace > -1) {
var whitespace = line.text.substr(0, firstNonSpace),
offset = countSpaces(whitespace, cm.getOption("tabSize")) * cm.defaultCharWidth(),
eltStyle = window.getComputedStyle(elt, null),
textIndent = -offset + "px",
noGutter = !PreferencesManager.get("showLineNumbers"),
paddingLeft = (noGutter ? DEFAULT_GUTTER_WIDTH : 0) + offset + "px";
if (textIndent != eltStyle["text-indent"]) {
elt.style.textIndent = textIndent;
}
if (paddingLeft != eltStyle["padding-left"]) {
elt.style.paddingLeft = paddingLeft;
}
}
addClass(elt, "softwraps-indented");
});
editor.refresh();
}
function countSpaces(ws, tabSize) {
var fullTabs = 0, remainderSpaces = 0;
for (var i = 0; i < ws.length; i++) {
if (ws[i] === "\t") {
fullTabs++;
remainderSpaces = 0;
} else { // Assuming all other whitespace chars are just spaces
remainderSpaces++;
if (remainderSpaces === tabSize) {
fullTabs++;
remainderSpaces = 0;
}
}
}
return fullTabs * tabSize + remainderSpaces;
}
function hasClass(elt, className) {
return (new RegExp("\\b" + className + "\\b")).test(elt.className);
}
function addClass(elt, newClassName) {
if (!hasClass(elt, newClassName)) {
if (elt.className.trim() === "") {
elt.className = newClassName;
} else {
elt.className += " " + newClassName;
}
}
}
AppInit.appReady(function () {
indentSoftWraps(EditorManager.getCurrentFullEditor());
$(EditorManager).on("activeEditorChange", function (event, focusedEditor, lostEditor) {
indentSoftWraps(focusedEditor);
});
});
});