From d4c396a9c9e31d027789a55a61f5509cadc6e0e0 Mon Sep 17 00:00:00 2001 From: Alex Gavrilov Date: Fri, 1 Mar 2024 13:26:35 -0800 Subject: [PATCH] Add using directive support. (#9982) * Add "ussing" keyword and "using" snippet, fixup tests * Add snippet completion item re-writer so we can change C# "using" snippet to say "using statement" * CR feedback * Removing unneeded using * Update src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Resources.resx CR feedback Co-authored-by: Dustin Campbell * Update src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Completion/RazorCompletionItemResolver.cs CR feedback Co-authored-by: Dustin Campbell * Restore ... suffix in snippet directive completion item labels * Adjusting sort text Adding space after "using" in the C# snippet sort text so that it's sorted after Razor "using" keyword and snippet instead of taking a substring with (length - 1) of directive name as Razor directive sort text. --------- Co-authored-by: Dustin Campbell --- .../src/Language/Legacy/CSharpCodeParser.cs | 8 + .../src/Language/Legacy/SyntaxConstants.cs | 1 + .../src/Language/Resources.resx | 7 +- .../Delegation/SnippetResponseRewriter.cs | 54 ++++++ .../Completion/RazorCompletionItemResolver.cs | 24 ++- .../RazorCompletionResolveEndpoint.cs | 5 +- .../IServiceCollectionExtensions.cs | 1 + .../Resources/SR.resx | 3 + .../Resources/xlf/SR.cs.xlf | 5 + .../Resources/xlf/SR.de.xlf | 5 + .../Resources/xlf/SR.es.xlf | 5 + .../Resources/xlf/SR.fr.xlf | 5 + .../Resources/xlf/SR.it.xlf | 5 + .../Resources/xlf/SR.ja.xlf | 5 + .../Resources/xlf/SR.ko.xlf | 5 + .../Resources/xlf/SR.pl.xlf | 5 + .../Resources/xlf/SR.pt-BR.xlf | 5 + .../Resources/xlf/SR.ru.xlf | 5 + .../Resources/xlf/SR.tr.xlf | 5 + .../Resources/xlf/SR.zh-Hans.xlf | 5 + .../Resources/xlf/SR.zh-Hant.xlf | 5 + .../DirectiveCompletionItemProvider.cs | 21 ++- .../Resources/SR.resx | 3 + .../Resources/xlf/SR.cs.xlf | 5 + .../Resources/xlf/SR.de.xlf | 5 + .../Resources/xlf/SR.es.xlf | 5 + .../Resources/xlf/SR.fr.xlf | 5 + .../Resources/xlf/SR.it.xlf | 5 + .../Resources/xlf/SR.ja.xlf | 5 + .../Resources/xlf/SR.ko.xlf | 5 + .../Resources/xlf/SR.pl.xlf | 5 + .../Resources/xlf/SR.pt-BR.xlf | 5 + .../Resources/xlf/SR.ru.xlf | 5 + .../Resources/xlf/SR.tr.xlf | 5 + .../Resources/xlf/SR.zh-Hans.xlf | 5 + .../Resources/xlf/SR.zh-Hant.xlf | 5 + .../Delegation/SnippetResponseRewriterTest.cs | 164 ++++++++++++++++++ .../Completion/DirectiveVerifier.cs | 38 ++++ .../LegacyRazorCompletionEndpointTest.cs | 21 +-- .../RazorCompletionListProvierTest.cs | 28 +-- .../DirectiveCompletionItemProviderTest.cs | 97 +++++------ .../RazorDirectiveCompletionSourceTest.cs | 9 +- 42 files changed, 508 insertions(+), 106 deletions(-) create mode 100644 src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Completion/Delegation/SnippetResponseRewriter.cs create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Completion/Delegation/SnippetResponseRewriterTest.cs create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Completion/DirectiveVerifier.cs diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/CSharpCodeParser.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/CSharpCodeParser.cs index 9e9d841efc0..7db73b8b5c5 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/CSharpCodeParser.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/CSharpCodeParser.cs @@ -30,6 +30,14 @@ internal class CSharpCodeParser : TokenizerBackedParser builder.Description = Resources.AddTagHelperDirective_Description; }); + internal static readonly DirectiveDescriptor UsingDirectiveDescriptor = DirectiveDescriptor.CreateDirective( + SyntaxConstants.CSharp.UsingKeyword, + DirectiveKind.SingleLine, + builder => + { + builder.Description = Resources.UsingDirective_Description; + }); + internal static readonly DirectiveDescriptor RemoveTagHelperDirectiveDescriptor = DirectiveDescriptor.CreateDirective( SyntaxConstants.CSharp.RemoveTagHelperKeyword, DirectiveKind.SingleLine, diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/SyntaxConstants.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/SyntaxConstants.cs index 9fa13dff0d5..6794dfe0390 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/SyntaxConstants.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/SyntaxConstants.cs @@ -17,6 +17,7 @@ internal static class SyntaxConstants public static class CSharp { public const int UsingKeywordLength = 5; + public const string UsingKeyword = "using"; public const string TagHelperPrefixKeyword = "tagHelperPrefix"; public const string AddTagHelperKeyword = "addTagHelper"; public const string RemoveTagHelperKeyword = "removeTagHelper"; diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Resources.resx b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Resources.resx index 7b3e4b05e05..f469eb79b96 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Resources.resx +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Resources.resx @@ -1,4 +1,4 @@ - +