Ejemplo n.º 1
0
            internal static List <CompletionResult> PSv2GenerateMatchSetOfCmdlets(CompletionExecutionHelper helper, string lastWord, string quote, bool completingAtStartOfLine)
            {
                bool flag;
                List <CompletionResult> results = new List <CompletionResult>();

                if (PSv2IsCommandLikeCmdlet(lastWord, out flag))
                {
                    Exception exception;
                    helper.CurrentPowerShell.AddCommand("Get-Command").AddParameter("Name", lastWord + "*").AddCommand("Sort-Object").AddParameter("Property", "Name");
                    Collection <PSObject> collection = helper.ExecuteCurrentPowerShell(out exception, null);
                    if ((collection == null) || (collection.Count <= 0))
                    {
                        return(results);
                    }
                    CommandAndName[] cmdlets = new CommandAndName[collection.Count];
                    for (int i = 0; i < collection.Count; i++)
                    {
                        PSObject psObject = collection[i];
                        string   fullName = CmdletInfo.GetFullName(psObject);
                        cmdlets[i] = new CommandAndName(psObject, PSSnapinQualifiedName.GetInstance(fullName));
                    }
                    if (flag)
                    {
                        foreach (CommandAndName name in cmdlets)
                        {
                            AddCommandResult(name, true, completingAtStartOfLine, quote, results);
                        }
                        return(results);
                    }
                    PrependSnapInNameForSameCmdletNames(cmdlets, completingAtStartOfLine, quote, results);
                }
                return(results);
            }
