public bool Cycle(TabCompletionContext context, Func <List <string> > evaluation, out string completion)
        {
            if (context.CompletionCandidate == lastCompletion && lastCompletion != null)
            {
                context.CompletionCandidate = lastSoFar;
            }

            var candidates = evaluation();

            if (context.CompletionCandidate == lastSoFar)
            {
                lastIndex = context.Shift ? lastIndex - 1 : lastIndex + 1;
            }
            if (lastIndex >= candidates.Count)
            {
                lastIndex = 0;
            }
            if (lastIndex < 0)
            {
                lastIndex = candidates.Count - 1;
            }
            lastSoFar = context.CompletionCandidate;

            if (candidates.Count == 0 || (candidates.Count > 1 && context.CompletionCandidate.Length < MinCharsBeforeCyclingBegins))
            {
                completion = null;
                return(false);
            }
            else
            {
                completion     = candidates[lastIndex];
                lastCompletion = completion;
                return(true);
            }
        }
        internal static TabCompletionContext ConvertContext(CommandLineArgumentsDefinition definition, RichCommandLineContext innerContext)
        {
            TabCompletionContext context = new TabCompletionContext();

            context.Definition          = definition;
            context.Shift               = innerContext.KeyPressed.Modifiers.HasFlag(ConsoleModifiers.Shift);
            context.PreviousToken       = innerContext.CurrentTokenIndex > 0 ? innerContext.PreviousNonWhitespaceToken.Value : string.Empty;
            context.CompletionCandidate = innerContext.CurrentToken.Value;

            if (context.CompletionCandidate == " ")
            {
                context.CompletionCandidate = "";
            }

            context.CommandLineText = new ConsoleString(innerContext.Buffer).ToString();
            context.TargetAction    = FindContextualAction(innerContext.Tokens.FirstOrDefault().Value, definition);
            context.TargetArgument  = FindContextualArgument(new FindContextualArgumentArgs()
            {
                ActionContext     = context.TargetAction,
                Definition        = definition,
                CommandLine       = new ConsoleString(innerContext.Buffer).StringValue,
                CurrentTokenIndex = innerContext.CurrentTokenIndex,
                CurrentToken      = innerContext.CurrentToken.Value,
                PreviousToken     = context.PreviousToken
            });
            return(context);
        }
Example #3
0
        public bool TryComplete(TabCompletionContext context, out string completion)
        {
            if (context.TargetArgument != target)
            {
                completion = null;
                return(false);
            }

            return(wrappedSource.TryComplete(context, out completion));
        }
        public bool TryComplete(TabCompletionContext context, out string completion)
        {
            if(context.TargetArgument != target)
            {
                completion = null;
                return false;
            }

            return wrappedSource.TryComplete(context.Shift, context.CompletionCandidate, out completion);
        }
        /// <summary>
        /// Iterates through the candidates to try to find a match.  If there are multiple possible matches it
        /// supports cycling through tem as the user continually presses tab.
        /// </summary>
        /// <param name="shift">Indicates if shift was being pressed</param>
        /// <param name="soFar">The text token that the user has typed before pressing tab.</param>
        /// <param name="context"></param>
        /// <param name="completion">The variable that you should assign the completed string to if you find a match.</param>
        /// <returns>true if the tab completion was successful, false otherwise</returns>
        public bool TryComplete(TabCompletionContext context, out string completion)
        {
            manager.MinCharsBeforeCyclingBegins = this.MinCharsBeforeCyclingBegins;

            bool ignoreCase = true;

            return(manager.Cycle(context, () =>
            {
                return (from c in CandidateFunction() where c.StartsWith(context.CompletionCandidate, ignoreCase, CultureInfo.CurrentCulture) select c).ToList();
            }, out completion));
        }
