This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Fix #3100: New lines always create white spaces #3209
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
|
@@ -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"); | ||
|
@@ -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 += " "; | ||
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -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); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 1339 has 2 tab chars. Remove or convert to spaces. |
||
|
@@ -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 */ | ||
|
@@ -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 */ | ||
|
@@ -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; | ||
}; | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
|
@@ -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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This range checking should be done once before this condition:
|
||
} | ||
|
||
// update indicator | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 asBoolean(value)
when needed.There was a problem hiding this comment.
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.