Ejemplo n.º 1
0
        private List <CompletionResult> GetResultForString(CompletionContext completionContext, ref int replacementIndex, ref int replacementLength, bool isQuotedString)
        {
            if (isQuotedString)
            {
                return(null);
            }
            Token tokenAtCursor = completionContext.TokenAtCursor;
            Ast   ast           = completionContext.RelatedAsts.Last <Ast>();
            List <CompletionResult>       list = null;
            ExpandableStringExpressionAst ast2 = ast as ExpandableStringExpressionAst;
            StringConstantExpressionAst   ast3 = ast as StringConstantExpressionAst;

            if ((ast3 != null) || (ast2 != null))
            {
                string             input = (ast3 != null) ? ast3.Value : ast2.Value;
                StringConstantType type  = (ast3 != null) ? ast3.StringConstantType : ast2.StringConstantType;
                string             str2  = null;
                if (type == StringConstantType.DoubleQuoted)
                {
                    Match match = Regex.Match(input, @"(\$[\w\d]+\.[\w\d\*]*)$");
                    if (match.Success)
                    {
                        str2 = match.Groups[1].Value;
                    }
                    else if ((match = Regex.Match(input, @"(\[[\w\d\.]+\]::[\w\d\*]*)$")).Success)
                    {
                        str2 = match.Groups[1].Value;
                    }
                }
                if (str2 != null)
                {
                    int num3;
                    int num4;
                    int offset = tokenAtCursor.Extent.StartScriptPosition.Offset;
                    int length = (this._cursorPosition.Offset - offset) - 1;
                    if (length >= input.Length)
                    {
                        length = input.Length;
                    }
                    CompletionAnalysis analysis = new CompletionAnalysis(this._ast, this._tokens, this._cursorPosition, this._options);
                    CompletionContext  context  = analysis.CreateCompletionContext(completionContext.ExecutionContext);
                    context.Helper = completionContext.Helper;
                    List <CompletionResult> list2 = analysis.GetResultHelper(context, out num3, out num4, true);
                    if ((list2 != null) && (list2.Count > 0))
                    {
                        list              = new List <CompletionResult>();
                        replacementIndex  = (offset + 1) + (length - str2.Length);
                        replacementLength = str2.Length;
                        string str3 = str2.Substring(0, num3);
                        foreach (CompletionResult result in list2)
                        {
                            string completionText = str3 + result.CompletionText;
                            if (result.ResultType.Equals(CompletionResultType.Property))
                            {
                                completionText = TokenKind.DollarParen.Text() + completionText + TokenKind.RParen.Text();
                            }
                            else if (result.ResultType.Equals(CompletionResultType.Method))
                            {
                                completionText = TokenKind.DollarParen.Text() + completionText;
                            }
                            completionText = completionText + "\"";
                            list.Add(new CompletionResult(completionText, result.ListItemText, result.ResultType, result.ToolTip));
                        }
                    }
                    return(list);
                }
                CommandElementAst stringAst = ast as CommandElementAst;
                string            str5      = CompletionCompleters.ConcatenateStringPathArguments(stringAst, string.Empty, completionContext);
                if (str5 == null)
                {
                    return(list);
                }
                completionContext.WordToComplete = str5;
                if ((ast.Parent is CommandAst) || (ast.Parent is CommandParameterAst))
                {
                    list              = CompletionCompleters.CompleteCommandArgument(completionContext);
                    replacementIndex  = completionContext.ReplacementIndex;
                    replacementLength = completionContext.ReplacementLength;
                    return(list);
                }
                list = new List <CompletionResult>(CompletionCompleters.CompleteFilename(completionContext));
                if (str5.IndexOf('-') != -1)
                {
                    List <CompletionResult> collection = CompletionCompleters.CompleteCommand(completionContext);
                    if ((collection != null) && (collection.Count > 0))
                    {
                        list.AddRange(collection);
                    }
                }
            }
            return(list);
        }