Example #6
0
 /// <summary>
 /// Iterates over the wrapped sources looking for a match
 /// </summary>
 /// <param name="shift">Indicates if shift was being pressed</param>
 /// <param name="soFar">The text token that the user has typed before pressing tab.</param>
 /// <param name="context"></param>
 /// <param name="completion">The variable that you should assign the completed string to if you find a match.</param>
 /// <returns></returns>
 public bool TryComplete(TabCompletionContext context, out string completion)
 {
     foreach (var source in sources)
     {
         if (source.TryComplete(context, out completion))
         {
             return(true);
         }
     }
     completion = null;
     return(false);
 }
        public bool TryComplete(TabCompletionContext context, out string completion)
        {
            var fixedUpCandidate = context.CompletionCandidate;

            if (fixedUpCandidate.StartsWith("-"))
            {
                fixedUpCandidate = fixedUpCandidate.Substring(1);
            }
            else if (fixedUpCandidate.StartsWith("/"))
            {
                fixedUpCandidate = fixedUpCandidate.Substring(1);
            }
            else
            {
                completion = null;
                return(false);
            }

            var match = definition.Arguments.Where(arg => arg.IsMatch(fixedUpCandidate)).SingleOrDefault();

            if (match == null)
            {
                foreach (var action in definition.Actions)
                {
                    match = action.Arguments.Where(arg => arg.IsMatch(fixedUpCandidate)).SingleOrDefault();
                    if (match != null)
                    {
                        break;
                    }
                }
            }

            if (match == null)
            {
                completion = null;
                return(false);
            }

            if (match != argument)
            {
                completion = null;
                return(false);
            }

            return(TryComplete(new ArgumentAwareTabCompletionContext()
            {
                InnerContext = context,
                Argument = argument
            }
                               , out completion));
        }
        public bool TryComplete(TabCompletionContext context, out string completion)
        {
            if (actionSource == null)
            {
                actionSource = new SimpleTabCompletionSource(FindActions(context.Definition))
                {
                    MinCharsBeforeCyclingBegins = 0
                };
                globalArgumentSource = new SimpleTabCompletionSource(FindGlobalArguments(context.Definition))
                {
                    MinCharsBeforeCyclingBegins = 0
                };
                actionSpecificArgumentSources = FindActionSpecificSources(context.Definition);
            }

            // if this is the first token and the definition contains actions then try to auto complete an action name
            if (string.IsNullOrEmpty(context.PreviousToken) && context.Definition.Actions.Count > 0)
            {
                return(actionSource.TryComplete(context, out completion));
            }
            // if there is no action in context and no argument in context then try to auto complete global argument names
            else if (context.TargetAction == null && context.TargetArgument == null)
            {
                return(globalArgumentSource.TryComplete(context, out completion));
            }
            // if there is an action in context and not argument in context then try to complete action specific argument names and then globals
            else if (context.TargetAction != null && context.TargetArgument == null)
            {
                var actionSpecificSource = actionSpecificArgumentSources[context.TargetAction];
                if (actionSpecificSource.TryComplete(context, out completion))
                {
                    return(true);
                }
                else
                {
                    return(globalArgumentSource.TryComplete(context, out completion));
                }
            }
            else
            {
                completion = null;
                return(false);
            }
        }
        public bool TryComplete(TabCompletionContext context, out string completion)
        {
            if(actionSource == null)
            {
                actionSource = new SimpleTabCompletionSource(FindActions(context.Definition)) { MinCharsBeforeCyclingBegins = 0 };
                globalArgumentSource = new SimpleTabCompletionSource(FindGlobalArguments(context.Definition)) { MinCharsBeforeCyclingBegins = 0 };
                actionSpecificArgumentSources = FindActionSpecificSources(context.Definition);
            }

            // if this is the first token and the definition contains actions then try to auto complete an action name
            if(string.IsNullOrEmpty(context.PreviousToken) && context.Definition.Actions.Count > 0)
            {
                return actionSource.TryComplete(context, out completion);
            }
            // if there is no action in context and no argument in context then try to auto complete global argument names
            else if(context.TargetAction == null && context.TargetArgument == null)
            {
                return globalArgumentSource.TryComplete(context, out completion);
            }
            // if there is an action in context and not argument in context then try to complete action specific argument names and then globals
            else if(context.TargetAction != null && context.TargetArgument == null)
            {
                var actionSpecificSource = actionSpecificArgumentSources[context.TargetAction];
                if (actionSpecificSource.TryComplete(context, out completion))
                {
                    return true;
                }
                else
                {
                    return globalArgumentSource.TryComplete(context, out completion);
                }
            }
            else
            {
                completion = null;
                return false;
            }
        }
