diff --git a/src/command/Commands.js b/src/command/Commands.js index d584f16be2e..b1a0821995a 100644 --- a/src/command/Commands.js +++ b/src/command/Commands.js @@ -77,6 +77,7 @@ define(function (require, exports, module) { exports.VIEW_INCREASE_FONT_SIZE = "view.increaseFontSize"; exports.VIEW_DECREASE_FONT_SIZE = "view.decreaseFontSize"; exports.VIEW_RESTORE_FONT_SIZE = "view.restoreFontSize"; + exports.TOGGLE_CLOSE_BRACKETS = "view.autoCloseBrackets"; exports.TOGGLE_JSLINT = "debug.jslint"; exports.SORT_WORKINGSET_BY_ADDED = "view.sortWorkingSetByAdded"; exports.SORT_WORKINGSET_BY_NAME = "view.sortWorkingSetByName"; diff --git a/src/command/DefaultMenus.js b/src/command/DefaultMenus.js index d9a24dbc729..1d8c0527cc8 100644 --- a/src/command/DefaultMenus.js +++ b/src/command/DefaultMenus.js @@ -95,6 +95,8 @@ define(function (require, exports, module) { menu.addMenuDivider(); menu.addMenuItem(Commands.EDIT_LINE_COMMENT); menu.addMenuItem(Commands.EDIT_BLOCK_COMMENT); + menu.addMenuDivider(); + menu.addMenuItem(Commands.TOGGLE_CLOSE_BRACKETS); /* * View menu diff --git a/src/editor/Editor.js b/src/editor/Editor.js index 6fb54262e24..dbc956ac7c0 100644 --- a/src/editor/Editor.js +++ b/src/editor/Editor.js @@ -73,7 +73,7 @@ define(function (require, exports, module) { ViewUtils = require("utils/ViewUtils"); var PREFERENCES_CLIENT_ID = "com.adobe.brackets.Editor", - defaultPrefs = { useTabChar: false, tabSize: 4, indentUnit: 4 }; + defaultPrefs = { useTabChar: false, tabSize: 4, indentUnit: 4, closeBrackets: false }; /** Editor preferences */ var _prefs = PreferencesManager.getPreferenceStorage(PREFERENCES_CLIENT_ID, defaultPrefs); @@ -81,12 +81,15 @@ define(function (require, exports, module) { /** @type {boolean} Global setting: When inserting new text, use tab characters? (instead of spaces) */ var _useTabChar = _prefs.getValue("useTabChar"); - /** @type {boolean} Global setting: Tab size */ + /** @type {number} Global setting: Tab size */ var _tabSize = _prefs.getValue("tabSize"); - /** @type {boolean} Global setting: Indent unit (i.e. number of spaces when indenting) */ + /** @type {number} Global setting: Indent unit (i.e. number of spaces when indenting) */ var _indentUnit = _prefs.getValue("indentUnit"); + /** @type {boolean} Global setting: Auto closes (, {, [, " and ' */ + var _closeBrackets = _prefs.getValue("closeBrackets"); + /** @type {boolean} Guard flag to prevent focus() reentrancy (via blur handlers), even across Editors */ var _duringFocus = false; @@ -345,6 +348,7 @@ define(function (require, exports, module) { matchBrackets: true, dragDrop: false, // work around issue #1123 extraKeys: codeMirrorKeyMap, + autoCloseBrackets: _closeBrackets, autoCloseTags: { whenOpening: true, whenClosing: true, @@ -1301,7 +1305,7 @@ define(function (require, exports, module) { }; /** @type {boolean} Gets whether all Editors use tab characters (vs. spaces) when inserting new text */ - Editor.getUseTabChar = function (value) { + Editor.getUseTabChar = function () { return _useTabChar; }; @@ -1319,7 +1323,7 @@ define(function (require, exports, module) { }; /** @type {number} Get indent unit */ - Editor.getTabSize = function (value) { + Editor.getTabSize = function () { return _tabSize; }; @@ -1337,10 +1341,28 @@ define(function (require, exports, module) { }; /** @type {number} Get indentation width */ - Editor.getIndentUnit = function (value) { + Editor.getIndentUnit = function () { return _indentUnit; }; + /** + * Sets the auto close brackets. Affects all Editors. + * @param {boolean} value + */ + Editor.setCloseBrackets = function (value) { + _closeBrackets = value; + _instances.forEach(function (editor) { + editor._codeMirror.setOption("autoCloseBrackets", _closeBrackets); + }); + + _prefs.setValue("closeBrackets", Boolean(_closeBrackets)); + }; + + /** @type {boolean} Gets whether all Editors use auto close brackets */ + Editor.getCloseBrackets = function () { + return _closeBrackets; + }; + // Define public API exports.Editor = Editor; exports.BOUNDARY_CHECK_NORMAL = BOUNDARY_CHECK_NORMAL; diff --git a/src/editor/EditorManager.js b/src/editor/EditorManager.js index 44f49473c22..fc3281796d2 100644 --- a/src/editor/EditorManager.js +++ b/src/editor/EditorManager.js @@ -696,6 +696,16 @@ define(function (require, exports, module) { return result.promise(); } + /** + * @private + * Activates/Deactivates the automatic close brackets option + */ + function _toggleCloseBrackets() { + Editor.setCloseBrackets(!Editor.getCloseBrackets()); + CommandManager.get(Commands.TOGGLE_CLOSE_BRACKETS).setChecked(Editor.getCloseBrackets()); + } + + function _updateLanguageInfo(editor) { $languageInfo.text(editor.document.getLanguage().getName()); } @@ -825,10 +835,14 @@ define(function (require, exports, module) { $indentWidthInput.focus(function () { $indentWidthInput.select(); }); _onActiveEditorChange(null, getFocusedEditor(), null); + + CommandManager.get(Commands.TOGGLE_CLOSE_BRACKETS).setChecked(Editor.getCloseBrackets()); } // Initialize: command handlers CommandManager.register(Strings.CMD_TOGGLE_QUICK_EDIT, Commands.TOGGLE_QUICK_EDIT, _toggleQuickEdit); + CommandManager.register(Strings.CMD_TOGGLE_CLOSE_BRACKETS, Commands.TOGGLE_CLOSE_BRACKETS, _toggleCloseBrackets); + // Initialize: register listeners $(DocumentManager).on("currentDocumentChange", _onCurrentDocumentChange); diff --git a/src/index.html b/src/index.html index 9e59f214f5e..b9d0d443ff5 100644 --- a/src/index.html +++ b/src/index.html @@ -100,6 +100,7 @@ + diff --git a/src/nls/root/strings.js b/src/nls/root/strings.js index c83aabb5f8d..59ddc103f3c 100644 --- a/src/nls/root/strings.js +++ b/src/nls/root/strings.js @@ -201,7 +201,7 @@ define({ "CMD_BLOCK_COMMENT" : "Toggle Block Comment", "CMD_LINE_UP" : "Move Line Up", "CMD_LINE_DOWN" : "Move Line Down", - + // View menu commands "VIEW_MENU" : "View", "CMD_HIDE_SIDEBAR" : "Hide Sidebar", @@ -209,6 +209,7 @@ define({ "CMD_INCREASE_FONT_SIZE" : "Increase Font Size", "CMD_DECREASE_FONT_SIZE" : "Decrease Font Size", "CMD_RESTORE_FONT_SIZE" : "Restore Font Size", + "CMD_TOGGLE_CLOSE_BRACKETS" : "Enable Close Brackets", "CMD_SORT_WORKINGSET_BY_ADDED" : "Sort by Added", "CMD_SORT_WORKINGSET_BY_NAME" : "Sort by Name", "CMD_SORT_WORKINGSET_BY_TYPE" : "Sort by Type",