コード例 #1
0
        private void AddMissingImports(List <string> importList)
        {
            if (importList.Count > 0)
            {
                var projEntry = _textView.TextBuffer.GetPythonProjectEntry();
                if (projEntry != null)
                {
                    PythonAst       ast;
                    IAnalysisCookie cookie;
                    projEntry.GetTreeAndCookie(out ast, out cookie);
                    if (ast != null)
                    {
                        var walker = new ImportWalker();
                        ast.Walk(walker);

                        foreach (var import in importList)
                        {
                            if (!walker.Imports.Contains(import))
                            {
                                MissingImportAnalysis.AddImport(
                                    _serviceProvider,
                                    _textView.TextBuffer,
                                    _textView,
                                    null,
                                    import
                                    );
                            }
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: ImportSmartTagAction.cs プロジェクト: xNUTs/PTVS
 public override void Invoke()
 {
     MissingImportAnalysis.AddImport(
         _serviceProvider,
         _buffer,
         _view,
         FromName,
         Name
         );
 }
コード例 #3
0
        private static ReadOnlyCollection <ISmartTagAction> GetImportTags(
            IServiceProvider serviceProvider,
            ITextBuffer textBuffer,
            ITextView textView,
            MissingImportAnalysis imports,
            CancellationToken cancel
            )
        {
            var actions = new List <ISmartTagAction>();

            try {
                foreach (var import in imports.AvailableImports)
                {
                    cancel.ThrowIfCancellationRequested();

                    if (import.IsDefinedInModule)
                    {
                        int lastDot;

                        if ((lastDot = import.Name.LastIndexOf('.')) == -1)
                        {
                            // simple import
                            actions.Add(new ImportSmartTagAction(import.Name, textBuffer, textView, serviceProvider));
                        }
                        else
                        {
                            // importing a package or member of a module
                            actions.Add(new ImportSmartTagAction(
                                            import.Name.Substring(0, lastDot),
                                            import.Name.Substring(lastDot + 1),
                                            textBuffer,
                                            textView,
                                            serviceProvider
                                            ));
                        }
                    }
                }

                if (actions.Any())
                {
                    actions.Sort(SmartTagComparer);
                }

                return(new ReadOnlyCollection <ISmartTagAction>(actions));
            } catch (OperationCanceledException) {
                throw;
            } catch (Exception ex) {
                if (ex.IsCriticalException())
                {
                    throw;
                }
                Debug.Fail(ex.ToString());
                return(null);
            }
        }
コード例 #4
0
        public void Invoke(CancellationToken cancellationToken)
        {
            Debug.Assert(!string.IsNullOrEmpty(_name));

            MissingImportAnalysis.AddImport(
                _source._provider,
                _buffer,
                _source._view,
                _fromModule,
                _name
                );
        }
コード例 #5
0
ファイル: SmartTagSource.cs プロジェクト: wenh123/PTVS
 public SmartTagAugmentTask(
     IServiceProvider serviceProvider,
     ITextBuffer textBuffer,
     ITextView textView,
     MissingImportAnalysis imports
 ) {
     ApplicableToSpan = imports.ApplicableToSpan;
     _cancel = new CancellationTokenSource();
     _task = Task.Run(() => GetImportTags(
         serviceProvider,
         textBuffer,
         textView,
         imports,
         _cancel.Token
     ), _cancel.Token);
 }
コード例 #6
0
ファイル: SmartTagSource.cs プロジェクト: xNUTs/PTVS
 public SmartTagAugmentTask(
     IServiceProvider serviceProvider,
     ITextBuffer textBuffer,
     ITextView textView,
     MissingImportAnalysis imports
     )
 {
     ApplicableToSpan = imports.ApplicableToSpan;
     _cancel          = new CancellationTokenSource();
     _task            = Task.Run(() => GetImportTags(
                                     serviceProvider,
                                     textBuffer,
                                     textView,
                                     imports,
                                     _cancel.Token
                                     ), _cancel.Token);
 }
コード例 #7
0
ファイル: SmartTagSource.cs プロジェクト: wenh123/PTVS
        private static ReadOnlyCollection<ISmartTagAction> GetImportTags(
            IServiceProvider serviceProvider,
            ITextBuffer textBuffer,
            ITextView textView,
            MissingImportAnalysis imports,
            CancellationToken cancel
        ) {
            var actions = new List<ISmartTagAction>();

            try {
                foreach (var import in imports.AvailableImports) {
                    cancel.ThrowIfCancellationRequested();

                    int lastDot;

                    if ((lastDot = import.Name.LastIndexOf('.')) == -1) {
                        // simple import
                        actions.Add(new ImportSmartTagAction(import.Name, textBuffer, textView, serviceProvider));
                    } else {
                        // importing a package or member of a module
                        actions.Add(new ImportSmartTagAction(
                            import.Name.Substring(0, lastDot),
                            import.Name.Substring(lastDot + 1),
                            textBuffer,
                            textView,
                            serviceProvider
                        ));
                    }
                }

                if (actions.Any()) {
                    actions.Sort(SmartTagComparer);
                }

                return new ReadOnlyCollection<ISmartTagAction>(actions);
            } catch (OperationCanceledException) {
                throw;
            } catch (Exception ex) {
                if (ex.IsCriticalException()) {
                    throw;
                }
                Debug.Fail(ex.ToString());
                return null;
            }
        }