Example #10
0
            public bool TryComplete(TabCompletionContext context, out string completion)
            {
                var allRemotes = new List<string>
                {
                    "windows",
                    "hyperv",
                    "linux",
                    "freebsd",
                    "openbsd",
                    "netbsd",
                    "generic",
                    "all",
                    "vmcs"
                };

                var list = allRemotes.Where(r => r.StartsWith(context.CompletionCandidate.ToLower(), StringComparison.InvariantCultureIgnoreCase))
                    .Select(r => ContextAssistSearchResult.FromString(r))
                    .ToList();

                completion = list.FirstOrDefault().RichDisplayText.StringValue;
                return !string.IsNullOrWhiteSpace(completion);
            }
 /// <summary>
 /// Iterates through the candidates to try to find a match.  If there are multiple possible matches it 
 /// supports cycling through tem as the user continually presses tab.
 /// </summary>
 /// <param name="context">Information about the tab completion</param>
 /// <param name="completion">The variable that you should assign the completed string to if you find a match.</param>
 /// <returns>true if the tab completion was successful, false otherwise</returns>
 public bool TryComplete(TabCompletionContext context, out string completion)
 {
     return TryComplete(context.Shift, context.CompletionCandidate, out completion);
 }
        internal static TabCompletionContext ConvertContext(CommandLineArgumentsDefinition definition, RichCommandLineContext innerContext)
        {
            TabCompletionContext context = new TabCompletionContext();
            context.Definition = definition;
            context.Shift = innerContext.KeyPressed.Modifiers.HasFlag(ConsoleModifiers.Shift);
            context.PreviousToken = innerContext.CurrentTokenIndex > 0 ? innerContext.PreviousNonWhitespaceToken.Value : string.Empty;
            context.CompletionCandidate = innerContext.CurrentToken.Value;

            if (context.CompletionCandidate == " ")
            {
                context.CompletionCandidate = "";
            }

            context.CommandLineText = new ConsoleString(innerContext.Buffer).ToString();
            context.TargetAction = FindContextualAction(innerContext.Tokens.FirstOrDefault().Value, definition);
            context.TargetArgument = FindContextualArgument(context.PreviousToken, context.TargetAction, definition);
            return context;
        }
 public bool TryComplete(TabCompletionContext context, out string completion)
 {
     return source.TryComplete(context, out completion);
 }
 public bool TryComplete(TabCompletionContext context, out string completion)
 {
     return TryComplete(context.Shift, context.CompletionCandidate, out completion);
 }
 public bool TryComplete(TabCompletionContext context, out string completion)
 {
     if (context.TargetArgument == Target)
     {
         return WrappedSource.TryComplete(context, out completion);
     }
     else
     {
         completion = null;
         return false;
     }
 }
        public bool TryComplete(TabCompletionContext context, out string completion)
        {
            completion = null;
            try
            {
                context.CompletionCandidate = context.CompletionCandidate.Replace("\"", "");
                if (context.CompletionCandidate == "")
                {
                    context.CompletionCandidate = lastSoFar ?? ".\\";
                }

                if (context.CompletionCandidate == lastCompletion)
                {
                    context.CompletionCandidate = lastSoFar;
                }
                else
                {
                    tabIndex = -1;
                }

                var dir = Path.GetDirectoryName(context.CompletionCandidate);

                if (Path.IsPathRooted(context.CompletionCandidate) == false)
                {
                    dir = Environment.CurrentDirectory;
                    context.CompletionCandidate = ".\\" + context.CompletionCandidate;
                }

                if (Directory.Exists(dir) == false)
                {
                    return(false);
                }
                var rest = Path.GetFileName(context.CompletionCandidate);

                var matches = from f in Directory.GetFiles(dir)
                              where f.ToLower().StartsWith(Path.GetFullPath(context.CompletionCandidate).ToLower())
                              select f;

                var matchesArray = (matches.Union(from d in Directory.GetDirectories(dir)
                                                  where d.ToLower().StartsWith(Path.GetFullPath(context.CompletionCandidate).ToLower())
                                                  select d)).ToArray();

                if (matchesArray.Length > 0)
                {
                    tabIndex = context.Shift ? tabIndex - 1 : tabIndex + 1;
                    if (tabIndex < 0)
                    {
                        tabIndex = matchesArray.Length - 1;
                    }
                    if (tabIndex >= matchesArray.Length)
                    {
                        tabIndex = 0;
                    }

                    completion = matchesArray[tabIndex];

                    if (completion.Contains(" "))
                    {
                        completion = '"' + completion + '"';
                    }
                    lastSoFar      = context.CompletionCandidate;
                    lastCompletion = completion.Replace("\"", "");
                    return(true);
                }
                else
                {
                    lastSoFar      = null;
                    lastCompletion = null;
                    tabIndex       = -1;
                    return(false);
                }
            }
            catch (UnauthorizedAccessException)
            {
                return(false);
            }
            catch (Exception ex)
            {
                // TODO P2 - Why do we have tracing here?
                Trace.TraceError(ex.ToString());
                return(false);  // We don't want a bug in this logic to break the app
            }
        }