Exemplo n.º 1
0
 internal ExpressionAnalysis(string expression, ModuleAnalysis analysis, int lineNo, ITrackingSpan span)
 {
     _expr     = expression;
     _analysis = analysis;
     _lineNo   = lineNo;
     _span     = span;
 }
Exemplo n.º 2
0
 private static IEnumerable <AnalysisValue> GetTestCaseClasses(ModuleAnalysis analysis)
 {
     return(analysis.GetAllAvailableMembersByIndex(0)
            .SelectMany(m => analysis.GetValuesByIndex(m.Name, 0))
            .Where(v => v.MemberType == PythonMemberType.Class)
            .Where(v => v.Mro.SelectMany(v2 => v2).Any(IsTestCaseClass)));
 }
Exemplo n.º 3
0
 public CompletionEventArgs(ModuleAnalysis analysis, PythonAst tree, SourceLocation location, CompletionList initialCompletionList)
 {
     Analysis       = analysis;
     Tree           = tree;
     Location       = location;
     CompletionList = initialCompletionList;
 }
Exemplo n.º 4
0
 private static IEnumerable <AnalysisValue> GetTestCaseClasses(ModuleAnalysis analysis)
 {
     return(analysis.GetAllAvailableMembers(SourceLocation.MinValue, GetMemberOptions.ExcludeBuiltins)
            .SelectMany(m => analysis.GetValues(m.Name, SourceLocation.MinValue))
            .Where(v => v.MemberType == PythonMemberType.Class)
            .Where(v => v.Mro.SelectMany(v2 => v2).Any(IsTestCaseClass)));
 }
Exemplo n.º 5
0
 public ClassifierWalker(PythonAst ast, ModuleAnalysis analysis, ITextSnapshot snapshot, Dictionary <string, IClassificationType> formatMap)
 {
     _ast       = ast;
     _analysis  = analysis;
     _snapshot  = snapshot;
     _formatMap = formatMap;
     Spans      = new List <List <CachedClassification> >();
 }
 internal ExpressionAnalysis(VsProjectAnalyzer analyzer, string expression, ModuleAnalysis analysis, int index, ITrackingSpan span, ITextSnapshot snapshot)
 {
     _expr     = expression;
     _analysis = analysis;
     _index    = index;
     _span     = span;
     _analyzer = analyzer;
     _snapshot = snapshot;
 }
Exemplo n.º 7
0
        private void DumpMembers(ModuleAnalysis moduleAnalysis, string memberCode, int depth)
        {
            var moduleMembers = moduleAnalysis.GetMembersByIndex(memberCode, 0).ToArray();

            Array.Sort(moduleMembers, (x, y) => String.Compare(x.Name, y.Name));

            if (depth < _dumpMembers.Value)
            {
                foreach (var member in moduleMembers)
                {
                    Console.WriteLine("    {0} {1}", new string(' ', depth * 4), member.Name);

                    DumpMembers(moduleAnalysis, memberCode + "." + member.Completion, depth + 1);
                }
            }
        }
Exemplo n.º 8
0
        public void ProcessCompletionList(ModuleAnalysis analysis, PythonAst tree, SourceLocation location, CompletionList completions)
        {
            var evt = PostProcessCompletion;

            if (evt != null)
            {
                var e = new Extensibility.CompletionEventArgs(analysis, tree, location, completions);
                try {
                    evt(this, e);
                    completions       = e.CompletionList;
                    completions.items = completions.items ?? Array.Empty <CompletionItem>();
                } catch (Exception ex) when(!ex.IsCriticalException())
                {
                    // We do not replace res in this case.
                }
            }
        }
