Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn off auto-formatting for a #region in Visual Studio 201x

Is there any way to turn off auto-formatting for arbitrary regions in Visual Studio?

I have automatic formatting set to indent exactly as I like. However, for a specific region (in particular, one having to do with creating an XML document), I'd like the indentation to make clear the structure of the XML document being created rather than the C# that creates it. For example:

// Format this normally.
public void WriteXMLDocument() {
    // ...
    using (XmlWriter x = XmlWriter.Create(filename)) {

    // Create XML document
    #region dont-format-this     // <-- Any way to get VS to recognize something like this?
        x.WriteStartDocument();
        x.WriteStartElement("RootElement");
            x.WriteStartElement("ChildElement1");
                x.WriteStartElement("GrandchildElement1a");
                    x.WriteElementString("GreatGrandchildElement1a1");
                    x.WriteElementString("GreatGrandchildElement1a2");
                x.WriteEndElement();
                x.WriteElementString("GrandchildElement1b");
            x.WriteEndElement();

            x.WriteStartElement("ChildElement2");
            x.WriteEndElement();
        x.WriteEndElement();
        x.WriteEndDocument();
    #endregion

    }
}

Obviously I don't expect Visual Studio to guess how to format what I'm doing, but I'd rather it just not try within this region. Otherwise every time I edit a line, VS gets ambitious and tries to undo my work.

like image 456
James Cronen Avatar asked Oct 16 '13 15:10

James Cronen


People also ask

Can you turn off auto formatting in Word?

As far as the "automatic options" are concerned, you can turn them off on the AutoFormat As You Type tab of the AutoCorrect dialog box (File > Options > AutoCorrect Options > AutoFormat As You Type).

How do I turn off auto format in Word for iPad?

Go into setting section for iPad scroll down until you see the Microsoft app and click it. Once you click the Microsoft app turn off "auto formatting".

How do I change auto format in Word?

Go to File > Options > Proofing. Select AutoCorrect Options, and then select the AutoFormat As You Type tab.


1 Answers

As far as I know there is no feature to have different behavior within the same file in Visual Studio and there is only a feature request for Resharper to add exactly this. But that hasn't been addressed yet afaik and Resharper wasn't mentioned to be available.
I would have another poor man's solution which also supports the intention that you are trying to follow to bundle statements of the same depth in style. You can add {}-brackets to enclose things that are logically inside of what you are doing. So the formating would stay.

Potential drawback (if you see it this way) is You have more lines because of the brackets:

    using (XmlWriter x = XmlWriter.Create(filename)) {
            // Create XML document

            #region dont-format-this     // <-- Any way to get VS to recognize something like this?

            x.WriteStartDocument();
            {
                x.WriteStartElement("RootElement");
                {
                    x.WriteStartElement("ChildElement1");
                    {
                        x.WriteStartElement("GrandchildElement1a");
                        {
                            x.WriteElementString("GreatGrandchildElement1a1");
                            x.WriteElementString("GreatGrandchildElement1a2");
                        }
                        x.WriteEndElement();
                        x.WriteElementString("GrandchildElement1b");
                    }
                    x.WriteEndElement();
                }

                x.WriteStartElement("ChildElement2");
                x.WriteEndElement();
            }
            x.WriteEndElement();
            x.WriteEndDocument();
        }
        #endregion

    }

This only addresses the OP Question to keep the indentation of the example problem. It is not disabling parts of the file as what is @Noseratio asking.

like image 74
Uwe Hafner Avatar answered Sep 21 '22 01:09

Uwe Hafner