SublimeText / ElasticTabstops

Tab characters automatically adjust to keep adjacent lines aligned.
https://github.com/SublimeText/ElasticTabstops
67 stars 6 forks source link

Translate tabs to spaces on save? #3

Closed AnnanFay closed 11 years ago

AnnanFay commented 12 years ago

Is it possible to hook into file saving and loading. When a file is saved it could replace all tabs with spaces and when it loads it replaces the last (total % tabwidth) spaces with a tab.

This would help a bit with portability of files.

Never done ST dev but I think the only things you'd need are ways to hook into file saving/loading and a way to work out how wide a tab is.

adzenith commented 12 years ago

It could work decently well, but the roundtrip from tabs to spaces to tabs is not lossless. For example, if a tab is only one space wide using the current tab-width settings and given the length of the text before it, and then we convert that tab to a space, how do we know on the way back that it was supposed to be a tab and not a space? Or conversely, if you put multiple spaces in the middle of your text, on load we would probably assume you meant for a tab to be there.

Are you having portability issues? The layout should work in any program that uses the same tab-width as Sublime Text is set up to use.

AnnanFay commented 12 years ago

I see what you mean about losing information. The only way I can think to get around that is by inspecting the lines above and below to see if they line up then inferring indentation from that.

If I open a file with another editor it should look okay, however if I start editing it I suspect things might get wacky since most editors expect you to use either tabs or spaces. I'll post specifics if I find a wacky editor.

I did a few tests on editing with vim then reopening then files with ST. Everything works fine in vim, though there's problems after reopening with ST.

Example:

I make file in ST, open it with vim using spaces. (lines 1,2) Add a new line that indents up to previous indents. (line 3) Open it with ST and try indenting up to the line made with vim. (line 4)

long_variable\t= 0; a\s\s\s\s\s\s\s\s\s\s\s\s\t= 0; b\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s=0; c\t=0;

Example2:

I make file in ST, open it with vim using tabs. (lines 1,2) Add a new line that indents up to previous indents. (line 3)

long_variable\t= 0; a\s\s\s\s\s\s\s\s\s\s\s\s\t= 0; b\t\t\t\t= 0;

Open it with ST and try indenting up to the line made with vim.

long_variable\t= 0; a\s\s\s\s\s\s\s\s\s\s\s\s\t= 0; b\s\s\s\s\s\s\s\s\s\s\s\s\t\t\t\t= 0; c\s\s\s\s\s\s\s\s\s\s\s\s\t= 0;

c4milo commented 11 years ago

does this exist already in the current dev ST2 release? is there any update?

adzenith commented 11 years ago

No, as I described above, it's a lossy transformation. You can convert to spaces yourself if you want, but then there's no way to be sure where the tabs used to be when reopening the file.

c4milo commented 11 years ago

ok, shouldn't this issue be closed then?

adzenith commented 11 years ago

Well, I've just been thinking about it and it occurs to me that something like this could be possible. If you found the longest run of non-indenting spaces in the file, and replaced each tab with that many spaces plus one, then you could almost unambiguously tell where the tabs were. I mean, unless someone just made a file with a lot of spaces. Hm.