示例#1
0
        private void AddSpansForStringToken(StringExpandableToken stringToken, int spanStart, List <ClassificationInfo> classificationInfo)
        {
            var startOffset = stringToken.Extent.StartOffset;

            foreach (var current in stringToken.NestedTokens)
            {
                ToClassificationInfo(stringToken, startOffset + spanStart, current.Extent.StartOffset - startOffset, classificationInfo);
                AddSpanForToken(current, spanStart, classificationInfo);
                startOffset = current.Extent.EndOffset;
            }

            ToClassificationInfo(stringToken, startOffset + spanStart, stringToken.Extent.EndOffset - startOffset, classificationInfo);
        }
示例#2
0
        internal CompletionContext CreateCompletionContext(ExecutionContext executionContext)
        {
            Func <Token, bool> predicate         = null;
            Func <Token, bool> func2             = null;
            Token           token                = null;
            IScriptPosition positionForAstSearch = this._cursorPosition;
            bool            flag   = false;
            Token           token2 = this._tokens.LastOrDefault <Token>(t => IsCursorWithinOrJustAfterExtent(this._cursorPosition, t.Extent) && IsInterestingToken(t));

            if (token2 == null)
            {
                if (predicate == null)
                {
                    predicate = t => IsCursorBeforeExtent(this._cursorPosition, t.Extent) && IsInterestingToken(t);
                }
                token = this._tokens.LastOrDefault <Token>(predicate);
                if (token != null)
                {
                    positionForAstSearch = token.Extent.EndScriptPosition;
                    flag = true;
                }
            }
            else
            {
                StringExpandableToken token3 = token2 as StringExpandableToken;
                if ((token3 != null) && (token3.NestedTokens != null))
                {
                    if (func2 == null)
                    {
                        func2 = t => IsCursorWithinOrJustAfterExtent(this._cursorPosition, t.Extent) && IsInterestingToken(t);
                    }
                    token2 = token3.NestedTokens.LastOrDefault <Token>(func2) ?? token3;
                }
            }
            IEnumerable <Ast> source = AstSearcher.FindAll(this._ast, ast => IsCursorWithinOrJustAfterExtent(positionForAstSearch, ast.Extent), true);

            return(new CompletionContext {
                TokenAtCursor = token2, TokenBeforeCursor = token, CursorPosition = this._cursorPosition, RelatedAsts = source.ToList <Ast>(), Options = this._options, ExecutionContext = executionContext, ReplacementIndex = flag ? this._cursorPosition.Offset : 0
            });
        }
示例#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);
        }