microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
162.62k stars 28.67k forks source link

Smart Tabs - Feature Request #33974

Open jednano opened 7 years ago

jednano commented 7 years ago

Some editors support what's called "smart tabs." The spec for vscode might look like the following:

Indentation defined by the nesting of code blocks is made of tabs, while any alignment that follows is made only of spaces.

Conceptually, this would ensure that mixed tabs and spaces for indentation would never occur.

tabs-vs-spaces

IntelliJ IDEA | Tabs and Indents | Smart Tabs Related discussion: https://github.com/editorconfig/editorconfig/issues/323#issuecomment-327935294

rmunn commented 7 years ago

Note that https://github.com/Microsoft/vscode/issues/4039#issuecomment-289645998 is a request for the same feature. However, #4039 tracks a different issue, so this is not a duplicate.

rmunn commented 7 years ago

Another non-duplicate: #14982 calls for the "Smart tabs" option from Visual Studio to be implemented in VS Code. However, despite sharing a name, this is not the same feature as what is being requested here. The "Smart tabs" option in Visual Studio is language-aware indentation: pressing Enter after typing an opening brace will automatically indent the next line by one more indentation unit without the user needing to press the Tab key. This feature request, on the other hand, is to implement what Emacs calls Smart Tabs: correctly mixing tabs and spaces at the start of the line, in the only way that will work for everyone. (I.e., tabs for indentation and spaces for alignment).

mr-intj commented 6 years ago

Here's a page devoted to "indent with tabs, align with spaces" for those who are new to the distinction: https://dmitryfrank.com/articles/indent_with_tabs_align_with_spaces

I'm going to look into whether this could be done with a VS Code extension. I've searched, but was unable to find one(*). If anyone knows of one, please do tell.


