Exemple #1
0
        public async Task <ExtractInterfaceTypeAnalysisResult> AnalyzeTypeAtPositionAsync(
            Document document,
            int position,
            TypeDiscoveryRule typeDiscoveryRule,
            CancellationToken cancellationToken)
        {
            var typeNode = await GetTypeDeclarationAsync(document, position, typeDiscoveryRule, cancellationToken).ConfigureAwait(false);

            if (typeNode == null)
            {
                var errorMessage = FeaturesResources.CouldNotExtractInterfaceSelection;
                return(new ExtractInterfaceTypeAnalysisResult(errorMessage));
            }

            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var type = semanticModel.GetDeclaredSymbol(typeNode, cancellationToken);

            if (type == null || type.Kind != SymbolKind.NamedType)
            {
                var errorMessage = FeaturesResources.CouldNotExtractInterfaceSelection;
                return(new ExtractInterfaceTypeAnalysisResult(errorMessage));
            }

            var typeToExtractFrom  = type as INamedTypeSymbol;
            var extractableMembers = typeToExtractFrom.GetMembers().Where(IsExtractableMember);

            if (!extractableMembers.Any())
            {
                var errorMessage = FeaturesResources.CouldNotExtractInterfaceTypeMember;
                return(new ExtractInterfaceTypeAnalysisResult(errorMessage));
            }

            return(new ExtractInterfaceTypeAnalysisResult(this, document, typeNode, typeToExtractFrom, extractableMembers));
        }
