Is there a solution-wide way to Untabify the whole files

后端 未结 2 1380
执念已碎
执念已碎 2021-02-12 16:30

I have downloaded a project and it is all using Tab , but I always use Space. There is the option that \"Edit->Advanced->Untabify\" but for then I have to select files one by

相关标签:
2条回答
  • 2021-02-12 16:48

    Your best bet is to use the Find and Replace in files (ctrl-shift-h). Turn on regular expressions, search for \t and replace with spaces. You can set the file filter to *.cs or whatever file type you need to clean up.

    0 讨论(0)
  • 2021-02-12 17:01

    I created a simple console app that untabify all files from a base directory.

    https://github.com/idenardi/TabsToSpace

    Here is the code:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    
    namespace TabToSpaces
    {
        class Program
        {
            static void Main(string[] args)
            {
                var root = args[0];
                var filePaths = Directory.GetFiles(root, "*.cs", SearchOption.AllDirectories);
    
                int updated = 0;
                foreach (var path in filePaths)
                {
                    var content = File.ReadAllLines(path);
                    string[] newfile = new string[content.Length];
    
                    int i = 0;
                    var bChanged = false;
    
                    foreach (var line in content)
                    {
                        newfile[i] = ReplaceTabs(line, out var bChangedLine);
                        i++;
    
                        if (bChangedLine)
                            bChanged = true;
                    }
    
                    if (bChanged)
                    {
                        ++updated;
                        Console.WriteLine("fixing " + path);
                        File.WriteAllLines(path, newfile, System.Text.Encoding.UTF8);
                    }
                }
    
                Console.WriteLine("fixed {0} files", updated);
                var x = Console.ReadKey();
            }
    
    
            public static string ReplaceTabs(string line, out bool bChanged)
            {
                bChanged = false;
                var i = 0;
                var lstIndex = new List<int>();
    
                foreach(char c in line)
                {
                    if (c == ' ')
                        continue;
                    else if (c == '\t')
                        lstIndex.Add(i);
                    else
                        break;
    
                    i++;
                }
    
                var lstIndexOrdered = lstIndex.OrderByDescending(c => c);
                foreach (var index in lstIndexOrdered)
                {
                    line = line.Remove(index, 1).Insert(index, "    ");
                    bChanged = true;
                }
    
                return line;
            }
        }
    }
    

    To convert you need call this app passing the base directory as the first argument:

    TabToSpace.exe C:\Projects\MyBaseDirectory

    0 讨论(0)
提交回复
热议问题