예제 #1
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);
        }
예제 #2
0
        public CompletionResult GetNextResult(bool forward)
        {
            CompletionResult result = null;
            int count = this.CompletionMatches.Count;

            if (count <= 0)
            {
                return(result);
            }
            this.CurrentMatchIndex += forward ? 1 : -1;
            if (this.CurrentMatchIndex >= count)
            {
                this.CurrentMatchIndex = 0;
            }
            else if (this.CurrentMatchIndex < 0)
            {
                this.CurrentMatchIndex = count - 1;
            }
            return(this.CompletionMatches[this.CurrentMatchIndex]);
        }
예제 #3
0
 private void DoReplacementForCompletion(CompletionResult completionResult, CommandCompletion completions)
 {
     var replacementText = completionResult.CompletionText;
     int cursorAdjustment = 0;
     if (completionResult.ResultType == CompletionResultType.ProviderContainer)
     {
         replacementText = GetReplacementTextForDirectory(replacementText, ref cursorAdjustment);
     }
     Replace(completions.ReplacementIndex, completions.ReplacementLength, replacementText);
     if (cursorAdjustment != 0)
     {
         _current += cursorAdjustment;
         PlaceCursor();
     }
     completions.ReplacementLength = replacementText.Length;
 }
        internal static CompletionDetails Create(CompletionResult completionResult)
        {
            Validate.IsNotNull("completionResult", completionResult);

            // Some tooltips may have newlines or whitespace for unknown reasons
            string toolTipText = completionResult.ToolTip;
            if (toolTipText != null)
            {
                toolTipText = toolTipText.Trim();
            }

            return new CompletionDetails
            {
                CompletionText = completionResult.CompletionText,
                ToolTipText = toolTipText,
                SymbolTypeName = ExtractSymbolTypeNameFromToolTip(completionResult.ToolTip),
                CompletionType = 
                    ConvertCompletionResultType(
                        completionResult.ResultType)
            };
        }
