Search code examples
eclipsecharacter-encodingeclipse-plugineclipse-cdt

Does eclipse support automatic replacement of dashes and other characters?


Consider the following - well - very basic code snippet:

int a = b – 2

The minus symbol in the middle is actually not a minus (or "hyphen"), but a dash. The correct snippet would look like this:

int a = b - 2 - the correct minus being slightly shorter.

So, I stumbled on this a lot of times already when copying code from some (online-)book or PDF to check it out in java. When writing the document the author probably didn't consider the word processor to replace the hyphens by dashes automatically. With java this is not a big deal - "illegal character" - and I'm done. But when coding C/C++ using the CDT, this is actually not so easy to see. And the compiler errors are not very helpful either.

So: back to my question: Is there a way, a checker, a plug-in, something - that is able to replace such invalid characters automatically by any (configured) correct values? Specifically it would be great, if that would work together with CDT. Or how would you deal with that problem (if it were a problem to you)?

Thanks


Solution

  • The only automatic solution, that prevents the use of dash instead of a hyphen symbol - and also other UTF symbols that are not permitted by most C compilers is: set the text file encoding to "US-ASCII". This can only be done for all files and languages in Eclipse however. But if accented characters or any other fancy arrows and dashes are not needed, this is a currently for me the best option to prevent inserting those characters by accident.

    This can be done in Window > Preferences > General > Workspace and in the field Text file encoding set Other > US-ASCII.

    Afterwards the dash character can still be inserted in the editor, but when saving eclipse will complain and one of the options is to select the first character, which is not of US-ASCII encoding.