Esempio n. 1
0
        /// <summary>
        /// Call to perform a rename of document or change in document folders. Returns additional code changes related to the document
        /// being modified, such as renaming symbols in the file.
        ///
        /// Each change is added as a <see cref="RenameDocumentAction"/> in the returned <see cref="RenameDocumentActionSet.ApplicableActions" />.
        ///
        /// Each action may individually encounter errors that prevent it from behaving correctly. Those are reported in <see cref="RenameDocumentAction.GetErrors(System.Globalization.CultureInfo?)"/>.
        ///
        /// <para />
        ///
        /// Current supported actions that may be returned:
        /// <list>
        ///  <item>Rename symbol action that will rename the type to match the document name.</item>
        ///  <item>Sync namespace action that will sync the namespace(s) of the document to match the document folders. </item>
        /// </list>
        ///
        /// </summary>
        /// <param name="document">The document to be modified</param>
        /// <param name="newDocumentName">The new name for the document. Pass null or the same name to keep unchanged.</param>
        /// <param name="newDocumentFolders">The new set of folders for the <see cref="TextDocument.Folders"/> property</param>
        public static async Task <RenameDocumentActionSet> RenameDocumentAsync(
            Document document,
            string newDocumentName,
            IReadOnlyList <string>?newDocumentFolders = null,
            OptionSet?optionSet = null,
            CancellationToken cancellationToken = default
            )
        {
            if (document is null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (document.Services.GetService <ISpanMappingService>() != null)
            {
                // Don't advertise that we can file rename generated documents that map to a different file.
                return(new RenameDocumentActionSet(
                           ImmutableArray <RenameDocumentAction> .Empty,
                           document.Id,
                           document.Name,
                           document.Folders.ToImmutableArray(),
                           document.Project.Solution.Options
                           ));
            }

            using var _ = ArrayBuilder <RenameDocumentAction> .GetInstance(out var actions);

            if (newDocumentName != null && !newDocumentName.Equals(document.Name))
            {
                var renameAction = await RenameSymbolDocumentAction
                                   .TryCreateAsync(document, newDocumentName, cancellationToken)
                                   .ConfigureAwait(false);

                actions.AddIfNotNull(renameAction);
            }

            if (newDocumentFolders != null && !newDocumentFolders.SequenceEqual(document.Folders))
            {
                var action = SyncNamespaceDocumentAction.TryCreate(
                    document,
                    newDocumentFolders,
                    cancellationToken
                    );
                actions.AddIfNotNull(action);
            }

            newDocumentName ??= document.Name;
            newDocumentFolders ??= document.Folders;
            optionSet ??= document.Project.Solution.Options;

            return(new RenameDocumentActionSet(
                       actions.ToImmutable(),
                       document.Id,
                       newDocumentName,
                       newDocumentFolders.ToImmutableArray(),
                       optionSet
                       ));
        }
Esempio n. 2
0
        /// <summary>
        /// Call to perform a rename of document or change in document folders. Returns additional code changes related to the document
        /// being modified, such as renaming symbols in the file.
        ///
        /// Each change is added as a <see cref="RenameDocumentAction"/> in the returned <see cref="RenameDocumentActionSet.ApplicableActions" />.
        ///
        /// Each action may individually encounter errors that prevent it from behaving correctly. Those are reported in <see cref="RenameDocumentAction.GetErrors(System.Globalization.CultureInfo?)"/>.
        ///
        /// <para />
        ///
        /// Current supported actions that may be returned:
        /// <list>
        ///  <item>Rename symbol action that will rename the type to match the document name.</item>
        ///  <item>Sync namespace action that will sync the namespace(s) of the document to match the document folders. </item>
        /// </list>
        ///
        /// </summary>
        /// <param name="document">The document to be modified</param>
        /// <param name="newDocumentName">The new name for the document. Pass null or the same name to keep unchanged.</param>
        /// <param name="newDocumentFolders">The new set of folders for the <see cref="TextDocument.Folders"/> property</param>
        public static async Task <RenameDocumentActionSet> RenameDocumentAsync(
            Document document,
            string newDocumentName,
            IReadOnlyList <string> newDocumentFolders = null,
            OptionSet optionSet = null,
            CancellationToken cancellationToken = default)
        {
            if (document is null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            using var _ = ArrayBuilder <RenameDocumentAction> .GetInstance(out var actions);

            if (newDocumentName != null && !newDocumentName.Equals(document.Name))
            {
                var renameAction = await RenameSymbolDocumentAction.TryCreateAsync(document, newDocumentName, cancellationToken).ConfigureAwait(false);

                actions.AddIfNotNull(renameAction);
            }

            if (newDocumentFolders != null && !newDocumentFolders.SequenceEqual(document.Folders))
            {
                var action = SyncNamespaceDocumentAction.TryCreate(document, newDocumentFolders, cancellationToken);
                actions.AddIfNotNull(action);
            }

            newDocumentName ??= document.Name;
            newDocumentFolders ??= document.Folders;
            optionSet ??= document.Project.Solution.Options;

            return(new RenameDocumentActionSet(
                       actions.ToImmutable(),
                       document.Id,
                       newDocumentName,
                       newDocumentFolders.ToImmutableArray(),
                       optionSet));
        }