public static bool IsInNestedScriptBlock(Ast ast, LineBreakpoint breakpoint)
            {
                var visitor = new CheckBreakpointInScript {
                    _breakpoint = breakpoint
                };

                ast.InternalVisit(visitor);
                return(visitor._result);
            }
Exemple #2
0
 internal bool TrySetBreakpoint(string scriptFile, FunctionContext functionContext)
 {
     if (scriptFile.Equals(base.Script, StringComparison.OrdinalIgnoreCase))
     {
         int num;
         System.Management.Automation.ScriptBlock block = functionContext._scriptBlock;
         Ast ast = block.Ast;
         if (!ast.Extent.ContainsLineAndColumn(this.Line, this.Column))
         {
             return(false);
         }
         IScriptExtent[] sequencePoints = block.SequencePoints;
         if ((sequencePoints.Length == 1) && (sequencePoints[0] == block.Ast.Extent))
         {
             return(false);
         }
         bool          flag   = CheckBreakpointInScript.IsInNestedScriptBlock(((IParameterMetadataProvider)ast).Body, this);
         IScriptExtent extent = FindSequencePoint(functionContext, this.Line, this.Column, out num);
         if ((extent != null) && (!flag || ((extent.StartLineNumber == this.Line) && (this.Column == 0))))
         {
             this.SetBreakpoint(functionContext, num);
             return(true);
         }
         if (flag)
         {
             return(false);
         }
         ScriptBlockAst body = ((IParameterMetadataProvider)ast).Body;
         if ((((body.DynamicParamBlock == null) || body.DynamicParamBlock.Extent.IsAfter(this.Line, this.Column)) && ((body.BeginBlock == null) || body.BeginBlock.Extent.IsAfter(this.Line, this.Column))) && (((body.ProcessBlock == null) || body.ProcessBlock.Extent.IsAfter(this.Line, this.Column)) && ((body.EndBlock == null) || body.EndBlock.Extent.IsAfter(this.Line, this.Column))))
         {
             this.SetBreakpoint(functionContext, 0);
             return(true);
         }
         if ((this.Column == 0) && (FindSequencePoint(functionContext, this.Line + 1, 0, out num) != null))
         {
             this.SetBreakpoint(functionContext, num);
             return(true);
         }
     }
     return(false);
 }
        internal bool TrySetBreakpoint(string scriptFile, FunctionContext functionContext)
        {
            Diagnostics.Assert(SequencePointIndex == -1, "shouldn't be trying to set on a pending breakpoint");

            if (!scriptFile.Equals(this.Script, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            // A quick check to see if the breakpoint is within the scriptblock.
            bool couldBeInNestedScriptBlock;
            var  scriptBlock = functionContext._scriptBlock;

            if (scriptBlock != null)
            {
                var ast = scriptBlock.Ast;
                if (!ast.Extent.ContainsLineAndColumn(Line, Column))
                {
                    return(false);
                }

                var sequencePoints = functionContext._sequencePoints;
                if (sequencePoints.Length == 1 && sequencePoints[0] == scriptBlock.Ast.Extent)
                {
                    // If there was no real executable code in the function (e.g. only function definitions),
                    // we added the entire scriptblock as a sequence point, but it shouldn't be allowed as a breakpoint.
                    return(false);
                }

                couldBeInNestedScriptBlock = CheckBreakpointInScript.IsInNestedScriptBlock(((IParameterMetadataProvider)ast).Body, this);
            }
            else
            {
                couldBeInNestedScriptBlock = false;
            }

            int sequencePointIndex;
            var sequencePoint = FindSequencePoint(functionContext, Line, Column, out sequencePointIndex);

            if (sequencePoint != null)
            {
                // If the bp could be in a nested script block, we want to be careful and get the bp in the correct script block.
                // If it's a simple line bp (no column specified), then the start line must match the bp line exactly, otherwise
                // we assume the bp is in the nested script block.
                if (!couldBeInNestedScriptBlock || (sequencePoint.StartLineNumber == Line && Column == 0))
                {
                    SetBreakpoint(functionContext, sequencePointIndex);
                    return(true);
                }
            }

            // Before using heuristics, make sure the breakpoint isn't in a nested function/script block.
            if (couldBeInNestedScriptBlock)
            {
                return(false);
            }

            // Not found.  First, we check if the line/column is before any real code.  If so, we'll
            // move the breakpoint to the first interesting sequence point (could be a dynamicparam,
            // begin, process, or end block.)
            if (scriptBlock != null)
            {
                var ast     = scriptBlock.Ast;
                var bodyAst = ((IParameterMetadataProvider)ast).Body;
                if ((bodyAst.DynamicParamBlock == null || bodyAst.DynamicParamBlock.Extent.IsAfter(Line, Column)) &&
                    (bodyAst.BeginBlock == null || bodyAst.BeginBlock.Extent.IsAfter(Line, Column)) &&
                    (bodyAst.ProcessBlock == null || bodyAst.ProcessBlock.Extent.IsAfter(Line, Column)) &&
                    (bodyAst.EndBlock == null || bodyAst.EndBlock.Extent.IsAfter(Line, Column)))
                {
                    SetBreakpoint(functionContext, 0);
                    return(true);
                }
            }

            // Still not found.  Try fudging a bit, but only if it's a simple line breakpoint.
            if (Column == 0 && FindSequencePoint(functionContext, Line + 1, 0, out sequencePointIndex) != null)
            {
                SetBreakpoint(functionContext, sequencePointIndex);
                return(true);
            }

            return(false);
        }
Exemple #4
0
 public static bool IsInNestedScriptBlock(Ast ast, LineBreakpoint breakpoint)
 {
     var visitor = new CheckBreakpointInScript { _breakpoint = breakpoint };
     ast.InternalVisit(visitor);
     return visitor._result;
 }