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

                var tree        = SemanticDocument.SyntaxTree;
                var root        = SemanticDocument.Root;
                var syntaxFacts = SemanticDocument.Document.GetLanguageService <ISyntaxFactsService>();

                // compiler declared types, anonymous types, types defined in metadata should be filtered out.
                if (SemanticDocument.SemanticModel.GetDeclaredSymbol(typeDeclaration, cancellationToken) is not INamedTypeSymbol typeSymbol ||
                    typeSymbol.Locations.Any(loc => loc.IsInMetadata) ||
                    typeSymbol.IsAnonymousType ||
                    typeSymbol.IsImplicitlyDeclared ||
                    typeSymbol.Name == string.Empty)
                {
                    return(false);
                }

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

                return(true);
            }
예제 #2
0
            private static async Task <Solution> GetNamespaceScopeChangedSolutionAsync(
                TNamespaceDeclarationSyntax namespaceDeclaration,
                TTypeDeclarationSyntax typeToMove,
                Document documentToEdit,
                CancellationToken cancellationToken)
            {
                var syntaxFactsService = documentToEdit.GetLanguageService <ISyntaxFactsService>();
                var childNodes         = syntaxFactsService.GetMembersOfNamespaceDeclaration(namespaceDeclaration);

                if (childNodes.Count <= 1)
                {
                    return(null);
                }

                var editor = await DocumentEditor.CreateAsync(documentToEdit, cancellationToken).ConfigureAwait(false);

                editor.RemoveNode(typeToMove, SyntaxRemoveOptions.KeepNoTrivia);

                var syntaxGenerator = editor.Generator;
                var index           = childNodes.IndexOf(typeToMove);

                var itemsBefore = index > 0 ? childNodes.Take(index) : Enumerable.Empty <SyntaxNode>();
                var itemsAfter  = index < childNodes.Count - 1 ? childNodes.Skip(index + 1) : Enumerable.Empty <SyntaxNode>();

                var name = syntaxFactsService.GetDisplayName(namespaceDeclaration, DisplayNameOptions.IncludeNamespaces);
                var newNamespaceDeclaration = syntaxGenerator.NamespaceDeclaration(name, WithElasticTrivia(typeToMove)).WithAdditionalAnnotations(NamespaceScopeMovedAnnotation);

                if (itemsBefore.Any() && itemsAfter.Any())
                {
                    var itemsAfterNamespaceDeclaration = syntaxGenerator.NamespaceDeclaration(name, WithElasticTrivia(itemsAfter));

                    foreach (var nodeToRemove in itemsAfter)
                    {
                        editor.RemoveNode(nodeToRemove, SyntaxRemoveOptions.KeepNoTrivia);
                    }

                    editor.InsertAfter(namespaceDeclaration, new[] { newNamespaceDeclaration, itemsAfterNamespaceDeclaration });
                }
                else if (itemsBefore.Any())
                {
                    editor.InsertAfter(namespaceDeclaration, newNamespaceDeclaration);

                    var nodeToCleanup = itemsBefore.Last();
                    editor.ReplaceNode(nodeToCleanup, WithElasticTrivia(nodeToCleanup, leading: false));
                }
                else if (itemsAfter.Any())
                {
                    editor.InsertBefore(namespaceDeclaration, newNamespaceDeclaration);

                    var nodeToCleanup = itemsAfter.First();
                    editor.ReplaceNode(nodeToCleanup, WithElasticTrivia(nodeToCleanup, trailing: false));
                }

                var changedDocument = editor.GetChangedDocument();

                return(changedDocument.Project.Solution);
            }
            private TTypeDeclarationSyntax RemoveLeadingBlankLines(
                TTypeDeclarationSyntax currentTypeNode)
            {
                var syntaxFacts       = State.SemanticDocument.Document.GetLanguageService <ISyntaxFactsService>();
                var withoutBlankLines = syntaxFacts.GetNodeWithoutLeadingBlankLines(currentTypeNode);

                // Add an elastic marker so the formatter can add any blank lines it thinks are
                // important to have (i.e. after a block of usings/imports).
                return(withoutBlankLines.WithPrependedLeadingTrivia(syntaxFacts.ElasticMarker));
            }
