コード例 #1
0
 public InvalidEncodingSquiggleProvider(IServiceProvider serviceProvider, TaskProvider taskProvider) :
     base(serviceProvider,
          taskProvider,
          o => o.InvalidEncodingWarning,
          new[] { PythonTextBufferInfoEvents.TextContentChangedLowPriority, PythonTextBufferInfoEvents.NewAnalysis, PythonTextBufferInfoEvents.DocumentEncodingChanged })
 {
 }
コード例 #2
0
        private TaskProvider GetTaskListProviderForProject(IProjectEntry projEntry)
        {
            TaskProvider provider    = null;
            object       providerObj = null;

            if (_errorList != null && projEntry.FilePath != null)
            {
                if (!projEntry.Properties.TryGetValue(typeof(TaskProvider), out providerObj))
                {
                    uint cookie;
                    projEntry.Properties[typeof(TaskProvider)] = provider = new TaskProvider(projEntry.FilePath);

                    ErrorHandler.ThrowOnFailure(((IVsTaskList)_errorList).RegisterTaskProvider(provider, out cookie));
                    provider.Cookie = cookie;

                    // unregister ourselves when the project entry is collected (and therefore our unregister becomes eligble for finalization)
                    projEntry.Properties[typeof(ErrorUnRegister)] = new ErrorUnRegister(provider, _errorList, cookie, _project, projEntry.FilePath);
                }
                else
                {
                    provider = providerObj as TaskProvider;
                }

                provider.Clear();
            }
            return(provider);
        }
コード例 #3
0
 public ErrorUnRegister(TaskProvider provider, IVsErrorList errorList, uint cookie, PythonProjectNode project, string filename)
 {
     _errorList = errorList;
     _cookie    = cookie;
     _project   = project;
     _filename  = filename;
     _provider  = provider;
 }
コード例 #4
0
        protected override async Task OnNewAnalysis(PythonTextBufferInfo bi, AnalysisEntry entry)
        {
            if (!Enabled && !_alwaysCreateSquiggle || bi?.Document == null || bi.Buffer?.Properties == null)
            {
                TaskProvider.Clear(bi.Filename, VsProjectAnalyzer.InvalidEncodingMoniker);
                return;
            }

            var snapshot = bi.CurrentSnapshot;

            var message = CheckEncoding(snapshot, bi.Document.Encoding, out var magicEncodingName, out var magicEncodingIndex);

            if (message != null)
            {
                if (!bi.Buffer.Properties.TryGetProperty <string>(VsProjectAnalyzer.InvalidEncodingMoniker, out var prevMessage) ||
                    prevMessage != message)
                {
                    bi.Buffer.Properties[VsProjectAnalyzer.InvalidEncodingMoniker] = message;
                    SourceSpan span;
                    if (string.IsNullOrEmpty(magicEncodingName))
                    {
                        var pt = new SnapshotPoint(snapshot, magicEncodingIndex).ToSourceLocation();
                        span = new SourceSpan(pt, new SourceLocation(pt.Line, int.MaxValue));
                    }
                    else
                    {
                        span = new SnapshotSpan(snapshot, magicEncodingIndex, magicEncodingName.Length).ToSourceSpan();
                    }

                    TaskProvider.ReplaceItems(
                        bi.Filename,
                        VsProjectAnalyzer.InvalidEncodingMoniker,
                        new List <TaskProviderItem> {
                        new TaskProviderItem(
                            Services.Site,
                            VsProjectAnalyzer.InvalidEncodingMoniker,
                            message,
                            span,
                            VSTASKPRIORITY.TP_NORMAL,
                            VSTASKCATEGORY.CAT_CODESENSE,
                            true,
                            bi.LocationTracker,
                            snapshot.Version.VersionNumber
                            )
                    });
                }
            }
            else
            {
                TaskProvider.Clear(bi.Filename, VsProjectAnalyzer.InvalidEncodingMoniker);
                bi.Buffer.Properties.RemoveProperty(VsProjectAnalyzer.InvalidEncodingMoniker);
            }
        }
コード例 #5
0
        public UnresolvedImportSquiggleProvider(IServiceProvider serviceProvider, TaskProvider taskProvider)
        {
            _serviceProvider = serviceProvider;
            _taskProvider    = taskProvider;
            var options = _serviceProvider.GetPythonToolsService()?.GeneralOptions;

            if (options != null)
            {
                _enabled         = options.UnresolvedImportWarning;
                options.Changed += GeneralOptions_Changed;
            }
        }
