Skip to content

Commit cdb3f6c

Browse files
committed
fix: predictable auto spacing brhavior with fixed tab spacing 4
1 parent 0996c4e commit cdb3f6c

File tree

2 files changed

+66
-24
lines changed

2 files changed

+66
-24
lines changed

src/editor/Editor.js

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,34 +2299,58 @@ define(function (require, exports, module) {
22992299

23002300
const fullPath = this.document.file.fullPath;
23012301
if(SPACING_OPTIONS.has(prefName)){
2302-
if(prefName === SPACE_UNITS){
2303-
newValue = Editor.getSpaceUnits(fullPath);
2304-
} else if(prefName === TAB_SIZE){
2305-
newValue = Editor.getTabSize(fullPath);
2306-
} else {
2307-
const newTabSpaceCfg = Editor.getAutoTabSpaces(fullPath);
2308-
if(newTabSpaceCfg){
2309-
_computeTabSpaces(this);
2302+
const newUseAutoTabs = Editor.getAutoTabSpaces(fullPath);
2303+
if(newUseAutoTabs){
2304+
_computeTabSpaces(this);
2305+
}
2306+
const newUseTabCharCfg = Editor.getUseTabChar(fullPath);
2307+
const newSpaceUnits = Editor.getSpaceUnits(fullPath);
2308+
const newTabSize = Editor.getTabSize(fullPath);
2309+
const newTabUnits = Editor.getAutoTabUnits(fullPath);
2310+
if(this._currentOptions[AUTO_TAB_SPACES] === newUseAutoTabs &&
2311+
this._currentOptions[USE_TAB_CHAR] === newUseTabCharCfg &&
2312+
this._currentOptions[SPACE_UNITS] === newSpaceUnits &&
2313+
this._currentOptions[TAB_SIZE] === newTabSize) {
2314+
// no change
2315+
const currentIndentUnit = this._codeMirror.getOption("indentUnit");
2316+
let expectedIndentUnit;
2317+
if(newUseAutoTabs) {
2318+
expectedIndentUnit = newUseTabCharCfg ?
2319+
newTabUnits * this._currentOptions[TAB_SIZE] :
2320+
this._currentOptions[SPACE_UNITS];
2321+
} else {
2322+
expectedIndentUnit = newUseTabCharCfg ?
2323+
this._currentOptions[TAB_SIZE]:
2324+
this._currentOptions[SPACE_UNITS];
23102325
}
2311-
const newUseTabCharCfg = Editor.getUseTabChar(fullPath);
2312-
if(this._currentOptions[AUTO_TAB_SPACES] === newTabSpaceCfg &&
2313-
this._currentOptions[USE_TAB_CHAR] === newUseTabCharCfg) {
2314-
// no change
2326+
if(currentIndentUnit === expectedIndentUnit) {
23152327
return;
23162328
}
2317-
this._currentOptions[AUTO_TAB_SPACES] = newTabSpaceCfg;
2318-
this._currentOptions[USE_TAB_CHAR] = newUseTabCharCfg;
2319-
this._currentOptions[SPACE_UNITS] = Editor.getSpaceUnits(fullPath);
2320-
this._currentOptions[TAB_SIZE] = Editor.getTabSize(fullPath);
2321-
this._codeMirror.setOption(cmOptions[USE_TAB_CHAR], newUseTabCharCfg);
2329+
}
2330+
this._currentOptions[AUTO_TAB_SPACES] = newUseAutoTabs;
2331+
this._currentOptions[USE_TAB_CHAR] = newUseTabCharCfg;
2332+
this._currentOptions[SPACE_UNITS] = newSpaceUnits;
2333+
this._currentOptions[TAB_SIZE] = newTabSize;
2334+
this._codeMirror.setOption(cmOptions[USE_TAB_CHAR], newUseTabCharCfg);
2335+
if(newUseAutoTabs) {
2336+
if(newUseTabCharCfg){
2337+
this._codeMirror.setOption(cmOptions[TAB_SIZE], this._currentOptions[TAB_SIZE]);
2338+
this._codeMirror.setOption("indentUnit", newTabUnits*this._currentOptions[TAB_SIZE]);
2339+
} else {
2340+
this._codeMirror.setOption(cmOptions[TAB_SIZE], this._currentOptions[TAB_SIZE]);
2341+
this._codeMirror.setOption("indentUnit", this._currentOptions[SPACE_UNITS]);
2342+
}
2343+
} else {
23222344
this._codeMirror.setOption("indentUnit", newUseTabCharCfg === true ?
2323-
this._currentOptions[TAB_SIZE] :
2345+
this._currentOptions[TAB_SIZE]:
23242346
this._currentOptions[SPACE_UNITS]
23252347
);
2326-
this.trigger("optionChange", AUTO_TAB_SPACES, newTabSpaceCfg);
2327-
this.trigger("optionChange", USE_TAB_CHAR, newUseTabCharCfg);
2328-
return;
2348+
this._codeMirror.setOption(cmOptions[TAB_SIZE], this._currentOptions[TAB_SIZE]);
23292349
}
2350+
this._codeMirror.setOption(cmOptions[USE_TAB_CHAR], newUseTabCharCfg);
2351+
this.trigger("optionChange", AUTO_TAB_SPACES, newUseAutoTabs);
2352+
this.trigger("optionChange", USE_TAB_CHAR, newUseTabCharCfg);
2353+
return;
23302354
}
23312355

23322356
if (oldValue !== newValue) {
@@ -2656,6 +2680,19 @@ define(function (require, exports, module) {
26562680
return PreferencesManager.get(TAB_SIZE, _buildPreferencesContext(fullPath));
26572681
};
26582682

2683+
/**
2684+
* Gets the number of tabs for the file. Will
2685+
* @param fullPath
2686+
* @returns {number|*}
2687+
*/
2688+
Editor.getAutoTabUnits = function (fullPath) {
2689+
let computedValues = _getCachedSpaceCfg(fullPath);
2690+
if(Editor.getAutoTabSpaces(fullPath) && computedValues && computedValues.tabUnits) {
2691+
return computedValues.tabUnits;
2692+
}
2693+
return EditorPreferences.MIN_SPACE_UNITS;
2694+
};
2695+
26592696
const MAX_LINES_TO_SCAN_FOR_INDENT = 700; // this is high to account for any js docs/ file comments
26602697
function _computeTabSpaces(editor, scanFullFile, recompute) {
26612698
const fullPath = editor.document.file.fullPath;
@@ -2673,8 +2710,9 @@ define(function (require, exports, module) {
26732710
tabSpacesStateManager.set(fullPath, null); // we dont have a remove api, so just nulling for now
26742711
computedTabSpaces.set(fullPath, {
26752712
useTabChar,
2676-
tabSize: useTabChar ? Math.min(amount, EditorPreferences.MAX_TAB_SIZE) : 0,
2677-
spaceUnits: useTabChar ? 0 : Math.min(amount, EditorPreferences.MAX_SPACE_UNITS)
2713+
tabSize: EditorPreferences.DEFAULT_TAB_SIZE,
2714+
spaceUnits: useTabChar ? 0 : Math.min(amount, EditorPreferences.MAX_SPACE_UNITS),
2715+
tabUnits: useTabChar ? Math.min(amount, EditorPreferences.MAX_AUTO_TAB_UNITS) : 0
26782716
});
26792717
}
26802718
Editor._autoDetectTabSpaces = function (editor, scanFullFile, recompute) {

src/editor/EditorHelper/EditorPreferences.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ define(function (require, exports, module) {
5959
MIN_TAB_SIZE = 1,
6060
DEFAULT_SPACE_UNITS = 4,
6161
DEFAULT_TAB_SIZE = 4,
62+
AUTO_TAB_SIZE = 4,
6263
MAX_SPACE_UNITS = 10,
63-
MAX_TAB_SIZE = 10;
64+
MAX_TAB_SIZE = 10,
65+
MAX_AUTO_TAB_UNITS = 3;
6466

6567
const LINE_NUMBER_GUTTER = "CodeMirror-linenumbers",
6668
LINE_NUMBER_GUTTER_PRIORITY = 100,
@@ -220,6 +222,8 @@ define(function (require, exports, module) {
220222
exports.DEFAULT_TAB_SIZE = DEFAULT_TAB_SIZE;
221223
exports.MAX_SPACE_UNITS = MAX_SPACE_UNITS;
222224
exports.MAX_TAB_SIZE = MAX_TAB_SIZE;
225+
exports.AUTO_TAB_SIZE = AUTO_TAB_SIZE;
226+
exports.MAX_AUTO_TAB_UNITS = MAX_AUTO_TAB_UNITS;
223227

224228
exports.LINE_NUMBER_GUTTER = LINE_NUMBER_GUTTER;
225229
exports.LINE_NUMBER_GUTTER_PRIORITY = LINE_NUMBER_GUTTER_PRIORITY;

0 commit comments

Comments
 (0)