This repository has been archived by the owner on Jan 23, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
75 lines (63 loc) · 2.2 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
/*jslint nomen: true, vars: true */
/*global define, brackets*/
define(function (require, exports, module) {
'use strict';
// Brackets modules
var EditorManager = brackets.getModule('editor/EditorManager'),
KeyEvent = brackets.getModule('utils/KeyEvent');
function _createIndentation(editor) {
return editor._getOption('useTabChar')
? '\t'
: Array(editor._getOption('spaceUnits') + 1).join(' ');
}
function _indent(editor, event, whenMatches) {
var document = editor.document,
cursorPos = editor.getCursorPos(),
ch = cursorPos.ch,
lineContent = document.getLine(cursorPos.line);
if (whenMatches === lineContent.substring(ch - 1).substring(0, 2)) {
var indent = _createIndentation(editor);
// Here we filter all tabs, so if:
// code_here
// <tab>another_code
//
// <tab>and_another
// code_again
// another_code haves a <tab>
var tabs = lineContent.split(/\S+/)[0];
document.replaceRange(
'\n' + tabs + indent + '\n' + tabs,
cursorPos,
cursorPos
);
event.preventDefault();
cursorPos = editor.getCursorPos();
editor.setCursorPos(cursorPos.line - 1, document.getLine(cursorPos.line - 1).length);
}
}
function _keyEventHandler($event, editor, event) {
// On ENTER pressed
if (event.keyCode === KeyEvent.DOM_VK_RETURN) {
var fileLang = editor.document.language._id;
// Stuff
// For HTML, PHP (mixed) and XML
if (fileLang === 'html' || fileLang === 'php' || fileLang === 'xml') {
_indent(editor, event, '><');
}
// For Javascript, PHP, CSS, LESS, SCSS support both '{}' and '()'
if (fileLang === 'javascript' || fileLang === 'php' || fileLang === 'css' || fileLang === 'less' || fileLang === 'scss') {
_indent(editor, event, '{}');
_indent(editor, event, '()');
}
}
}
function _activeEditorChangeHandler($event, focusedEditor, lostEditor) {
if (lostEditor) {
lostEditor.off('keydown', _keyEventHandler);
}
if (focusedEditor) {
focusedEditor.on('keydown', _keyEventHandler);
}
}
EditorManager.on('activeEditorChange', _activeEditorChangeHandler);
});