/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/LanguageServer/Protocol/Handler/Diagnostics/DiagnosticSources/TaskListDiagnosticSource.cs
84 строки
4 KB
David Wengier
Allow Razor to get task list items for a document
01 сен 2025, 10:13
01 сен 2025, 10:13
a8af102
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Collections.Immutable; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Options; using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.TaskList; namespace Microsoft.CodeAnalysis.LanguageServer.Handler.Diagnostics; using static PullDiagnosticConstants; internal sealed class TaskListDiagnosticSource(Document document, IGlobalOptionService globalOptions) : AbstractDocumentDiagnosticSource<Document>(document) { private static readonly ImmutableArray<string> s_todoCommentCustomTags = [TaskItemCustomTag]; private static readonly ImmutableDictionary<string, string?> s_lowPriorityProperties = ImmutableDictionary<string, string?>.Empty.Add(Priority, Low); private static readonly ImmutableDictionary<string, string?> s_mediumPriorityProperties = ImmutableDictionary<string, string?>.Empty.Add(Priority, Medium); private static readonly ImmutableDictionary<string, string?> s_highPriorityProperties = ImmutableDictionary<string, string?>.Empty.Add(Priority, High); private static Tuple<ImmutableArray<string>, ImmutableArray<TaskListItemDescriptor>> s_lastRequestedTokens = Tuple.Create(ImmutableArray<string>.Empty, ImmutableArray<TaskListItemDescriptor>.Empty); private readonly IGlobalOptionService _globalOptions = globalOptions; public override Task<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(RequestContext context, CancellationToken cancellationToken) => GetTaskListItemsAsync(this.Document, _globalOptions, cancellationToken); internal static async Task<ImmutableArray<DiagnosticData>> GetTaskListItemsAsync(Document document, IGlobalOptionService globalOptions, CancellationToken cancellationToken) { var service = document.GetLanguageService<ITaskListService>(); if (service == null) return []; var options = globalOptions.GetTaskListOptions(); var descriptors = GetAndCacheDescriptors(options.Descriptors); var items = await service.GetTaskListItemsAsync(document, descriptors, cancellationToken).ConfigureAwait(false); if (items.Length == 0) return []; return items.SelectAsArray(i => new DiagnosticData( id: "TODO", category: "TODO", message: i.Message, severity: DiagnosticSeverity.Info, defaultSeverity: DiagnosticSeverity.Info, isEnabledByDefault: true, warningLevel: 0, customTags: s_todoCommentCustomTags, properties: GetProperties(i.Priority), projectId: document.Project.Id, language: document.Project.Language, location: new DiagnosticDataLocation(i.Span, document.Id, mappedFileSpan: i.MappedSpan))); } private static ImmutableDictionary<string, string?> GetProperties(TaskListItemPriority priority) => priority switch { TaskListItemPriority.Low => s_lowPriorityProperties, TaskListItemPriority.Medium => s_mediumPriorityProperties, TaskListItemPriority.High => s_highPriorityProperties, _ => s_mediumPriorityProperties, }; private static ImmutableArray<TaskListItemDescriptor> GetAndCacheDescriptors(ImmutableArray<string> tokenList) { var lastRequested = s_lastRequestedTokens; if (!lastRequested.Item1.SequenceEqual(tokenList)) { var descriptors = TaskListItemDescriptor.Parse(tokenList); lastRequested = Tuple.Create(tokenList, descriptors); s_lastRequestedTokens = lastRequested; } return lastRequested.Item2; } }