Skip to content

Commit

Permalink
vscode support of editor.indentSize
Browse files Browse the repository at this point in the history
Also updates tabSize, as API does not correspond to documentation.

Contributed on behalf of STMicroelectronics

Signed-off-by: Remi Schnekenburger <rschnekenburger@eclipsesource.com>
  • Loading branch information
rschnekenbu committed Nov 27, 2023
1 parent 59613f9 commit 1f54497
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

- [task] prevent task widget title from being changed by task process [#13003](https://github.com/eclipse-theia/theia/pull/13003)
- [vscode] Added Notebook CodeActionKind [#13093](https://github.com/eclipse-theia/theia/pull/13093) - contributed on behalf of STMicroelectronics
- [vscode] Added support for editor.indentSize API [#13105](https://github.com/eclipse-theia/theia/pull/13105) - contributed on behalf of STMicroelectronics

## v1.43.0 - 10/26/2023

Expand Down
10 changes: 6 additions & 4 deletions packages/monaco/src/browser/monaco-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,12 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
({
label: size === tabSize ? size + ' ' + nls.localizeByDefault('Configured Tab Size') : size.toString(),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
execute: () => model.updateOptions({
tabSize: size || tabSize,
insertSpaces: useSpaces
})
execute: () =>
model.updateOptions({
tabSize: size || tabSize,
indentSize: size || tabSize,
insertSpaces: useSpaces
})
})
);
this.quickInputService?.showQuickPick(tabSizeOptions, { placeholder: nls.localizeByDefault('Select Tab Size for Current File') });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ export class MonacoStatusBarContribution implements FrontendApplicationContribut
if (editor && editorModel) {
const modelOptions = editorModel.getOptions();
const tabSize = modelOptions.tabSize;
const indentSize = modelOptions.indentSize;
const spaceOrTabSizeMessage = modelOptions.insertSpaces
? nls.localizeByDefault('Spaces: {0}', tabSize)
? nls.localizeByDefault('Spaces: {0}', indentSize)
: nls.localizeByDefault('Tab Size: {0}', tabSize);
this.statusBar.setElement('editor-status-tabbing-config', {
text: spaceOrTabSizeMessage,
Expand Down
7 changes: 6 additions & 1 deletion packages/monaco/src/browser/monaco-text-model-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,15 @@ export class MonacoTextModelService implements ITextModelService {

protected readonly modelOptions: { [name: string]: (keyof ITextModelUpdateOptions | undefined) } = {
'editor.tabSize': 'tabSize',
'editor.insertSpaces': 'insertSpaces'
'editor.insertSpaces': 'insertSpaces',
'editor.indentSize': 'tabSize'
};

protected toModelOption(editorPreference: EditorPreferenceChange['preferenceName']): keyof ITextModelUpdateOptions | undefined {
switch (editorPreference) {
case 'editor.tabSize': return 'tabSize';
// monaco-uplift: uncomment this line once 'editor.indentSize' preference is available
// case 'editor.indentSize': return 'tabSize';
case 'editor.insertSpaces': return 'insertSpaces';
case 'editor.bracketPairColorization.enabled':
case 'editor.bracketPairColorization.independentColorPoolPerBracketType':
Expand Down Expand Up @@ -170,6 +173,8 @@ export class MonacoTextModelService implements ITextModelService {
const overrideIdentifier = typeof arg === 'string' ? undefined : arg.languageId;
return {
tabSize: this.editorPreferences.get({ preferenceName: 'editor.tabSize', overrideIdentifier }, undefined, uri),
// monaco-uplift: when available, switch to 'editor.indentSize' preference.
indentSize: this.editorPreferences.get({ preferenceName: 'editor.tabSize', overrideIdentifier }, undefined, uri),
insertSpaces: this.editorPreferences.get({ preferenceName: 'editor.insertSpaces', overrideIdentifier }, undefined, uri),
bracketColorizationOptions: {
enabled: this.editorPreferences.get({ preferenceName: 'editor.bracketPairColorization.enabled', overrideIdentifier }, undefined, uri),
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1124,13 +1124,15 @@ export interface Selection {

export interface TextEditorConfiguration {
tabSize: number;
indentSize: number;
insertSpaces: boolean;
cursorStyle: TextEditorCursorStyle;
lineNumbers: TextEditorLineNumbersStyle;
}

export interface TextEditorConfigurationUpdate {
tabSize?: number | 'auto';
indentSize?: number | 'tabSize';
insertSpaces?: boolean | 'auto';
cursorStyle?: TextEditorCursorStyle;
lineNumbers?: TextEditorLineNumbersStyle;
Expand Down
9 changes: 9 additions & 0 deletions packages/plugin-ext/src/main/browser/text-editor-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ export class TextEditorMain implements Disposable {
if (typeof newConfiguration.tabSize !== 'undefined') {
newOpts.tabSize = newConfiguration.tabSize;
}
if (typeof newConfiguration.indentSize !== 'undefined') {
if (newConfiguration.indentSize === 'tabSize') {
newOpts.indentSize = newConfiguration.tabSize;
} else if (typeof newConfiguration.indentSize == 'number') {
newOpts.indentSize = newConfiguration.indentSize;
}
}
this.model.updateOptions(newOpts);
}

Expand Down Expand Up @@ -408,6 +415,7 @@ export class TextEditorPropertiesMain {
const modelOptions = model.getOptions();
return {
insertSpaces: modelOptions.insertSpaces,
indentSize: modelOptions.indentSize,
tabSize: modelOptions.tabSize,
cursorStyle,
lineNumbers,
Expand Down Expand Up @@ -443,6 +451,7 @@ export class TextEditorPropertiesMain {
return (
a.tabSize === b.tabSize
&& a.insertSpaces === b.insertSpaces
&& a.indentSize === b.indentSize
&& a.cursorStyle === b.cursorStyle
&& a.lineNumbers === b.lineNumbers
);
Expand Down
65 changes: 61 additions & 4 deletions packages/plugin-ext/src/plugin/text-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ export class TextEditorExt implements theia.TextEditor {
}

export class TextEditorOptionsExt implements theia.TextEditorOptions {
private _tabSize?: number;
private _tabSize: number;
private _indentSize: number;
private _insertSpace: boolean;
private _cursorStyle: TextEditorCursorStyle;
private _lineNumbers: TextEditorLineNumbersStyle;
Expand All @@ -289,12 +290,13 @@ export class TextEditorOptionsExt implements theia.TextEditorOptions {

accept(source: TextEditorConfiguration): void {
this._tabSize = source.tabSize;
this._indentSize = source.indentSize;
this._insertSpace = source.insertSpaces;
this._cursorStyle = source.cursorStyle;
this._lineNumbers = source.lineNumbers;
}

get tabSize(): number | string | undefined {
get tabSize(): number {
return this._tabSize;
}

Expand All @@ -305,10 +307,10 @@ export class TextEditorOptionsExt implements theia.TextEditorOptions {
}

if (typeof tabSize === 'number') {
if (this.tabSize === tabSize) {
if (this._tabSize === tabSize) {
return;
}
this.tabSize = tabSize;
this._tabSize = tabSize;
}
warnOnError(this.proxy.$trySetOptions(this.id, {
tabSize
Expand All @@ -334,6 +336,48 @@ export class TextEditorOptionsExt implements theia.TextEditorOptions {
return undefined;
}

get indentSize(): number {
return this._indentSize;
}

set indentSize(val: number | string | undefined) {
const indentSize = this.validateIndentSize(val);
if (!indentSize) {
return; // ignore invalid values
}

if (typeof indentSize === 'number') {
if (this._indentSize === indentSize) {
return;
}
this._indentSize = indentSize;
} else if (val === 'tabSize') {
this._indentSize = this._tabSize!;
}
warnOnError(this.proxy.$trySetOptions(this.id, {
indentSize
}));
}

private validateIndentSize(val: number | string | undefined): number | 'tabSize' | undefined {
if (val === 'tabSize') {
return 'tabSize';
}

if (typeof val === 'number') {
const r = Math.floor(val);
return r > 0 ? r : undefined;
}
if (typeof val === 'string') {
const r = parseInt(val, undefined);
if (isNaN(r)) {
return undefined;
}
return r > 0 ? r : undefined;
}
return undefined;
}

get insertSpaces(): boolean | string {
return this._insertSpace;
}
Expand Down Expand Up @@ -395,6 +439,19 @@ export class TextEditorOptionsExt implements theia.TextEditorOptions {
}
}

if (typeof newOptions.indentSize !== 'undefined') {
const indentSize = this.validateIndentSize(newOptions.indentSize);
if (indentSize === 'tabSize') {
hasUpdate = true;
configurationUpdate.indentSize = indentSize;
} else if (typeof indentSize === 'number' && this._indentSize !== indentSize) {
// reflect the new indentSize value immediately
this._indentSize = indentSize;
hasUpdate = true;
configurationUpdate.indentSize = indentSize;
}
}

if (typeof newOptions.insertSpaces !== 'undefined') {
const insertSpaces = this.validateInsertSpaces(newOptions.insertSpaces);
if (insertSpaces === 'auto') {
Expand Down
8 changes: 8 additions & 0 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,14 @@ export module '@theia/plugin' {
*/
tabSize?: number | string;

/**
* The number of spaces to insert when {@link TextEditorOptions.insertSpaces insertSpaces} is true.
*
* When getting a text editor's options, this property will always be a number (resolved).
* When setting a text editor's options, this property is optional and it can be a number or `"tabSize"`.
*/
indentSize?: number | string;

/**
* When pressing Tab insert {@link TextEditorOptions.tabSize n} spaces.
* When getting a text editor's options, this property will always be a boolean (resolved).
Expand Down

0 comments on commit 1f54497

Please sign in to comment.