-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Show TODO Razor comments in the Task List in VS #11540
base: main
Are you sure you want to change the base?
Changes from all commits
d3df3e5
ecd935f
aff7fe5
4b9bb18
45683c5
8202cd5
b16e8a4
e2f2c34
8333d03
8b79a45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,10 @@ | |
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis.Razor.Settings; | ||
using Microsoft.Extensions.Internal; | ||
|
||
namespace Microsoft.AspNetCore.Razor.LanguageServer.Hosting; | ||
|
||
|
@@ -16,7 +19,8 @@ internal record RazorLSPOptions( | |
bool AutoInsertAttributeQuotes, | ||
bool ColorBackground, | ||
bool CodeBlockBraceOnNextLine, | ||
bool CommitElementsWithSpace) | ||
bool CommitElementsWithSpace, | ||
ImmutableArray<string> TaskListDescriptors) | ||
{ | ||
public readonly static RazorLSPOptions Default = new(Formatting: FormattingFlags.All, | ||
AutoClosingTags: true, | ||
|
@@ -27,7 +31,15 @@ internal record RazorLSPOptions( | |
AutoInsertAttributeQuotes: true, | ||
ColorBackground: false, | ||
CodeBlockBraceOnNextLine: false, | ||
CommitElementsWithSpace: true); | ||
CommitElementsWithSpace: true, | ||
TaskListDescriptors: []); | ||
|
||
public ImmutableArray<string> TaskListDescriptors | ||
{ | ||
get; | ||
init => field = value.NullToEmpty(); | ||
|
||
} = TaskListDescriptors.NullToEmpty(); | ||
|
||
/// <summary> | ||
/// Initializes the LSP options with the settings from the passed in client settings, and default values for anything | ||
|
@@ -43,7 +55,8 @@ internal static RazorLSPOptions From(ClientSettings settings) | |
settings.AdvancedSettings.AutoInsertAttributeQuotes, | ||
settings.AdvancedSettings.ColorBackground, | ||
settings.AdvancedSettings.CodeBlockBraceOnNextLine, | ||
settings.AdvancedSettings.CommitElementsWithSpace); | ||
settings.AdvancedSettings.CommitElementsWithSpace, | ||
settings.AdvancedSettings.TaskListDescriptors); | ||
|
||
private static FormattingFlags GetFormattingFlags(ClientSettings settings) | ||
{ | ||
|
@@ -60,4 +73,37 @@ private static FormattingFlags GetFormattingFlags(ClientSettings settings) | |
|
||
return flags; | ||
} | ||
|
||
public virtual bool Equals(RazorLSPOptions? other) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this |
||
{ | ||
return other is not null && | ||
Formatting == other.Formatting && | ||
AutoClosingTags == other.AutoClosingTags && | ||
InsertSpaces == other.InsertSpaces && | ||
TabSize == other.TabSize && | ||
AutoShowCompletion == other.AutoShowCompletion && | ||
AutoListParams == other.AutoListParams && | ||
AutoInsertAttributeQuotes == other.AutoInsertAttributeQuotes && | ||
ColorBackground == other.ColorBackground && | ||
CodeBlockBraceOnNextLine == other.CodeBlockBraceOnNextLine && | ||
CommitElementsWithSpace == other.CommitElementsWithSpace && | ||
TaskListDescriptors.SequenceEqual(other.TaskListDescriptors); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
var hash = HashCodeCombiner.Start(); | ||
hash.Add(Formatting); | ||
hash.Add(AutoClosingTags); | ||
hash.Add(InsertSpaces); | ||
hash.Add(TabSize); | ||
hash.Add(AutoShowCompletion); | ||
hash.Add(AutoListParams); | ||
hash.Add(AutoInsertAttributeQuotes); | ||
hash.Add(ColorBackground); | ||
hash.Add(CodeBlockBraceOnNextLine); | ||
hash.Add(CommitElementsWithSpace); | ||
hash.Add(TaskListDescriptors); | ||
return hash; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Immutable; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
using Microsoft.AspNetCore.Razor.Language.Syntax; | ||
using Microsoft.AspNetCore.Razor.PooledObjects; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Microsoft.VisualStudio.LanguageServer.Protocol; | ||
using LspDiagnostic = Microsoft.VisualStudio.LanguageServer.Protocol.Diagnostic; | ||
using LspDiagnosticSeverity = Microsoft.VisualStudio.LanguageServer.Protocol.DiagnosticSeverity; | ||
|
||
namespace Microsoft.CodeAnalysis.Razor.Diagnostics; | ||
|
||
internal static class TaskListDiagnosticProvider | ||
{ | ||
private static readonly DiagnosticTag[] s_taskItemTags = [VSDiagnosticTags.TaskItem]; | ||
|
||
public static ImmutableArray<LspDiagnostic> GetTaskListDiagnostics(RazorCodeDocument codeDocument, ImmutableArray<string> taskListDescriptors) | ||
{ | ||
var source = codeDocument.Source.Text; | ||
var tree = codeDocument.GetSyntaxTree(); | ||
|
||
using var diagnostics = new PooledArrayBuilder<LspDiagnostic>(); | ||
|
||
foreach (var node in tree.Root.DescendantNodes()) | ||
{ | ||
if (node is RazorCommentBlockSyntax comment) | ||
{ | ||
var i = comment.Comment.SpanStart; | ||
|
||
while (char.IsWhiteSpace(source[i])) | ||
{ | ||
i++; | ||
} | ||
|
||
foreach (var token in taskListDescriptors) | ||
{ | ||
if (i + token.Length + 2 > comment.EndCommentStar.SpanStart || // Enough room in the comment for the token and some content? | ||
!Matches(source, i, token) || // Does the prefix match? | ||
char.IsLetter(source[i + token.Length + 1])) // Is there something after the prefix, so we don't match "TODOLOL" | ||
Comment on lines
+39
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It feels like the first and last checks could be pulled into |
||
{ | ||
continue; | ||
} | ||
|
||
diagnostics.Add(new LspDiagnostic | ||
{ | ||
Code = "TODO", | ||
Message = comment.Comment.Content.Trim(), | ||
Source = "Razor", | ||
Severity = LspDiagnosticSeverity.Information, | ||
Range = source.GetRange(comment.Comment.Span), | ||
Tags = s_taskItemTags | ||
}); | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
return diagnostics.ToImmutable(); | ||
} | ||
|
||
private static bool Matches(SourceText source, int i, string token) | ||
{ | ||
for (var j = 0; j < token.Length; j++) | ||
{ | ||
if (source.Length < i + j) | ||
{ | ||
return false; | ||
} | ||
|
||
if (char.ToLowerInvariant(source[i + j]) != char.ToLowerInvariant(token[j])) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you intend for new records to be derived from
RazorLSPOptions
? If not, please markRazorLSPOptions
assealed
. If you do wantRazorLSPOptions
to be subtyped, add a check here to compareEqualityContract
and add it to `GetHashCode() as well. Otherwise, equality between subtypes will be incorrect.