Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ 13 Ult. Changes encoding randomly

So I have many modules that have different encodings (UTF-8 and ISO-8859-7) And I set their respective encodings in the File encoding section of settings and my maven builds state the encoding of each project as well. I also have these files in git and I am not alone in these project ie other people commit in them as well. enter image description here

What happens for the last month is this: Randomly on some files the encoding changes to UTF-8 and when I try to change it back to ISO-8859-7 it doesnt change when I select the option "Reload" it does nothing. The encoding does not change. enter image description here What I have to do is the following steps:

  1. Select ISO-8859-7 as encoding
  2. Press Convert in the options
  3. then when the file is marked as changed in git I revert it
  4. only then does the encoding go back to normal

My IntelliJ version:
enter image description here

My question is this: Is this a bug or am I doing something wrong/missing something that I should have configured?

UPDATE: I want to add some more info I have found useful

  • The problem appears every time I open IntelliJ and on files that I have fixed their encoding the day before.
  • (I dont know if this is related but it happens as well) When I convert the file to the ISO encoding the encoding and then revert the file so that I can see the text, the git branch becomes detached
  • Also I am the only one working on this project so noone else changes the files commited in git
like image 874
zpontikas Avatar asked May 21 '14 08:05

zpontikas


1 Answers

I was able to reproduce this by committing a project with mixed files to Git without IntelliJ IDEA project files, which is what should be done usually because people use different IDEs.

Then if I clone the project to another directory and create a new IDEA project, the encoding settings are different, but even if I correct them, the first time a Greek file is loaded IDEA thinks it is UTF-8 because "Autodetect UTF-encoded files" is active. If the setting is inactive it is the other way around: Greek files will be displayed correctly but UTF-8 ones are shown as Greek.

Whatever went wrong, I was easily able to switch the encoding and select "reload" from the pop-up dialogue.

Then I committed the IDEA files (.idea subdirectory plus MyProject.iml file) to the same repository and cloned again. This time the files were both displayed correctly. So this is my suggested workaround. Hopefully it works for all IDEA users in your project. I have no idea what happens if e.g. an Eclipse user adds a new file to the repo which is still unknown to IDEA when you pull/fetch next time. Probably you will have to assign the right encoding again.

So how does IDEA remember which files or directories have which encoding? Look at the file .idea/encodings.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" defaultCharsetForPropertiesFiles="ISO-8859-7">
    <file url="file://$PROJECT_DIR$/src/utf8/Unicode.java" charset="UTF-8" />
    <file url="PROJECT" charset="ISO-8859-7" />
  </component>
</project>

There you can see that I have ISO-8859-7 set as the default for normal and properties files and UTF guessing switched on, just like you in your screenshots. You also see that project file $PROJECT_DIR$/src/utf8/Unicode.java has an UTF-8 encoding. What you do not see listed there are files with Greek encoding because that is the project default anyway.

Bottom line: If you at least commit .idea/encodings.xml you can preserve the encoding information. This is not a bug in IDEA in my opinion but a normal thing. How can IDEA know the encoding if you do not have a saved project state? Encodings are not stored in Git or on the file system, it is meta information. This is probably why XML files mention their own encoding right at the beginning on the very first line (as you can also see above in my snippet). Java files do not have such an encoding tag. So for mixed encoding projects you just need to be careful. ;-)

Update: My test repo is on GitHub if you like to play around.

like image 56
kriegaex Avatar answered Oct 20 '22 07:10

kriegaex