Beispiel #1
0
        private async Task FormatCodeAsync()
        {
            try
            {
                Document activeDocument = await VisualStudioHelper.GetActiveDocumentAsync();

                if (VisualStudioHelper.FileIsExcludedType(activeDocument.FilePath))
                {
                    return;
                }
                SyntaxNode root = await VisualStudioHelper.GetDocumentRootAsync(activeDocument);

                bool isCSharpDocument = root.Language == VisualStudioHelper.CSharpLanguageName;
                if (!isCSharpDocument)
                {
                    return;
                }

                var        longLineFormatter = new LongLineFormatter(CSharpenerConfigSettings.LengthOfLineToBreakOn);
                SyntaxNode formattedRoot     = longLineFormatter.Visit(root);

                Document newDocument = activeDocument.WithSyntaxRoot(formattedRoot);
                bool     success     = await VisualStudioHelper.ApplyDocumentChangesAsync(newDocument);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
Beispiel #2
0
        private async Task FormatAndSortCodeAsync()
        {
            try
            {
                Document activeDocument = await VisualStudioHelper.GetActiveDocumentAsync();

                if (VisualStudioHelper.FileIsExcludedType(activeDocument.FilePath))
                {
                    return;
                }

                SyntaxNode root = await VisualStudioHelper.GetDocumentRootAsync(activeDocument);

                bool isCSharpDocument = root.Language == VisualStudioHelper.CSharpLanguageName;
                if (!isCSharpDocument)
                {
                    return;
                }

                var        regionRemover = new RegionRemover();
                SyntaxNode regionRoot    = regionRemover.Visit(root);

                var        usingsPlacer = new UsingsPlacer();
                SyntaxNode usingsRoot   = usingsPlacer.ProcessUsings(regionRoot);

                string     fileName = Path.GetFileName(activeDocument.FilePath);
                var        fhf      = new FileHeaderFormatter();
                SyntaxNode fhfRoot  = fhf.AddHeader(usingsRoot, fileName);

                var        sorter     = new CSharpSorter();
                SyntaxNode sorterRoot = sorter.Visit(fhfRoot);

                var        accessModifier = new AccessLevelModifierFormatter();
                SyntaxNode accessRoot     = accessModifier.Visit(sorterRoot);

                var        newLineFormatter = new NewlineFormatter();
                SyntaxNode newLineRoot      = newLineFormatter.Visit(accessRoot);

                var        ebf     = new ExpressionBodiedFormatter();
                SyntaxNode ebfRoot = ebf.Visit(newLineRoot);

                Document newDocument = activeDocument.WithSyntaxRoot(ebfRoot);
                bool     success     = await VisualStudioHelper.ApplyDocumentChangesAsync(newDocument);

                await VisualStudioHelper.InvokeCommandAsync(VisualStudioHelper.RunDefaultCodeCleanup);

                await VisualStudioHelper.InvokeCommandAsync(VisualStudioHelper.FormatDocumentCommandName);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }