From 6b557e0ba30f281b70ce08fadb615b1e56bbd67a Mon Sep 17 00:00:00 2001 From: WMJ Date: Mon, 10 Jun 2024 13:56:28 +0800 Subject: [PATCH] ! Improve comment tagger to handle more situations --- Codist/Taggers/CommentTagger.cs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/Codist/Taggers/CommentTagger.cs b/Codist/Taggers/CommentTagger.cs index db8088ee..43dd521a 100644 --- a/Codist/Taggers/CommentTagger.cs +++ b/Codist/Taggers/CommentTagger.cs @@ -28,9 +28,11 @@ static Dictionary InitCodeTypeExtensions() { { "h", CodeType.C }, { "cxx", CodeType.C }, { "css", CodeType.Css }, - { "less", CodeType.Common }, + { "less", CodeType.AlternativeC }, + { "scss", CodeType.AlternativeC }, { "json", CodeType.Common }, - { "cshtml", CodeType.CSharp }, + { "cshtml", CodeType.AlternativeC }, + { "razor", CodeType.AlternativeC }, { "go", CodeType.Go }, { "html", CodeType.Markup }, { "xhtml", CodeType.Markup }, @@ -119,6 +121,8 @@ public static CommentTagger Create(IClassificationTypeRegistryService registry, case CodeType.BashShell: case CodeType.Common: return new CommonCommentTagger(registry, textView, textBuffer); + case CodeType.AlternativeC: + return new AlternativeCCommentTagger(registry, textView, textBuffer); } return null; } @@ -316,9 +320,10 @@ static CodeType GetCodeType(ITextBuffer textBuffer) { : t.IsOfType("TypeScript") || t.IsOfType("JavaScript") ? CodeType.Js : t.IsOfType("code++.MagicPython") ? CodeType.Python : t.IsOfType("html") || t.IsOfType("htmlx") || t.IsOfType("XAML") || t.IsOfType("XML") || t.IsOfType(Constants.CodeTypes.HtmlxProjection) ? CodeType.Markup - : t.IsOfType("Razor") ? CodeType.Common + : t.IsOfType("Razor") ? CodeType.AlternativeC : t.IsOfType("code++.css") ? CodeType.Css : t.IsOfType("code++.LESS") ? CodeType.Common + : t.IsOfType("css.extensions") ? CodeType.AlternativeC : t.IsOfType("code++.JSON (Javascript Next)") ? CodeType.Common : t.IsOfType("code++.Shell Script (Bash)") || t.IsOfType("InBoxPowerShell") ? CodeType.BashShell : t.IsOfType("code++.Batch File") ? CodeType.Batch @@ -375,7 +380,7 @@ public void Dispose() { enum CodeType { - None, Common, CSharp, Markup, C, Css, Go, Rust, Js, Sql, Python, Batch, BashShell + None, Common, CSharp, Markup, C, AlternativeC, Css, Go, Rust, Js, Sql, Python, Batch, BashShell } sealed class CommonCommentTagger : CommentTagger @@ -443,6 +448,20 @@ protected override int GetCommentEndIndex(SnapshotSpan content) { } } + sealed class AlternativeCCommentTagger : CommentTagger + { + public AlternativeCCommentTagger(IClassificationTypeRegistryService registry, ITextView textView, ITextBuffer buffer) : base(registry, textView, buffer) { + } + + protected override int GetCommentStartIndex(SnapshotSpan content) { + return content.Length > 2 && content.CharAt(1) == '/' ? 2 + : SlashStarCommentTagger.GetStartIndexOfMultilineSlashStartComment(content, 0); + } + protected override int GetCommentEndIndex(SnapshotSpan content) { + return content.CharAt(1) == '*' ? content.Length - 2 : content.Length; + } + } + sealed class CSharpCommentTagger : CommentTagger { readonly ClassificationTag _PreprocessorKeywordTag;