Exemple #2
0
        protected override async Task <SyntaxNode> GetTypeDeclarationAsync(
            Document document,
            int position,
            TypeDiscoveryRule typeDiscoveryRule,
            CancellationToken cancellationToken
            )
        {
            var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);

            var root = await tree.GetRootAsync(cancellationToken).ConfigureAwait(false);

            var token = root.FindToken(
                position != tree.Length ? position : Math.Max(0, position - 1)
                );
            var typeDeclaration = token.GetAncestor <TypeDeclarationSyntax>();

            if (typeDeclaration == null || typeDiscoveryRule == TypeDiscoveryRule.TypeDeclaration)
            {
                return(typeDeclaration);
            }

            var spanStart = typeDeclaration.Identifier.SpanStart;
            var spanEnd   =
                typeDeclaration.TypeParameterList != null
                    ? typeDeclaration.TypeParameterList.Span.End
                    : typeDeclaration.Identifier.Span.End;
            var span = new TextSpan(spanStart, spanEnd - spanStart);

            return(span.IntersectsWith(position) ? typeDeclaration : null);
        }
        public async Task<ExtractInterfaceTypeAnalysisResult> AnalyzeTypeAtPositionAsync(
            Document document,
            int position,
            TypeDiscoveryRule typeDiscoveryRule,
            CancellationToken cancellationToken)
        {
            var typeNode = await GetTypeDeclarationAsync(document, position, typeDiscoveryRule, cancellationToken).ConfigureAwait(false);
            if (typeNode == null)
            {
                var errorMessage = FeaturesResources.Could_not_extract_interface_colon_The_selection_is_not_inside_a_class_interface_struct;
                return new ExtractInterfaceTypeAnalysisResult(errorMessage);
            }

            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
            var type = semanticModel.GetDeclaredSymbol(typeNode, cancellationToken);
            if (type == null || type.Kind != SymbolKind.NamedType)
            {
                var errorMessage = FeaturesResources.Could_not_extract_interface_colon_The_selection_is_not_inside_a_class_interface_struct;
                return new ExtractInterfaceTypeAnalysisResult(errorMessage);
            }

            var typeToExtractFrom = type as INamedTypeSymbol;
            var extractableMembers = typeToExtractFrom.GetMembers().Where(IsExtractableMember);
            if (!extractableMembers.Any())
            {
                var errorMessage = FeaturesResources.Could_not_extract_interface_colon_The_type_does_not_contain_any_member_that_can_be_extracted_to_an_interface;
                return new ExtractInterfaceTypeAnalysisResult(errorMessage);
            }

            return new ExtractInterfaceTypeAnalysisResult(this, document, typeNode, typeToExtractFrom, extractableMembers);
        }
        public async Task<ExtractInterfaceTypeAnalysisResult> AnalyzeTypeAtPositionAsync(
            Document document,
            int position,
            TypeDiscoveryRule typeDiscoveryRule,
            CancellationToken cancellationToken)
        {
            var typeNode = GetTypeDeclaration(document, position, typeDiscoveryRule, cancellationToken);
            if (typeNode == null)
            {
                var errorMessage = FeaturesResources.CouldNotExtractInterfaceSelection;
                return new ExtractInterfaceTypeAnalysisResult(errorMessage);
            }

            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
            var type = semanticModel.GetDeclaredSymbol(typeNode, cancellationToken);
            if (type == null || type.Kind != SymbolKind.NamedType)
            {
                var errorMessage = FeaturesResources.CouldNotExtractInterfaceSelection;
                return new ExtractInterfaceTypeAnalysisResult(errorMessage);
            }

            var typeToExtractFrom = type as INamedTypeSymbol;
            var extractableMembers = typeToExtractFrom.GetMembers().Where(IsExtractableMember);
            if (!extractableMembers.Any())
            {
                var errorMessage = FeaturesResources.CouldNotExtractInterfaceTypeMember;
                return new ExtractInterfaceTypeAnalysisResult(errorMessage);
            }

            return new ExtractInterfaceTypeAnalysisResult(this, document, typeNode, typeToExtractFrom, extractableMembers);
        }
        public async Task <ExtractInterfaceTypeAnalysisResult> AnalyzeTypeAtPositionAsync(
            Document document,
            int position,
            TypeDiscoveryRule typeDiscoveryRule,
            CancellationToken cancellationToken)
        {
            var typeNode = await GetTypeDeclarationAsync(document, position, typeDiscoveryRule, cancellationToken).ConfigureAwait(false);

            if (typeNode == null)
            {
                var errorMessage = FeaturesResources.Could_not_extract_interface_colon_The_selection_is_not_inside_a_class_interface_struct;
                return(new ExtractInterfaceTypeAnalysisResult(errorMessage));
            }

            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var type = semanticModel.GetDeclaredSymbol(typeNode, cancellationToken);

            if (type == null || type.Kind != SymbolKind.NamedType)
            {
                var errorMessage = FeaturesResources.Could_not_extract_interface_colon_The_selection_is_not_inside_a_class_interface_struct;
                return(new ExtractInterfaceTypeAnalysisResult(errorMessage));
            }

            var typeToExtractFrom  = type as INamedTypeSymbol;
            var extractableMembers = typeToExtractFrom.GetMembers().Where(IsExtractableMember);

            if (!extractableMembers.Any())
            {
                var errorMessage = FeaturesResources.Could_not_extract_interface_colon_The_type_does_not_contain_any_member_that_can_be_extracted_to_an_interface;
                return(new ExtractInterfaceTypeAnalysisResult(errorMessage));
            }

            return(new ExtractInterfaceTypeAnalysisResult(this, document, typeNode, typeToExtractFrom, extractableMembers));
        }
 public Task<ExtractInterfaceTypeAnalysisResult> GetTypeAnalysisResultAsync(TypeDiscoveryRule typeDiscoveryRule)
 {
     return ExtractInterfaceService.AnalyzeTypeAtPositionAsync(
         ExtractFromDocument,
         _testDocument.CursorPosition.Value,
         typeDiscoveryRule,
         CancellationToken.None);
 }
Exemple #7
0
 public Task <ExtractInterfaceTypeAnalysisResult> GetTypeAnalysisResultAsync(TypeDiscoveryRule typeDiscoveryRule)
 {
     return(ExtractInterfaceService.AnalyzeTypeAtPositionAsync(
                ExtractFromDocument,
                _testDocument.CursorPosition.Value,
                typeDiscoveryRule,
                CancellationToken.None));
 }
Exemple #8
0
        private static async Task TestTypeDiscoveryAsync(
            string markup,
            TypeDiscoveryRule typeDiscoveryRule,
            bool expectedExtractable)
        {
            using var testState = ExtractInterfaceTestState.Create(markup, LanguageNames.CSharp, compilationOptions: null);
            var result = await testState.GetTypeAnalysisResultAsync(typeDiscoveryRule);

            Assert.Equal(expectedExtractable, result.CanExtractInterface);
        }
