/// <summary> /// Generates a set of valid strings--parseable to this type--that /// contain the provided string as a strict prefix. /// </summary> /// <param name="context">Context for parsing.</param> /// <param name="valueToComplete">The string to complete.</param> /// <returns>An enumeration of a set of completion strings; if no such /// strings could be generated, or if the type doesn't support /// completion, then an empty enumeration is returned.</returns> /// <exception cref="ArgumentNullException">Thrown when <paramref name="context"/> /// or <paramref name="valueToComplete"/> is null.</exception> public override IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (valueToComplete == null) { throw new ArgumentNullException(nameof(valueToComplete)); } string[] tokens; if (context.ParseContext.ElementSeparators.Any()) { tokens = valueToComplete.Split( context.ParseContext.ElementSeparators.ToArray(), StringSplitOptions.None); } else { tokens = new[] { valueToComplete }; } Debug.Assert(tokens.Length >= 1); var currentTokenIndex = tokens.Length - 1; var currentToken = tokens[currentTokenIndex]; tokens[currentTokenIndex] = string.Empty; var preferredSeparator = GetPreferredElementSeparatorOrDefault(context.ParseContext) ?? string.Empty; return(_elementArgumentType.GetCompletions(context, currentToken) .Select(completion => string.Join(preferredSeparator, tokens) + completion)); }
/// <summary> /// Get possible completions of the provided path prefix string. /// </summary> /// <param name="context">Context for completion.</param> /// <param name="pathPrefix">Path prefix to complete.</param> /// <returns>Possible completions.</returns> /// <exception cref="ArgumentNullException">Thrown when <paramref name="context"/> /// is null.</exception> public static IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string pathPrefix) { if (context == null) { throw new ArgumentNullException(nameof(context)); } // If the string to be completed is empty, then treat it as if it // were a reference to the current working directory (with the // intention of completing to names of files in it). if (string.IsNullOrEmpty(pathPrefix)) { pathPrefix = "." + System.IO.Path.DirectorySeparatorChar; } try { // If the string to be completed has no valid directory part, // then use its root as the directory name. If there's no // root either, then assume the current working directory // is what we should use. var directoryPath = System.IO.Path.GetDirectoryName(pathPrefix); if (string.IsNullOrEmpty(directoryPath)) { var rootPath = System.IO.Path.GetPathRoot(pathPrefix); directoryPath = !string.IsNullOrEmpty(rootPath) ? rootPath : "."; } // Construct a glob-style file pattern for matching. var filePattern = System.IO.Path.GetFileName(pathPrefix) + "*"; // Enumerate all matching files in the selected directory. return(context.ParseContext.FileSystemReader.EnumerateFileSystemEntries(directoryPath, filePattern)); } catch (Exception ex) when( ex is IOException || ex is ArgumentException || ex is NotSupportedException || ex is SecurityException || ex is UnauthorizedAccessException) { return(new List <string>()); } }
/// <summary> /// Generates a set of valid strings--parseable to this type--that /// contain the provided string as a strict prefix. /// </summary> /// <param name="context">Context for parsing.</param> /// <param name="valueToComplete">The string to complete.</param> /// <returns>An enumeration of a set of completion strings; if no such /// strings could be generated, or if the type doesn't support /// completion, then an empty enumeration is returned.</returns> /// <exception cref="ArgumentNullException">Thrown when <paramref name="context"/> /// or <paramref name="valueToComplete"/> is null.</exception> public override IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (valueToComplete == null) { throw new ArgumentNullException(nameof(valueToComplete)); } var separatorIndex = valueToComplete.IndexOf(KeyValueSeparatorChar); if (separatorIndex < 0) { return(_keyType.GetCompletions(context, valueToComplete)); } var valueSoFar = valueToComplete.Substring(separatorIndex + 1); return(_valueType.GetCompletions(context, valueSoFar) .Select(completion => valueToComplete.Substring(0, separatorIndex + 1) + completion)); }
/// <summary> /// Generates a set of valid strings--parseable to this type--that /// contain the provided string as a strict prefix. /// </summary> /// <param name="context">Context for parsing.</param> /// <param name="valueToComplete">The string to complete.</param> /// <returns>An enumeration of a set of completion strings; if no such /// strings could be generated, or if the type doesn't support /// completion, then an empty enumeration is returned.</returns> /// <exception cref="ArgumentNullException">Thrown when <paramref name="context"/> /// or <paramref name="valueToComplete"/> is null.</exception> public override IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (valueToComplete == null) { throw new ArgumentNullException(nameof(valueToComplete)); } var tokens = valueToComplete.Split(ItemSeparatorChar); Debug.Assert(tokens.Length >= 1); var currentTokenIndex = tokens.Length - 1; var currentToken = tokens[currentTokenIndex]; tokens[currentTokenIndex] = string.Empty; return(_argTypeParameters[currentTokenIndex].GetCompletions(context, currentToken) .Select(completion => string.Join(ItemSeparatorChar.ToString(), tokens) + completion)); }
/// <summary> /// Generates a set of valid strings--parseable to this type--that /// contain the provided string as a strict prefix. /// </summary> /// <param name="context">Context for parsing.</param> /// <param name="valueToComplete">The string to complete.</param> /// <returns>An enumeration of a set of completion strings; if no such /// strings could be generated, or if the type doesn't support /// completion, then an empty enumeration is returned.</returns> public override IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) { return(_completionHandler != null ? _completionHandler(context, valueToComplete) : base.GetCompletions(context, valueToComplete)); }
/// <summary> /// Generates a set of valid strings--parseable to this type--that /// contain the provided string as a strict prefix. /// </summary> /// <param name="context">Context for parsing.</param> /// <param name="valueToComplete">The string to complete.</param> /// <returns>An enumeration of a set of completion strings; if no such /// strings could be generated, or if the type doesn't support /// completion, then an empty enumeration is returned.</returns> public override IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) => SelectCompletions(context, valueToComplete, new object[] { false, true });
/// <summary> /// Generates a set of valid strings--parseable to this type--that /// contain the provided string as a strict prefix. /// </summary> /// <param name="context">Context for parsing.</param> /// <param name="valueToComplete">The string to complete.</param> /// <returns>An enumeration of a set of completion strings; if no such /// strings could be generated, or if the type doesn't support /// completion, then an empty enumeration is returned.</returns> public virtual IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) { // By default, return an empty enumeration of strings. return(Enumerable.Empty <string>()); }
/// <summary> /// Generates a set of valid strings--parseable to this type--that /// contain the provided string as a strict prefix. /// </summary> /// <param name="context">Context for parsing.</param> /// <param name="valueToComplete">The string to complete.</param> /// <returns>An enumeration of a set of completion strings; if no such /// strings could be generated, or if the type doesn't support /// completion, then an empty enumeration is returned.</returns> public override IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) => _commandArgType.GetCompletions(context, valueToComplete);
/// <summary> /// Generates a set of valid strings--parseable to this type--that /// contain the provided string as a strict prefix. /// </summary> /// <param name="context">Context for parsing.</param> /// <param name="valueToComplete">The string to complete.</param> /// <returns>An enumeration of a set of completion strings; if no such /// strings could be generated, or if the type doesn't support /// completion, then an empty enumeration is returned.</returns> public virtual IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) => (Completer ?? InnerType).GetCompletions(context, valueToComplete);
/// <summary> /// Generates a set of valid strings--parseable to this type--that /// contain the provided string as a strict prefix. /// </summary> /// <param name="context">Context for parsing.</param> /// <param name="valueToComplete">The string to complete.</param> /// <returns>An enumeration of a set of completion strings; if no such /// strings could be generated, or if the type doesn't support /// completion, then an empty enumeration is returned.</returns> public override IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) => // Only complete to long names. SelectCompletions(context, valueToComplete, _values.Where(v => !v.Hidden && !v.Disallowed) .Select(v => v.LongName));
/// <summary> /// Constructs a string comparison policy appropriate for use in matching string completions /// for values of this type (i.e. with appropriate case sensitivity). /// </summary> /// <param name="context">Completion context.</param> /// <returns>String comparison value.</returns> protected static StringComparison GetStringComparison(ArgumentCompletionContext context) => context.CaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
/// <summary> /// Filters the provided candidate string completion list based on the /// provided string to complete. /// </summary> /// <param name="context">Context for completion.</param> /// <param name="valueToComplete">String to complete.</param> /// <param name="candidates">Candidate objects whose formatted strings /// should be selected from.</param> /// <returns>An enumeration of the selected strings.</returns> protected static IEnumerable <string> SelectCompletions(ArgumentCompletionContext context, string valueToComplete, IEnumerable <object> candidates) => SelectCompletions(context, valueToComplete, candidates.Select(candidate => candidate.ToString()));
/// <summary> /// Filters the provided candidate string completion list based on the /// provided string to complete. /// </summary> /// <param name="context">Context for completion.</param> /// <param name="valueToComplete">String to complete.</param> /// <param name="candidates">Candidate strings to select from.</param> /// <returns>An enumeration of the selected strings.</returns> protected static IEnumerable <string> SelectCompletions(ArgumentCompletionContext context, string valueToComplete, IEnumerable <string> candidates) => candidates.Where(name => name.StartsWith(valueToComplete, GetStringComparison(context))) .OrderBy(name => name, GetStringComparer(context));