Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit f6a2c23

Browse files
committed
Merge pull request #3040 from TomMalbran/tom/close-brackets
Adding the CodeMirror built-in Auto Close Brackets addon
2 parents af230c3 + 319439c commit f6a2c23

File tree

6 files changed

+48
-7
lines changed

6 files changed

+48
-7
lines changed

src/command/Commands.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ define(function (require, exports, module) {
7777
exports.VIEW_INCREASE_FONT_SIZE = "view.increaseFontSize";
7878
exports.VIEW_DECREASE_FONT_SIZE = "view.decreaseFontSize";
7979
exports.VIEW_RESTORE_FONT_SIZE = "view.restoreFontSize";
80+
exports.TOGGLE_CLOSE_BRACKETS = "view.autoCloseBrackets";
8081
exports.TOGGLE_JSLINT = "debug.jslint";
8182
exports.SORT_WORKINGSET_BY_ADDED = "view.sortWorkingSetByAdded";
8283
exports.SORT_WORKINGSET_BY_NAME = "view.sortWorkingSetByName";

src/command/DefaultMenus.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ define(function (require, exports, module) {
9595
menu.addMenuDivider();
9696
menu.addMenuItem(Commands.EDIT_LINE_COMMENT);
9797
menu.addMenuItem(Commands.EDIT_BLOCK_COMMENT);
98+
menu.addMenuDivider();
99+
menu.addMenuItem(Commands.TOGGLE_CLOSE_BRACKETS);
98100

99101
/*
100102
* View menu

src/editor/Editor.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,23 @@ define(function (require, exports, module) {
7373
ViewUtils = require("utils/ViewUtils");
7474

7575
var PREFERENCES_CLIENT_ID = "com.adobe.brackets.Editor",
76-
defaultPrefs = { useTabChar: false, tabSize: 4, indentUnit: 4 };
76+
defaultPrefs = { useTabChar: false, tabSize: 4, indentUnit: 4, closeBrackets: false };
7777

7878
/** Editor preferences */
7979
var _prefs = PreferencesManager.getPreferenceStorage(PREFERENCES_CLIENT_ID, defaultPrefs);
8080

8181
/** @type {boolean} Global setting: When inserting new text, use tab characters? (instead of spaces) */
8282
var _useTabChar = _prefs.getValue("useTabChar");
8383

84-
/** @type {boolean} Global setting: Tab size */
84+
/** @type {number} Global setting: Tab size */
8585
var _tabSize = _prefs.getValue("tabSize");
8686

87-
/** @type {boolean} Global setting: Indent unit (i.e. number of spaces when indenting) */
87+
/** @type {number} Global setting: Indent unit (i.e. number of spaces when indenting) */
8888
var _indentUnit = _prefs.getValue("indentUnit");
8989

90+
/** @type {boolean} Global setting: Auto closes (, {, [, " and ' */
91+
var _closeBrackets = _prefs.getValue("closeBrackets");
92+
9093
/** @type {boolean} Guard flag to prevent focus() reentrancy (via blur handlers), even across Editors */
9194
var _duringFocus = false;
9295

@@ -345,6 +348,7 @@ define(function (require, exports, module) {
345348
matchBrackets: true,
346349
dragDrop: false, // work around issue #1123
347350
extraKeys: codeMirrorKeyMap,
351+
autoCloseBrackets: _closeBrackets,
348352
autoCloseTags: {
349353
whenOpening: true,
350354
whenClosing: true,
@@ -1301,7 +1305,7 @@ define(function (require, exports, module) {
13011305
};
13021306

13031307
/** @type {boolean} Gets whether all Editors use tab characters (vs. spaces) when inserting new text */
1304-
Editor.getUseTabChar = function (value) {
1308+
Editor.getUseTabChar = function () {
13051309
return _useTabChar;
13061310
};
13071311

@@ -1319,7 +1323,7 @@ define(function (require, exports, module) {
13191323
};
13201324

13211325
/** @type {number} Get indent unit */
1322-
Editor.getTabSize = function (value) {
1326+
Editor.getTabSize = function () {
13231327
return _tabSize;
13241328
};
13251329

@@ -1337,10 +1341,28 @@ define(function (require, exports, module) {
13371341
};
13381342

13391343
/** @type {number} Get indentation width */
1340-
Editor.getIndentUnit = function (value) {
1344+
Editor.getIndentUnit = function () {
13411345
return _indentUnit;
13421346
};
13431347

1348+
/**
1349+
* Sets the auto close brackets. Affects all Editors.
1350+
* @param {boolean} value
1351+
*/
1352+
Editor.setCloseBrackets = function (value) {
1353+
_closeBrackets = value;
1354+
_instances.forEach(function (editor) {
1355+
editor._codeMirror.setOption("autoCloseBrackets", _closeBrackets);
1356+
});
1357+
1358+
_prefs.setValue("closeBrackets", Boolean(_closeBrackets));
1359+
};
1360+
1361+
/** @type {boolean} Gets whether all Editors use auto close brackets */
1362+
Editor.getCloseBrackets = function () {
1363+
return _closeBrackets;
1364+
};
1365+
13441366
// Define public API
13451367
exports.Editor = Editor;
13461368
exports.BOUNDARY_CHECK_NORMAL = BOUNDARY_CHECK_NORMAL;

src/editor/EditorManager.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,16 @@ define(function (require, exports, module) {
696696
return result.promise();
697697
}
698698

699+
/**
700+
* @private
701+
* Activates/Deactivates the automatic close brackets option
702+
*/
703+
function _toggleCloseBrackets() {
704+
Editor.setCloseBrackets(!Editor.getCloseBrackets());
705+
CommandManager.get(Commands.TOGGLE_CLOSE_BRACKETS).setChecked(Editor.getCloseBrackets());
706+
}
707+
708+
699709
function _updateLanguageInfo(editor) {
700710
$languageInfo.text(editor.document.getLanguage().getName());
701711
}
@@ -825,10 +835,14 @@ define(function (require, exports, module) {
825835
$indentWidthInput.focus(function () { $indentWidthInput.select(); });
826836

827837
_onActiveEditorChange(null, getFocusedEditor(), null);
838+
839+
CommandManager.get(Commands.TOGGLE_CLOSE_BRACKETS).setChecked(Editor.getCloseBrackets());
828840
}
829841

830842
// Initialize: command handlers
831843
CommandManager.register(Strings.CMD_TOGGLE_QUICK_EDIT, Commands.TOGGLE_QUICK_EDIT, _toggleQuickEdit);
844+
CommandManager.register(Strings.CMD_TOGGLE_CLOSE_BRACKETS, Commands.TOGGLE_CLOSE_BRACKETS, _toggleCloseBrackets);
845+
832846

833847
// Initialize: register listeners
834848
$(DocumentManager).on("currentDocumentChange", _onCurrentDocumentChange);

src/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<script src="thirdparty/jquery-1.7.min.js"></script>
101101
<script src="thirdparty/CodeMirror2/lib/codemirror.js"></script>
102102
<script src="thirdparty/CodeMirror2/addon/edit/matchbrackets.js"></script>
103+
<script src="thirdparty/CodeMirror2/addon/edit/closebrackets.js"></script>
103104

104105
<!-- JS for CodeMirror search support -->
105106
<script src="thirdparty/CodeMirror2/addon/search/searchcursor.js"></script>

src/nls/root/strings.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,15 @@ define({
202202
"CMD_BLOCK_COMMENT" : "Toggle Block Comment",
203203
"CMD_LINE_UP" : "Move Line Up",
204204
"CMD_LINE_DOWN" : "Move Line Down",
205-
205+
206206
// View menu commands
207207
"VIEW_MENU" : "View",
208208
"CMD_HIDE_SIDEBAR" : "Hide Sidebar",
209209
"CMD_SHOW_SIDEBAR" : "Show Sidebar",
210210
"CMD_INCREASE_FONT_SIZE" : "Increase Font Size",
211211
"CMD_DECREASE_FONT_SIZE" : "Decrease Font Size",
212212
"CMD_RESTORE_FONT_SIZE" : "Restore Font Size",
213+
"CMD_TOGGLE_CLOSE_BRACKETS" : "Enable Close Brackets",
213214
"CMD_SORT_WORKINGSET_BY_ADDED" : "Sort by Added",
214215
"CMD_SORT_WORKINGSET_BY_NAME" : "Sort by Name",
215216
"CMD_SORT_WORKINGSET_BY_TYPE" : "Sort by Type",

0 commit comments

Comments
 (0)