Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort selected text from Visual Studio context menu

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?

like image 855
abatishchev Avatar asked Jul 28 '10 07:07

abatishchev


People also ask

How do you move multiple lines in VS Code?

Select the lines you want and then press: Windows: Shift + Alt + i. Mac: shift + option + i.

How can I see the output code in Visual Studio?

To display the Output window whenever you build a project, in the Options dialog box, on the Projects and Solutions > General page, select Show Output window when build starts.

How do you resolve Save conflict VS Code?

VS Code blocks saving the file to prevent overwriting changes that have been made outside of the editor. Use the actions in the editor toolbar to resolve the save conflict. You can either Accept your changes and thereby overwriting any changes on disk, or Revert to the version on disk.


2 Answers

A nice AddOn for Visual Studio is 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

like image 193
Knasterbax Avatar answered Oct 24 '22 18:10

Knasterbax


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 
like image 26
Tim Robinson Avatar answered Oct 24 '22 18:10

Tim Robinson