(*) I did find 'Dynamic Tab Insertion' by zhiayang (https://github.com/zhiayang/vscode-tabindentspacealign), but as of Aug 25, 2018 it didn't work for me in VS Code 1.26.1.

j-zeppenfeld commented 5 years ago

Since there seems to be no interest nor understanding from the VS Code team for supporting tabs for indentation and spaces for alignment (as seen by the responses or lack thereof to this and related issues), I ended up writing an extension to achieve this:

Tab-Indent Space-Align

I've used it a fair bit before publishing it and it has worked great for me, but I am sure that there are some corner cases that I overlooked. Feedback is very welcome!

totkeks commented 5 years ago

Emacs also supports this for a long time and has a good wiki article on the topic: https://www.emacswiki.org/emacs/SmartTabs

@j-zeppenfeld you can also just use prettier(js) and the corresponding vscode plugin. They support smart tabs.

vscodebot[bot] commented 4 years ago

We try to keep VS Code lean and we think the functionality you're asking for is great for a VS Code extension. Maybe you can already find one that suits you in the VS Code Marketplace. Just in case, in a few simple steps you can get started writing your own extension. See also our issue reporting guidelines.

Happy Coding!

jednano commented 4 years ago

Did a robot just decide the fate of this issue because of inactivity? I'm very confused by this and I think it makes way more sense if it's built in. Be nice to have a human from Microsoft weigh in on this conversation.

j-zeppenfeld commented 4 years ago

I fear that writing an extension to provide basic support for smart tabs was a mistake if it lead to this issue being closed...

Indentation is such a fundamental feature of an editor that writing an extension to perform it will always have issues. For example, autoindentation is impossible to implement in an extension, given that the current language configuration cannot be retrieved.

Many other features integral to the editor (such as adjusting indentation on paste, etc.) would all have to be reimplemented by an extension. As stated above, indentation is simply too fundamental a feature to be completely replaced by an extension - it quickly turns into reimplementing significant parts of the editor.

@jedmao I believe the bot closed this because it was tagged as extension candidate. @bpasero Please consider reopening this feature request. Smart tabs cannot be fully implemented as an extension.

mr-intj commented 4 years ago

This kind of tabs/spaces trade-off really ought to be the preferred 'good practice'. I suspect it isn't only because it's not widely implemented yet.

Seems like it should be a core behavior, but I've been watching this with the intent of writing (or at least contributing to) the extension if this doesn't eventually get any love from the dev team.

Right now I'm slammed, but in another couple of months, who knows?

jednano commented 4 years ago

@roblourens can you clarify why the *extension-candidate tag was added to this issue? Which ultimately led it to being closed by a bot?

roblourens commented 4 years ago

I yield to @bpasero...

bpasero commented 4 years ago

@roblourens

image

roblourens commented 4 years ago

Back then it didn't cause the issue to be closed, it was just a suggestion

jednano commented 4 years ago

Thanks @bpasero! ❤️

Is there any additional context that the community can provide about this feature or is it completely clear? Just wondering what the next steps would be before being pulled into a milestone?

totkeks commented 4 years ago

@LAK132 this is not an example of Smart Tabs. The whole point behind Smart Tabs is, that you use tabs for indentation and spaces for alignment. Your example just shows that someone didn't use them properly, namely spaces for indentation of the for loops and insufficient tabs for the indentation of the three code lines.

LeaVerou commented 4 years ago

Suggesting an extension for something as fundamental as this, is like making an editor that only supports spaces and saying write an extension to support tabs if you want that. Smart tabs are the proper way to use tabs for indentation, and is a rather significant lacking that VS Code doesn't support them (FWIW Atom does).

jednano commented 4 years ago

It’s like a smart phone, with all its bells and whistles; yet, can’t perform basic phone features. I loved how Windows Phone would let you link contacts without merging them. It’s a feature I miss so much these days and I consider it to be a pretty basic requirement of a “phone.” Show us how it’s done, Microsoft! You have an excellent team here.

mloureiro commented 2 years ago

Did anyone find an alternative for this? I keep going back and forward between vscode and webstorm, and this is one of the few reasons why.

totkeks commented 4 months ago

I just came back here after watching the latest video by Scott Hanselman where they shortly touched the topic tabs VS spaces, and thought, hey, let's discuss this for VSCode. To my surprise there already was an issue and I already commented here years ago.

I'd rather open a discussion thread though, but those are limited to discussing extensions here, which is a bit of a pity. So if someone has a better suggestion where to discuss this, because I think an issue / feature request is not the right place, yet, before the fundamentals of how deep these changes would reach into the architecture, have been discussed.

In particular I'm referring to, how and what would need to be changed to allow support for smart tabs in VSCode. Which APIs would need to be touched in the data and visual layers and how the storage layer would look like. Probably language server protocols too.

But first, so we are on the same page, what are smart tabs? It basically boils down to differentiating between alignment and indentation when it comes to the use of whitespace. Indentation is a unit-less measure, so it works nicely with both spaces (fixed width) and tabs (dynamic width based on user choice in the visual layer). Alignment on the other hand comes with a unit, number of characters, and is therefore easy to break when using a dynamic width tab character. Hence tabs must not be used for alignment.

And because tabs have been used for alignment in the past, they started this whole tabs VS spaces debate.

So I'm asking again. If anyone knows a better place to discuss the fundamentals of this, please let me know. Or which maintainer to ping for input.

plinss commented 4 months ago

Hi @totkeks I'm glad to see any kind of action/interest on this issue, but I'm not sure this is the best place to discuss what smart tabs are. There are a number of places where they're discussed, e.g. what they are and why you want them. I don't think there's debate about what this feature should be, there just seems to be a lack of interest in actually implementing it.

totkeks commented 4 months ago

Yes, I see that lack. Which is why I don't want to discuss what smart tabs are, but instead what needs to be done to implement them. Analyze it first, so a more elaborate feature request can be created that makes it easier to estimate the actual work required and impact on the VSCode core.