Search code examples
visual-studiovisual-studio-extensions

Visual Studio Extension Selection.Text


I am creating Visual Studio Extension and I have a problem with Selection.Text.

My code looks like this:

  DTE dte = Package.GetGlobalService(typeof(DTE)) as DTE;
    
            if (dte.ActiveDocument != null)
            {
                var selection = (TextSelection)dte.ActiveDocument.Selection;
                string text = selection.Text;
                selection.Text = text;
            }

This code should only to get and set same text. When I call this extension with input text like this :

 {
        "$schema": "****",
        "contentVersion": "*******",
        "functions": [
        ],

My extension would do this:

{
        "$schema": "****",
                "contentVersion": "*******",
                        "functions": [
                                ],}

Why is this happening? Why each line is moved to right by one tab?

Does anyone know how to get and set some text without selection in Visual Studio extension?

Does anyone know how to set folds in Visual Studio extension?


Solution

  • Visual Studio automatically formats code when using selection.Text = text;

    You can use selection.Insert(text); instead to set text as is.