public static KeywordContext BuildFromInput( Ast ast, IReadOnlyList <Token> tokens, IScriptPosition cursorPosition) { // Now find the AST we're in CompletionPositionContext positionContext = GetEffectiveScriptPosition(cursorPosition, tokens); Ast containingAst = FindAstFromPositionVisitor.GetContainingAstOfPosition(ast, positionContext.LastNonNewlineToken.Extent.EndScriptPosition); if (containingAst is null) { return(null); } // Find the command AST that we're in CommandAst containingCommandAst = GetFirstParentCommandAst(containingAst); var context = new KeywordContext { ContainingAst = containingAst, ContainingCommandAst = containingCommandAst, FullAst = ast, LastTokenIndex = positionContext.LastTokenIndex, LastToken = positionContext.LastToken, LastNonNewlineToken = positionContext.LastNonNewlineToken, EffectivePositionToken = positionContext.EffectivePositionToken, Tokens = tokens, Position = cursorPosition }; // Build a list of the keyword ASTs we're in going up Ast currAst = containingAst; var commandAsts = new List <CommandAst>(); do { if (currAst is CommandAst commandAst) { commandAsts.Add(commandAst); } currAst = currAst.Parent; } while (currAst != null); // Then build the context list going back down var keywordStack = new List <KeywordContextFrame>(commandAsts.Count); int frameIndex = 0; for (int i = commandAsts.Count - 1; i >= 0; i--) { keywordStack.Add(new KeywordContextFrame(context, frameIndex, commandAsts[i])); frameIndex++; } context.KeywordStack = keywordStack; return(context); }
public KeywordContextFrame( KeywordContext context, int contextIndex, CommandAst commandAst) { ParentContext = context; ContextIndex = contextIndex; CommandAst = commandAst; _resourceNameLazy = new Lazy <ArmResourceName?>(GetResourceName); }
private static KeywordResult?GetCurrentKeyword(Ast ast, IReadOnlyList <Token> tokens, IScriptPosition cursorPosition) { KeywordContext context = KeywordContext.BuildFromInput(ast, tokens, cursorPosition); if (context is null) { return(null); } DslKeywordSchema currentSchema = PSArmSchemaInformation.PSArmSchema; KeywordContextFrame currentFrame = null; for (int i = 0; i < context.KeywordStack.Count; i++) { KeywordContextFrame frame = context.KeywordStack[i]; string commandName = frame.CommandAst.GetCommandName(); if (commandName is null) { continue; } IReadOnlyDictionary <string, DslKeywordSchema> subschemas = currentSchema.GetInnerKeywords(currentFrame); if (subschemas is null || !subschemas.TryGetValue(commandName, out DslKeywordSchema subschema)) { continue; } currentSchema = subschema; currentFrame = frame; } if (currentFrame is null || currentSchema is null) { return(null); } return(new KeywordResult(currentSchema, currentFrame)); }