Exemple #9
0
 private static void TestTypeDiscovery(
     string markup,
     TypeDiscoveryRule typeDiscoveryRule,
     bool expectedExtractable)
 {
     using (var testState = new ExtractInterfaceTestState(markup, LanguageNames.CSharp, compilationOptions: null))
     {
         var result = testState.GetTypeAnalysisResult(typeDiscoveryRule);
         Assert.Equal(expectedExtractable, result.CanExtractInterface);
     }
 }
        internal override SyntaxNode GetTypeDeclaration(Document document, int position, TypeDiscoveryRule typeDiscoveryRule, CancellationToken cancellationToken)
        {
            var tree = document.GetCSharpSyntaxTreeAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            var token = tree.GetRoot(cancellationToken).FindToken(position != tree.Length ? position : Math.Max(0, position - 1));
            var typeDeclaration = token.GetAncestor<TypeDeclarationSyntax>();

            if (typeDeclaration == null ||
                typeDiscoveryRule == TypeDiscoveryRule.TypeDeclaration)
            {
                return typeDeclaration;
            }

            var spanStart = typeDeclaration.Identifier.SpanStart;
            var spanEnd = typeDeclaration.TypeParameterList != null ? typeDeclaration.TypeParameterList.Span.End : typeDeclaration.Identifier.Span.End;
            var span = new TextSpan(spanStart, spanEnd - spanStart);

            return span.IntersectsWith(position) ? typeDeclaration : null;
        }
        internal override async Task<SyntaxNode> GetTypeDeclarationAsync(Document document, int position, TypeDiscoveryRule typeDiscoveryRule, CancellationToken cancellationToken)
        {
            var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
            var root = await tree.GetRootAsync(cancellationToken).ConfigureAwait(false);
            var token = root.FindToken(position != tree.Length ? position : Math.Max(0, position - 1));
            var typeDeclaration = token.GetAncestor<TypeDeclarationSyntax>();

            if (typeDeclaration == null ||
                typeDiscoveryRule == TypeDiscoveryRule.TypeDeclaration)
            {
                return typeDeclaration;
            }

            var spanStart = typeDeclaration.Identifier.SpanStart;
            var spanEnd = typeDeclaration.TypeParameterList != null ? typeDeclaration.TypeParameterList.Span.End : typeDeclaration.Identifier.Span.End;
            var span = new TextSpan(spanStart, spanEnd - spanStart);

            return span.IntersectsWith(position) ? typeDeclaration : null;
        }
 internal abstract SyntaxNode GetTypeDeclaration(
     Document document,
     int position,
     TypeDiscoveryRule typeDiscoveryRule,
     CancellationToken cancellationToken);
 internal abstract SyntaxNode GetTypeDeclaration(
     Document document,
     int position,
     TypeDiscoveryRule typeDiscoveryRule,
     CancellationToken cancellationToken);
 protected abstract Task <SyntaxNode> GetTypeDeclarationAsync(
     Document document,
     int position,
     TypeDiscoveryRule typeDiscoveryRule,
     CancellationToken cancellationToken);
 private static void TestTypeDiscovery(
     string markup,
     TypeDiscoveryRule typeDiscoveryRule,
     bool expectedExtractable)
 {
     using (var testState = new ExtractInterfaceTestState(markup, LanguageNames.CSharp, compilationOptions: null))
     {
         var result = testState.GetTypeAnalysisResult(typeDiscoveryRule);
         Assert.Equal(expectedExtractable, result.CanExtractInterface);
     }
 }
 private static async Task TestTypeDiscoveryAsync(
     string markup,
     TypeDiscoveryRule typeDiscoveryRule,
     bool expectedExtractable)
 {
     using (var testState = await ExtractInterfaceTestState.CreateAsync(markup, LanguageNames.CSharp, compilationOptions: null))
     {
         var result = await testState.GetTypeAnalysisResultAsync(typeDiscoveryRule);
         Assert.Equal(expectedExtractable, result.CanExtractInterface);
     }
 }
        internal override SyntaxNode GetTypeDeclaration(Document document, int position, TypeDiscoveryRule typeDiscoveryRule, CancellationToken cancellationToken)
        {
            var tree            = document.GetCSharpSyntaxTreeAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            var token           = tree.GetRoot(cancellationToken).FindToken(position != tree.Length ? position : Math.Max(0, position - 1));
            var typeDeclaration = token.GetAncestor <TypeDeclarationSyntax>();

            if (typeDeclaration == null ||
                typeDiscoveryRule == TypeDiscoveryRule.TypeDeclaration)
            {
                return(typeDeclaration);
            }

            var spanStart = typeDeclaration.Identifier.SpanStart;
            var spanEnd   = typeDeclaration.TypeParameterList != null ? typeDeclaration.TypeParameterList.Span.End : typeDeclaration.Identifier.Span.End;
            var span      = new TextSpan(spanStart, spanEnd - spanStart);

            return(span.IntersectsWith(position) ? typeDeclaration : null);
        }