private bool TryInitialize(
                TextSpan textSpan,
                CancellationToken cancellationToken)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(false);
                }

                var tree        = this.SemanticDocument.SyntaxTree;
                var root        = this.SemanticDocument.Root;
                var syntaxFacts = this.SemanticDocument.Project.LanguageServices.GetService <ISyntaxFactsService>();

                var typeDeclaration = _service.GetNodeToAnalyze(root, textSpan) as TTypeDeclarationSyntax;

                if (typeDeclaration == null)
                {
                    return(false);
                }

                var typeSymbol = this.SemanticDocument.SemanticModel.GetDeclaredSymbol(typeDeclaration, cancellationToken) as INamedTypeSymbol;

                // compiler declared types, anonymous types, types defined in metadata should be filtered out.
                if (typeSymbol == null ||
                    typeSymbol.Locations.Any(loc => loc.IsInMetadata) ||
                    typeSymbol.IsAnonymousType ||
                    typeSymbol.IsImplicitlyDeclared)
                {
                    return(false);
                }

                TypeNode     = typeDeclaration;
                TypeName     = typeSymbol.Name;
                DocumentName = Path.GetFileNameWithoutExtension(this.SemanticDocument.Document.Name);
                IsDocumentNameAValidIdentifier = syntaxFacts.IsValidIdentifier(DocumentName);

                // TODO: Make this check better, it won't detect Outer.Inner.cs cases.
                if (string.Equals(DocumentName, TypeName, StringComparison.CurrentCulture))
                {
                    // if type name matches document name in a case sensitive manner, we have nothing more to do.
                    return(false);
                }

                TargetFileNameCandidate = Path.Combine(typeSymbol.Name + Path.GetExtension(this.SemanticDocument.Document.Name));

                return(true);
            }
Example #2
0
            private bool TryInitialize(
                TextSpan textSpan,
                CancellationToken cancellationToken)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(false);
                }

                var tree        = this.SemanticDocument.SyntaxTree;
                var root        = this.SemanticDocument.Root;
                var syntaxFacts = this.SemanticDocument.Project.LanguageServices.GetService <ISyntaxFactsService>();

                var typeDeclaration = _service.GetNodeToAnalyze(root, textSpan) as TTypeDeclarationSyntax;

                if (typeDeclaration == null)
                {
                    return(false);
                }

                var typeSymbol = this.SemanticDocument.SemanticModel.GetDeclaredSymbol(typeDeclaration, cancellationToken) as INamedTypeSymbol;

                // compiler declared types, anonymous types, types defined in metadata should be filtered out.
                if (typeSymbol == null ||
                    typeSymbol.Locations.Any(loc => loc.IsInMetadata) ||
                    typeSymbol.IsAnonymousType ||
                    typeSymbol.IsImplicitlyDeclared)
                {
                    return(false);
                }

                TypeNode = typeDeclaration;
                TypeName = typeSymbol.Name;
                DocumentNameWithoutExtension   = Path.GetFileNameWithoutExtension(this.SemanticDocument.Document.Name);
                IsDocumentNameAValidIdentifier = syntaxFacts.IsValidIdentifier(DocumentNameWithoutExtension);

                // if type name matches document name, per style conventions, we have nothing to do.
                return(!_service.TypeMatchesDocumentName(
                           TypeNode,
                           TypeName,
                           DocumentNameWithoutExtension,
                           SemanticDocument.SemanticModel,
                           cancellationToken));
            }