Пример #1
0
            private static CSharpCodeGenerator Create(
                InsertionPoint insertionPoint,
                SelectionResult selectionResult,
                AnalyzerResult analyzerResult,
                CSharpCodeGenerationOptions options,
                NamingStylePreferencesProvider namingPreferences,
                bool localFunction)
            {
                if (ExpressionCodeGenerator.IsExtractMethodOnExpression(selectionResult))
                {
                    return(new ExpressionCodeGenerator(insertionPoint, selectionResult, analyzerResult, options, namingPreferences, localFunction));
                }

                if (SingleStatementCodeGenerator.IsExtractMethodOnSingleStatement(selectionResult))
                {
                    return(new SingleStatementCodeGenerator(insertionPoint, selectionResult, analyzerResult, options, namingPreferences, localFunction));
                }

                if (MultipleStatementsCodeGenerator.IsExtractMethodOnMultipleStatements(selectionResult))
                {
                    return(new MultipleStatementsCodeGenerator(insertionPoint, selectionResult, analyzerResult, options, namingPreferences, localFunction));
                }

                throw ExceptionUtilities.UnexpectedValue(selectionResult);
            }
Пример #2
0
            private async Task <bool> TryInitializeAsync(
                AbstractGenerateConstructorFromMembersCodeRefactoringProvider service,
                Document document,
                TextSpan textSpan,
                INamedTypeSymbol containingType,
                Accessibility?desiredAccessibility,
                ImmutableArray <ISymbol> selectedMembers,
                NamingStylePreferencesProvider fallbackOptions,
                CancellationToken cancellationToken)
            {
                if (!selectedMembers.All(IsWritableInstanceFieldOrProperty))
                {
                    return(false);
                }

                SelectedMembers = selectedMembers;
                ContainingType  = containingType;
                Accessibility   = desiredAccessibility ?? (ContainingType.IsAbstractClass() ? Accessibility.Protected : Accessibility.Public);
                TextSpan        = textSpan;
                if (ContainingType == null || ContainingType.TypeKind == TypeKind.Interface)
                {
                    return(false);
                }

                IsContainedInUnsafeType = service.ContainingTypesOrSelfHasUnsafeKeyword(containingType);

                var rules = await document.GetNamingRulesAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);

                Parameters          = DetermineParameters(selectedMembers, rules);
                MatchingConstructor = GetMatchingConstructorBasedOnParameterTypes(ContainingType, Parameters);
                // We are going to create a new contructor and pass part of the parameters into DelegatedConstructor,
                // so parameters should be compared based on types since we don't want get a type mismatch error after the new constructor is genreated.
                DelegatedConstructor = GetDelegatedConstructorBasedOnParameterTypes(ContainingType, Parameters);
                return(true);
            }
            private async Task <bool> TryInitializeAsync(
                ImmutableArray <ISymbol> selectedMembers,
                Document document,
                NamingStylePreferencesProvider fallbackOptions,
                CancellationToken cancellationToken)
            {
                ContainingType = selectedMembers[0].ContainingType;

                var rules = await document.GetNamingRulesAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);

                var parametersForSelectedMembers = DetermineParameters(selectedMembers, rules);

                if (!selectedMembers.All(IsWritableInstanceFieldOrProperty) ||
                    ContainingType == null ||
                    ContainingType.TypeKind == TypeKind.Interface ||
                    parametersForSelectedMembers.IsEmpty)
                {
                    return(false);
                }

                ConstructorCandidates = await GetConstructorCandidatesInfoAsync(
                    ContainingType, selectedMembers, document, parametersForSelectedMembers, cancellationToken).ConfigureAwait(false);

                return(!ConstructorCandidates.IsEmpty);
            }
Пример #4
0
        /// <summary>
        /// Gets the set of naming rules the user has set for this document.  Will include a set of default naming rules
        /// that match if the user hasn't specified any for a particular symbol type.  The are added at the end so they
        /// will only be used if the user hasn't specified a preference.
        /// </summary>
        public static async Task <ImmutableArray <NamingRule> > GetNamingRulesAsync(
            this Document document, NamingStylePreferencesProvider fallbackOptions, CancellationToken cancellationToken)
        {
            var options = await document.GetNamingStylePreferencesAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);

            return(options.CreateRules().NamingRules.AddRange(FallbackNamingRules.Default));
        }
