Sort selected text from Visual Studio context menu

情到浓时终转凉″ 提交于 2019-12-18 18:33:25

问题


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

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

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


回答1:


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! =)

There is a 2017 version now: Menees VS Tools 2017




回答2:


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

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.

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



回答3:


A nice AddOn for Visual Studio is Code Maid

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

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




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/3350793/sort-selected-text-from-visual-studio-context-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!