在命令行自动套用格式的代码 [英] autoformat code from command line

查看:286
本文介绍了在命令行自动套用格式的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以运行自动格式代码全部或为解决特定文件,就像(按Ctrl + K,Ctrl + D键)在Visual Studio中,但它单曲由命令行格式?
或使用ReSharper的的清理也由命令行的解决方案文件?

Is it possible to run auto-format code for all or for specific file in solution, like (Ctrl+K, Ctrl+D) formatting in Visual Studio but from it`s command line? Or use Resharper's cleanup also from command line for solution files?

推荐答案

创建自己的工具。您可以使用 EnvDTE EnvDTE80 以创建Visual Studio项目,并加载要在飞行格式的文件。一旦你完成删除Visual Studio项目。您可以指定不显示Visual Studio的窗口,同时格式化。如果你有兴趣让我知道我可以给你一些代码来完成这项工作。

Create your own tool. You can use EnvDTE, EnvDTE80 to create Visual Studio project and load the files you want to format on the fly. Once you are done delete the Visual Studio project. You can specify to not to show Visual Studio window while formatting. If you are interested let me know I can give you some code to make this work.

更​​新:
我复制我的代码。我用它来格式化* .js文件。我删除了一些代码,你不需要。随意问,如果它不能正常工作。

UPDATE: I am copying the code I have. I used it to format *.js files. I removed some code which you don't need. Feel free to ask if it doesn't work.

    //You need to make a reference to two dlls:
    envdte
    envdte80



    void FormatFiles(List<FileInfo> files)
    {       
        //If it throws exeption you may want to retry couple more times
        EnvDTE.Solution soln = System.Activator.CreateInstance(Type.GetTypeFromProgID("VisualStudio.Solution.11.0")) as EnvDTE.Solution;
        //try this if you have Visual Studio 2010
        //EnvDTE.Solution soln = System.Activator.CreateInstance(Type.GetTypeFromProgID("VisualStudio.Solution.10.0")) as EnvDTE.Solution;
        soln.DTE.MainWindow.Visible = false;
        EnvDTE80.Solution2 soln2 = soln as EnvDTE80.Solution2;
        //Creating Visual Studio project
        string csTemplatePath = soln2.GetProjectTemplate("ConsoleApplication.zip", "CSharp");
        soln.AddFromTemplate(csTemplatePath, tempPath, "FormattingFiles", false);
        //If it throws exeption you may want to retry couple more times
        Project project = soln.Projects.Item(1);

        foreach (FileInfo file in files)
        {
            ProjectItem addedItem;
            bool existingFile = false;
            int _try = 0;
            while (true)
            {            
                try
                {
                    string fileName = file.Name;
                    _try++;
                    if (existingFile)
                    {
                        fileName = file.Name.Substring(0, (file.Name.Length - file.Extension.Length) - 1);
                        fileName = fileName + "_" + _try + file.Extension;
                    }
                    addedItem = project.ProjectItems.AddFromTemplate(file.FullName, fileName);
                    existingFile = false;
                    break;
                }
                catch(Exception ex)
                {
                    if (ex.Message.Contains(file.Name) && ex.Message.Contains("already a linked file"))
                    {
                        existingFile = true;
                    }
                }
            }
            while (true)
            {
                //sometimes formatting file might throw an exception. Thats why I am using loop.
                //usually first time will work
                try
                {
                    addedItem.Open(Constants.vsViewKindCode);
                    addedItem.Document.Activate();
                    addedItem.Document.DTE.ExecuteCommand("Edit.FormatDocument");
                    addedItem.SaveAs(file.FullName);
                    break;
                }
                catch
                {
                    //repeat
                }
            }
        }
        try
        {
            soln.Close();
            soln2.Close();
            soln = null;
            soln2 = null;
        }
        catch
        {
            //for some reason throws exception. Not all the times.
            //if this doesn't closes the solution CleanUp() will take care of this thing
        }
        finally
        {
            CleanUp();
        }
    }   

    void CleanUp()
    {
        List<System.Diagnostics.Process> visualStudioProcesses = System.Diagnostics.Process.GetProcesses().Where(p => p.ProcessName.Contains("devenv")).ToList();
        foreach (System.Diagnostics.Process process in visualStudioProcesses)
        {
            if (process.MainWindowTitle == "")
            {
                process.Kill();
                break;
            }
        }
        tempPath = System.IO.Path.GetTempPath();
        tempPath = tempPath + "\\FormattingFiles";
        new DirectoryInfo(tempPath).Delete(true);
    } 



我希望这有助于。

I hope this helps.

这篇关于在命令行自动套用格式的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