Пример #5
0
 public MultipleStatementsCodeGenerator(
     InsertionPoint insertionPoint,
     SelectionResult selectionResult,
     AnalyzerResult analyzerResult,
     CSharpCodeGenerationOptions options,
     NamingStylePreferencesProvider namingPreferences,
     bool localFunction)
     : base(insertionPoint, selectionResult, analyzerResult, options, namingPreferences, localFunction)
 {
 }
Пример #6
0
            public static Task <GeneratedCode> GenerateAsync(
                InsertionPoint insertionPoint,
                SelectionResult selectionResult,
                AnalyzerResult analyzerResult,
                CSharpCodeGenerationOptions options,
                NamingStylePreferencesProvider namingPreferences,
                bool localFunction,
                CancellationToken cancellationToken)
            {
                var codeGenerator = Create(insertionPoint, selectionResult, analyzerResult, options, namingPreferences, localFunction);

                return(codeGenerator.GenerateAsync(cancellationToken));
            }
Пример #7
0
            protected CSharpCodeGenerator(
                InsertionPoint insertionPoint,
                SelectionResult selectionResult,
                AnalyzerResult analyzerResult,
                CSharpCodeGenerationOptions options,
                NamingStylePreferencesProvider namingPreferences,
                bool localFunction)
                : base(insertionPoint, selectionResult, analyzerResult, options, namingPreferences, localFunction)
            {
                Contract.ThrowIfFalse(SemanticDocument == selectionResult.SemanticDocument);

                var nameToken = CreateMethodName();

                _methodName = nameToken.WithAdditionalAnnotations(MethodNameAnnotation);
            }
            public static async Task <State?> GenerateAsync(
                ImmutableArray <ISymbol> selectedMembers,
                Document document,
                NamingStylePreferencesProvider fallbackOptions,
                CancellationToken cancellationToken)
            {
                var state = new State();

                if (!await state.TryInitializeAsync(
                        selectedMembers, document, fallbackOptions, cancellationToken).ConfigureAwait(false))
                {
                    return(null);
                }

                return(state);
            }
            protected CodeGenerator(InsertionPoint insertionPoint, SelectionResult selectionResult, AnalyzerResult analyzerResult, TCodeGenerationOptions options, NamingStylePreferencesProvider namingPreferences, bool localFunction)
            {
                Contract.ThrowIfFalse(insertionPoint.SemanticDocument == analyzerResult.SemanticDocument);

                InsertionPoint   = insertionPoint;
                SemanticDocument = insertionPoint.SemanticDocument;

                SelectionResult = selectionResult;
                AnalyzerResult  = analyzerResult;

                Options           = options;
                NamingPreferences = namingPreferences;
                LocalFunction     = localFunction;

                MethodNameAnnotation       = new SyntaxAnnotation();
                CallSiteAnnotation         = new SyntaxAnnotation();
                MethodDefinitionAnnotation = new SyntaxAnnotation();
            }
Пример #10
0
            public static async Task <State?> TryGenerateAsync(
                AbstractGenerateConstructorFromMembersCodeRefactoringProvider service,
                Document document,
                TextSpan textSpan,
                INamedTypeSymbol containingType,
                Accessibility?desiredAccessibility,
                ImmutableArray <ISymbol> selectedMembers,
                NamingStylePreferencesProvider fallbackOptions,
                CancellationToken cancellationToken)
            {
                var state = new State();

                if (!await state.TryInitializeAsync(service, document, textSpan, containingType, desiredAccessibility, selectedMembers, fallbackOptions, cancellationToken).ConfigureAwait(false))
                {
                    return(null);
                }

                return(state);
            }
Пример #11
0
    public static async ValueTask <ExtractMethodGenerationOptions> GetExtractMethodGenerationOptionsAsync(this Document document, ExtractMethodGenerationOptions?fallbackOptions, CancellationToken cancellationToken)
    {
        fallbackOptions ??= ExtractMethodGenerationOptions.GetDefault(document.Project.LanguageServices);

        var extractOptions = fallbackOptions.Value.ExtractOptions;
        var codeGenerationOptions = await document.GetCodeGenerationOptionsAsync(fallbackOptions.Value.CodeGenerationOptions, cancellationToken).ConfigureAwait(false);

        var addImportOptions = await document.GetAddImportPlacementOptionsAsync(fallbackOptions.Value.AddImportOptions, cancellationToken).ConfigureAwait(false);

        var documentOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

        var namingPreferences = documentOptions.GetOption(NamingStyleOptions.NamingPreferences, document.Project.Language);
        var namingPreferencesProvider = new NamingStylePreferencesProvider(language => namingPreferences);

        return(new ExtractMethodGenerationOptions(
                   extractOptions,
                   codeGenerationOptions,
                   addImportOptions,
                   namingPreferencesProvider));
    }
