コード例 #1
0
        public static void FormatFileRangeAndAddUsingDirectives(
            ISolution solution,
            IDocument document,
            TreeTextRange treeTextRange,
            IReadOnlyCollection <IUsingDirective> usingDirectives)
        {
            IPsiSourceFile sourceFile  = document.GetPsiSourceFile(solution);
            IPsiServices   psiServices = solution.GetPsiServices();

            psiServices.Files.CommitAllDocuments();

            PsiTransactionCookie.ExecuteConditionally(
                psiServices,
                () =>
            {
                if (sourceFile?.GetPsiFile(CSharpLanguage.Instance, new DocumentRange(document, 0)) is ICSharpFile psiFile)
                {
                    psiFile.FormatFileRange(treeTextRange);

                    foreach (IUsingDirective usingDirective in usingDirectives)
                    {
                        psiFile.AddImport(usingDirective);
                    }
                }

                return(true);
            },
                "Format T4 Generated File"
                );
        }
コード例 #2
0
        public static void OrginizeUsingsAndFormatFile(ISolution solution, IDocument document)
        {
            IPsiSourceFile sourceFile  = document.GetPsiSourceFile(solution);
            IPsiServices   psiServices = solution.GetPsiServices();

            psiServices.Files.CommitAllDocuments();

            PsiTransactionCookie.ExecuteConditionally(
                psiServices,
                () =>
            {
                if (sourceFile?.GetPsiFile(CSharpLanguage.Instance, new DocumentRange(document, 0)) is ICSharpFile psiFile)
                {
                    SortImports(psiFile);

                    psiFile.OptimizeImportsAndRefs(
                        new RangeMarker(document.ToPointer(), new TextRange(0, document.DocumentRange.Length)),
                        true /* optimizeUsings */,
                        true /* shortenReferences */,
                        NullProgressIndicator.Create()
                        );

                    psiFile.FormatNode();
                }

                return(true);
            },
                "Format T4 Generated File"
                );
        }