예제 #4
0
            internal static State Generate(
                SemanticDocument document, TTypeDeclarationSyntax typeDeclaration,
                CancellationToken cancellationToken)
            {
                var state = new State(document);

                if (!state.TryInitialize(typeDeclaration, cancellationToken))
                {
                    return(null);
                }

                return(state);
            }
            internal static State Generate(
                SemanticDocument document, TTypeDeclarationSyntax typeDeclaration, SyntaxFormattingOptionsProvider fallbackOptions,
                CancellationToken cancellationToken)
            {
                var state = new State(document, fallbackOptions);

                if (!state.TryInitialize(typeDeclaration, cancellationToken))
                {
                    return(null);
                }

                return(state);
            }
            private TTypeDeclarationSyntax RemoveLeadingWhitespace(
                TTypeDeclarationSyntax currentTypeNode)
            {
                var syntaxFacts     = State.SemanticDocument.Document.GetLanguageService <ISyntaxFactsService>();
                var leadingTrivia   = currentTypeNode.GetLeadingTrivia();
                var afterWhitespace = leadingTrivia.SkipWhile(
                    t => syntaxFacts.IsWhitespaceTrivia(t) || syntaxFacts.IsEndOfLineTrivia(t));

                var withoutLeadingWhitespace = currentTypeNode.WithLeadingTrivia(afterWhitespace);

                return(withoutLeadingWhitespace.ReplaceToken(
                           withoutLeadingWhitespace.GetFirstToken(),
                           withoutLeadingWhitespace.GetFirstToken().WithAdditionalAnnotations(Formatter.Annotation)));
            }
예제 #7
0
 private static IEnumerable <TTypeDeclarationSyntax> GetClassDeclarationNodes(INamedTypeSymbol namedType, CancellationToken cancellationToken)
 {
     foreach (SyntaxNode syntax in namedType.DeclaringSyntaxReferences.Select(s => s.GetSyntax(cancellationToken)))
     {
         if (syntax != null)
         {
             TTypeDeclarationSyntax classDecl = syntax.FirstAncestorOrSelf <TTypeDeclarationSyntax>(ascendOutOfTrivia: false);
             if (classDecl != null)
             {
                 yield return(classDecl);
             }
         }
     }
 }
예제 #8
0
            internal static State Generate(
                SemanticDocument document, TextSpan textSpan,
                TTypeDeclarationSyntax typeDeclaration, bool isSelectionOnTypeHeader,
                CancellationToken cancellationToken)
            {
                var state = new State(document);

                if (!state.TryInitialize(textSpan, typeDeclaration, isSelectionOnTypeHeader, cancellationToken))
                {
                    return(null);
                }

                return(state);
            }
예제 #9
0
            private bool TryInitialize(
                TTypeDeclarationSyntax typeDeclaration,
                CancellationToken cancellationToken)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(false);
                }

                var tree        = SemanticDocument.SyntaxTree;
                var root        = SemanticDocument.Root;
                var syntaxFacts = SemanticDocument.Document.GetLanguageService <ISyntaxFactsService>();

                // compiler declared types, anonymous types, types defined in metadata should be filtered out.
                if (SemanticDocument.SemanticModel.GetDeclaredSymbol(typeDeclaration, cancellationToken) is not INamedTypeSymbol typeSymbol ||
                    typeSymbol.Locations.Any(static loc => loc.IsInMetadata) ||
예제 #10
0
            private bool TryInitialize(
                TextSpan textSpan,
                TTypeDeclarationSyntax typeDeclaration,
                CancellationToken cancellationToken)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(false);
                }

                var tree        = this.SemanticDocument.SyntaxTree;
                var root        = this.SemanticDocument.Root;
                var syntaxFacts = this.SemanticDocument.Document.GetLanguageService <ISyntaxFactsService>();

                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(!TypeMatchesDocumentName(
                           TypeNode,
                           TypeName,
                           DocumentNameWithoutExtension,
                           SemanticDocument.SemanticModel,
                           cancellationToken));
            }