Пример #12
0
 internal abstract Task <string> GetFieldNameAsync(Document document, IPropertySymbol propertySymbol, NamingStylePreferencesProvider fallbackOptions, CancellationToken cancellationToken);
Пример #13
0
        public static async Task <NamingRule> GetApplicableNamingRuleAsync(this Document document, ISymbol symbol, NamingStylePreferencesProvider fallbackOptions, CancellationToken cancellationToken)
        {
            var rules = await document.GetNamingRulesAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);

            foreach (var rule in rules)
            {
                if (rule.SymbolSpecification.AppliesTo(symbol))
                {
                    return(rule);
                }
            }

            throw ExceptionUtilities.Unreachable;
        }
Пример #14
0
        public static async Task <NamingRule> GetApplicableNamingRuleAsync(
            this Document document, SymbolKindOrTypeKind kind, DeclarationModifiers modifiers, Accessibility?accessibility, NamingStylePreferencesProvider fallbackOptions, CancellationToken cancellationToken)
        {
            var rules = await document.GetNamingRulesAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);

            foreach (var rule in rules)
            {
                if (rule.SymbolSpecification.AppliesTo(kind, modifiers, accessibility))
                {
                    return(rule);
                }
            }

            throw ExceptionUtilities.Unreachable;
        }
Пример #15
0
 protected abstract Task <GeneratedCode> GenerateCodeAsync(InsertionPoint insertionPoint, SelectionResult selectionResult, AnalyzerResult analyzeResult, CodeGenerationOptions options, NamingStylePreferencesProvider namingPreferences, CancellationToken cancellationToken);
Пример #16
0
        public static async ValueTask <NamingStylePreferences> GetNamingStylePreferencesAsync(this Document document, NamingStylePreferencesProvider fallbackOptionsProvider, CancellationToken cancellationToken)
        {
            var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false);

            if (configOptions.TryGetEditorConfigOption <NamingStylePreferences>(NamingStyleOptions.NamingPreferences, out var value))
            {
                return(value);
            }

            return(await fallbackOptionsProvider.GetOptionsAsync(document.Project.LanguageServices, cancellationToken).ConfigureAwait(false));
        }
 protected override Task <GeneratedCode> GenerateCodeAsync(InsertionPoint insertionPoint, SelectionResult selectionResult, AnalyzerResult analyzeResult, CodeGenerationOptions options, NamingStylePreferencesProvider namingPreferences, CancellationToken cancellationToken)
 => CSharpCodeGenerator.GenerateAsync(insertionPoint, selectionResult, analyzeResult, (CSharpCodeGenerationOptions)options, namingPreferences, LocalFunction, cancellationToken);
        private static async Task <bool> NameIsHighlyUnlikelyToWarrantSymbolAsync(
            Document document, State state, SymbolKind kind, Accessibility accessibility, NamingStylePreferencesProvider fallbackOptions, CancellationToken cancellationToken)
        {
            // Check If the user explicitly used _ as the start of the name they're generating.  Don't offer to generate
            // a non-field symbol unless that's genuinely the naming style they have setup.
            if (state.IdentifierToken.ValueText.StartsWith("_"))
            {
                var namingStyle = await document.GetApplicableNamingRuleAsync(kind, accessibility, fallbackOptions, cancellationToken).ConfigureAwait(false);

                if (namingStyle.NamingStyle.Prefix != "_")
                {
                    return(true);
                }
            }

            return(false);
        }
        internal override async Task <string> GetFieldNameAsync(Document document, IPropertySymbol property, NamingStylePreferencesProvider fallbackOptions, CancellationToken cancellationToken)
        {
            var rule = await document.GetApplicableNamingRuleAsync(
                new SymbolSpecification.SymbolKindOrTypeKind(SymbolKind.Field),
                property.IsStatic?DeclarationModifiers.Static : DeclarationModifiers.None,
                Accessibility.Private,
                fallbackOptions,
                cancellationToken).ConfigureAwait(false);

            var fieldName = rule.NamingStyle.MakeCompliant(property.Name).First();

            return(NameGenerator.GenerateUniqueName(fieldName, n => !property.ContainingType.GetMembers(n).Any()));
        }