public CSharpCodeGenerationPreferences(
     bool placeSystemNamespaceFirst,
     ExpressionBodyPreference preferExpressionBodiedMethods,
     ExpressionBodyPreference preferExpressionBodiedAccessors,
     ExpressionBodyPreference preferExpressionBodiedProperties,
     ExpressionBodyPreference preferExpressionBodiedIndexers,
     ExpressionBodyPreference preferExpressionBodiedConstructors,
     ExpressionBodyPreference preferExpressionBodiedOperators,
     ExpressionBodyPreference preferExpressionBodiedLocalFunctions,
     NamespaceDeclarationPreference namespaceDeclarations,
     AddImportPlacement preferredUsingDirectivePlacement,
     LanguageVersion languageVersion)
     : base(placeSystemNamespaceFirst)
 {
     PreferExpressionBodiedMethods        = preferExpressionBodiedMethods;
     PreferExpressionBodiedAccessors      = preferExpressionBodiedAccessors;
     PreferExpressionBodiedProperties     = preferExpressionBodiedProperties;
     PreferExpressionBodiedIndexers       = preferExpressionBodiedIndexers;
     PreferExpressionBodiedConstructors   = preferExpressionBodiedConstructors;
     PreferExpressionBodiedOperators      = preferExpressionBodiedOperators;
     PreferExpressionBodiedLocalFunctions = preferExpressionBodiedLocalFunctions;
     NamespaceDeclarations            = namespaceDeclarations;
     PreferredUsingDirectivePlacement = preferredUsingDirectivePlacement;
     LanguageVersion = languageVersion;
 }
        private static async Task <Document> GetTransformedDocumentAsync(
            Document document,
            CompilationUnitSyntax compilationUnit,
            IEnumerable <UsingDirectiveSyntax> allUsingDirectives,
            AddImportPlacement placement,
            CancellationToken cancellationToken)
        {
            var bannerService = document.GetRequiredLanguageService <IFileBannerFactsService>();

            // Expand usings so that they can be properly simplified after they are relocated.
            var compilationUnitWithExpandedUsings = await ExpandUsingDirectivesAsync(document, compilationUnit, allUsingDirectives, cancellationToken).ConfigureAwait(false);

            // Remove the file header from the compilation unit so that we do not lose it when making changes to usings.
            var(compilationUnitWithoutHeader, fileHeader) = RemoveFileHeader(compilationUnitWithExpandedUsings, bannerService);

            // A blanket warning that this codefix may change code so that it does not compile.
            var warningAnnotation = WarningAnnotation.Create(CSharpAnalyzersResources.Warning_colon_Moving_using_directives_may_change_code_meaning);

            var newCompilationUnit = placement == AddImportPlacement.InsideNamespace
                ? MoveUsingsInsideNamespace(compilationUnitWithoutHeader, warningAnnotation)
                : MoveUsingsOutsideNamespaces(compilationUnitWithoutHeader, warningAnnotation);

            // Re-attach the header now that using have been moved and LeadingTrivia is no longer being altered.
            var newCompilationUnitWithHeader = AddFileHeader(newCompilationUnit, fileHeader);
            var newDocument = document.WithSyntaxRoot(newCompilationUnitWithHeader);

            // Simplify usings now that they have been moved and are in the proper context.
            var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

            return(await Simplifier.ReduceAsync(newDocument, Simplifier.Annotation, options, cancellationToken).ConfigureAwait(false));
        }
示例#3
0
        internal void TestParseUsingDirectivesPlacement(string optionString, AddImportPlacement parsedValue, ReportDiagnostic?severity)
        {
            var defaultValue = new CodeStyleOption2 <AddImportPlacement>(AddImportPlacement.InsideNamespace, NotificationOption2.Error);

            severity ??= ReportDiagnostic.Error;
            var codeStyleOption = CSharpCodeStyleOptions.ParseUsingDirectivesPlacement(optionString, defaultValue);

            Assert.NotSame(defaultValue, codeStyleOption);
            Assert.Equal(parsedValue, codeStyleOption.Value);
            Assert.Equal(severity, codeStyleOption.Notification.Severity);
        }