Visual Studio 2013 / TFS:以一种风格工作,在另一种风格中提交?

时间:2014-12-11 14:59:59

标签: visual-studio tfs visual-studio-2013 coding-style tfs2013

我想知道是否有办法在我签出文件时这样做,它格式化为我的Visual Studio样式,但它在签入之前格式化回自定义样式(请注意,任何样式都可以配置在Visual Studio中,但我只知道如何一次配置一个)。我想自动化这个,但即使能够让Visual Studio记住我可以手动切换的两种不同样式也会有所帮助(例如,CTRL-K CTRL-D代表样式1,CTRL-K CTRL-E代表样式2 )。

到目前为止,研究只为TFS提供了服务器端事件处理程序,但我想在我的桌面上使用''溶液

最糟糕的情况我会尝试编写自己的VS扩展程序,但这是其中之一“我不能成为第一个想要这样做的人”#39;时刻。

1 个答案:

答案 0 :(得分:1)

所以我已经找到了部分解决方案。这不是很好或很漂亮,但对于有类似愿望的人来说,这是一个不错的起点。我希望将来能够改进它。

首先,在Visual Studio中以正常方式设置样式1,以便CTRL-K,CTRL-D将其格式化为样式1.

其次,下载并安装该软件以制作Visual Studio扩展。 Here是它的基本指南。

第三,为菜单命令创建一个开箱即用的标准VSPackage。 Here是基础知识。

第四,在创建的代码中,在MenuItemCallback中添加以下内容。这将设置一个自定义函数(示例中为editFiles)以在签入时运行。注意,这需要您在单击菜单项时查看团队资源管理器的Pending Changes页面(我计划在不必这样做。)

ITeamExplorer teamExplorer = (ITeamExplorer)this.GetService(typeof(ITeamExplorer));

if(teamExplorer != null)
{
    if(teamExplorer.CurrentPage != null && teamExplorer.CurrentPage.GetType().ToString() == "Microsoft.VisualStudio.TeamFoundation.VersionControl.PendingChanges.PendingChangesPageVS")
    {
        if (teamExplorer.CurrentPage.PageContent != null)
        {
            Type pendingChangesPageViewType = teamExplorer.CurrentPage.PageContent.GetType();
            FieldInfo field = pendingChangesPageViewType.GetField("checkinButton", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);
            System.Windows.Controls.Button checkinButton = (System.Windows.Controls.Button)field.GetValue(teamExplorer.CurrentPage.PageContent);
            checkinButton.Click += new System.Windows.RoutedEventHandler(editFiles);
        }
    }
    else
    {
        IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
        Guid clsid = Guid.Empty;
        int result;
        Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(
                0,
                ref clsid,
                "VSPTest01",
                string.Format(CultureInfo.CurrentCulture, "This function only works on the Pending Changes page of Team Explorer."),
                string.Empty,
                0,
                OLEMSGBUTTON.OLEMSGBUTTON_OK,
                OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
                OLEMSGICON.OLEMSGICON_INFO,
                0,        // false
                out result));
    }
}

第五,在MenuItemCallback所在的同一个类中,添加按钮点击时调用的函数。

public void editFiles(object sender, RoutedEventArgs e)
{
    EnvDTE.IVsExtensibility extensibility = GetService(typeof(EnvDTE.IVsExtensibility)) as EnvDTE.IVsExtensibility;
    EnvDTE80.DTE2 dte = extensibility.GetGlobalsObject(null).DTE as EnvDTE80.DTE2;

    VersionControlExt vce = dte.GetObject("Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt") as VersionControlExt;

    foreach(var pendingChange in vce.PendingChanges.IncludedChanges)
    {
        string filePath = pendingChange.LocalItem;
        //Do your style changes here, file by file, however you would like.
        //Preferably have some styling software you call.
    }
}

最后,将此VSPackage添加到Visual Studio中。

正如我所说,这是一个粗略的解决方案,仍然需要一些工作。一旦我得到了解决方案,我就会改进这一点。如果有人知道更好的事情,请一定要用它回答这个问题。