Search |
||
No tabs? Yes, you ARE nuts!Posted by opinali on September 26, 2007 at 10:28 AM PDT
Quote: (WHAT? NO TABS? ... yes, no tabs ... ARE YOU NUTS? ... at times ... WHY? ... because tabs create a source display problem ... BUT IT WORKS FINE FOR ME IN VI/EMACS! ... yes, but what about everybody else?) Tabs are only a problem if you mix tabs and spaces before the first non-whitespace char, or indent some lines with tabs and others with spaces, or if you use tabs after the first non-whitespace char. If you have discipline to use tabs the way God intended when He created the ASCII charset; that is, using tabs (and only tabs) for indentation only - then tabs have the advantage of allowing each programmer to pick his or her preferred indentation size, and none of the claimed disadvantages. I have written a small Java utility that will scan a directory tree with text files, and fix tab/whitespace usage automatically. I wrote this utility after searching, and not finding, anything similar on the Internet - it seems there are some utilities (created from programmers from the dark side of the force) that will do the reverse operation of expanding tabs to spaces. I think only Jalopy would do what I wanted, but at the cost of forcing me to run a full reformat. The code is very small, so I'm just including it in this blog; enjoy if you find it useful. It's released to the public domain. The program is easy to use and efficient, and I won't claim that any code is bug-free but I've been using it for a couple years without any issues. I fear however, that this code may be confused by filesystems with links (limitation of java.io), or arbitrary Unicode data (I assume 8-bit chars). And I didn't have time to add really cool features like IDE integrations. Clearly it's not a sufficiently advanced program to make me the next famous open source project leader, so I'm posting this here just to help other programmers who are in the Right side of the Tab Wars... yeah, you know what you're supposed to do now: just wait till all your coworkers go home next Friday night; checkout the entire company repositories' sources; run FixTabs from the root; commit. ;-)
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.regex.Pattern;
/**
* Fixes usage of physical tabs in text files: Tabs are mandatory for indentation,
* and only allowed for that purpose (forbidden after first non-blank in line).
*
* @author osvaldo
*/
public class FixTabs
{
private static int spacesPerTab = 4;
private static final ByteArrayOutputStream baos = new ByteArrayOutputStream();
private static final String[] defaultIncludes = { ".*\\.java", ".*\\.properties" };
private static final String[] defaultExcludes =
{
"\\..*", // .svn, Unix, Eclipse hidden directories
"CVS", // CVS
"bin", // Common output directory for Eclipse
"dist", // Common output directory for NetBeans
"build", // Common output directory for NetBeans
"nbproject", // Common output directory for NetBeans
"target", // Common output directory for Maven
};
public static void main (final String[] args)
{
if (args.length > 3 ||
(args.length > 0 && ("-?".equals(args[0]) || "-help".equals(args[1]))))
help();
final File root = new File(args.length < 1 ? "." : args[0]);
if (args.length >= 2) try
{
spacesPerTab = Integer.parseInt(args[1]);
}
catch (NumberFormatException e)
{
help();
}
final ArrayList
»
Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|