Ejemplo n.º 1
0
        private void OnNewAnalysis(object sender, EventArgs e)
        {
            if (_provider._serviceProvider.GetPythonToolsService().AdvancedOptions.ColorNames == false)
            {
                lock (_spanCacheLock) {
                    if (_spanCache != null)
                    {
                        _spanCache = null;
                        OnNewClassifications(_buffer.CurrentSnapshot);
                    }
                }
                return;
            }

            PythonAst       tree;
            IAnalysisCookie cookie;

            _entry.GetTreeAndCookie(out tree, out cookie);
            var sCookie  = cookie as SnapshotCookie;
            var snapshot = sCookie != null ? sCookie.Snapshot : null;

            if (tree == null || snapshot == null)
            {
                return;
            }


            var moduleAnalysis = (_provider._serviceProvider.GetPythonToolsService().AdvancedOptions.ColorNamesWithAnalysis)
                ? _entry.Analysis
                : null;

            var walker = new ClassifierWalker(tree, moduleAnalysis, snapshot, Provider.CategoryMap);

            tree.Walk(walker);
            var newCache = walker.Spans;

            lock (_spanCacheLock) {
                if (snapshot == snapshot.TextBuffer.CurrentSnapshot)
                {
                    // Ensure we have not raced with another update
                    _spanCache = newCache;
                }
                else
                {
                    snapshot = null;
                }
            }
            if (snapshot != null)
            {
                OnNewClassifications(snapshot);
            }
        }
Ejemplo n.º 2
0
        private static void UpdateAnalysisTree(IPythonProjectEntry pyEntry, SortedDictionary<int, ParseResult> parseResults) {
            IAnalysisCookie cookie = new VersionCookie(
                parseResults.ToDictionary(
                    x => x.Key,
                    x => new BufferVersion(x.Value.Version, x.Value.Ast)
                )
            );

            var asts = parseResults.Where(x => x.Value.Ast != null).Select(x => x.Value.Ast).ToArray();
            PythonAst finalAst;
            if (asts.Length == 1) {
                finalAst = asts[0];
            } else if (asts.Length > 0) {
                // multiple ASTs, merge them together
                finalAst = new PythonAst(
                    new SuiteStatement(
                        asts.Select(ast => ast.Body).ToArray()
                    ),
                    new NewLineLocation[0],
                    asts[0].LanguageVersion
                );
            } else {
                // we failed to get any sort of AST out, so we can't analyze...
                // But we need to balance the UpdateTree call, so just fetch the
                // last valid ast and cookie.
                pyEntry.GetTreeAndCookie(out finalAst, out cookie);
            }

            pyEntry.UpdateTree(finalAst, cookie);
        }