Ejemplo n.º 2
0
        internal List <CompletionResult> GetResultHelper(CompletionContext completionContext, out int replacementIndex, out int replacementLength, bool isQuotedString)
        {
            replacementIndex  = -1;
            replacementLength = -1;
            Token tokenAtCursor          = completionContext.TokenAtCursor;
            Ast   lastAst                = completionContext.RelatedAsts.Last <Ast>();
            List <CompletionResult> list = null;

            if (tokenAtCursor == null)
            {
                if (!isQuotedString && (((((lastAst is CommandParameterAst) || (lastAst is CommandAst)) || ((lastAst is ExpressionAst) && (lastAst.Parent is CommandAst))) || ((lastAst is ExpressionAst) && (lastAst.Parent is CommandParameterAst))) || (((lastAst is ExpressionAst) && (lastAst.Parent is ArrayLiteralAst)) && ((lastAst.Parent.Parent is CommandAst) || (lastAst.Parent.Parent is CommandParameterAst)))))
                {
                    completionContext.WordToComplete = string.Empty;
                    HashtableAst hashtableAst = lastAst as HashtableAst;
                    if (hashtableAst != null)
                    {
                        completionContext.ReplacementIndex  = replacementIndex = completionContext.CursorPosition.Offset;
                        completionContext.ReplacementLength = replacementLength = 0;
                        list = CompletionCompleters.CompleteHashtableKey(completionContext, hashtableAst);
                    }
                    else
                    {
                        list              = CompletionCompleters.CompleteCommandArgument(completionContext);
                        replacementIndex  = completionContext.ReplacementIndex;
                        replacementLength = completionContext.ReplacementLength;
                    }
                }
                else if (!isQuotedString)
                {
                    bool flag = false;
                    if ((lastAst is ErrorExpressionAst) && (lastAst.Parent is FileRedirectionAst))
                    {
                        flag = true;
                    }
                    else if ((lastAst is ErrorStatementAst) && CompleteAgainstSwitchFile(lastAst, completionContext.TokenBeforeCursor))
                    {
                        flag = true;
                    }
                    if (flag)
                    {
                        completionContext.WordToComplete = string.Empty;
                        list              = new List <CompletionResult>(CompletionCompleters.CompleteFilename(completionContext));
                        replacementIndex  = completionContext.ReplacementIndex;
                        replacementLength = completionContext.ReplacementLength;
                    }
                }
            }
            else
            {
                TokenKind kind;
                replacementIndex  = tokenAtCursor.Extent.StartScriptPosition.Offset;
                replacementLength = tokenAtCursor.Extent.EndScriptPosition.Offset - replacementIndex;
                completionContext.ReplacementIndex  = replacementIndex;
                completionContext.ReplacementLength = replacementLength;
                switch (tokenAtCursor.Kind)
                {
                case TokenKind.ColonColon:
                case TokenKind.Dot:
                    replacementIndex += tokenAtCursor.Text.Length;
                    replacementLength = 0;
                    list = CompletionCompleters.CompleteMember(completionContext, tokenAtCursor.Kind == TokenKind.ColonColon);
                    goto Label_05DC;

                case TokenKind.Multiply:
                case TokenKind.Identifier:
                case TokenKind.Generic:
                    list = this.GetResultForIdentifier(completionContext, ref replacementIndex, ref replacementLength, isQuotedString);
                    goto Label_05DC;

                case TokenKind.Minus:
                    if (CompleteOperator(tokenAtCursor, lastAst))
                    {
                        list = CompletionCompleters.CompleteOperator("");
                    }
                    else if (CompleteAgainstStatementFlags(completionContext.RelatedAsts[0], null, tokenAtCursor, out kind))
                    {
                        completionContext.WordToComplete = tokenAtCursor.Text;
                        list = CompletionCompleters.CompleteStatementFlags(kind, completionContext.WordToComplete);
                    }
                    goto Label_05DC;

                case TokenKind.Redirection:
                    if ((lastAst is ErrorExpressionAst) && (lastAst.Parent is FileRedirectionAst))
                    {
                        completionContext.WordToComplete    = string.Empty;
                        completionContext.ReplacementIndex  = replacementIndex += tokenAtCursor.Text.Length;
                        completionContext.ReplacementLength = replacementLength = 0;
                        list = new List <CompletionResult>(CompletionCompleters.CompleteFilename(completionContext));
                    }
                    goto Label_05DC;

                case TokenKind.Variable:
                case TokenKind.SplattedVariable:
                    completionContext.WordToComplete = ((VariableToken)tokenAtCursor).VariablePath.UserPath;
                    list = CompletionCompleters.CompleteVariable(completionContext);
                    goto Label_05DC;

                case TokenKind.Parameter:
                    if (!isQuotedString)
                    {
                        completionContext.WordToComplete = tokenAtCursor.Text;
                        CommandAst parent = lastAst.Parent as CommandAst;
                        if ((!(lastAst is StringConstantExpressionAst) || (parent == null)) || (parent.CommandElements.Count != 1))
                        {
                            if (CompleteAgainstStatementFlags(null, lastAst, null, out kind))
                            {
                                list = CompletionCompleters.CompleteStatementFlags(kind, completionContext.WordToComplete);
                            }
                            else if (CompleteOperator(tokenAtCursor, lastAst))
                            {
                                list = CompletionCompleters.CompleteOperator(completionContext.WordToComplete);
                            }
                            else if (completionContext.WordToComplete.EndsWith(":", StringComparison.Ordinal))
                            {
                                replacementIndex  = tokenAtCursor.Extent.EndScriptPosition.Offset;
                                replacementLength = 0;
                                completionContext.WordToComplete = string.Empty;
                                list = CompletionCompleters.CompleteCommandArgument(completionContext);
                            }
                            else
                            {
                                list = CompletionCompleters.CompleteCommandParameter(completionContext);
                            }
                        }
                        else
                        {
                            list = CompleteFileNameAsCommand(completionContext);
                        }
                    }
                    goto Label_05DC;

                case TokenKind.Number:
                    if ((lastAst is ConstantExpressionAst) && (((lastAst.Parent is CommandAst) || (lastAst.Parent is CommandParameterAst)) || ((lastAst.Parent is ArrayLiteralAst) && ((lastAst.Parent.Parent is CommandAst) || (lastAst.Parent.Parent is CommandParameterAst)))))
                    {
                        completionContext.WordToComplete = tokenAtCursor.Text;
                        list              = CompletionCompleters.CompleteCommandArgument(completionContext);
                        replacementIndex  = completionContext.ReplacementIndex;
                        replacementLength = completionContext.ReplacementLength;
                    }
                    goto Label_05DC;

                case TokenKind.Comment:
                    if (!isQuotedString)
                    {
                        completionContext.WordToComplete = tokenAtCursor.Text;
                        list = CompletionCompleters.CompleteComment(completionContext);
                    }
                    goto Label_05DC;

                case TokenKind.StringLiteral:
                case TokenKind.StringExpandable:
                    list = this.GetResultForString(completionContext, ref replacementIndex, ref replacementLength, isQuotedString);
                    goto Label_05DC;

                case TokenKind.RBracket:
                    if (lastAst is TypeExpressionAst)
                    {
                        TypeExpressionAst       targetExpr = (TypeExpressionAst)lastAst;
                        List <CompletionResult> results    = new List <CompletionResult>();
                        CompletionCompleters.CompleteMemberHelper(true, "*", targetExpr, completionContext, results);
                        if (results.Count > 0)
                        {
                            replacementIndex++;
                            replacementLength = 0;
                            list = (from entry in results
                                    let completionText = TokenKind.ColonColon.Text() + entry.CompletionText
                                                         select new CompletionResult(completionText, entry.ListItemText, entry.ResultType, entry.ToolTip)).ToList <CompletionResult>();
                        }
                    }
                    goto Label_05DC;

                case TokenKind.Comma:
                    if ((lastAst is ErrorExpressionAst) && ((lastAst.Parent is CommandAst) || (lastAst.Parent is CommandParameterAst)))
                    {
                        replacementIndex += replacementLength;
                        replacementLength = 0;
                        list = CompletionCompleters.CompleteCommandArgument(completionContext);
                    }
                    goto Label_05DC;
                }
                if ((tokenAtCursor.TokenFlags & TokenFlags.Keyword) != TokenFlags.None)
                {
                    completionContext.WordToComplete = tokenAtCursor.Text;
                    list = CompleteFileNameAsCommand(completionContext);
                    List <CompletionResult> collection = CompletionCompleters.CompleteCommand(completionContext);
                    if ((collection != null) && (collection.Count > 0))
                    {
                        list.AddRange(collection);
                    }
                }
                else
                {
                    replacementIndex  = -1;
                    replacementLength = -1;
                }
            }
Label_05DC:
            if ((list == null) || (list.Count == 0))
            {
                TypeExpressionAst ast5 = completionContext.RelatedAsts.OfType <TypeExpressionAst>().FirstOrDefault <TypeExpressionAst>();
                TypeName          name = null;
                if (ast5 != null)
                {
                    name = FindTypeNameToComplete(ast5.TypeName, this._cursorPosition);
                }
                else
                {
                    TypeConstraintAst ast6 = completionContext.RelatedAsts.OfType <TypeConstraintAst>().FirstOrDefault <TypeConstraintAst>();
                    if (ast6 != null)
                    {
                        name = FindTypeNameToComplete(ast6.TypeName, this._cursorPosition);
                    }
                }
                if (name != null)
                {
                    replacementIndex  = name.Extent.StartOffset;
                    replacementLength = name.Extent.EndOffset - replacementIndex;
                    completionContext.WordToComplete = name.FullName;
                    list = CompletionCompleters.CompleteType(completionContext, "", "");
                }
            }
            if ((list == null) || (list.Count == 0))
            {
                HashtableAst ast7 = lastAst as HashtableAst;
                if (ast7 != null)
                {
                    completionContext.ReplacementIndex  = replacementIndex = completionContext.CursorPosition.Offset;
                    completionContext.ReplacementLength = replacementLength = 0;
                    list = CompletionCompleters.CompleteHashtableKey(completionContext, ast7);
                }
            }
            if ((list == null) || (list.Count == 0))
            {
                string text = completionContext.RelatedAsts[0].Extent.Text;
                if ((Regex.IsMatch(text, @"^[\S]+$") && (completionContext.RelatedAsts.Count > 0)) && (completionContext.RelatedAsts[0] is ScriptBlockAst))
                {
                    replacementIndex  = completionContext.RelatedAsts[0].Extent.StartScriptPosition.Offset;
                    replacementLength = completionContext.RelatedAsts[0].Extent.EndScriptPosition.Offset - replacementIndex;
                    completionContext.WordToComplete = text;
                    list = CompleteFileNameAsCommand(completionContext);
                }
            }
            return(list);
        }
