コード例 #1
0
        protected override void ProcessRecord()
        {
            var commandName   = CommandName is null ? null : WildcardPattern.Get(CommandName, WildcardOptions.IgnoreCase);
            var parameterName = ParameterName is null ? null : WildcardPattern.Get(ParameterName, WildcardOptions.IgnoreCase);

            var completions = LearnedCompletionData.GetLearnedCompletions()
                              .Where(c => commandName?.IsMatch(c.CommandName) ?? true)
                              .Where(c => parameterName?.IsMatch(c.ParameterName) ?? true)
                              .ToList();

            WriteObject(completions, true);
            if ((CommandName != null && !WildcardPattern.ContainsWildcardCharacters(CommandName) ||
                 ParameterName != null && !WildcardPattern.ContainsWildcardCharacters(ParameterName)
                 ) && completions.Count == 0)
            {
                WriteError(new ErrorRecord(
                               new ItemNotFoundException("No learned completions were found."),
                               "CompletionNotFound",
                               ErrorCategory.ObjectNotFound,
                               $"{CommandName ?? "*"}:{ParameterName ?? "*"}")
                {
                    ErrorDetails = new ErrorDetails($"No learned completions were found for command " +
                                                    $"'{CommandName ?? "*"}', parameter '{ParameterName ?? "*"}'.")
                });
            }
        }
コード例 #2
0
        protected override void ProcessRecord()
        {
            var completions = LearnedCompletionData.GetLearnedCompletions();

            if (completions.Count == 0)
            {
                WriteDebug("No learned completions exist to be cleared.");
                return;
            }
            if (CommandName is null && ParameterName is null)
            {
                if (ShouldProcess($"Removing all {completions.Count} completions.",
                                  $"Remove all {completions.Count} completions?",
                                  "Clear-LearnedCompletion"))
                {
                    File.Delete(LearnedCompletionData.LearningStoragePath);
                }
                return;
            }
            IEnumerable <LearnedCompletionData> filteredCompletions = completions;

            if (CommandName != null)
            {
                var wc = WildcardPattern.Get(CommandName, WildcardOptions.IgnoreCase);
                filteredCompletions = completions.Where(i => wc.IsMatch(i.CommandName));
            }
            if (ParameterName != null)
            {
                var wc = WildcardPattern.Get(ParameterName, WildcardOptions.IgnoreCase);
                filteredCompletions = filteredCompletions.Where(i => wc.IsMatch(i.ParameterName));
            }
            var filteredCompletionsList = filteredCompletions.ToList();

            if (filteredCompletionsList.Count == 0)
            {
                WriteDebug($"No learned completions were identified with command '{CommandName ?? "*"}'," +
                           $" parameter '{ParameterName ?? "*"}' to be cleared.");
                return;
            }
            int commandCount = filteredCompletionsList
                               .GroupBy(i => i.CommandName, StringComparer.OrdinalIgnoreCase)
                               .Count();
            int parameterCount = filteredCompletionsList
                                 .GroupBy(i => i.ParameterName, StringComparer.OrdinalIgnoreCase)
                                 .Count();

            if (ShouldProcess($"Removing {filteredCompletionsList.Count} completions for {commandCount} " +
                              $"command(s) and {parameterCount} parameter(s).",
                              $"Remove {filteredCompletionsList.Count} completions for {commandCount} command(s) and {parameterCount} parameter(s)?",
                              "Clear-LearnedCompletion"))
            {
                LearnedCompletionData.SetLearnedCompletions(completions.Except(filteredCompletionsList).ToList());
            }
        }