From 1cd7dea04f328596c23fe1b74b9d40f5aff83643 Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Fri, 1 Dec 2023 16:00:58 +0000 Subject: [PATCH] Allow separate tabSize and indentSize Fixes #346 --- package-lock.json | 12 ++++----- src/api.ts | 48 ++++++++++++++++++++++++++---------- src/test/suite/api.test.ts | 15 ++++++++--- src/test/suite/index.test.ts | 16 ++++++++++-- 4 files changed, 66 insertions(+), 25 deletions(-) diff --git a/package-lock.json b/package-lock.json index b70ff54..1506cbc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -170,9 +170,9 @@ "dev": true }, "node_modules/@types/vscode": { - "version": "1.51.0", - "resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.51.0.tgz", - "integrity": "sha512-C/jZ35OT5k/rsJyAK8mS1kM++vMcm89oSWegkzxRCvHllIq0cToZAkIDs6eCY4SKrvik3nrhELizyLcM0onbQA==", + "version": "1.84.2", + "resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.84.2.tgz", + "integrity": "sha512-LCe1FvCDMJKkPdLVGYhP0HRJ1PDop2gRVm/zFHiOKwYLBRS7vEV3uOOUId4HMV+L1IxqyS+IZXMmlSMRbZGIAw==", "dev": true }, "node_modules/@typescript-eslint/eslint-plugin": { @@ -5077,9 +5077,9 @@ "dev": true }, "@types/vscode": { - "version": "1.51.0", - "resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.51.0.tgz", - "integrity": "sha512-C/jZ35OT5k/rsJyAK8mS1kM++vMcm89oSWegkzxRCvHllIq0cToZAkIDs6eCY4SKrvik3nrhELizyLcM0onbQA==", + "version": "1.84.2", + "resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.84.2.tgz", + "integrity": "sha512-LCe1FvCDMJKkPdLVGYhP0HRJ1PDop2gRVm/zFHiOKwYLBRS7vEV3uOOUId4HMV+L1IxqyS+IZXMmlSMRbZGIAw==", "dev": true }, "@typescript-eslint/eslint-plugin": { diff --git a/src/api.ts b/src/api.ts index 24d4d35..0a7b670 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,5 +1,12 @@ import * as editorconfig from 'editorconfig' -import { TextDocument, TextEditorOptions, Uri, window, workspace } from 'vscode' +import { + TextDocument, + TextEditorOptions, + Uri, + window, + workspace, + commands, +} from 'vscode' /** * Resolves `TextEditorOptions` for a `TextDocument`, combining the editor's @@ -51,6 +58,7 @@ export async function applyTextEditorOptions( return } + commands.executeCommand('editor.action.detectIndentation') // <--- HACK!! editor.options = newOptions if (onSuccess) { @@ -74,6 +82,11 @@ export function pickWorkspaceDefaults( * this property value will be `undefined`. */ insertSpaces?: boolean + /** + * The number of spaces used for indentation or `undefined` if + * `editor.detectIndentation` is on. + */ + indentSize?: number | string } { const workspaceConfig = workspace.getConfiguration('editor', doc) const detectIndentation = workspaceConfig.get('detectIndentation') @@ -82,6 +95,7 @@ export function pickWorkspaceDefaults( ? {} : { tabSize: workspaceConfig.get('tabSize'), + indentSize: workspaceConfig.get('indentSize'), insertSpaces: workspaceConfig.get('insertSpaces'), } } @@ -150,26 +164,34 @@ export function fromEditorConfig( ): TextEditorOptions { const resolved: TextEditorOptions = { tabSize: - config.indent_style === 'tab' + (config.indent_style === 'tab' ? config.tab_width ?? config.indent_size - : config.indent_size ?? config.tab_width, + : config.tab_width) ?? defaults.tabSize, + indentSize: + (config.indent_style === 'tab' + ? config.indent_size ?? 'tabSize' + : config.indent_size) ?? defaults.indentSize, } if (resolved.tabSize === 'tab') { resolved.tabSize = config.tab_width } - return { - ...(config.indent_style === 'tab' || + if (resolved.indentSize === 'tab') { + resolved.indentSize = 'tabSize' + } + if ( + config.indent_style === 'tab' || config.indent_size === 'tab' || config.indent_style === 'space' - ? { - insertSpaces: config.indent_style === 'space', - } - : {}), - tabSize: - resolved.tabSize && resolved.tabSize >= 0 - ? resolved.tabSize - : defaults.tabSize, + ) { + resolved.insertSpaces = config.indent_style === 'space' + } + if (resolved.tabSize === undefined || resolved.tabSize === 'unset') { + delete resolved.tabSize + } + if (resolved.indentSize === undefined || resolved.indentSize === 'unset') { + delete resolved.indentSize } + return resolved } /** diff --git a/src/test/suite/api.test.ts b/src/test/suite/api.test.ts index d5597c3..31865e8 100644 --- a/src/test/suite/api.test.ts +++ b/src/test/suite/api.test.ts @@ -19,6 +19,7 @@ suite('EditorConfig extension', () => { expected: { insertSpaces: false, tabSize: 5, + indentSize: 5, }, }, { @@ -33,6 +34,7 @@ suite('EditorConfig extension', () => { expected: { insertSpaces: false, tabSize: 5, + indentSize: 'tabSize', }, }, { @@ -46,7 +48,8 @@ suite('EditorConfig extension', () => { }, expected: { insertSpaces: true, - tabSize: 5, + tabSize: 4, + indentSize: 5, }, }, { @@ -58,7 +61,8 @@ suite('EditorConfig extension', () => { tabSize: 4, }, expected: { - tabSize: 5, + tabSize: 4, + indentSize: 5, }, }, { @@ -82,7 +86,8 @@ suite('EditorConfig extension', () => { tabSize: 4, }, expected: { - tabSize: 5, + tabSize: 4, + indentSize: 5, }, }, { @@ -135,6 +140,7 @@ suite('EditorConfig extension', () => { expected: { insertSpaces: false, tabSize: 3, + indentSize: 'tabSize', }, }, { @@ -166,7 +172,8 @@ suite('EditorConfig extension', () => { defaults: {}, expected: { insertSpaces: true, - tabSize: 2, + tabSize: 4, + indentSize: 2, }, }, ].forEach(scenario => { diff --git a/src/test/suite/index.test.ts b/src/test/suite/index.test.ts index fa390d8..2513441 100644 --- a/src/test/suite/index.test.ts +++ b/src/test/suite/index.test.ts @@ -215,12 +215,18 @@ suite('EditorConfig extension', function () { 'tab_width', 'indent-style-space', ]) - const expectedTabSize = 2 + const expectedTabSize = 8 + const expectedIndentSize = 2 assert.strictEqual( options.tabSize, expectedTabSize, `editor has a tabSize of ${options.tabSize} instead of ${expectedTabSize}`, ) + assert.strictEqual( + options.indentSize, + expectedIndentSize, + `editor has an indentSize of ${options.indentSize} instead of ${expectedIndentSize}`, + ) assert.strictEqual( options.insertSpaces, true, @@ -234,12 +240,18 @@ suite('EditorConfig extension', function () { 'tab_width', 'indent-style-tab', ]) - const expectedTabSize = 4 + const expectedTabSize = 8 + const expectedIndentSize = 4 assert.strictEqual( options.tabSize, expectedTabSize, `editor has a tabSize of ${options.tabSize} instead of ${expectedTabSize}`, ) + assert.strictEqual( + options.indentSize, + expectedIndentSize, + `editor has an indentSize of ${options.indentSize} instead of ${expectedIndentSize}`, + ) assert.strictEqual( options.insertSpaces, false,