visual-studio 从 Visual Studio 上下文菜单中对所选文本进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3350793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:15:54  来源:igfitidea点击:

Sort selected text from Visual Studio context menu

visual-studiovisual-studio-2010sortingvisual-studio-addins

提问by abatishchev

Currently VS has a very useful feature: sort usings (C#).

目前 VS 有一个非常有用的功能:排序使用(C#)。

I want the same functionality for any random text, for example - XML nodes in config files.

我想要任何随机文本的相同功能,例如 - 配置文件中的 XML 节点。

How complex to implement that? VS addin, right? Is it possible to call some VS API which is used for sorting usings?

实施起来有多复杂?VS插件,对吧?是否可以调用一些用于排序使用的 VS API?

采纳答案by Yurik

Just found a good free addon: Menees VS Tools 2012(or 2010 ver) - does exactly that and a few more text tricks. There was a few minor negatives when I installed it, but after leaving a comment on the review page it got fixed within days. Waay to go! =)

刚刚找到了一个很好的免费插件:Menees VS Tools 2012(或2010 版)——正是这样做的,还有一些文本技巧。我安装它时有一些小的负面影响,但在评论页面上发表评论后,它在几天内得到了修复。加油!=)

There is a 2017 version now: Menees VS Tools 2017

现在有一个 2017 版本:Menees VS Tools 2017

回答by Tim Robinson

Edit:Note that this solution does not work on VS2013 or higher, since support for macros was removed.

编辑:请注意,此解决方案不适用于 VS2013 或更高版本,因为已删除对宏的支持。

You don't necessarily need to code a VS addin to do this: Visual Studio has macros built in. To get started, use Tools, Macros, Record Temporary Macro.

您不一定需要编写 VS 插件来执行此操作:Visual Studio 内置了宏。要开始使用,请使用工具、宏、记录临时宏。

Here's a 'Sort Lines' command I hacked together based on the code that Record Temporary Macro gave me:

这是我根据记录临时宏给我的代码编写的“排序行”命令:

Imports System
Imports EnvDTE

Public Module TimModule
    Sub SortLines()
        Dim Selection As TextSelection = DTE.ActiveDocument.Selection
        Dim Lines() As String = Selection.Text.Replace(Environment.NewLine, Chr(13)).Split(Chr(13))
        Array.Sort(Lines)
        DTE.UndoContext.Open("Sort Lines")
        ' Edit - see comments
        ' Selection.Text = String.Join(Environment.NewLine, Lines)
        Selection.Delete
        Selection.Insert(String.Join(Environment.NewLine, Lines)) 
        DTE.UndoContext.Close()
    End Sub
End Module

回答by Knasterbax

A nice AddOn for Visual Studio is Code Maid

一个不错的 Visual Studio 插件是Code Maid

You select some lines and chose from Context Menu "Sort Lines"

您选择一些行并从上下文菜单“排序行”中选择

enter image description here

在此处输入图片说明

And voilá, your lines are sorted nicely in alphabetical order:

瞧,您的行按字母顺序排列得很好:

enter image description here

在此处输入图片说明

回答by William

You can copy the code into Sublime Text, select the section of code and hit F9 (or go to Edit > Sort Lines). You can then copy it back into Visual Studio.

您可以将代码复制到 Sublime Text,选择代码部分并按 F9(或转到“编辑”>“排序行”)。然后,您可以将其复制回 Visual Studio。

回答by Philip Atanassov

I am personally using the Web Essentialsextension by Mads Kristensen. You just select the lines you want to sort and Alt+3 or Alt+4 (asc/desc).

我个人使用的是 Mads Kristensen的Web Essentials扩展。您只需选择要排序的行和 Alt+3 或 Alt+4 (asc/desc)。