Beispiel #1
0
 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;
 }
Beispiel #2
0
        private void Parse(bool enqueOnly)
        {
            if (_tree == null)
            {
                return;
            }

            var oldParent = _myScope.ParentPackage;

            if (_filePath != null)
            {
                ProjectState.ModulesByFilename[_filePath] = _myScope;
            }

            if (oldParent != null)
            {
                // update us in our parent package
                _myScope.ParentPackage = oldParent;
                oldParent.Scope.SetVariable(_tree, _unit, _moduleName.Substring(_moduleName.IndexOf('.') + 1), _myScope.SelfSet, false);
            }

            var unit = _unit = new AnalysisUnit(_tree, new InterpreterScope[] { _myScope.Scope });

            _scopeTree = new Stack <InterpreterScope>();
            _scopeTree.Push(MyScope.Scope);

            // create new analysis object and add to the queue to be analyzed
            var newAnalysis = new ModuleAnalysis(_unit, _scopeTree);

            _unit.Enqueue();

            // collect top-level definitions first
            var walker = new OverviewWalker(this, unit);

            _tree.Walk(walker);

            PublishPackageChildrenInPackage();

            if (!enqueOnly)
            {
                ((IGroupableAnalysisProject)_projectState).AnalyzeQueuedEntries();
            }

            // publish the analysis now that it's complete
            _currentAnalysis = newAnalysis;

            foreach (var variableInfo in _myScope.Scope.Variables)
            {
                variableInfo.Value.ClearOldValues(this);
            }
        }
 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));
 }
        /// <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;
            }
        }
Beispiel #5
0
        internal static SourceLocation TranslateIndex(int index, ITextSnapshot fromSnapshot, ModuleAnalysis toAnalysisSnapshot) {
            SnapshotCookie snapshotCookie;
            // TODO: buffers differ in the REPL window case, in the future we should handle this better
            if (toAnalysisSnapshot != null &&
                fromSnapshot != null &&
                (snapshotCookie = toAnalysisSnapshot.AnalysisCookie as SnapshotCookie) != null &&
                snapshotCookie.Snapshot != null &&
                snapshotCookie.Snapshot.TextBuffer == fromSnapshot.TextBuffer) {

                var fromPoint = new SnapshotPoint(fromSnapshot, index);
                var fromLine = fromPoint.GetContainingLine();
                var toPoint = fromPoint.TranslateTo(snapshotCookie.Snapshot, PointTrackingMode.Negative);
                var toLine = toPoint.GetContainingLine();

                Debug.Assert(fromLine != null, "Unable to get 'from' line from " + fromPoint.ToString());
                Debug.Assert(toLine != null, "Unable to get 'to' line from " + toPoint.ToString());

                return new SourceLocation(
                    toPoint.Position,
                    (toLine != null ? toLine.LineNumber : fromLine != null ? fromLine.LineNumber : 0) + 1,
                    index - (fromLine != null ? fromLine.Start.Position : 0) + 1
                );
            } else if (fromSnapshot != null) {
                var fromPoint = new SnapshotPoint(fromSnapshot, index);
                var fromLine = fromPoint.GetContainingLine();

                return new SourceLocation(
                    index,
                    fromLine.LineNumber + 1,
                    index - fromLine.Start.Position + 1
                );
            } else {
                return new SourceLocation(index, 1, 1);
            }
        }
Beispiel #6
0
        private void Parse(bool enqueueOnly, CancellationToken cancel)
        {
            PythonAst tree;
            IAnalysisCookie cookie;
            GetTreeAndCookie(out tree, out cookie);
            if (tree == null) {
                return;
            }

            var oldParent = _myScope.ParentPackage;
            if (_filePath != null) {
                ProjectState.ModulesByFilename[_filePath] = _myScope;
            }

            if (oldParent != null) {
                // update us in our parent package
                oldParent.AddChildPackage(_myScope, _unit);
            } else if (_filePath != null) {
                // we need to check and see if our parent is a package for the case where we're adding a new
                // file but not re-analyzing our parent package.
                string parentFilename;
                if (ModulePath.IsInitPyFile(_filePath)) {
                    // subpackage
                    parentFilename = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(_filePath)), "__init__.py");
                } else {
                    // just a module
                    parentFilename = Path.Combine(Path.GetDirectoryName(_filePath), "__init__.py");
                }

                ModuleInfo parentPackage;
                if (ProjectState.ModulesByFilename.TryGetValue(parentFilename, out parentPackage)) {
                    parentPackage.AddChildPackage(_myScope, _unit);
                }
            }

            _unit = new AnalysisUnit(tree, _myScope.Scope);
            AnalysisLog.NewUnit(_unit);

            foreach (var value in MyScope.Scope.AllVariables) {
                value.Value.EnqueueDependents();
            }

            MyScope.Scope.Children.Clear();
            MyScope.Scope.ClearNodeScopes();
            MyScope.Scope.ClearNodeValues();
            MyScope.ClearUnresolvedModules();

            // collect top-level definitions first
            var walker = new OverviewWalker(this, _unit, tree);
            tree.Walk(walker);

            _myScope.Specialize();

            // It may be that we have analyzed some child packages of this package already, but because it wasn't analyzed,
            // the children were not registered. To handle this possibility, scan analyzed packages for children of this
            // package (checked by module name first, then sanity-checked by path), and register any that match.
            if (ModulePath.IsInitPyFile(_filePath)) {
                string pathPrefix = Path.GetDirectoryName(_filePath) + "\\";
                var children =
                    from pair in _projectState.ModulesByFilename
                    // Is the candidate child package in a subdirectory of our package?
                    let fileName = pair.Key
                    where fileName.StartsWith(pathPrefix)
                    let moduleName = pair.Value.Name
                    // Is the full name of the candidate child package qualified with the name of our package?
                    let lastDot = moduleName.LastIndexOf('.')
                    where lastDot > 0
                    let parentModuleName = moduleName.Substring(0, lastDot)
                    where parentModuleName == _myScope.Name
                    select pair.Value;
                foreach (var child in children) {
                    _myScope.AddChildPackage(child, _unit);
                }
            }

            _unit.Enqueue();

            if (!enqueueOnly) {
                _projectState.AnalyzeQueuedEntries(cancel);
            }

            // publish the analysis now that it's complete/running
            _currentAnalysis = new ModuleAnalysis(
                _unit,
                ((ModuleScope)_unit.Scope).CloneForPublish()
            );
        }
Beispiel #7
0
        private void Parse(bool enqueueOnly, CancellationToken cancel)
        {
            PythonAst       tree;
            IAnalysisCookie cookie;

            GetTreeAndCookie(out tree, out cookie);
            if (tree == null)
            {
                return;
            }

            var oldParent = _myScope.ParentPackage;

            if (_filePath != null)
            {
                ProjectState.ModulesByFilename[_filePath] = _myScope;
            }

            if (oldParent != null)
            {
                // update us in our parent package
                oldParent.AddChildPackage(_myScope, _unit);
            }
            else if (_filePath != null)
            {
                // we need to check and see if our parent is a package for the case where we're adding a new
                // file but not re-analyzing our parent package.
                string parentFilename;
                if (ModulePath.IsInitPyFile(_filePath))
                {
                    // subpackage
                    parentFilename = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(_filePath)), "__init__.py");
                }
                else
                {
                    // just a module
                    parentFilename = Path.Combine(Path.GetDirectoryName(_filePath), "__init__.py");
                }

                ModuleInfo parentPackage;
                if (ProjectState.ModulesByFilename.TryGetValue(parentFilename, out parentPackage))
                {
                    parentPackage.AddChildPackage(_myScope, _unit);
                }
            }

            _unit = new AnalysisUnit(tree, _myScope.Scope);
            AnalysisLog.NewUnit(_unit);

            foreach (var value in MyScope.Scope.AllVariables)
            {
                value.Value.EnqueueDependents();
            }

            MyScope.Scope.Children.Clear();
            MyScope.Scope.ClearNodeScopes();
            MyScope.Scope.ClearNodeValues();
            MyScope.ClearUnresolvedModules();

            // collect top-level definitions first
            var walker = new OverviewWalker(this, _unit, tree);

            tree.Walk(walker);

            _myScope.Specialize();

            // It may be that we have analyzed some child packages of this package already, but because it wasn't analyzed,
            // the children were not registered. To handle this possibility, scan analyzed packages for children of this
            // package (checked by module name first, then sanity-checked by path), and register any that match.
            if (ModulePath.IsInitPyFile(_filePath))
            {
                string pathPrefix = Path.GetDirectoryName(_filePath) + "\\";
                var    children   =
                    from pair in _projectState.ModulesByFilename
                    // Is the candidate child package in a subdirectory of our package?
                    let fileName = pair.Key
                                   where fileName.StartsWith(pathPrefix)
                                   let moduleName = pair.Value.Name
                                                    // Is the full name of the candidate child package qualified with the name of our package?
                                                    let lastDot = moduleName.LastIndexOf('.')
                                                                  where lastDot > 0
                                                                  let parentModuleName = moduleName.Substring(0, lastDot)
                                                                                         where parentModuleName == _myScope.Name
                                                                                         select pair.Value;
                foreach (var child in children)
                {
                    _myScope.AddChildPackage(child, _unit);
                }
            }

            _unit.Enqueue();

            if (!enqueueOnly)
            {
                _projectState.AnalyzeQueuedEntries(cancel);
            }

            // publish the analysis now that it's complete/running
            _currentAnalysis = new ModuleAnalysis(
                _unit,
                ((ModuleScope)_unit.Scope).CloneForPublish()
                );
        }
