private static void AssertNoExpr(PythonAstAndSource astAndSource, int startLine, int startColumn, int endLine, int endColumn)
        {
            var ast     = astAndSource.Ast;
            var code    = astAndSource.Source;
            var options = astAndSource.Options;

            int start       = ast.LocationToIndex(new SourceLocation(startLine, 1));
            int end         = ast.LocationToIndex(new SourceLocation(endLine + 1, 1));
            var fullLine    = code.Substring(start);
            int lastNewline = Environment.NewLine.Select(c => fullLine.LastIndexOf(c, end - start - 1)).Where(i => i > 0).DefaultIfEmpty(-1).Min();

            if (lastNewline > 0)
            {
                fullLine = fullLine.Remove(lastNewline);
            }

            var finder = new ExpressionFinder(ast, options);
            var range  = new SourceSpan(new SourceLocation(startLine, startColumn), new SourceLocation(endLine, endColumn));
            var span   = finder.GetExpressionSpan(range);

            if (span == null || span.Value.Start == span.Value.End)
            {
                return;
            }

            start = ast.LocationToIndex(span.Value.Start);
            end   = ast.LocationToIndex(span.Value.End);
            var actual = code.Substring(start, end - start);

            Assert.Fail($"Found unexpected expression <{actual}> at from {range} at {span.Value} in <{fullLine}>");
        }
        private static void AssertExpr(PythonAstAndSource astAndSource, int startLine, int startColumn, int endLine, int endColumn, string expected)
        {
            var ast     = astAndSource.Ast;
            var code    = astAndSource.Source;
            var options = astAndSource.Options;

            int start       = ast.LocationToIndex(new SourceLocation(startLine, 1));
            int end         = ast.LocationToIndex(new SourceLocation(endLine + 1, 1));
            var fullLine    = code.Substring(start);
            int lastNewline = Environment.NewLine.Select(c => fullLine.LastIndexOf(c, end - start - 1)).Where(i => i > 0).DefaultIfEmpty(-1).Min();

            if (lastNewline > 0)
            {
                fullLine = fullLine.Remove(lastNewline);
            }

            var finder = new ExpressionFinder(ast, options);
            var range  = new SourceSpan(new SourceLocation(startLine, startColumn), new SourceLocation(endLine, endColumn));
            var span   = finder.GetExpressionSpan(range);

            Assert.IsNotNull(span, $"Did not find any expression at {range} in <{fullLine}>");

            start = ast.LocationToIndex(span.Value.Start);
            end   = ast.LocationToIndex(span.Value.End);
            var actual = code.Substring(start, end - start);

            Assert.AreEqual(expected, actual, $"Mismatched expression from {range} at {span.Value} in <{fullLine}>");
        }
Ejemplo n.º 3
0
        internal SnapshotSpan?GetExpressionAtPointWorker(SnapshotSpan span, GetExpressionOptions options)
        {
            // First do some very quick tokenization to save a full analysis
            if (!IsPossibleExpressionAtPoint(span.Start))
            {
                return(null);
            }

            if (span.End.GetContainingLine() != span.Start.GetContainingLine() &&
                !IsPossibleExpressionAtPoint(span.End))
            {
                return(null);
            }

            var sourceSpan = new SnapshotSpanSourceCodeReader(
                new SnapshotSpan(span.Snapshot, 0, span.End.Position)
                );

            PythonAst ast;

            using (var parser = Parser.CreateParser(sourceSpan, LanguageVersion)) {
                ast = parser.ParseFile();
            }

            var finder     = new ExpressionFinder(ast, options);
            var actualExpr = finder.GetExpressionSpan(span.ToSourceSpan());

            return(actualExpr?.ToSnapshotSpan(span.Snapshot));
        }