Skip to content

Commit

Permalink
Allow separate tabSize and indentSize
Browse files Browse the repository at this point in the history
  • Loading branch information
yshui committed Dec 1, 2023
1 parent b86ebd4 commit 504f834
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 25 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 35 additions & 13 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -51,6 +58,7 @@ export async function applyTextEditorOptions(
return
}

commands.executeCommand('editor.action.detectIndentation')
editor.options = newOptions

if (onSuccess) {
Expand All @@ -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<boolean>('detectIndentation')
Expand All @@ -82,6 +95,7 @@ export function pickWorkspaceDefaults(
? {}
: {
tabSize: workspaceConfig.get<number>('tabSize'),
indentSize: workspaceConfig.get<number | string>('indentSize'),
insertSpaces: workspaceConfig.get<boolean>('insertSpaces'),
}
}
Expand Down Expand Up @@ -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
}

/**
Expand Down
15 changes: 11 additions & 4 deletions src/test/suite/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ suite('EditorConfig extension', () => {
expected: {
insertSpaces: false,
tabSize: 5,
indentSize: 5,
},
},
{
Expand All @@ -33,6 +34,7 @@ suite('EditorConfig extension', () => {
expected: {
insertSpaces: false,
tabSize: 5,
indentSize: 'tabSize',
},
},
{
Expand All @@ -46,7 +48,8 @@ suite('EditorConfig extension', () => {
},
expected: {
insertSpaces: true,
tabSize: 5,
tabSize: 4,
indentSize: 5,
},
},
{
Expand All @@ -58,7 +61,8 @@ suite('EditorConfig extension', () => {
tabSize: 4,
},
expected: {
tabSize: 5,
tabSize: 4,
indentSize: 5,
},
},
{
Expand All @@ -82,7 +86,8 @@ suite('EditorConfig extension', () => {
tabSize: 4,
},
expected: {
tabSize: 5,
tabSize: 4,
indentSize: 5,
},
},
{
Expand Down Expand Up @@ -135,6 +140,7 @@ suite('EditorConfig extension', () => {
expected: {
insertSpaces: false,
tabSize: 3,
indentSize: 'tabSize',
},
},
{
Expand Down Expand Up @@ -166,7 +172,8 @@ suite('EditorConfig extension', () => {
defaults: {},
expected: {
insertSpaces: true,
tabSize: 2,
tabSize: 4,
indentSize: 2,
},
},
].forEach(scenario => {
Expand Down
16 changes: 14 additions & 2 deletions src/test/suite/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 504f834

Please sign in to comment.