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

Add support for mixed tab/spaces indentation #5394

Open
eladts opened this issue Apr 16, 2016 · 31 comments
Open

Add support for mixed tab/spaces indentation #5394

eladts opened this issue Apr 16, 2016 · 31 comments
Labels
editor-core Editor basic functionality editor-rendering Editor rendering issues feature-request Request for new features or functionality
Milestone

Comments

@eladts
Copy link

eladts commented Apr 16, 2016

Traditional Unix editors (emacs, vim etc) use a mix of tabs and spaces for indentation. The tab length is always assumed to be 8 and if the indentation size is different, indentation is created by inserting as much tabs as possible and inserting spaces for the remainder. Right now, in order to view such files correctly one needs to set the tab size to 8, convert the file to use only spaces and then set the indentation size to the correct value. However, when working on legacy code it is desired to keep the indentation method the same to avoid unnecessary commits. Therefore VSCode should support this method of indentation, auto detect it and allow to easily convert from and to it.

  • VSCode Version: 1.0.0
  • OS Version: OS X 10.11.4

Steps to Reproduce:

  1. Load a file with mixed indentation
  2. VSCode won't display the file correctly
@isidorn
Copy link
Contributor

isidorn commented Apr 18, 2016

Our current indentation was designed to help convert to only one type of indentation, more details about this can be found here. As a workaround for your scenario I suggest to set in your settings:
"editor.detectIndentation": false

Leaving this open as a feature reqeust
fyi @alexandrudima

@isidorn isidorn added the feature-request Request for new features or functionality label Apr 18, 2016
@isidorn isidorn added this to the Backlog milestone Apr 18, 2016
@isidorn isidorn removed their assignment Apr 18, 2016
@alexdima
Copy link
Member

I'm thinking we need to add a setting called fixedTabSize: true|false or something like that.

@eladts I wonder how other editors deal with this. i.e. Do emacs and vim offer an equivalent setting to switch behaviour?

@eladts
Copy link
Author

eladts commented Apr 28, 2016

I think such a setting should be applied per-file, not just glibally. As far as I know, Emacs always uses tab size of 8, there is no way to change that. I'm less familiar with VIM. In my opinion a separate indent size and tab size would provide a more generalized solution to the problem. This way you can set indent size to 4 and tab size to 8, for example.

@jdmansour
Copy link

Yes, vim allows you to set a different size for tabs (that already exist in the file), and indentation (how many spaces it moves if you press the TAB key). You can choose if the TAB key indents only with spaces, or if it should replace some spaces by a tab, if there are more than e.g. 8.

@julian-klode
Copy link
Contributor

For example, in APT we indent at 3 spaces each, but every 8 spaces are replaced by a tab, so our indentation goes like this:

  1. 3 spaces
  2. 6 spaces
  3. 1 tab + 1 space (equaling 9 spaces)
  4. 1 tab + 4 spaces (equaling 12 spaces)

and so on. Both atom and vscode can't handle that, making it impossible to use them on such code.

@julian-klode
Copy link
Contributor

JFTR: clang-format also uses the "IndentWidth" (how many spaces to indent) and "TabWidth" (how long a tab should be interpreted as), coupled with a "UseTab" option. Going the same approach for vscode would make a lot of sense given that you can then easily use the same settings for both without thinking much :)

So for example, for APT weird's indentation, we have IndentWidth: 3, TabWidth: 8, UseTabs: Always and it just works.

@jonathansnell
Copy link

jonathansnell commented Sep 29, 2017

Vim allows specifying different values for both hard and soft tabstops, and can either use as many tabs as possible, or expand them all to spaces. It also has a separate setting for shift width, which roughly maps to the key combinations Ctrl+[ and Ctrl+] in VS Code.

Theoretically editor config also supports this use case, since you can specify a different indentation size from tabstop size. The plugin for VS Code supports setting both values, but it doesn't function as expected in this case; it just sets the tabstop size to the indentation size (if indent_style = space) or visa versa (if indent_style = tab).

Java code following the "Code Conventions for the Java Programming Language" is another use case for this feature. Section 4 - Indentation begins: "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)."

@simark
Copy link

simark commented Dec 1, 2017

The GNU coding style also mandates indenting by two columns, using as many tabs as possible (where 1 tab is 8 columns) and the rest using spaces. So it's impossible to use vscode (or any other monaco-based editor) to work on these.

@notameadow
Copy link

Just wanted to poke this issue as it still seems to be open.
I'm using Code as a wrapper for vim and I like everything about it, but I have to work on code written in vi(m) with tabStop=8 and shiftWidth=4 and there still doesn't seem to be the way to make it work.

@rdebath
Copy link

rdebath commented Jul 12, 2018

Agreed, the Unix world learnt to use a fixed physical tabstop (of every 8 monospaced characters) a very long time ago. It is the only way that generic tools (like XTerm or Powershell!) and text editors can reliably expand tabs in any file they come across.

The problem is that many Windows editors pretend they can flout the rules and cause many problems for themselves and others. A common misconception is that tabs can be any number of spaces and this can be used to change the indenting of a file. A little bit of thought shows that this only works if tabs only occur at the start of the line and spaces are only ever used as lone characters so you cannot align things anywhere except the indent.

So at the moment vscode cannot deal with tabs correctly.

Edit: Clarify the rules for "any-width" tabs in files.

@lapo-luchini
Copy link

cfr. #42740

@Zevensoft
Copy link

When can we expect this feature?

@whlkw
Copy link

whlkw commented Aug 19, 2019

I also need this feature!

@mike2307
Copy link

Yes, please get this done

@ghost
Copy link

ghost commented Sep 7, 2019

