Exemplo n.º 1
0
        private async void OnNewAnalysis(object sender, EventArgs e)
        {
            var bi = sender as PythonTextBufferInfo;

            if (bi == null)
            {
                Debug.Fail("Unexpected Sender type " + sender?.GetType() ?? "(null)");
                return;
            }
            if (!_enabled && !_alwaysCreateSquiggle)
            {
                _taskProvider.Clear(bi.AnalysisEntry, VsProjectAnalyzer.UnresolvedImportMoniker);
                return;
            }

            var entry = bi.AnalysisEntry;

            if (entry == null)
            {
                return;
            }

            var missingImports = await entry.Analyzer.GetMissingImportsAsync(entry, bi.Buffer);

            if (missingImports == null)
            {
                return;
            }

            if (missingImports.Data.unresolved.Any())
            {
                var translator = missingImports.GetTracker(missingImports.Data.version);
                if (translator != null)
                {
                    var f = new TaskProviderItemFactory(translator);

                    _taskProvider.ReplaceItems(
                        entry,
                        VsProjectAnalyzer.UnresolvedImportMoniker,
                        missingImports.Data.unresolved.Select(t => f.FromUnresolvedImport(
                                                                  _services.Site,
                                                                  entry.Analyzer.InterpreterFactory as IPythonInterpreterFactoryWithDatabase,
                                                                  t.name,
                                                                  new SourceSpan(
                                                                      new SourceLocation(t.startIndex, t.startLine, t.startColumn),
                                                                      new SourceLocation(t.endIndex, t.endLine, t.endColumn)
                                                                      )
                                                                  )).ToList()
                        );
                }
            }
            else
            {
                _taskProvider.Clear(entry, VsProjectAnalyzer.UnresolvedImportMoniker);
            }
        }
        private async void OnNewAnalysis(AnalysisEntry entry, ITextBuffer buffer)
        {
            if (!_alwaysCreateSquiggle)
            {
                var service = _serviceProvider.GetPythonToolsService();
                if (service == null || !service.GeneralOptions.UnresolvedImportWarning)
                {
                    return;
                }
            }

            var missingImports = await entry.Analyzer.GetMissingImportsAsync(entry, buffer);

            if (missingImports != null)
            {
                var missing = missingImports.Data;

                if (missing.unresolved.Any())
                {
                    var translator = missingImports.GetTracker(missingImports.Data.version);
                    if (translator != null)
                    {
                        var f = new TaskProviderItemFactory(translator);

                        _taskProvider.ReplaceItems(
                            entry,
                            VsProjectAnalyzer.UnresolvedImportMoniker,
                            missingImports.Data.unresolved.Select(t => f.FromUnresolvedImport(
                                                                      _serviceProvider,
                                                                      entry.Analyzer.InterpreterFactory as IPythonInterpreterFactoryWithDatabase,
                                                                      t.name,
                                                                      new SourceSpan(
                                                                          new SourceLocation(t.startIndex, t.startLine, t.startColumn),
                                                                          new SourceLocation(t.endIndex, t.endLine, t.endColumn)
                                                                          )
                                                                      )).ToList()
                            );
                    }
                }
                else
                {
                    _taskProvider.Clear(entry, VsProjectAnalyzer.UnresolvedImportMoniker);
                }
            }
        }
        protected override async Task OnNewAnalysis(PythonTextBufferInfo bi, AnalysisEntry entry)
        {
            if (!Enabled && !_alwaysCreateSquiggle || entry == null)
            {
                TaskProvider.Clear(bi.Filename, VsProjectAnalyzer.UnresolvedImportMoniker);
                return;
            }

            var missingImports = await entry.Analyzer.GetMissingImportsAsync(entry, bi.Buffer);

            if (missingImports == null)
            {
                return;
            }

            if (missingImports.Data.unresolved.Any())
            {
                var translator = missingImports.GetTracker(missingImports.Data.version);
                if (translator != null)
                {
                    var f = new TaskProviderItemFactory(translator);

                    TaskProvider.ReplaceItems(
                        bi.Filename,
                        VsProjectAnalyzer.UnresolvedImportMoniker,
                        missingImports.Data.unresolved.Select(t => f.FromUnresolvedImport(
                                                                  Services.Site,
                                                                  entry.Analyzer.InterpreterFactory as IPythonInterpreterFactoryWithDatabase,
                                                                  t.name,
                                                                  new SourceSpan(
                                                                      new SourceLocation(t.startLine, t.startColumn),
                                                                      new SourceLocation(t.endLine, t.endColumn)
                                                                      )
                                                                  )).ToList()
                        );
                }
            }
            else
            {
                TaskProvider.Clear(bi.Filename, VsProjectAnalyzer.UnresolvedImportMoniker);
            }
        }
Exemplo n.º 4
0
        private void OnNewAnalysis(object sender, EventArgs e)
        {
            if (!_alwaysCreateSquiggle &&
                !_serviceProvider.GetPythonToolsService().GeneralOptions.UnresolvedImportWarning
                )
            {
                return;
            }

            var entry = sender as IPythonProjectEntry;

            if (entry == null ||
                string.IsNullOrEmpty(entry.ModuleName) ||
                string.IsNullOrEmpty(entry.FilePath)
                )
            {
                return;
            }

            var analysis = entry.Analysis;
            var analyzer = analysis != null ? analysis.ProjectState : null;

            if (analyzer == null)
            {
                return;
            }

            PythonAst       ast;
            IAnalysisCookie cookie;

            entry.GetTreeAndCookie(out ast, out cookie);
            var snapshotCookie = cookie as SnapshotCookie;
            var snapshot       = snapshotCookie != null ? snapshotCookie.Snapshot : null;

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

            var walker = new ImportStatementWalker(entry, analyzer);

            ast.Walk(walker);

            if (walker.Imports.Any())
            {
                var f = new TaskProviderItemFactory(snapshot);

                _taskProvider.ReplaceItems(
                    entry,
                    VsProjectAnalyzer.UnresolvedImportMoniker,
                    walker.Imports.Select(t => f.FromUnresolvedImport(
                                              _serviceProvider,
                                              analyzer.InterpreterFactory as IPythonInterpreterFactoryWithDatabase,
                                              t.Item1,
                                              t.Item2.GetSpan(ast)
                                              )).ToList()
                    );
            }
            else
            {
                _taskProvider.Clear(entry, VsProjectAnalyzer.UnresolvedImportMoniker);
            }
        }