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

Allow separate tabSize and indentSize #358

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.

2 changes: 1 addition & 1 deletion src/DocumentWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default class DocumentWatcher {

subscriptions.push(
workspace.onWillSaveTextDocument(async e => {
let selections: Selection[] = []
let selections: readonly Selection[] = []
const activeEditor = window.activeTextEditor
const activeDoc = activeEditor?.document
if (activeDoc && activeDoc === e.document && activeEditor) {
Expand Down
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') // <--- HACK!!
yshui marked this conversation as resolved.
Show resolved Hide resolved
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