Search code examples
visual-studio-codeindentation

VScode automatic indentation & indent guides, and tabulation size (editor.tabSize)


I have file using an indentation level of 4 columns, and assuming that a tabulation character corresponds to 8 spaces, like this (I use . to represent a space, and <------> for a tabulation character):

class Foo {
....void bar() {
<------>if (boz) {
<------>....return x;
<------>}
....}
}

This is common for certain coding styles like Oracle coding conventions for Java:

Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4).

I'm having trouble rendering this properly with VSCode:

  • If I set editor.tabSize to 4, then it renders badly as
class Foo {
....void bar() {
<-->if (boz) {
<-->....return x;
<-->}
....}
}
  • If I set editor.tabSize to 8, then the text is rendered properly, but the indentation guides are incorrect (a guide is missing for the void bar() indentation level):

indentation guides

More importantly, automatic indentation (pressing the "tab" key, or on-the-fly indentation when pressing "return" after a { character) now indents with 8 columns, making the editor barely usable.

An obvious workaround is to use only spaces for indenting, but this is not applicable when opening a pre-existing file.

Is there a way to configure the indentation guides to be displayed every 4 columns, while still rendering tabs every 8 columns?

In the editorconfig cross-editor configuration file specification, this corresponds to the tab_width and indent_size properties, that I would like to be able to change independently.

I'm a former Emacs-user, and this would correspond to tab-width and c-basic-offset for example.


Solution

  • The issue mentioned in the comments in 2020, microsoft/vscode issue 10339, has finally been closed in Nov. 2022(!)

    PR 155450 enables having separate values for indentation and the display width of tab characters, which is a common requirement of some older projects and/or coding styles.

    In addition to adding support for an editor.indentSize property, the indentation options on the status bar have been updated to allow independently configuring editor.indentSize and editor.tabSize.

    So:

    editor.indentSize: The number of spaces used for indentation or 'tabSize' to use the value from editor.tabSize.
    This setting is overridden based on the file contents when editor.detectIndentation is on.

    This should be available soon in VSCode Insiders and released with VSCode 1.74 (Nov. 2022).