コード例 #6
0
        public UnresolvedImportSquiggleProvider(IServiceProvider serviceProvider, TaskProvider taskProvider)
        {
            if (taskProvider == null)
            {
                throw new ArgumentNullException(nameof(taskProvider));
            }
            _services     = serviceProvider.GetComponentModel().GetService <PythonEditorServices>();
            _taskProvider = taskProvider;
            var options = _services.Python?.GeneralOptions;

            if (options != null)
            {
                _enabled         = options.UnresolvedImportWarning;
                options.Changed += GeneralOptions_Changed;
            }
        }
コード例 #7
0
        public UnresolvedImportSquiggleProvider(IServiceProvider serviceProvider, TaskProvider taskProvider)
        {
            if (taskProvider == null)
            {
                throw new ArgumentNullException(nameof(taskProvider));
            }
            _serviceProvider = serviceProvider;
            _taskProvider    = taskProvider;
            var options = _serviceProvider.GetPythonToolsService()?.GeneralOptions;

            if (options != null)
            {
                _enabled         = options.UnresolvedImportWarning;
                options.Changed += GeneralOptions_Changed;
            }
        }
コード例 #8
0
        public void ParseFile(IProjectEntry projectEntry, string filename, TextReader content, Severity indentationSeverity)
        {
            IProjectEntry         analysis;
            IExternalProjectEntry externalEntry;

            if (filename.EndsWith(".py", StringComparison.OrdinalIgnoreCase))
            {
                PythonAst           ast;
                CollectingErrorSink errorSink;
                ParsePythonCode(content, indentationSeverity, out ast, out errorSink);

                if (ast != null)
                {
                    IPythonProjectEntry pyAnalysis;
                    if (_projectFiles.TryGetValue(filename, out analysis) &&
                        (pyAnalysis = analysis as IPythonProjectEntry) != null)
                    {
                        pyAnalysis.UpdateTree(ast, new FileCookie(filename));
                        _analysisQueue.Enqueue(analysis, AnalysisPriority.Normal);
                    }
                }

                TaskProvider provider = GetTaskListProviderForProject(projectEntry);
                if (provider != null)
                {
                    foreach (ErrorResult warning in errorSink.Warnings)
                    {
                        provider.AddWarning(warning);
                    }

                    foreach (ErrorResult error in errorSink.Errors)
                    {
                        provider.AddError(error);
                    }

                    UpdateErrorList(errorSink, projectEntry.FilePath, provider);
                }
            }
            else if (_projectFiles.TryGetValue(filename, out analysis) && (externalEntry = (analysis as IExternalProjectEntry)) != null)
            {
                externalEntry.ParseContent(content, new FileCookie(filename));
                _analysisQueue.Enqueue(analysis, AnalysisPriority.Normal);
            }
        }
コード例 #9
0
        private void UpdateErrorList(CollectingErrorSink errorSink, string filepath, TaskProvider provider)
        {
            if (_project != null && provider != null)
            {
                if (errorSink.Errors.Count > 0)
                {
                    _project.ErrorFiles.Add(filepath);
                }
                else
                {
                    _project.ErrorFiles.Remove(filepath);
                }
            }

            if (provider != null)
            {
                ((IVsTaskList)_errorList).RefreshTasks(provider.Cookie);
            }
        }
コード例 #10
0
        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);
            }
        }
コード例 #11
0
        public BufferAnalysisSquiggleProviderBase(
            IServiceProvider serviceProvider,
            TaskProvider taskProvider,
            Func <GeneralOptions, bool> getSetting,
            PythonTextBufferInfoEvents[] triggerEvents)
        {
            Services     = serviceProvider.GetComponentModel().GetService <PythonEditorServices>();
            TaskProvider = taskProvider ?? throw new ArgumentNullException(nameof(taskProvider));

            _getSetting    = getSetting;
            _triggerEvents = triggerEvents;

            var options = Services.Python?.GeneralOptions;

            if (options != null)
            {
                Enabled          = _getSetting(options);
                options.Changed += GeneralOptions_Changed;
            }
        }
コード例 #12
0
 public UnresolvedImportSquiggleProvider(IServiceProvider serviceProvider, TaskProvider taskProvider)
 {
     _serviceProvider = serviceProvider;
     _taskProvider    = taskProvider;
 }
コード例 #13
0
 public UnresolvedImportSquiggleProvider(IServiceProvider serviceProvider, TaskProvider taskProvider) :
     base(serviceProvider, taskProvider, o => o.UnresolvedImportWarning, new[] { PythonTextBufferInfoEvents.NewAnalysis })
 {
 }