I can't believe I found this 3 years old unresolved issue on Google.

I happened to write a bash script awhile ago and came across heredoc indentation problem. Example of my code is:

tee /home/user/test.log > /dev/null <<-END
    this is line 1
    this is line 2
    this is line 3
END

I ended up having 4 leading space since it was not indented using tab. heredoc <<- was supposed to remove the leading spaces only if tab is used. Details here.

I don't want the whole document to use tab but only on the heredoc part and there is no way to mix it without entering the rest of the spaces manually... Not sure how to explain this, but that "leading spaces" will only get ignored when tab is used not space upon calling "<<-"...

Edit: Or perhaps this is actually bash' or heredoc limitation?

@jrieken jrieken added editor editor-autoindent Editor auto indentation issues labels Oct 9, 2019
@alexdima alexdima added editor-core Editor basic functionality editor-rendering Editor rendering issues and removed editor-autoindent Editor auto indentation issues editor labels Oct 25, 2019
@Wintaru
Copy link

Wintaru commented Feb 12, 2020

Just wanted to chime in here as well. A feature like this would put Code ahead of Sublime in my mind, they don't do this either and their general response is "fix your codebase" which is not feasible for larger projects.

@dolangish
Copy link

dolangish commented May 18, 2020

It's beyond frustrating that this feature is not getting any attention from the vscode team at Microsoft. Or if it is, it'd be nice if they could give some indication of when/where it lands on the roadmap.

@cascading-jox
Copy link

I am editing old .cgi scripts from 2001 and I find it baffling that vscode does not support combining tabs and spaces. Like ghost said, in heredoc parts it actually matters if it is spaces or tabs. Is there some great reason why it is not supported?

@brlcad
Copy link

brlcad commented Oct 2, 2020

Here's another ping on this requesting a separation of tab size from indentation width. This will make it possible to use VSCode on projects that have coding standards requiring different tab size and indent width settings. Examples of codes being excluded include most FreeBSD (and style(1)) conforming codes, most of the GNU ecosystem, X11, and other stalwarts like the sources to Emacs and Vim themselves. This is related to request #42740.

@magicus
Copy link
Contributor

magicus commented Oct 22, 2020

To add another example of lecagy style documents that's hard to edit without separating the concepts of "indent size" and "tab width" are makefiles. In make, tabs are used not as generic whitespace, but as a special character representing a recipe line. To make them stand out, a traditional tab width of 8 is (almost) required. But you'd want the rest of the file formatted using space and a reasonable indent size (like 2 or 4).

@marxin
Copy link

marxin commented Jun 1, 2021

I would like the features as well. My example is GCC compiler, where we mix spaces (we indent by 2) and tabs (equal to 8 spaces).
Without that I can't start using vscode for the project. Thanks.

@marxin
Copy link

marxin commented Aug 31, 2021

Just for the record, the Atom editor has the very same limitation: atom/atom#21371.

Not being a vscode developer, I briefly grepped for insertSpaces and tabSize and there's a huge number of usages. I've also tried prototyping a proof-of-concept, but I haven't managed in the few hours I gave it.
The future tabStop feature would require changes in how tabs are displayed, in column navigation, shift/unshift indentation, and likely many other places.

@nyetwurk
Copy link

nyetwurk commented Dec 8, 2021

Bump. Please make shiftwidth: and tabstop: work with embedded modelines as well.

@alexdima
Copy link
Member

Done via #155450

@danielkasza
Copy link

@alexdima, does that PR address this requirement?

indentation is created by inserting as much tabs as possible and inserting spaces for the remainder

From the discussion on the PR:

It doesn't convert spaces into tabs. With the above configuration, any existing tabs in the file will be aligned to 8 spaces, but pressing the tab key will only ever insert spaces

that seems to do as it says then, it's just not clear how someone would actually input a tab with this setup apart from copying the character from somewhere else

it sounds like the PR does not really address the issue. We could already display files correctly by setting tab size to 8. The issue is that when you start editing the file, you start to mess it up by not mixing tabs and spaces correctly. My previous workaround was to manually insert tabs and spaces as needed (or use a different editor).

@sthibaul
Copy link

Does "editor.insertSpaces": false not allow to get tabs?

@hexane360
Copy link

hexane360 commented Nov 16, 2022

PR #155450 has information on the thought process. It seems like an important step for supporting alternate indentation styles, especially the GNU "hard tab-stop" style.

But I agree that without the changes to indentation style (such as replacing editor.insertSpaces with an enum), this issue should probably stay open.

In other words, the PR addresses the issue, but it alone does not fix the issue.

@brlcad
Copy link

brlcad commented Nov 16, 2022

@alexdima As mentioned above, the PR does not actually address this issue for editing. #5394 and #42740 should remain open as they are not actually fixed by the PR.

@zeroimpl
Copy link
Contributor

zeroimpl commented Nov 17, 2022

Agreed, my PR #155450 doesn't fully address this issue.

As a proof of concept, I've implemented a quick fix that seems to do what folks want, in terms of allowing mixed indentation that continues to insert both tabs and spaces: zeroimpl@ba9ac4f, https://github.com/zeroimpl/vscode/tree/zeroimpl/mixed_tabs

i think it needs some more work to fully productize though. There's a normalizeIndentation function used in a bunch of places that likely needs updating. The UI/status bar probably needs some adjustment. Documentation should be updated too. I don't know enough about the code base to make all the relevant changes though.

@alexdima
Copy link
Member

Reopening since #155450 doesn't address the replacing as many spaces as possible with tabs part.

@brlcad
Copy link

brlcad commented Sep 23, 2024

Annual ping, any update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editor-core Editor basic functionality editor-rendering Editor rendering issues feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests