How to change space indentation to tab indentation on the entire project?

已回答

If we have a project that uses indentation using spaces and tabs,

how could we convert all space indentation (preferably 2 and 4 spaces) to tab indentation on the entire project?

 

1

Configure the code style to use tabs, then run Reformat Code on the project (or sources) root.

See https://www.jetbrains.com/help/idea/editor-basics.html#reformat_rearrange_code for details.

0

Thanks for the tip, but won't that also make irrelevant changes to my project besides converting tabs?

0

Guess the best option would be the good old text replace, even though it will also replace spaces in string literals...

With a bit of regex can probably filter out false positives:

Replace

```

\n    ([^ ])

```

With

```

\n    $1

```

0
but won't that also make irrelevant changes to my project besides converting tabs?

It shouldn't make any other changes besides replacing indentation characters. Did you encounter any issues after enabling the option Settings (⌘ + , on macOS or Ctrl + Alt + S on Win/Linux) > Editor > Code Style > Java > Use tab character and then reformatting the code (⌥ ⌘ + L on macOS or Ctrl + Alt + L on Win/Linux)?

0

Wow, thanks for the reply in this ancient thread =D

For the sake of experiment, I just ran the "Reformat Code" on my project directory

https://www.jetbrains.com/help/idea/reformat-and-rearrange-code.html#reformat_module_directory

It (quite expected) produced changes not related to space-tab conversion in 1335 files. By the "irrelevant changes" that this action caused I meant stuff like this:

-       private void checkAdventuresWonByPet(PetRaw pet)
-       {
+       private void checkAdventuresWonByPet(PetRaw pet) {

0

Most of these changes are actually pretty nice, as they fix style inconsistencies in our project, but some of them erase custom formatting in places where we would rather keep it. A random example:

        @Query("SELECT new com.madsword.cat.pet.core.UserPetCount( " +
-                               "a.user1.id , " +
-                               "COUNT (a)) " +
-                       "FROM " +
-                               "ActivityRaw a " +
-                       "WHERE " +
-                               "a.user1.isHiddenFromLeaderboards = FALSE AND " +
-                               "a.type = :type " +
-                       "GROUP BY " +
-                               "a.user1 " +
-                       "ORDER BY " +
-                               "COUNT(a) DESC"
+               "a.user1.id , " +
+               "COUNT (a)) " +
+               "FROM " +
+               "ActivityRaw a " +
+               "WHERE " +
+               "a.user1.isHiddenFromLeaderboards = FALSE AND " +
+               "a.type = :type " +
+               "GROUP BY " +
+               "a.user1 " +
+               "ORDER BY " +
+               "COUNT(a) DESC"

0

I believe what OP and me would actually want is the ability to just run the _Edit -> Convert Indents_ on the entire project, without changing any other, maybe opiniated, but still intended code style choices used in various project files.

 

Well, for me it's nor relevant anymore, as I just ran the regex replace on my project, and judging that there is little upvotes on this post, guess it's not such a common problem to want to fix space-tab inconsistencies across the project.

0

There is a similar old request filed for this: https://youtrack.jetbrains.com/issue/IDEA-89028.
You can vote for it and follow its progress.

1

请先登录再写评论。