Separate settings for indent level and tab length


#1

Hello everyone,

I recently started using Atom, and I have run into an issue that I have not been able to solve. I am working on a large C++ codebase that has been actively developed since the 80s, and the indentation style have varied over time, so in the same file tabs are mixed with spaces, even on the same line sometimes. The indentation looks OK with atom if I set tab lentgh to 8, but then when I write new code, the indentation level is also 8, which is not OK.

Is it possible to set the indentation level separate from the tab length using style sheets or some other way that I haven’t been able to figure out? I think the indentation level should be a setting next to the tab length setting, but since it isn’t I am really hoping that there is a way around that.


#2

I’m not sure what you mean by “indentation level”, because I would define it as “the number of tabs or tab-equivalent spaces before the first non-whitespace character”. But it wouldn’t make any sense for everything to suddenly jump to 8 tabs indented when you change the tab length to 8 characters.

Is it impossible to standardize the indentation style so that each file you work on has all tabs or all spaces?


#3

My first thought would be to replace the TAB with spaces.

Best to follow @DamnedScholar notes first as to reduce confusion of what is required.


#6

Forum usage notes
Use the </> button to add code.


OR… (preferred method)

```c
 your code
```

Example:

```c
if (a) {
  foo();
}
```

will give ->

if (a) {
  foo();
}

#7

Thanks for your responses.

Alright, to try to clarify: I use soft tabs and auto-indent (in case that matters).

With tab length set to 4, I get this:

if (a) {
    foo();
}

When tab length is 8, I get this:

if (a) {
        foo();
}

So the auto-indent uses the tab length setting to determine how many spaces it should use to indent the ‘foo()’ line.

I would like a separate setting for how many spaces the next indentation level should insert compared to the previous one, so that I when I write new code the editor inserts for instance 4 more spaces for each indentation level, while still interpreting a TAB character as 8 spaces (or whatever I tell it). Other editors, like qtcreator and emacs have separate settings for this.

Unfortunately, going through the entire code and converting tabs to spaces is not likely, as I would have to convince other people we need to do it although I am the only one who has a problem with it. Even if I could make it happen, it would be good to be able to handle this type of thing in the editor.


#8

That makes sense, but I have a serious question: why do you want them to be different? It would drive me absolutely mad if pressing tab for spacing had a behavior other than “indent this line one level or insert the requisite number of spaces to move the cursor to the next multiple of the tab length”. If I were building an editor from the ground up, I can’t see myself doing anything different.

You could always set the tab length to 4, and then rebind the tab key to insert two \t characters instead of one. However, if you do that in code that you then give to other people, you can expect them to get quite annoyed at you.

Large codebases that have been around for decades ought to have standardized rules of style. I understand bureaucratic inertia, but I don’t understand how anyone can tolerate writing in a code file with inconsistencies like that without fixing those inconsistencies. If your colleagues are truly that lax about formatting, then maybe it’s a lost cause.


#9

Yeah, sure, and I may have been a bit pessimistic about the likelyhood of fixing the code, we actually do usually improve stuff like this when we find it. However, to my knowledge, it is only in Atom that this is actually a problem, I never even noticed it until I switched to Atom. So for the sake of argument, can we pretend like we have code that can’t be fixed, and that Atom needs to be able to handle this somehow?

When I edit code I want the tab key to do ‘auto-indent’. In my previous editor I had another keybinding for ‘insert spaces until the next tab delimiter’ for when that is what I really wanted to do. And when it does auto-indent, I want to be able to set the number of spaces independently of the tab length.


#10

I don’t think it would be possible to make \t and one level of indentation mean different things without changing the editor’s internals. You can, however, make tab insert \t\t instead of \t, and much as that solution makes me mildly uncomfortable, it’s probably a drop in the bucket compared to what’s already been done to the codebase. Just put this in your init.coffee file:

atom.commands.onDidDispatch (evt) ->
  if evt.type == "editor:indent"
    editor = atom.workspace.getActiveTextEditor()
    editor.insertText("\t")

#11

Thanks, I’ll give it a try.