Beispiel #8
0
        private void Parse(bool enqueueOnly, CancellationToken cancel)
        {
#if DEBUG
            Debug.Assert(Monitor.IsEntered(this));
#endif
            var parse  = GetCurrentParse();
            var tree   = parse?.Tree;
            var cookie = parse?.Cookie;
            if (tree == null)
            {
                return;
            }

            var oldParent = MyScope.ParentPackage;
            if (FilePath != null)
            {
                ProjectState.ModulesByFilename[FilePath] = MyScope;
            }

            if (oldParent != null)
            {
                // update us in our parent package
                oldParent.AddChildPackage(MyScope, _unit);
            }
            else if (FilePath != null)
            {
                // we need to check and see if our parent is a package for the case where we're adding a new
                // file but not re-analyzing our parent package.
                string parentFilename;
                if (ModulePath.IsInitPyFile(FilePath))
                {
                    // subpackage
                    parentFilename = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(FilePath)), "__init__.py");
                }
                else
                {
                    // just a module
                    parentFilename = Path.Combine(Path.GetDirectoryName(FilePath), "__init__.py");
                }

                if (ProjectState.ModulesByFilename.TryGetValue(parentFilename, out var parentPackage))
                {
                    parentPackage.AddChildPackage(MyScope, _unit);
                }
            }

            _unit = new AnalysisUnit(tree, MyScope.Scope);
            AnalysisLog.NewUnit(_unit);

            MyScope.Scope.Children = new List <InterpreterScope>();
            MyScope.Scope.ClearNodeScopes();
            MyScope.Scope.ClearNodeValues();
            MyScope.Scope.ClearLinkedVariables();
            MyScope.Scope.ClearVariables();
            MyScope.ClearReferencedModules();
            MyScope.ClearUnresolvedModules();
            _unit.State.ClearDiagnostics(this);

            MyScope.EnsureModuleVariables(_unit.State);

            foreach (var value in MyScope.Scope.AllVariables)
            {
                value.Value.EnqueueDependents();
            }

            // collect top-level definitions first
            var walker = new OverviewWalker(this, _unit, tree);
            tree.Walk(walker);

            MyScope.Specialize();

            // It may be that we have analyzed some child packages of this package already, but because it wasn't analyzed,
            // the children were not registered. To handle this possibility, scan analyzed packages for children of this
            // package (checked by module name first, then sanity-checked by path), and register any that match.
            if (ModulePath.IsInitPyFile(FilePath))
            {
                string pathPrefix = PathUtils.EnsureEndSeparator(Path.GetDirectoryName(FilePath));
                var    children   =
                    from pair in ProjectState.ModulesByFilename
                    // Is the candidate child package in a subdirectory of our package?
                    let fileName = pair.Key
                                   where fileName.StartsWithOrdinal(pathPrefix, ignoreCase: true)
                                   let moduleName = pair.Value.Name
                                                    // Is the full name of the candidate child package qualified with the name of our package?
                                                    let lastDot = moduleName.LastIndexOf('.')
                                                                  where lastDot > 0
                                                                  let parentModuleName = moduleName.Substring(0, lastDot)
                                                                                         where parentModuleName == MyScope.Name
                                                                                         select pair.Value;
                foreach (var child in children)
                {
                    MyScope.AddChildPackage(child, _unit);
                }
            }

            _unit.Enqueue();

            if (!enqueueOnly)
            {
                ProjectState.AnalyzeQueuedEntries(cancel);
            }

            // publish the analysis now that it's complete/running
            Analysis = new ModuleAnalysis(
                _unit,
                ((ModuleScope)_unit.Scope).CloneForPublish(),
                DocumentUri,
                AnalysisVersion
                );
        }
 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>>();
 }
Beispiel #10
0
 public ClassifierWalker(PythonAst ast, ModuleAnalysis analysis) {
     _ast = ast;
     _analysis = analysis;
     Spans = new List<AP.AnalysisClassification>();
 }