public static void FindExpression(PythonAst ast, SourceLocation position, FindExpressionOptions options, out Node expression, out Node statement, out ScopeStatement scope)
        {
            expression = null;
            statement  = null;
            scope      = null;

            if (ast == null)
            {
                return;
            }

            var finder = new ExpressionFinder(ast, options);

            var index = ast.LocationToIndex(position);

            finder.Get(index, index, out expression, out statement, out scope);

            var col = position.Column;

            while (CanBackUp(ast, expression, statement, scope, col))
            {
                col   -= 1;
                index -= 1;
                finder.Get(index, index, out expression, out statement, out scope);
            }

            expression = expression ?? (statement as ExpressionStatement)?.Expression;
        }
Example #2
0
        public CompletionAnalysis(
            IModuleAnalysis analysis,
            PythonAst tree,
            SourceLocation position,
            GetMemberOptions opts,
            ServerSettings.PythonCompletionOptions completionSettings,
            DocumentationBuilder textBuilder,
            ILogger log,
            Func <TextReader> openDocument
            )
        {
            Analysis      = analysis ?? throw new ArgumentNullException(nameof(analysis));
            Tree          = tree ?? throw new ArgumentNullException(nameof(tree));
            Position      = position;
            Index         = Tree.LocationToIndex(Position);
            Options       = opts;
            _pathResolver = analysis.ProjectState.CurrentPathResolver;
            _textBuilder  = textBuilder;
            _log          = log;
            _openDocument = openDocument;
            _addBrackets  = completionSettings.addBrackets;

            var finder = new ExpressionFinder(Tree, new GetExpressionOptions {
                Names              = true,
                Members            = true,
                NamedArgumentNames = true,
                ImportNames        = true,
                ImportAsNames      = true,
                Literals           = true,
                Errors             = true
            });

            finder.Get(Index, Index, out var node, out _statement, out _scope);

            var index = Index;
            var col   = Position.Column;

            while (CanBackUp(Tree, node, _statement, _scope, col))
            {
                col   -= 1;
                index -= 1;
                finder.Get(index, index, out node, out _statement, out _scope);
            }

            Node = node ?? (_statement as ExpressionStatement)?.Expression;
        }
Example #3
0
        public CompletionAnalysis(
            ModuleAnalysis analysis,
            PythonAst tree,
            SourceLocation position,
            GetMemberOptions opts,
            DocumentationBuilder textBuilder,
            ILogger log
            )
        {
            Analysis     = analysis ?? throw new ArgumentNullException(nameof(analysis));
            Tree         = tree ?? throw new ArgumentNullException(nameof(tree));
            Position     = position;
            Index        = Tree.LocationToIndex(Position);
            Options      = opts;
            _textBuilder = textBuilder;
            _log         = log;

            var finder = new ExpressionFinder(Tree, new GetExpressionOptions {
                Names              = true,
                Members            = true,
                NamedArgumentNames = true,
                ImportNames        = true,
                ImportAsNames      = true,
                Literals           = true,
            });

            finder.Get(Index, Index, out _node, out _statement, out _scope);

            int index = Index;
            int col   = Position.Column;

            while (CanBackUp(Tree, _node, _statement, _scope, col))
            {
                col   -= 1;
                index -= 1;
                finder.Get(index, index, out _node, out _statement, out _scope);
            }

            _node = _node ?? (_statement as ExpressionStatement)?.Expression;
        }
Example #4
0
        public UniqueNameGenerator(IDocumentAnalysis analysis, int position)
        {
            _analysis       = analysis;
            _uniqueInModule = position < 0;

            if (!_uniqueInModule)
            {
                var finder = new ExpressionFinder(analysis.Ast, new FindExpressionOptions()
                {
                    Names = true
                });
                finder.Get(position, position, out _, out _, out _scope);
            }
        }
Example #5
0
        public CompletionAnalysis(ModuleAnalysis analysis, PythonAst tree, SourceLocation position, GetMemberOptions opts, Action <FormattableString> trace)
        {
            Analysis = analysis ?? throw new ArgumentNullException(nameof(analysis));
            Tree     = tree ?? throw new ArgumentNullException(nameof(tree));
            Position = position;
            Index    = Tree.LocationToIndex(Position);
            Options  = opts;
            _trace   = trace;

            var finder = new ExpressionFinder(Tree, new GetExpressionOptions {
                Names              = true,
                MemberName         = true,
                NamedArgumentNames = true,
                ImportNames        = true,
                ImportAsNames      = true,
                Literals           = true,
            });

            finder.Get(Index, Index, out _node, out _statement, out _scope);
        }
Example #6
0
        public CompletionAnalysis(ModuleAnalysis analysis, PythonAst tree, SourceLocation position, GetMemberOptions opts, DocumentationBuilder textBuilder, ILogger log)
        {
            Analysis     = analysis ?? throw new ArgumentNullException(nameof(analysis));
            Tree         = tree ?? throw new ArgumentNullException(nameof(tree));
            Position     = position;
            Index        = Tree.LocationToIndex(Position);
            Options      = opts;
            _textBuilder = textBuilder;
            _log         = log;

            var finder = new ExpressionFinder(Tree, new GetExpressionOptions {
                Names              = true,
                MemberName         = true,
                NamedArgumentNames = true,
                ImportNames        = true,
                ImportAsNames      = true,
                Literals           = true,
            });

            finder.Get(Index, Index, out _node, out _statement, out _scope);
        }