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

Adding the CodeMirror built-in Auto Close Brackets addon #3040

Merged
merged 3 commits into from
Mar 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/command/Commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 2 additions & 0 deletions src/command/DefaultMenus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 28 additions & 6 deletions src/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,23 @@ 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);

/** @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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the code cleanup!

return _useTabChar;
};

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

/** @type {number} Get indent unit */
Editor.getTabSize = function (value) {
Editor.getTabSize = function () {
return _tabSize;
};

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

/** @type {number} Get indentation width */
Editor.getIndentUnit = function (value) {
Editor.getIndentUnit = function () {
return _indentUnit;
};

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to remove the newline at the end of the document? I, personally, prefer to have them in, so unless this is required, I think it should be restored.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, to update the file to the latest version, I copied the new version from Github and added my modifications again, just to make sure I didn't make any mistakes in the merge (since the automatic merge didnt worked in this file). So it was removed in that copy paste. I can added it again.

* 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);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RaymondLim I think you're researching preferences: Is this ok if changing the setting updates all editor windows? I think most other settings only update the current window.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All 3 tab/space indentation settings update all the editors, which is ok since are global settings. Close brackets is also a global setting, so I believe that they should change the settings on all the open instances. If it changes only one instance, it needs to became a local setting and would need to be setted for each instance, which doesn't sound great.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is the right way to go. We want to update all instances of editor in the same Brackets window so that we will have consistent setting among all the editors (inline or a different editor when switching documents in the working set or opening new one from Search panel or JSLint panel).


_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;
Expand Down
14 changes: 14 additions & 0 deletions src/editor/EditorManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<script src="thirdparty/jquery-1.7.min.js"></script>
<script src="thirdparty/CodeMirror2/lib/codemirror.js"></script>
<script src="thirdparty/CodeMirror2/addon/edit/matchbrackets.js"></script>
<script src="thirdparty/CodeMirror2/addon/edit/closebrackets.js"></script>

<!-- JS for CodeMirror search support -->
<script src="thirdparty/CodeMirror2/addon/search/searchcursor.js"></script>
Expand Down
3 changes: 2 additions & 1 deletion src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,15 @@ 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",
"CMD_SHOW_SIDEBAR" : "Show Sidebar",
"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",
Expand Down