コード例 #1
0
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var cancellationToken = context.CancellationToken;

            var syntaxRoot = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
            var compilationUnit = (CompilationUnitSyntax)syntaxRoot;

#if CODE_STYLE
            var options = document.Project.AnalyzerOptions.GetAnalyzerOptionSet(syntaxRoot.SyntaxTree, cancellationToken);
            var simplifierOptions = CSharpSimplifierOptions.Create(options, fallbackOptions: null);
#else
            var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
            var simplifierOptions = await SimplifierOptions.FromDocumentAsync(document, fallbackOptions: context.Options(document.Project.LanguageServices).SimplifierOptions, cancellationToken).ConfigureAwait(false);
#endif
            var codeStyleOption = options.GetOption(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement);

            // Read the preferred placement option and verify if it can be applied to this code file.
            // There are cases where we will not be able to fix the diagnostic and the user will need to resolve
            // it manually.
            var (placement, preferPreservation) = DeterminePlacement(compilationUnit, codeStyleOption);
            if (preferPreservation)
                return;

            foreach (var diagnostic in context.Diagnostics)
            {
                context.RegisterCodeFix(
                    CodeAction.Create(
                        CSharpAnalyzersResources.Move_misplaced_using_directives,
                        token => GetTransformedDocumentAsync(document, compilationUnit, GetAllUsingDirectives(compilationUnit), placement, simplifierOptions, token),
                        nameof(CSharpAnalyzersResources.Move_misplaced_using_directives)),
                    diagnostic);
            }
        }
コード例 #2
0
    internal static CSharpSimplifierOptions GetCSharpSimplifierOptions(this AnalyzerOptions options, SyntaxTree syntaxTree)
    {
        var configOptions = options.AnalyzerConfigOptionsProvider.GetOptions(syntaxTree);
        var ideOptions    = options.GetIdeOptions();

        return(CSharpSimplifierOptions.Create(configOptions, (CSharpSimplifierOptions?)ideOptions.SimplifierOptions));
    }
コード例 #3
0
        public TypeStyleResult(CSharpTypeStyleHelper helper, TypeSyntax typeName, SemanticModel semanticModel, CSharpSimplifierOptions options, bool isStylePreferred, ReportDiagnostic severity, CancellationToken cancellationToken) : this()
        {
            _helper            = helper;
            _typeName          = typeName;
            _semanticModel     = semanticModel;
            _options           = options;
            _cancellationToken = cancellationToken;

            IsStylePreferred = isStylePreferred;
            Severity         = severity;
        }
コード例 #4
0
    internal static CSharpSimplifierOptions GetCSharpSimplifierOptions(this AnalyzerOptions options, SyntaxTree syntaxTree)
    {
        var configOptions = options.AnalyzerConfigOptionsProvider.GetOptions(syntaxTree);
        var ideOptions    = options.GetIdeOptions();

#if CODE_STYLE
        var fallbackOptions = (CSharpSimplifierOptions?)null;
#else
        var fallbackOptions = (CSharpSimplifierOptions?)ideOptions.CleanupOptions?.SimplifierOptions;
#endif
        return(CSharpSimplifierOptions.Create(configOptions, fallbackOptions));
    }
コード例 #5
0
        public TypeSyntaxSimplifierWalker(CSharpSimplifyTypeNamesDiagnosticAnalyzer analyzer, SemanticModel semanticModel, CSharpSimplifierOptions options, SimpleIntervalTree <TextSpan, TextSpanIntervalIntrospector>?ignoredSpans, CancellationToken cancellationToken)
            : base(SyntaxWalkerDepth.StructuredTrivia)
        {
            _analyzer          = analyzer;
            _semanticModel     = semanticModel;
            _options           = options;
            _ignoredSpans      = ignoredSpans;
            _cancellationToken = cancellationToken;

            var root = semanticModel.SyntaxTree.GetRoot(cancellationToken);

            _aliasedNames = PooledHashSet <string> .GetInstance();

            AddAliasedNames((CompilationUnitSyntax)root);
        }
コード例 #6
0
            public State(
                SyntaxNode declaration, SemanticModel semanticModel,
                CSharpSimplifierOptions options, CancellationToken cancellationToken)
            {
                TypeStylePreference      = default;
                IsInIntrinsicTypeContext = default;
                IsTypeApparentInContext  = default;

                var styleForIntrinsicTypes = options.VarForBuiltInTypes;
                var styleForApparent       = options.VarWhenTypeIsApparent;
                var styleForElsewhere      = options.VarElsewhere;

                _forBuiltInTypes    = styleForIntrinsicTypes.Notification.Severity;
                _whenTypeIsApparent = styleForApparent.Notification.Severity;
                _elsewhere          = styleForElsewhere.Notification.Severity;

                var stylePreferences = UseVarPreference.None;

                if (styleForIntrinsicTypes.Value)
                {
                    stylePreferences |= UseVarPreference.ForBuiltInTypes;
                }

                if (styleForApparent.Value)
                {
                    stylePreferences |= UseVarPreference.WhenTypeIsApparent;
                }

                if (styleForElsewhere.Value)
                {
                    stylePreferences |= UseVarPreference.Elsewhere;
                }

                this.TypeStylePreference = stylePreferences;

                IsTypeApparentInContext =
                    declaration.IsKind(SyntaxKind.VariableDeclaration, out VariableDeclarationSyntax? varDecl) &&
                    IsTypeApparentInDeclaration(varDecl, semanticModel, TypeStylePreference, cancellationToken);

                IsInIntrinsicTypeContext =
                    IsPredefinedTypeInDeclaration(declaration, semanticModel) ||
                    IsInferredPredefinedType(declaration, semanticModel);
            }
コード例 #7
0
 protected abstract TypeStyleResult AnalyzeTypeName(TypeSyntax typeName, SemanticModel semanticModel, CSharpSimplifierOptions options, CancellationToken cancellationToken);
コード例 #8
0
 protected override TypeStyleResult AnalyzeTypeName(TypeSyntax typeName, SemanticModel semanticModel, CSharpSimplifierOptions options, CancellationToken cancellationToken)
 => CSharpUseExplicitTypeHelper.Instance.AnalyzeTypeName(typeName, semanticModel, options, cancellationToken);