public InvalidEncodingSquiggleProvider(IServiceProvider serviceProvider, TaskProvider taskProvider) : base(serviceProvider, taskProvider, o => o.InvalidEncodingWarning, new[] { PythonTextBufferInfoEvents.TextContentChangedLowPriority, PythonTextBufferInfoEvents.NewAnalysis, PythonTextBufferInfoEvents.DocumentEncodingChanged }) { }
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); }
public ErrorUnRegister(TaskProvider provider, IVsErrorList errorList, uint cookie, PythonProjectNode project, string filename) { _errorList = errorList; _cookie = cookie; _project = project; _filename = filename; _provider = provider; }
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); } }
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; } }
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; } }
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; } }
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); } }
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); } }
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); } }
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; } }
public UnresolvedImportSquiggleProvider(IServiceProvider serviceProvider, TaskProvider taskProvider) { _serviceProvider = serviceProvider; _taskProvider = taskProvider; }
public UnresolvedImportSquiggleProvider(IServiceProvider serviceProvider, TaskProvider taskProvider) : base(serviceProvider, taskProvider, o => o.UnresolvedImportWarning, new[] { PythonTextBufferInfoEvents.NewAnalysis }) { }
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); } } }
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); } } }
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); } }