Ejemplo n.º 3
0
        private List <CompletionResult> GetResultForIdentifier(CompletionContext completionContext, ref int replacementIndex, ref int replacementLength, bool isQuotedString)
        {
            Token tokenAtCursor            = completionContext.TokenAtCursor;
            Ast   lastAst                  = completionContext.RelatedAsts.Last <Ast>();
            List <CompletionResult> source = null;

            completionContext.WordToComplete = tokenAtCursor.Text;
            StringConstantExpressionAst ast2 = lastAst as StringConstantExpressionAst;

            if ((ast2 != null) && ast2.Value.Equals("$", StringComparison.Ordinal))
            {
                completionContext.WordToComplete = "";
                return(CompletionCompleters.CompleteVariable(completionContext));
            }
            if ((tokenAtCursor.TokenFlags & TokenFlags.CommandName) != TokenFlags.None)
            {
                if ((completionContext.RelatedAsts.Count > 0) && (completionContext.RelatedAsts[0] is ScriptBlockAst))
                {
                    Ast lastAstAtCursor             = null;
                    InternalScriptPosition position = (InternalScriptPosition)this._cursorPosition;
                    int offset = position.Offset - tokenAtCursor.Text.Length;
                    if (offset >= 0)
                    {
                        InternalScriptPosition cursorPosition = position.CloneWithNewOffset(offset);
                        ScriptBlockAst         scriptBlockAst = (ScriptBlockAst)completionContext.RelatedAsts[0];
                        lastAstAtCursor = GetLastAstAtCursor(scriptBlockAst, cursorPosition);
                    }
                    if (((lastAstAtCursor != null) && (lastAstAtCursor.Extent.EndLineNumber == tokenAtCursor.Extent.StartLineNumber)) && (lastAstAtCursor.Extent.EndColumnNumber == tokenAtCursor.Extent.StartColumnNumber))
                    {
                        if (tokenAtCursor.Text.IndexOfAny(new char[] { '\\', '/' }) == 0)
                        {
                            string str = CompletionCompleters.ConcatenateStringPathArguments(lastAstAtCursor as CommandElementAst, tokenAtCursor.Text, completionContext);
                            if (str != null)
                            {
                                completionContext.WordToComplete = str;
                                source = new List <CompletionResult>(CompletionCompleters.CompleteFilename(completionContext));
                                if (source.Count > 0)
                                {
                                    replacementIndex   = lastAstAtCursor.Extent.StartScriptPosition.Offset;
                                    replacementLength += lastAstAtCursor.Extent.Text.Length;
                                }
                                return(source);
                            }
                            VariableExpressionAst variableAst = lastAstAtCursor as VariableExpressionAst;
                            string str2 = (variableAst != null) ? CompletionCompleters.CombineVariableWithPartialPath(variableAst, tokenAtCursor.Text, completionContext.ExecutionContext) : null;
                            if (str2 == null)
                            {
                                return(source);
                            }
                            completionContext.WordToComplete = str2;
                            replacementIndex   = lastAstAtCursor.Extent.StartScriptPosition.Offset;
                            replacementLength += lastAstAtCursor.Extent.Text.Length;
                            completionContext.ReplacementIndex  = replacementIndex;
                            completionContext.ReplacementLength = replacementLength;
                        }
                        else if (!(lastAstAtCursor is ErrorExpressionAst) || !(lastAstAtCursor.Parent is IndexExpressionAst))
                        {
                            return(source);
                        }
                    }
                }
                if (!isQuotedString)
                {
                    StringExpandableToken token2 = tokenAtCursor as StringExpandableToken;
                    if (((token2 != null) && (token2.NestedTokens != null)) && (ast2 != null))
                    {
                        try
                        {
                            string expandedString = null;
                            ExpandableStringExpressionAst expandableStringAst = new ExpandableStringExpressionAst(ast2.Extent, ast2.Value, StringConstantType.BareWord);
                            if (CompletionCompleters.IsPathSafelyExpandable(expandableStringAst, string.Empty, completionContext.ExecutionContext, out expandedString))
                            {
                                completionContext.WordToComplete = expandedString;
                            }
                            else
                            {
                                return(source);
                            }
                        }
                        catch (Exception exception)
                        {
                            CommandProcessorBase.CheckForSevereException(exception);
                            return(source);
                        }
                    }
                    source = CompleteFileNameAsCommand(completionContext);
                    List <CompletionResult> collection = CompletionCompleters.CompleteCommand(completionContext);
                    if ((collection != null) && (collection.Count > 0))
                    {
                        source.AddRange(collection);
                    }
                }
                return(source);
            }
            if (((tokenAtCursor.Text.Length == 1) && tokenAtCursor.Text[0].IsDash()) && (lastAst.Parent is CommandAst))
            {
                if (isQuotedString)
                {
                    return(source);
                }
                return(CompletionCompleters.CompleteCommandParameter(completionContext));
            }
            TokenKind unknown = TokenKind.Unknown;
            bool      flag    = lastAst.Parent is MemberExpressionAst;
            bool      @static = flag ? ((MemberExpressionAst)lastAst.Parent).Static : false;
            bool      flag3   = false;

            if (!flag)
            {
                if (tokenAtCursor.Text.Equals(TokenKind.Dot.Text(), StringComparison.Ordinal))
                {
                    unknown = TokenKind.Dot;
                    flag    = true;
                }
                else if (tokenAtCursor.Text.Equals(TokenKind.ColonColon.Text(), StringComparison.Ordinal))
                {
                    unknown = TokenKind.ColonColon;
                    flag    = true;
                }
                else if (tokenAtCursor.Kind.Equals(TokenKind.Multiply) && (lastAst is BinaryExpressionAst))
                {
                    BinaryExpressionAst item          = (BinaryExpressionAst)lastAst;
                    MemberExpressionAst left          = item.Left as MemberExpressionAst;
                    IScriptExtent       errorPosition = item.ErrorPosition;
                    if (((left != null) && (item.Operator == TokenKind.Multiply)) && (errorPosition.StartOffset == left.Member.Extent.EndOffset))
                    {
                        @static = left.Static;
                        unknown = @static ? TokenKind.ColonColon : TokenKind.Dot;
                        flag    = true;
                        flag3   = true;
                        completionContext.RelatedAsts.Remove(item);
                        completionContext.RelatedAsts.Add(left);
                        StringConstantExpressionAst member = left.Member as StringConstantExpressionAst;
                        if (member != null)
                        {
                            replacementIndex   = member.Extent.StartScriptPosition.Offset;
                            replacementLength += member.Extent.Text.Length;
                        }
                    }
                }
            }
            if (flag)
            {
                source = CompletionCompleters.CompleteMember(completionContext, @static || (unknown == TokenKind.ColonColon));
                if (source.Any <CompletionResult>())
                {
                    if (!flag3 && (unknown != TokenKind.Unknown))
                    {
                        replacementIndex += tokenAtCursor.Text.Length;
                        replacementLength = 0;
                    }
                    return(source);
                }
            }
            if (lastAst.Parent is HashtableAst)
            {
                source = CompletionCompleters.CompleteHashtableKey(completionContext, (HashtableAst)lastAst.Parent);
                if ((source != null) && source.Any <CompletionResult>())
                {
                    return(source);
                }
            }
            if (!isQuotedString)
            {
                bool flag4 = false;
                if ((lastAst.Parent is FileRedirectionAst) || CompleteAgainstSwitchFile(lastAst, completionContext.TokenBeforeCursor))
                {
                    string str4 = CompletionCompleters.ConcatenateStringPathArguments(lastAst as CommandElementAst, string.Empty, completionContext);
                    if (str4 != null)
                    {
                        flag4 = true;
                        completionContext.WordToComplete = str4;
                    }
                }
                else if (tokenAtCursor.Text.IndexOfAny(new char[] { '\\', '/' }) == 0)
                {
                    CommandBaseAst parent = lastAst.Parent as CommandBaseAst;
                    if ((parent != null) && parent.Redirections.Any <RedirectionAst>())
                    {
                        FileRedirectionAst ast11 = parent.Redirections[0] as FileRedirectionAst;
                        if (((ast11 != null) && (ast11.Extent.EndLineNumber == lastAst.Extent.StartLineNumber)) && (ast11.Extent.EndColumnNumber == lastAst.Extent.StartColumnNumber))
                        {
                            string str5 = CompletionCompleters.ConcatenateStringPathArguments(ast11.Location, tokenAtCursor.Text, completionContext);
                            if (str5 != null)
                            {
                                flag4 = true;
                                completionContext.WordToComplete = str5;
                                replacementIndex   = ast11.Location.Extent.StartScriptPosition.Offset;
                                replacementLength += ast11.Location.Extent.EndScriptPosition.Offset - replacementIndex;
                                completionContext.ReplacementIndex  = replacementIndex;
                                completionContext.ReplacementLength = replacementLength;
                            }
                        }
                    }
                }
                if (flag4)
                {
                    return(new List <CompletionResult>(CompletionCompleters.CompleteFilename(completionContext)));
                }
                string str6 = CompletionCompleters.ConcatenateStringPathArguments(lastAst as CommandElementAst, string.Empty, completionContext);
                if (str6 != null)
                {
                    completionContext.WordToComplete = str6;
                }
                source            = CompletionCompleters.CompleteCommandArgument(completionContext);
                replacementIndex  = completionContext.ReplacementIndex;
                replacementLength = completionContext.ReplacementLength;
            }
            return(source);
        }