Ejemplo n.º 2
0
 private static bool PSv2ShouldFullyQualifyPathsPath(CompletionExecutionHelper helper, string lastWord)
 {
     if ((lastWord.StartsWith("~", StringComparison.OrdinalIgnoreCase) || lastWord.StartsWith(@"\", StringComparison.OrdinalIgnoreCase)) || lastWord.StartsWith("/", StringComparison.OrdinalIgnoreCase))
     {
         return(true);
     }
     helper.CurrentPowerShell.AddCommand("Split-Path").AddParameter("Path", lastWord).AddParameter("IsAbsolute", true);
     return(helper.ExecuteCommandAndGetResultAsBool());
 }
Ejemplo n.º 3
0
            internal static List <CompletionResult> PSv2GenerateMatchSetOfFiles(CompletionExecutionHelper helper, string lastWord, bool completingAtStartOfLine, string quote)
            {
                List <CompletionResult> list = new List <CompletionResult>();

                lastWord = lastWord ?? string.Empty;
                bool   flag  = string.IsNullOrEmpty(lastWord);
                bool   flag2 = !flag && lastWord.EndsWith("*", StringComparison.Ordinal);
                bool   flag3 = WildcardPattern.ContainsWildcardCharacters(lastWord);
                string str   = lastWord + "*";
                bool   shouldFullyQualifyPaths = PSv2ShouldFullyQualifyPathsPath(helper, lastWord);
                bool   flag5 = lastWord.StartsWith(@"\\", StringComparison.Ordinal) || lastWord.StartsWith("//", StringComparison.Ordinal);
                List <PathItemAndConvertedPath> list2 = null;
                List <PathItemAndConvertedPath> list3 = null;

                if (flag3 && !flag)
                {
                    list2 = PSv2FindMatches(helper, lastWord, shouldFullyQualifyPaths);
                }
                if (!flag2)
                {
                    list3 = PSv2FindMatches(helper, str, shouldFullyQualifyPaths);
                }
                IEnumerable <PathItemAndConvertedPath> enumerable = CombineMatchSets(list2, list3);

                if (enumerable != null)
                {
                    foreach (PathItemAndConvertedPath path in enumerable)
                    {
                        string str2           = WildcardPattern.Escape(path.Path);
                        string str3           = WildcardPattern.Escape(path.ConvertedPath);
                        string completionText = flag5 ? str3 : str2;
                        completionText = AddQuoteIfNecessary(completionText, quote, completingAtStartOfLine);
                        bool?  nullable     = SafeGetProperty <bool?>(path.Item, "PSIsContainer");
                        string listItemText = SafeGetProperty <string>(path.Item, "PSChildName");
                        string toolTip      = CompletionExecutionHelper.SafeToString(path.ConvertedPath);
                        if (nullable.HasValue && !string.IsNullOrEmpty(listItemText) && toolTip != null)
                        {
                            CompletionResultType resultType = nullable.Value ? CompletionResultType.ProviderContainer : CompletionResultType.ProviderItem;
                            list.Add(new CompletionResult(completionText, listItemText, resultType, toolTip));
                        }
                    }
                }
                return(list);
            }
Ejemplo n.º 4
0
        private static List <CompletionResult> InvokeLegacyTabExpansion(PowerShell powershell, string input, int cursorIndex, bool remoteToWin7, out int replacementIndex, out int replacementLength)
        {
            List <CompletionResult> list = null;
            char      ch;
            Exception exception;
            string    sentence = (cursorIndex != input.Length) ? input.Substring(0, cursorIndex) : input;
            string    str2     = LastWordFinder.FindLastWord(sentence, out replacementIndex, out ch);

            replacementLength = sentence.Length - replacementIndex;
            CompletionExecutionHelper helper = new CompletionExecutionHelper(powershell);

            powershell.AddCommand("TabExpansion").AddArgument(sentence).AddArgument(str2);
            Collection <PSObject> collection = helper.ExecuteCurrentPowerShell(out exception, null);

            if (collection != null)
            {
                list = new List <CompletionResult>();
                foreach (PSObject obj2 in collection)
                {
                    CompletionResult item = PSObject.Base(obj2) as CompletionResult;
                    if (item == null)
                    {
                        string completionText = obj2.ToString();
                        if (((ch != '\0') && (completionText.Length > 2)) && (completionText[0] != ch))
                        {
                            completionText = ch + completionText + ch;
                        }
                        item = new CompletionResult(completionText);
                    }
                    list.Add(item);
                }
            }
            if (remoteToWin7 && ((list == null) || (list.Count == 0)))
            {
                string quote = (ch == '\0') ? string.Empty : ch.ToString();
                list = PSv2CompletionCompleter.PSv2GenerateMatchSetOfFiles(helper, str2, replacementIndex == 0, quote);
                List <CompletionResult> list2 = PSv2CompletionCompleter.PSv2GenerateMatchSetOfCmdlets(helper, str2, quote, replacementIndex == 0);
                if ((list2 != null) && (list2.Count > 0))
                {
                    list.AddRange(list2);
                }
            }
            return(list);
        }
Ejemplo n.º 5
0
            private static List <PathItemAndConvertedPath> PSv2FindMatches(CompletionExecutionHelper helper, string path, bool shouldFullyQualifyPaths)
            {
                Exception exception;
                List <PathItemAndConvertedPath> list = new List <PathItemAndConvertedPath>();
                PowerShell currentPowerShell         = helper.CurrentPowerShell;

                if (!shouldFullyQualifyPaths)
                {
                    currentPowerShell.AddScript(string.Format(CultureInfo.InvariantCulture, "& {{ trap {{ continue }} ; resolve-path {0} -Relative -WarningAction SilentlyContinue | %{{,($_,(get-item $_ -WarningAction SilentlyContinue),(convert-path $_ -WarningAction SilentlyContinue))}} }}", new object[] { path }));
                }
                else
                {
                    currentPowerShell.AddScript(string.Format(CultureInfo.InvariantCulture, "& {{ trap {{ continue }} ; resolve-path {0} -WarningAction SilentlyContinue | %{{,($_,(get-item $_ -WarningAction SilentlyContinue),(convert-path $_ -WarningAction SilentlyContinue))}} }}", new object[] { path }));
                }
                Collection <PSObject> collection = helper.ExecuteCurrentPowerShell(out exception, null);

                if ((collection == null) || (collection.Count == 0))
                {
                    return(null);
                }
                foreach (PSObject obj2 in collection)
                {
                    IList baseObject = obj2.BaseObject as IList;
                    if ((baseObject != null) && (baseObject.Count == 3))
                    {
                        object   obj3 = baseObject[0];
                        PSObject item = baseObject[1] as PSObject;
                        object   obj5 = baseObject[1];
                        if (((obj3 != null) && (item != null)) && (obj5 != null))
                        {
                            list.Add(new PathItemAndConvertedPath(CompletionExecutionHelper.SafeToString(obj3), item, CompletionExecutionHelper.SafeToString(obj5)));
                        }
                    }
                }
                if (list.Count == 0)
                {
                    return(null);
                }
                list.Sort((Comparison <PathItemAndConvertedPath>)((x, y) => string.Compare(x.Path, y.Path, CultureInfo.CurrentCulture, CompareOptions.IgnoreCase)));
                return(list);
            }