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

Fix #3100: New lines always create white spaces #3209

Merged
merged 2 commits into from
Mar 22, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 30 additions & 19 deletions src/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ define(function (require, exports, module) {
TokenUtils = require("utils/TokenUtils"),
ViewUtils = require("utils/ViewUtils");

var defaultPrefs = { useTabChar: false, tabSize: 4, indentUnit: 4, closeBrackets: false,
var defaultPrefs = { useTabChar: false, tabSize: 4, spaceUnits: 4, closeBrackets: false,
showLineNumbers: true, styleActiveLine: true, wordWrap: true };

/** Editor preferences */
Expand All @@ -86,8 +86,8 @@ define(function (require, exports, module) {
/** @type {number} Global setting: Tab size */
var _tabSize = _prefs.getValue("tabSize");

/** @type {number} Global setting: Indent unit (i.e. number of spaces when indenting) */
var _indentUnit = _prefs.getValue("indentUnit");
/** @type {number} Global setting: Space units (i.e. number of spaces when indenting) */
var _spaceUnits = _prefs.getValue("spaceUnits");

/** @type {boolean} Global setting: Auto closes (, {, [, " and ' */
var _closeBrackets = _prefs.getValue("closeBrackets");
Expand Down Expand Up @@ -154,7 +154,7 @@ define(function (require, exports, module) {
if (instance.getOption("indentWithTabs")) {
CodeMirror.commands.insertTab(instance);
} else {
var i, ins = "", numSpaces = _indentUnit;
var i, ins = "", numSpaces = instance.getOption("indentUnit");
numSpaces -= to.ch % numSpaces;
for (i = 0; i < numSpaces; i++) {
ins += " ";
Expand All @@ -175,12 +175,13 @@ define(function (require, exports, module) {
function _handleSoftTabNavigation(instance, direction, functionName) {
var handled = false;
if (!instance.getOption("indentWithTabs")) {
var cursor = instance.getCursor(),
jump = cursor.ch % _indentUnit,
line = instance.getLine(cursor.line);
var indentUnit = instance.getOption("indentUnit"),
cursor = instance.getCursor(),
jump = cursor.ch % indentUnit,
line = instance.getLine(cursor.line);

if (direction === 1) {
jump = _indentUnit - jump;
jump = indentUnit - jump;

if (cursor.ch + jump > line.length) { // Jump would go beyond current line
return false;
Expand All @@ -199,7 +200,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 = _indentUnit;
jump = indentUnit;
}

// Search backwards to the first non-space character
Expand Down Expand Up @@ -354,7 +355,7 @@ define(function (require, exports, module) {
electricChars: false, // we use our own impl of this to avoid CodeMirror bugs; see _checkElectricChars()
indentWithTabs: _useTabChar,
tabSize: _tabSize,
indentUnit: _indentUnit,
indentUnit: _useTabChar ? _tabSize : _spaceUnits,
lineNumbers: _showLineNumbers,
lineWrapping: _wordWrap,
styleActiveLine: _styleActiveLine,
Expand Down Expand Up @@ -1312,6 +1313,17 @@ define(function (require, exports, module) {
// Global settings that affect all Editor instances (both currently open Editors as well as those created
// in the future)

/**
* @private
* Updates Editor option with the given value. Affects all Editors.
* @param {boolean | number} value
* @param {string} cmOption - CodeMirror option string
*/
function _setEditorOption(value, cmOption) {
_instances.forEach(function (editor) {
editor._codeMirror.setOption(cmOption, value);
});
}

/**
* @private
Expand All @@ -1321,10 +1333,7 @@ define(function (require, exports, module) {
* @param {string} prefName - preference name string
*/
function _setEditorOptionAndPref(value, cmOption, prefName) {
_instances.forEach(function (editor) {
editor._codeMirror.setOption(cmOption, value);
});

_setEditorOption(value, cmOption);
_prefs.setValue(prefName, (typeof value === "boolean") ? Boolean(value) : value);
Copy link
Contributor

Choose a reason for hiding this comment

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

I can see that you didn't add this line, but it doesn't seem to make sense. The way I read it is "if type of var 'value' is a boolean, then convert it to type boolean". Huh?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right. This was added by @RaymondLim I believe.

What is trying to do is convert to boolean anything that might be sent as truthly to set a boolean option. It could be fixed by inverting the if to (typeof value !== "number") ? Boolean(value) : value or just call _setEditorOptionAndPref with a value as Boolean(value) when needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

But that's only slightly better. What if someone wants to set a preference using a string? I, personally, would remove the entire line unless I knew it fixed a specific problem.

}

Copy link
Contributor

Choose a reason for hiding this comment

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

Line 1339 has 2 tab chars. Remove or convert to spaces.

Expand All @@ -1335,6 +1344,7 @@ define(function (require, exports, module) {
Editor.setUseTabChar = function (value) {
_useTabChar = value;
_setEditorOptionAndPref(value, "indentWithTabs", "useTabChar");
_setEditorOption(_useTabChar ? _tabSize : _spaceUnits, "indentUnit");
};

/** @type {boolean} Gets whether all Editors use tab characters (vs. spaces) when inserting new text */
Expand All @@ -1349,6 +1359,7 @@ define(function (require, exports, module) {
Editor.setTabSize = function (value) {
_tabSize = value;
_setEditorOptionAndPref(value, "tabSize", "tabSize");
_setEditorOption(value, "indentUnit");
};

/** @type {number} Get indent unit */
Expand All @@ -1360,14 +1371,14 @@ define(function (require, exports, module) {
* Sets indentation width. Affects all Editors.
* @param {number} value
*/
Editor.setIndentUnit = function (value) {
_indentUnit = value;
_setEditorOptionAndPref(value, "indentUnit", "indentUnit");
Editor.setSpaceUnits = function (value) {
_spaceUnits = value;
_setEditorOptionAndPref(value, "indentUnit", "spaceUnits");
};

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

/**
Expand Down
4 changes: 2 additions & 2 deletions src/editor/EditorStatusBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ define(function (require, exports, module) {
}

function _getIndentSize() {
return Editor.getUseTabChar() ? Editor.getTabSize() : Editor.getIndentUnit();
return Editor.getUseTabChar() ? Editor.getTabSize() : Editor.getSpaceUnits();
}

function _updateIndentSize() {
Expand Down Expand Up @@ -106,7 +106,7 @@ define(function (require, exports, module) {
if (Editor.getUseTabChar()) {
Editor.setTabSize(Math.max(Math.min(value, 10), 1));
} else {
Editor.setIndentUnit(Math.max(Math.min(value, 10), 1));
Editor.setSpaceUnits(Math.max(Math.min(value, 10), 1));
Copy link
Contributor

Choose a reason for hiding this comment

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

This range checking should be done once before this condition:

        value = Math.max(Math.min(value, 10), 1);
        if (Editor.getUseTabChar()) {
            Editor.setTabSize(value);
        } else {
            Editor.setSpaceUnits(value);
        }

}

// update indicator
Expand Down