1

Tab and spaces are mix used on same projects.

I need to code indentation but do not replace tab with spaces and vice versa. I only need to indent code (new indentation with space or tabs does not matter).

Eclipse IDE is used but plugin or other tools are welcome.

P.S I know that at same project same white space strategy (tab or white) using is better.

As an example \t --> tab \b --> space. I think need to some property that how many space equals to tab 1 character. For our example 4.

Before

void foo() {
\b\b\btest1();  // 3 spaces
\ttest2();  // 1 tab
}

After

void foo() {
\b\b\b\btest1();  // 4 space (1 extra space to be indent correctly)
\ttest2();  // 1 tab  (do not change tab char with spaces because of that has correct indentation)
}
Erdinç Taşkın
  • 1,548
  • 3
  • 17
  • 28
  • 1
    I really have a tough time understanding your question. Eclipse indents your code as you type, or you can manually call the formatter using CTRL+SHIFT+F (also works for selections). You can either set it to spaces or tabs and have an auto save action to fix the whole file if you want that. – reto Mar 20 '14 at 07:59

2 Answers2

0

In Eclipse, in the Project menu, go into Properties -> Java Code Style and press the Edit button to edit your current profile. In this window, you can configure your indentation parameters.

To apply them, in your code press ctrl + a to select your code then press ctrl + i to format it according to your parameters

Padrus
  • 2,013
  • 1
  • 24
  • 37
  • If codes indent with space, after CTRL+A & CTRL+i combination, space replace with tabs. I don't want to this. I want to be stay as space. – Erdinç Taşkın Mar 20 '14 at 08:05
  • In the same window, you can specify 'Spaces only' for the 'Tab policy'. – Padrus Mar 20 '14 at 08:08
  • If this can't solve your problem, i may have misunderstood your question. If so, could you provide an example or somethig? – Padrus Mar 20 '14 at 08:24
  • 1
    I add an example to question, i hope more clear to being understand. – Erdinç Taşkın Mar 20 '14 at 08:39
  • OK, I understand better your question now. I don't think what you want is posible in Eclipse (even with a plugin). But if I may ask, why do you want to keep the actual mixed indentation? I don't think it's a good practice. – Padrus Mar 20 '14 at 09:24
  • If possible, do not change to file (history). Easy to compare with history. Yes, i know some diff tool support that ignoring whitespace, but in git, file has been changed but actually nothing is changed. – Erdinç Taşkın Mar 20 '14 at 09:30
  • Oh ok, I understand now. Maybe you could reindent your code correctly and try to configure Git to ignore spaces and tabs. There are some posts about it on SO. – Padrus Mar 20 '14 at 09:39
  • Reindenting existing code is extremely painful to the people who need to merge your changes into the same file in the branches they're working on. – Arfur Narf Aug 30 '23 at 17:33
0

There is an active question how to reconfigure Eclipse for spaces at How do I change Eclipse to use spaces instead of tabs?. This is very tedious.

I also encountered this issue at a project. I did not want to deal with huge IDE setup. Editorconfig is a solution

  1. Install editor config Eclipse plugin

  2. Create an .editorconfig with following content:

    root = true
    
    [*]
    indent_style = space
    indent_size = 4
    insert_final_newline = true
    charset = utf-8
    

You can do even more as changing the line ending style to LF using end_of_line = lf

This way, also users of IntelliJ and VS.Code can have the same configuration when editing the files.

koppor
  • 19,079
  • 15
  • 119
  • 161