[C++] I wrote a visual studio extension to fix the annoying smart indenting issues around UE4 macros

What’s about such command? It fixes current doc after format by replacing >1 indents with exactly 1 on the specifier line and next if empty after macro ignoring access modifiers.



using EnvDTE;
using EnvDTE80;
using System.Text.RegularExpressions;

public class C : VisualCommanderExt.ICommand
{
    private static readonly string MACRO_REGEX = @"^(?<leading_whitespace>\s]*)(UPROPERTY|UFUNCTION|GENERATED_(USTRUCT_|UCLASS_|(U|I)INTERFACE_)?BODY)\(.*\)\s*(?<body>\S]*).*";
    private static readonly string ACCESS_MODIFIER = @"^.*(public|private|protected)\:.*";

    public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
    {
        string macroLine;
        string indent = "";
        bool fixNext = false;
        TextSelection sel = (TextSelection)DTE.ActiveDocument.Selection;
        var editPoint = sel.ActivePoint.CreateEditPoint();
        editPoint.EndOfDocument();
        var lineNum = editPoint.Line;
        var macroLineNum = 0;

        while (++macroLineNum <= lineNum) {
            macroLine = editPoint.GetLines(macroLineNum, macroLineNum+1);
            if (fixNext) {
                sel.GotoLine(macroLineNum);
                sel.DeleteWhitespace(EnvDTE.vsWhitespaceOptions.vsWhitespaceOptionsHorizontal);
                var modMatch=Regex.Match(macroLine, ACCESS_MODIFIER);
                if (!modMatch.Success && indent != "") sel.Indent();
                fixNext = false;
            }
            var macroMatch=Regex.Match(macroLine, MACRO_REGEX);
            if (macroMatch.Success) {
                sel.GotoLine(macroLineNum);
                sel.DeleteWhitespace(EnvDTE.vsWhitespaceOptions.vsWhitespaceOptionsHorizontal);
                indent = macroMatch.Groups"leading_whitespace"].ToString();
                if (indent != "") sel.Indent();
                fixNext = (macroMatch.Groups"body"].ToString() == "");
            }
        }
    }
}