Exemplo n.º 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
                                    );
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
 public override void Invoke()
 {
     MissingImportAnalysis.AddImport(
         _serviceProvider,
         _buffer,
         _view,
         FromName,
         Name
         );
 }
        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);
            }
        }
Exemplo n.º 4
0
        public void Invoke(CancellationToken cancellationToken)
        {
            Debug.Assert(!string.IsNullOrEmpty(_name));

            MissingImportAnalysis.AddImport(
                _source._provider,
                _buffer,
                _source._view,
                _fromModule,
                _name
                );
        }
Exemplo n.º 5
0
 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);
 }
Exemplo n.º 6
0
 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);
 }
Exemplo n.º 7
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();

                    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;
            }
        }