Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating tabSize should also update indentSize if indentSize is configured to be equal to tabSize #168984

Merged
merged 1 commit into from
Dec 13, 2022
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
20 changes: 16 additions & 4 deletions src/vs/editor/common/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,24 +424,35 @@ export class TextModelResolvedOptions {

readonly tabSize: number;
readonly indentSize: number;
private readonly _indentSizeIsTabSize: boolean;
readonly insertSpaces: boolean;
readonly defaultEOL: DefaultEndOfLine;
readonly trimAutoWhitespace: boolean;
readonly bracketPairColorizationOptions: BracketPairColorizationOptions;

public get originalIndentSize(): number | 'tabSize' {
return this._indentSizeIsTabSize ? 'tabSize' : this.indentSize;
}

/**
* @internal
*/
constructor(src: {
tabSize: number;
indentSize: number;
indentSize: number | 'tabSize';
insertSpaces: boolean;
defaultEOL: DefaultEndOfLine;
trimAutoWhitespace: boolean;
bracketPairColorizationOptions: BracketPairColorizationOptions;
}) {
this.tabSize = Math.max(1, src.tabSize | 0);
this.indentSize = Math.max(1, src.indentSize | 0);
if (src.indentSize === 'tabSize') {
this.indentSize = this.tabSize;
this._indentSizeIsTabSize = true;
} else {
this.indentSize = Math.max(1, src.indentSize | 0);
this._indentSizeIsTabSize = false;
}
this.insertSpaces = Boolean(src.insertSpaces);
this.defaultEOL = src.defaultEOL | 0;
this.trimAutoWhitespace = Boolean(src.trimAutoWhitespace);
Expand All @@ -454,6 +465,7 @@ export class TextModelResolvedOptions {
public equals(other: TextModelResolvedOptions): boolean {
return (
this.tabSize === other.tabSize
&& this._indentSizeIsTabSize === other._indentSizeIsTabSize
&& this.indentSize === other.indentSize
&& this.insertSpaces === other.insertSpaces
&& this.defaultEOL === other.defaultEOL
Expand All @@ -480,7 +492,7 @@ export class TextModelResolvedOptions {
*/
export interface ITextModelCreationOptions {
tabSize: number;
indentSize: number;
indentSize: number | 'tabSize';
insertSpaces: boolean;
detectIndentation: boolean;
trimAutoWhitespace: boolean;
Expand All @@ -497,7 +509,7 @@ export interface BracketPairColorizationOptions {

export interface ITextModelUpdateOptions {
tabSize?: number;
indentSize?: number;
indentSize?: number | 'tabSize';
insertSpaces?: boolean;
trimAutoWhitespace?: boolean;
bracketColorizationOptions?: BracketPairColorizationOptions;
Expand Down
14 changes: 3 additions & 11 deletions src/vs/editor/common/model/textModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,23 +193,15 @@ export class TextModel extends Disposable implements model.ITextModel, IDecorati
const guessedIndentation = guessIndentation(textBuffer, options.tabSize, options.insertSpaces);
return new model.TextModelResolvedOptions({
tabSize: guessedIndentation.tabSize,
indentSize: guessedIndentation.tabSize, // TODO@Alex: guess indentSize independent of tabSize
indentSize: 'tabSize', // TODO@Alex: guess indentSize independent of tabSize
insertSpaces: guessedIndentation.insertSpaces,
trimAutoWhitespace: options.trimAutoWhitespace,
defaultEOL: options.defaultEOL,
bracketPairColorizationOptions: options.bracketPairColorizationOptions,
});
}

return new model.TextModelResolvedOptions({
tabSize: options.tabSize,
indentSize: options.indentSize,
insertSpaces: options.insertSpaces,
trimAutoWhitespace: options.trimAutoWhitespace,
defaultEOL: options.defaultEOL,
bracketPairColorizationOptions: options.bracketPairColorizationOptions,
});

return new model.TextModelResolvedOptions(options);
}

//#region Events
Expand Down Expand Up @@ -629,7 +621,7 @@ export class TextModel extends Disposable implements model.ITextModel, IDecorati
public updateOptions(_newOpts: model.ITextModelUpdateOptions): void {
this._assertNotDisposed();
const tabSize = (typeof _newOpts.tabSize !== 'undefined') ? _newOpts.tabSize : this._options.tabSize;
const indentSize = (typeof _newOpts.indentSize !== 'undefined') ? _newOpts.indentSize : this._options.indentSize;
const indentSize = (typeof _newOpts.indentSize !== 'undefined') ? _newOpts.indentSize : this._options.originalIndentSize;
const insertSpaces = (typeof _newOpts.insertSpaces !== 'undefined') ? _newOpts.insertSpaces : this._options.insertSpaces;
const trimAutoWhitespace = (typeof _newOpts.trimAutoWhitespace !== 'undefined') ? _newOpts.trimAutoWhitespace : this._options.trimAutoWhitespace;
const bracketPairColorizationOptions = (typeof _newOpts.bracketColorizationOptions !== 'undefined') ? _newOpts.bracketColorizationOptions : this._options.bracketPairColorizationOptions;
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/common/services/modelService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class ModelService extends Disposable implements IModelService {
}
}

let indentSize = tabSize;
let indentSize: number | 'tabSize' = 'tabSize';
if (config.editor && typeof config.editor.indentSize !== 'undefined' && config.editor.indentSize !== 'tabSize') {
const parsedIndentSize = parseInt(config.editor.indentSize, 10);
if (!isNaN(parsedIndentSize)) {
Expand Down
17 changes: 17 additions & 0 deletions src/vs/editor/test/common/model/textModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,23 @@ suite('Editor Model - TextModel', () => {
assert.strictEqual(m.getValueInRange(new Range(NaN, NaN, NaN, NaN)), '');
m.dispose();
});

test('issue #168836: updating tabSize should also update indentSize when indentSize is set to "tabSize"', () => {
const m = createTextModel('some text', null, {
tabSize: 2,
indentSize: 'tabSize'
});
assert.strictEqual(m.getOptions().tabSize, 2);
assert.strictEqual(m.getOptions().indentSize, 2);
assert.strictEqual(m.getOptions().originalIndentSize, 'tabSize');
m.updateOptions({
tabSize: 4
});
assert.strictEqual(m.getOptions().tabSize, 4);
assert.strictEqual(m.getOptions().indentSize, 4);
assert.strictEqual(m.getOptions().originalIndentSize, 'tabSize');
m.dispose();
});
});

suite('TextModel.mightContainRTL', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/test/common/testTextModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function withEditorModel(text: string[], callback: (model: TextModel) =>

export interface IRelaxedTextModelCreationOptions {
tabSize?: number;
indentSize?: number;
indentSize?: number | 'tabSize';
insertSpaces?: boolean;
detectIndentation?: boolean;
trimAutoWhitespace?: boolean;
Expand Down
3 changes: 2 additions & 1 deletion src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,7 @@ declare namespace monaco.editor {
readonly defaultEOL: DefaultEndOfLine;
readonly trimAutoWhitespace: boolean;
readonly bracketPairColorizationOptions: BracketPairColorizationOptions;
get originalIndentSize(): number | 'tabSize';
}

export interface BracketPairColorizationOptions {
Expand All @@ -1787,7 +1788,7 @@ declare namespace monaco.editor {

export interface ITextModelUpdateOptions {
tabSize?: number;
indentSize?: number;
indentSize?: number | 'tabSize';
insertSpaces?: boolean;
trimAutoWhitespace?: boolean;
bracketColorizationOptions?: BracketPairColorizationOptions;
Expand Down
6 changes: 1 addition & 5 deletions src/vs/workbench/api/browser/mainThreadEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,7 @@ export class MainThreadTextEditor {
newOpts.tabSize = newConfiguration.tabSize;
}
if (typeof newConfiguration.indentSize !== 'undefined') {
if (newConfiguration.indentSize === 'tabSize') {
newOpts.indentSize = newOpts.tabSize || creationOpts.tabSize;
} else {
newOpts.indentSize = newConfiguration.indentSize;
}
newOpts.indentSize = newConfiguration.indentSize;
}
this._model.updateOptions(newOpts);
}
Expand Down