예제 #5
0
        private void PossibleCompletionsImpl(ConsoleKeyInfo? key, object arg, bool menuSelect)
        {
            var completions = GetCompletions();
            if (completions == null || completions.CompletionMatches.Count == 0)
            {
                Ding();
                return;
            }

            if (completions.CompletionMatches.Count >= _options.CompletionQueryItems)
            {
                if (!PromptYesOrNo(string.Format(PSReadLineResources.DisplayAllPossibilities, completions.CompletionMatches.Count)))
                {
                    return;
                }
            }

            // Don't overwrite any of the line - so move to first line after the end of our buffer.
            var coords = ConvertOffsetToCoordinates(_buffer.Length);
            PlaceCursor(0, coords.Y + 1);

            var matches = completions.CompletionMatches;
            var minColWidth = matches.Max(c => c.ListItemText.Length);
            minColWidth += 2;
            var menuColumnWidth = minColWidth;

            CompletionResult[,] matchesMatrix;
            int displayColumns;
            int displayRows;
            var bufferWidth = Console.BufferWidth;
            var sb = new StringBuilder(bufferWidth);
            if (Options.ShowToolTips)
            {
                const string seperator = "- ";
                var maxTooltipWidth = bufferWidth - minColWidth - seperator.Length;

                matchesMatrix = new CompletionResult[1, matches.Count];
                displayRows = matches.Count;
                displayColumns = 1;
                for (int index = 0; index < matches.Count; index++)
                {
                    var match = matches[index];
                    matchesMatrix[0, index] = match;
                    var listItemText = HandleNewlinesForPossibleCompletions(match.ListItemText);
                    sb.Append(listItemText);
                    var spacesNeeded = minColWidth - listItemText.Length;
                    if (spacesNeeded > 0)
                        sb.Append(' ', spacesNeeded);
                    sb.Append(seperator);
                    var toolTip = HandleNewlinesForPossibleCompletions(match.ToolTip);
                    toolTip = toolTip.Length <= maxTooltipWidth
                                  ? toolTip
                                  : toolTip.Substring(0, maxTooltipWidth);
                    sb.Append(toolTip);

                    // Make sure we always write out exactly 1 buffer width
                    spacesNeeded = bufferWidth - sb.Length;
                    if (spacesNeeded > 0)
                    {
                        sb.Append(' ', spacesNeeded);
                    }
                    Console.Write(sb.ToString());
                    sb.Clear();
                }
                menuColumnWidth = bufferWidth;
            }
            else
            {
                var screenColumns = bufferWidth;
                displayColumns = Math.Max(1, screenColumns / minColWidth);
                displayRows = (completions.CompletionMatches.Count + displayColumns - 1) / displayColumns;
                matchesMatrix = new CompletionResult[displayColumns, displayRows];
                for (var row = 0; row < displayRows; row++)
                {
                    for (var col = 0; col < displayColumns; col++)
                    {
                        var index = row + (displayRows * col);
                        if (index >= matches.Count)
                            break;
                        var match = matches[index];
                        matchesMatrix[col, row] = match;
                        var item = HandleNewlinesForPossibleCompletions(match.ListItemText);
                        sb.Append(item);
                        sb.Append(' ', minColWidth - item.Length);
                    }

                    // Make sure we always write out exactly 1 buffer width
                    var spacesNeeded = bufferWidth - sb.Length;
                    if (spacesNeeded > 0)
                    {
                        sb.Append(' ', spacesNeeded);
                    }
                    Console.Write(sb.ToString());
                    sb.Clear();
                }
            }

            if (menuSelect)
            {
                // Move cursor back to the line.
                PlaceCursor();

                StartEditGroup();

                int top = coords.Y + 1;
                int selectedItem = 0;
                bool undo = false;

                InvertSelectedCompletion(top, 0, menuColumnWidth);
                DoReplacementForCompletion(matchesMatrix[0, 0], completions);

                int previousItem = selectedItem;

                bool processingKeys = true;
                while (processingKeys)
                {
                    var nextKey = ReadKey();
                    if (nextKey == Keys.RightArrow)
                    {
                        selectedItem = Math.Min(selectedItem + displayRows, matches.Count - 1);
                    }
                    else if (nextKey == Keys.LeftArrow)
                    {
                        selectedItem = Math.Max(selectedItem - displayRows, 0);
                    }
                    else if (nextKey == Keys.DownArrow)
                    {
                        selectedItem = Math.Min(selectedItem + 1, matches.Count - 1);
                    }
                    else if (nextKey == Keys.UpArrow)
                    {
                        selectedItem = Math.Max(selectedItem - 1, 0);
                    }
                    else if (nextKey == Keys.Tab)
                    {
                        selectedItem = (selectedItem + 1) % matches.Count;
                    }
                    else if (nextKey == Keys.ShiftTab)
                    {
                        selectedItem = (selectedItem - 1) % matches.Count;
                        if (selectedItem < 0)
                        {
                            selectedItem += matches.Count;
                        }
                    }
                    else if (nextKey == Keys.CtrlG || nextKey == Keys.Escape)
                    {
                        undo = true;
                        processingKeys = false;
                    }
                    else
                    {
                        PrependQueuedKeys(nextKey);
                        processingKeys = false;
                    }

                    if (selectedItem != previousItem)
                    {
                        var selectedX = selectedItem / displayRows;
                        var selectedY = selectedItem - (selectedX * displayRows);
                        var completionResult = matchesMatrix[selectedX, selectedY];
                        if (completionResult != null)
                        {
                            var previousX = previousItem / displayRows;
                            var previousY = previousItem - (previousX * displayRows);
                            InvertSelectedCompletion(previousY + top, previousX, menuColumnWidth);
                            InvertSelectedCompletion(selectedY + top, selectedX, menuColumnWidth);
                            DoReplacementForCompletion(completionResult, completions);
                            previousItem = selectedItem;
                        }
                        else
                        {
                            selectedItem = previousItem;
                        }
                    }
                }

                var blanks = new CHAR_INFO[displayRows * bufferWidth];
                for (int i = 0; i < displayRows; i++)
                {
                    blanks[i].BackgroundColor = Console.BackgroundColor;
                    blanks[i].ForegroundColor = Console.ForegroundColor;
                    blanks[i].UnicodeChar = ' ';
                }
                WriteBufferLines(blanks, ref top);

                EndEditGroup();

                if (undo)
                {
                    // Pretend it never happened.
                    Undo();
                }
            }
            else
            {
                _initialY = Console.CursorTop;
                Render();
            }
        }