Exemplo n.º 9
0
        public CompletionAnalysis(
            ModuleAnalysis analysis,
            PythonAst tree,
            SourceLocation position,
            GetMemberOptions opts,
            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;
            _textBuilder  = textBuilder;
            _log          = log;
            _openDocument = openDocument;

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

            Node node;

            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;
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Get Test Case Members for a class.  If the class has 'test*' tests
        /// return those.  If there aren't any 'test*' tests return (if one at
        /// all) the runTest overridden method
        /// </summary>
        private static IEnumerable <KeyValuePair <string, IAnalysisSet> > GetTestCaseMembers(
            ModuleAnalysis analysis,
            AnalysisValue classValue
            )
        {
            var methodFunctions = classValue.GetAllMembers(analysis.InterpreterContext)
                                  .Where(v => v.Value.Any(m => m.MemberType == PythonMemberType.Function || m.MemberType == PythonMemberType.Method));

            var tests   = methodFunctions.Where(v => v.Key.StartsWith("test"));
            var runTest = methodFunctions.Where(v => v.Key.Equals("runTest"));

            if (tests.Any())
            {
                return(tests);
            }
            else
            {
                return(runTest);
            }
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Get Test Case Members for a class.  If the class has 'test*' tests
        /// return those.  If there aren't any 'test*' tests return (if one at
        /// all) the runTest overridden method
        /// </summary>
        private static IEnumerable <KeyValuePair <string, LocationInfo> > GetTestCaseMembers(
            PythonAst ast,
            string sourceFile,
            Uri documentUri,
            ModuleAnalysis analysis,
            AnalysisValue classValue
            )
        {
            IEnumerable <KeyValuePair <string, LocationInfo> > tests = null, runTest = null;

            if (ast != null && !string.IsNullOrEmpty(sourceFile))
            {
                var walker = new TestMethodWalker(ast, sourceFile, documentUri, classValue.Locations);
                ast.Walk(walker);
                tests   = walker.Methods.Where(v => v.Key.StartsWithOrdinal("test"));
                runTest = walker.Methods.Where(v => v.Key.Equals("runTest"));
            }

            var methodFunctions = classValue.GetAllMembers(analysis.InterpreterContext)
                                  .Where(v => v.Value.Any(m => m.MemberType == PythonMemberType.Function || m.MemberType == PythonMemberType.Method))
                                  .Select(v => new KeyValuePair <string, LocationInfo>(v.Key, v.Value.SelectMany(av => av.Locations).FirstOrDefault(l => l != null)));

            var analysisTests = methodFunctions.Where(v => v.Key.StartsWithOrdinal("test"));
            var analysisRunTest = methodFunctions.Where(v => v.Key.Equals("runTest"));

            tests   = tests?.Concat(analysisTests) ?? analysisTests;
            runTest = runTest?.Concat(analysisRunTest) ?? analysisRunTest;

            if (tests.Any())
            {
                return(tests);
            }
            else
            {
                return(runTest);
            }
        }
Exemplo n.º 14
0
 private static IEnumerable <MemberResult> GetChildScopesVariables(ModuleAnalysis analysis, InterpreterScope scope, GetMemberOptions opts, int currentDepth)
 => currentDepth < _symbolHierarchyDepthLimit
         ? scope.Children.SelectMany(c => GetScopeVariables(analysis, c, opts, currentDepth))
         : Enumerable.Empty <MemberResult>();
Exemplo n.º 15
0
 public ClassifierWalker(PythonAst ast, ModuleAnalysis analysis)
 {
     _ast      = ast;
     _analysis = analysis;
     Spans     = new List <AP.AnalysisClassification>();
 }
Exemplo n.º 16
0
 public ClassifierWalker(PythonAst ast, ModuleAnalysis analysis)
 {
     _ast      = ast;
     _analysis = analysis;
     Spans     = new List <TaggedSpan>();
 }
Exemplo n.º 17
0
 private static IEnumerable <MemberResult> GetScopeVariables(ModuleAnalysis analysis, InterpreterScope scope, GetMemberOptions opts)
 => analysis.GetAllAvailableMembersFromScope(scope, opts).Concat(GetChildScopesVariables(analysis, scope, opts));
Exemplo n.º 18
0
 private static IEnumerable <MemberResult> GetChildScopesVariables(ModuleAnalysis analysis, InterpreterScope scope, GetMemberOptions opts)
 => scope.Children.SelectMany(c => GetScopeVariables(analysis, c, opts));
Exemplo n.º 19
0
        private static IEnumerable <MemberResult> GetModuleVariables(ProjectEntry entry, GetMemberOptions opts, string prefix, ModuleAnalysis analysis)
        {
            var all = analysis.GetAllAvailableMembers(SourceLocation.None, opts);

            return(all
                   .Where(m => {
                if (m.Values.Any(v => v.DeclaringModule == entry || v.Locations.Any(l => l.DocumentUri == entry.DocumentUri)))
                {
                    if (string.IsNullOrEmpty(prefix) || m.Name.StartsWithOrdinal(prefix, ignoreCase: true))
                    {
                        return true;
                    }
                }
                return false;
            })
                   .Concat(GetChildScopesVariables(analysis, analysis.Scope, opts)));
        }
Exemplo n.º 20
0
        private bool HandleOldStyleCompletionExtension(ModuleAnalysis analysis, PythonAst tree, SourceLocation location, CompletionList completions)
        {
            if (_oldServer == null)
            {
                return(false);
            }
            // Backward compatibility case
            var cl = new PythonTools.Analysis.LanguageServer.CompletionList {
                items = completions.items.Select(x => new PythonTools.Analysis.LanguageServer.CompletionItem {
                    // Partial copy
                    label      = x.label,
                    kind       = (PythonTools.Analysis.LanguageServer.CompletionItemKind)x.kind,
                    detail     = x.detail,
                    sortText   = x.sortText,
                    filterText = x.filterText,
                    preselect  = x.preselect,
                    insertText = x.insertText,
                }).ToArray()
            };

            var oldItems = new HashSet <string>();

            foreach (var x in completions.items)
            {
                oldItems.Add(x.label);
            }

            _oldServer.ProcessCompletionList(analysis as ModuleAnalysis, tree, location, cl);

            var newItems = cl.items.Where(x => !oldItems.Contains(x.label)).ToArray();

            if (newItems.Length == 0)
            {
                return(false);
            }

            var converted = newItems.Select(x => new CompletionItem {
                label      = x.label,
                kind       = (CompletionItemKind)x.kind,
                detail     = x.detail,
                sortText   = x.sortText,
                filterText = x.filterText,
                preselect  = x.preselect,
                insertText = x.insertText,
                textEdit   = x.textEdit.HasValue
                    ? new TextEdit {
                    range = new Range {
                        start = new Position {
                            line      = x.textEdit.Value.range.start.line,
                            character = x.textEdit.Value.range.start.character,
                        },
                        end = new Position {
                            line      = x.textEdit.Value.range.end.line,
                            character = x.textEdit.Value.range.end.character,
                        }
                    },
                    newText = x.textEdit.Value.newText
                } : (TextEdit?)null,
                command = x.command.HasValue
                    ? new Command {
                    title     = x.command.Value.title,
                    command   = x.command.Value.command,
                    arguments = x.command.Value.arguments
                } : (Command?)null,
                data = x.data
            });

            completions.items = completions.items.Concat(converted).ToArray();
            return(true);
        }
Exemplo n.º 21
0
 public override IEnumerable <AnalysisVariable> GetVariablesForDef(string name, VariableDef def)
 {
     return(ModuleAnalysis.ReferencablesToVariables(this.Class.GetDefinitions(name)));
 }