コード例 #14
0
 private static void AddWarnings(ITextSnapshot snapshot, CollectingErrorSink errorSink, SimpleTagger <ErrorTag> squiggles, TaskProvider provider)
 {
     foreach (ErrorResult warning in errorSink.Warnings)
     {
         var span  = warning.Span;
         var tspan = CreateSpan(snapshot, span);
         squiggles.CreateTagSpan(tspan, new ErrorTag(PredefinedErrorTypeNames.Warning, warning.Message));
         if (provider != null)
         {
             provider.AddWarning(warning);
         }
     }
 }
コード例 #15
0
 private static void AddErrors(ITextSnapshot snapshot, CollectingErrorSink errorSink, SimpleTagger <ErrorTag> squiggles, TaskProvider provider)
 {
     foreach (ErrorResult error in errorSink.Errors)
     {
         var span  = error.Span;
         var tspan = CreateSpan(snapshot, span);
         squiggles.CreateTagSpan(tspan, new ErrorTag(PredefinedErrorTypeNames.SyntaxError, error.Message));
         if (provider != null)
         {
             provider.AddError(error);
         }
     }
 }
コード例 #16
0
        public void ParseBuffers(BufferParser bufferParser, Severity indentationSeverity, params ITextSnapshot[] snapshots)
        {
            IProjectEntry analysis;

            lock (_openFiles) {
                if (!_openFiles.TryGetValue(bufferParser, out analysis))
                {
                    return;
                }
            }

            IPythonProjectEntry pyProjEntry = analysis as IPythonProjectEntry;
            List <PythonAst>    asts        = new List <PythonAst>();
            bool hasErrors = false;

            foreach (var snapshot in snapshots)
            {
                var snapshotContent = new SnapshotSpanSourceCodeReader(new SnapshotSpan(snapshot, new Span(0, snapshot.Length)));

                if (pyProjEntry != null && snapshot.TextBuffer.ContentType.IsOfType(PythonCoreConstants.ContentType))
                {
                    if (!snapshot.IsReplBufferWithCommand())
                    {
                        PythonAst           ast;
                        CollectingErrorSink errorSink;

                        ParsePythonCode(snapshotContent, indentationSeverity, out ast, out errorSink);
                        if (ast != null)
                        {
                            asts.Add(ast);

                            if (errorSink.Errors.Count != 0)
                            {
                                hasErrors = true;
                            }

                            // update squiggles for the buffer
                            var buffer = snapshot.TextBuffer;
                            SimpleTagger <ErrorTag> squiggles = _errorProvider.GetErrorTagger(snapshot.TextBuffer);
                            squiggles.RemoveTagSpans(x => true);

                            TaskProvider provider = GetTaskListProviderForProject(bufferParser._currentProjEntry);

                            AddWarnings(snapshot, errorSink, squiggles, provider);

                            AddErrors(snapshot, errorSink, squiggles, provider);

                            UpdateErrorList(errorSink, buffer.GetFilePath(), provider);
                        }
                    }
                }
                else
                {
                    // other file such as XAML
                    IExternalProjectEntry externalEntry;
                    if ((externalEntry = (analysis as IExternalProjectEntry)) != null)
                    {
                        externalEntry.ParseContent(snapshotContent, new SnapshotCookie(snapshotContent.Snapshot));
                        _analysisQueue.Enqueue(analysis, AnalysisPriority.High);
                    }
                }
            }

            if ((!hasErrors && asts.Count > 0) || asts.Count > 1)
            {
                // only update the AST when we're error free, this way we don't remove
                // a useful analysis with an incomplete and useless analysis.
                // If we have more than one AST we're in the REPL - we'll update the
                // AST in that case as errors won't go away.

                PythonAst finalAst;
                if (asts.Count == 1)
                {
                    finalAst = asts[0];
                }
                else
                {
                    // multiple ASTs, merge them together
                    List <Statement> bodies = new List <Statement>();
                    foreach (var ast in asts)
                    {
                        bodies.Add(ast.Body);
                    }
                    finalAst = new PythonAst(new SuiteStatement(bodies.ToArray()), new int[0]);
                }

                pyProjEntry.UpdateTree(finalAst, new SnapshotCookie(snapshots[0])); // SnapshotCookie is ot entirely right, we should merge the snapshots
                _analysisQueue.Enqueue(analysis, AnalysisPriority.High);
            }
        }