Skip to content

Implement indentation type and size preferences #1801

Merged
merged 4 commits into from Oct 9, 2012
View
71 src/editor/Editor.js
@@ -73,7 +73,19 @@ define(function (require, exports, module) {
ViewUtils = require("utils/ViewUtils");
var PREFERENCES_CLIENT_ID = "com.adobe.brackets.Editor",
- defaultPrefs = { useTabChar: false };
+ defaultPrefs = { useTabChar: false, tabSize: 4, indentUnit: 4 };
+
+ /** 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 */
+ var _tabSize = _prefs.getValue("tabSize");
+
+ /** @type {boolean} Global setting: Indent unit (i.e. number of spaces when indenting) */
+ var _indentUnit = _prefs.getValue("indentUnit");
/**
* @private
@@ -121,7 +133,7 @@ define(function (require, exports, module) {
if (instance.getOption("indentWithTabs")) {
CodeMirror.commands.insertTab(instance);
} else {
- var i, ins = "", numSpaces = instance.getOption("tabSize");
+ var i, ins = "", numSpaces = _indentUnit;
numSpaces -= to.ch % numSpaces;
for (i = 0; i < numSpaces; i++) {
ins += " ";
@@ -143,12 +155,11 @@ define(function (require, exports, module) {
var handled = false;
if (!instance.getOption("indentWithTabs")) {
var cursor = instance.getCursor(),
- tabSize = instance.getOption("tabSize"),
- jump = cursor.ch % tabSize,
+ jump = cursor.ch % _indentUnit,
line = instance.getLine(cursor.line);
if (direction === 1) {
- jump = tabSize - jump;
+ jump = _indentUnit - jump;
if (cursor.ch + jump > line.length) { // Jump would go beyond current line
return false;
@@ -167,7 +178,7 @@ define(function (require, exports, module) {
// If we are on the tab boundary, jump by the full amount,
// but not beyond the start of the line.
if (jump === 0) {
- jump = tabSize;
+ jump = _indentUnit;
}
// Search backwards to the first non-space character
@@ -228,20 +239,13 @@ define(function (require, exports, module) {
}
}
- /** Editor preferences */
- var _prefs = PreferencesManager.getPreferenceStorage(PREFERENCES_CLIENT_ID, defaultPrefs);
-
/**
* List of all current (non-destroy()ed) Editor instances. Needed when changing global preferences
* that affect all editors, e.g. tabbing or color scheme settings.
* @type {Array.<Editor>}
*/
var _instances = [];
- /** @type {boolean} Global setting: When inserting new text, use tab characters? (instead of spaces) */
- var _useTabChar = _prefs.getValue("useTabChar");
-
-
/**
* @constructor
@@ -336,8 +340,9 @@ define(function (require, exports, module) {
// (note: CodeMirror doesn't actually require using 'new', but jslint complains without it)
this._codeMirror = new CodeMirror(container, {
electricChars: false, // we use our own impl of this to avoid CodeMirror bugs; see _checkElectricChars()
- indentUnit: 4,
indentWithTabs: _useTabChar,
+ tabSize: _tabSize,
+ indentUnit: _indentUnit,
lineNumbers: true,
matchBrackets: true,
dragDrop: false, // work around issue #1123
@@ -1075,15 +1080,49 @@ define(function (require, exports, module) {
editor._codeMirror.setOption("indentWithTabs", _useTabChar);
});
- // Remember the setting across launches
_prefs.setValue("useTabChar", Boolean(_useTabChar));
};
- /** @type {boolean} Gets whether all Editors use tab characters (vs. spaces) when inserting new text */
+ /** @type {boolean} Gets whether all Editors use tab characters (vs. spaces) when inserting new text */
Editor.getUseTabChar = function (value) {
return _useTabChar;
};
+ /**
+ * Sets tab character width. Affects all Editors.
+ * @param {number} value
+ */
+ Editor.setTabSize = function (value) {
+ _tabSize = value;
+ _instances.forEach(function (editor) {
+ editor._codeMirror.setOption("tabSize", _tabSize);
+ });
+
+ _prefs.setValue("tabSize", _tabSize);
+ };
+
+ /** @type {number} Get indent unit */
+ Editor.getTabSize = function (value) {
+ return _tabSize;
+ };
+
+ /**
+ * Sets indentation width. Affects all Editors.
+ * @param {number} value
+ */
+ Editor.setIndentUnit = function (value) {
+ _indentUnit = value;
+ _instances.forEach(function (editor) {
+ editor._codeMirror.setOption("indentUnit", _indentUnit);
+ });
+
+ _prefs.setValue("indentUnit", _indentUnit);
+ };
+
+ /** @type {number} Get indentation width */
+ Editor.getIndentUnit = function (value) {
+ return _indentUnit;
+ };
// Global commands that affect the currently focused Editor instance, wherever it may be
CommandManager.register(Strings.CMD_SELECT_ALL, Commands.EDIT_SELECT_ALL, _handleSelectAll);
View
40 src/editor/EditorManager.js
@@ -588,30 +588,29 @@ define(function (require, exports, module) {
$fileInfo.text(StringUtils.format(Strings.STATUSBAR_LINE_COUNT, editor.lineCount()));
}
- function _updateIndentInfo(editor) {
- var indentWithTabs = editor._codeMirror.getOption("indentWithTabs");
+ function _updateIndentType() {
+ var indentWithTabs = Editor.getUseTabChar();
$indentType.text(indentWithTabs ? Strings.STATUSBAR_TAB_SIZE : Strings.STATUSBAR_SPACES);
$indentType.attr("title", indentWithTabs ? Strings.STATUSBAR_INDENT_TOOLTIP_SPACES : Strings.STATUSBAR_INDENT_TOOLTIP_TABS);
-
- $indentWidth.text(editor._codeMirror.getOption("tabSize"));
+ }
+
+ function _updateIndentSize(inc) {
+ $indentWidth.text(Editor.getUseTabChar() ? Editor.getTabSize() : Editor.getIndentUnit());
}
function _toggleIndentType() {
- var editor = getFocusedEditor(),
- indentWithTabs = editor._codeMirror.getOption("indentWithTabs");
-
- editor._codeMirror.setOption("indentWithTabs", !indentWithTabs);
-
- _updateIndentInfo(editor);
+ Editor.setUseTabChar(!Editor.getUseTabChar());
+ _updateIndentType();
+ _updateIndentSize();
}
- function _updateIndentSize(inc) {
- var editor = getFocusedEditor(),
- size = editor._codeMirror.getOption("tabSize");
-
- editor._codeMirror.setOption("tabSize", size + inc);
-
- _updateIndentInfo(editor);
+ function _changeIndentSize(inc) {
+ if (Editor.getUseTabChar()) {
+ Editor.setTabSize(Editor.getTabSize() + inc);
+ } else {
+ Editor.setIndentUnit(Editor.getIndentUnit() + inc);
+ }
+ _updateIndentSize();
}
function _updateCursorInfo(event, editor) {
@@ -643,7 +642,8 @@ define(function (require, exports, module) {
_updateCursorInfo(null, current);
_updateModeInfo(current);
_updateFileInfo(current);
- _updateIndentInfo(current);
+ _updateIndentType();
+ _updateIndentSize();
}
}
@@ -672,8 +672,8 @@ define(function (require, exports, module) {
// indentation event handlers
$indentType.on("click", _toggleIndentType);
- $indentDecrement.on("click", function () { _updateIndentSize(-1); });
- $indentIncrement.on("click", function () { _updateIndentSize(1); });
+ $indentDecrement.on("click", function () { _changeIndentSize(-1); });
+ $indentIncrement.on("click", function () { _changeIndentSize(1); });
StatusBar.hide();
_onFocusedEditorChange(null, getFocusedEditor(), null);
View
7 src/preferences/PreferencesManager.js
@@ -60,6 +60,13 @@ define(function (require, exports, module) {
// create a new empty preferences object
prefs = (defaults && JSON.stringify(defaults)) ? defaults : {};
prefStorage[clientID] = prefs;
+ } else if (defaults) {
+ // add new defaults
+ Object.keys(defaults).forEach(function (key) {
+ if (prefs[key] === undefined) {
+ prefs[key] = defaults[key];
+ }
+ });
}
return new PreferenceStorage(clientID, prefs);
Something went wrong with that request. Please try again.