Merge pull request #7877 from davidwengier/UpdateWebToolsTestBits

This commit is contained in:
David Wengier 2022-11-04 17:01:07 +11:00 коммит произвёл GitHub
Родитель 876b3de7d8 33d8a7cbbd
Коммит 08b7662c45
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 83 добавлений и 35 удалений

Просмотреть файл

@ -73,7 +73,7 @@
--> -->
<PropertyGroup> <PropertyGroup>
<!-- Several packages from the editor are used for testing HTML support, and share the following version. --> <!-- Several packages from the editor are used for testing HTML support, and share the following version. -->
<Tooling_HtmlEditorPackageVersion>16.10.57-preview1</Tooling_HtmlEditorPackageVersion> <Tooling_HtmlEditorPackageVersion>17.5.101-preview-0002</Tooling_HtmlEditorPackageVersion>
<!-- Several packages share the MS.CA.Testing version --> <!-- Several packages share the MS.CA.Testing version -->
<Tooling_MicrosoftCodeAnalysisTestingVersion>1.1.2-beta1.22512.1</Tooling_MicrosoftCodeAnalysisTestingVersion> <Tooling_MicrosoftCodeAnalysisTestingVersion>1.1.2-beta1.22512.1</Tooling_MicrosoftCodeAnalysisTestingVersion>
<MicrosoftVisualStudioShellPackagesVersion>17.2.32330.158</MicrosoftVisualStudioShellPackagesVersion> <MicrosoftVisualStudioShellPackagesVersion>17.2.32330.158</MicrosoftVisualStudioShellPackagesVersion>
@ -118,9 +118,9 @@
<MicrosoftVisualStudioTextDataPackageVersion>$(MicrosoftVisualStudioPackagesVersion)</MicrosoftVisualStudioTextDataPackageVersion> <MicrosoftVisualStudioTextDataPackageVersion>$(MicrosoftVisualStudioPackagesVersion)</MicrosoftVisualStudioTextDataPackageVersion>
<MicrosoftVisualStudioTextImplementationPackageVersion>$(MicrosoftVisualStudioPackagesVersion)</MicrosoftVisualStudioTextImplementationPackageVersion> <MicrosoftVisualStudioTextImplementationPackageVersion>$(MicrosoftVisualStudioPackagesVersion)</MicrosoftVisualStudioTextImplementationPackageVersion>
<MicrosoftVisualStudioTextLogicPackageVersion>$(MicrosoftVisualStudioPackagesVersion)</MicrosoftVisualStudioTextLogicPackageVersion> <MicrosoftVisualStudioTextLogicPackageVersion>$(MicrosoftVisualStudioPackagesVersion)</MicrosoftVisualStudioTextLogicPackageVersion>
<MicrosoftVisualStudioThreadingPackageVersion>17.3.1-alpha</MicrosoftVisualStudioThreadingPackageVersion> <MicrosoftVisualStudioThreadingPackageVersion>17.4.27</MicrosoftVisualStudioThreadingPackageVersion>
<MicrosoftVisualStudioWebPackageVersion>16.10.0-preview-1-31008-014</MicrosoftVisualStudioWebPackageVersion> <MicrosoftVisualStudioWebPackageVersion>16.10.0-preview-1-31008-014</MicrosoftVisualStudioWebPackageVersion>
<MicrosoftVisualStudioValidationPackageVersion>17.0.53</MicrosoftVisualStudioValidationPackageVersion> <MicrosoftVisualStudioValidationPackageVersion>17.0.64</MicrosoftVisualStudioValidationPackageVersion>
<MicrosoftWebToolsLanguagesHtmlPackageVersion>$(Tooling_HtmlEditorPackageVersion)</MicrosoftWebToolsLanguagesHtmlPackageVersion> <MicrosoftWebToolsLanguagesHtmlPackageVersion>$(Tooling_HtmlEditorPackageVersion)</MicrosoftWebToolsLanguagesHtmlPackageVersion>
<MicrosoftWebToolsLanguagesLanguageServerServerPackageVersion>$(Tooling_HtmlEditorPackageVersion)</MicrosoftWebToolsLanguagesLanguageServerServerPackageVersion> <MicrosoftWebToolsLanguagesLanguageServerServerPackageVersion>$(Tooling_HtmlEditorPackageVersion)</MicrosoftWebToolsLanguagesLanguageServerServerPackageVersion>
<MicrosoftWebToolsLanguagesSharedPackageVersion>$(Tooling_HtmlEditorPackageVersion)</MicrosoftWebToolsLanguagesSharedPackageVersion> <MicrosoftWebToolsLanguagesSharedPackageVersion>$(Tooling_HtmlEditorPackageVersion)</MicrosoftWebToolsLanguagesSharedPackageVersion>

Просмотреть файл

@ -20,14 +20,13 @@ using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Razor; using Microsoft.CodeAnalysis.Razor;
using Microsoft.CodeAnalysis.Razor.Workspaces.Extensions; using Microsoft.CodeAnalysis.Razor.Workspaces.Extensions;
using Microsoft.CodeAnalysis.Text; using Microsoft.CodeAnalysis.Text;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.VisualStudio.LanguageServer.Protocol; using Microsoft.VisualStudio.LanguageServer.Protocol;
using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Threading;
using Microsoft.VisualStudio.Utilities; using Microsoft.VisualStudio.Utilities;
using Microsoft.Web.Editor;
using Microsoft.WebTools.Languages.Shared.ContentTypes; using Microsoft.WebTools.Languages.Shared.ContentTypes;
using Microsoft.WebTools.Languages.Shared.Editor.Composition;
using Microsoft.WebTools.Languages.Shared.Editor.Text; using Microsoft.WebTools.Languages.Shared.Editor.Text;
using Microsoft.WebTools.Shared;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using OmniSharp.Extensions.LanguageServer.Protocol; using OmniSharp.Extensions.LanguageServer.Protocol;
@ -48,29 +47,60 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting
_documents.TryAdd("/" + path, codeDocument); _documents.TryAdd("/" + path, codeDocument);
} }
private RazorDocumentFormattingResponse Format(DocumentOnTypeFormattingParams _) private RazorDocumentFormattingResponse Format(DocumentOnTypeFormattingParams @params)
{ {
var response = new RazorDocumentFormattingResponse(); var generatedHtml = GetGeneratedHtml(@params.TextDocument.Uri);
var generatedHtmlSource = SourceText.From(generatedHtml, Encoding.UTF8);
var absoluteIndex = @params.Position.GetRequiredAbsoluteIndex(generatedHtmlSource, null);
response.Edits = Array.Empty<TextEdit>(); var request = $@"{{
""Options"": {{
// TODO: Update WebTools dependency and call via reflection ""UseSpaces"": {(@params.Options.InsertSpaces ? "true" : "false")},
""TabSize"": {@params.Options.TabSize},
return response; ""IndentSize"": {@params.Options.TabSize}
}},
""Uri"": ""{@params.TextDocument.Uri}"",
""GeneratedChanges"": [
],
""OperationType"": ""FormatOnType"",
""SpanToFormat"": {{ ""Start"": {absoluteIndex}, ""End"": {absoluteIndex} }}
}}
";
return CallWebToolsApplyFormattedEditsHandler(request, @params.TextDocument.Uri, generatedHtml);
} }
private RazorDocumentFormattingResponse Format(DocumentFormattingParams @params) private RazorDocumentFormattingResponse Format(DocumentFormattingParams @params)
{ {
var options = @params.Options; var generatedHtml = GetGeneratedHtml(@params.TextDocument.Uri);
var request = $@"{{
""Options"": {{
""UseSpaces"": {(@params.Options.InsertSpaces ? "true" : "false")},
""TabSize"": {@params.Options.TabSize},
""IndentSize"": {@params.Options.TabSize}
}},
""Uri"": ""{@params.TextDocument.Uri}"",
""GeneratedChanges"": [
]
}}
";
return CallWebToolsApplyFormattedEditsHandler(request, @params.TextDocument.Uri, generatedHtml);
}
private string GetGeneratedHtml(Uri uri)
{
var codeDocument = _documents[uri.GetAbsoluteOrUNCPath()];
var generatedHtml = codeDocument.GetHtmlDocument().GeneratedHtml;
return generatedHtml.Replace("\r", "", StringComparison.Ordinal).Replace("\n", "\r\n", StringComparison.Ordinal);
}
private RazorDocumentFormattingResponse CallWebToolsApplyFormattedEditsHandler(string serializedValue, Uri documentUri, string generatedHtml)
{
var response = new RazorDocumentFormattingResponse(); var response = new RazorDocumentFormattingResponse();
response.Edits = Array.Empty<TextEdit>(); response.Edits = Array.Empty<TextEdit>();
var codeDocument = _documents[@params.TextDocument.Uri.GetAbsoluteOrUNCPath()];
var generatedHtml = codeDocument.GetHtmlDocument().GeneratedHtml;
generatedHtml = generatedHtml.Replace("\r", "", StringComparison.Ordinal).Replace("\n", "\r\n", StringComparison.Ordinal);
var generatedHtmlSource = SourceText.From(generatedHtml, Encoding.UTF8);
var editHandlerAssembly = Assembly.Load("Microsoft.WebTools.Languages.LanguageServer.Server, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); var editHandlerAssembly = Assembly.Load("Microsoft.WebTools.Languages.LanguageServer.Server, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
var editHandlerType = editHandlerAssembly.GetType("Microsoft.WebTools.Languages.LanguageServer.Server.Html.OperationHandlers.ApplyFormatEditsHandler", throwOnError: true); var editHandlerType = editHandlerAssembly.GetType("Microsoft.WebTools.Languages.LanguageServer.Server.Html.OperationHandlers.ApplyFormatEditsHandler", throwOnError: true);
var bufferManagerType = editHandlerAssembly.GetType("Microsoft.WebTools.Languages.LanguageServer.Server.Shared.Buffer.BufferManager", throwOnError: true); var bufferManagerType = editHandlerAssembly.GetType("Microsoft.WebTools.Languages.LanguageServer.Server.Shared.Buffer.BufferManager", throwOnError: true);
@ -86,12 +116,10 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting
var textBufferFactoryService = exportProvider.GetExportedValue<ITextBufferFactoryService>(); var textBufferFactoryService = exportProvider.GetExportedValue<ITextBufferFactoryService>();
var textBufferListeners = Array.Empty<Lazy<IWebTextBufferListener, IOrderedComponentContentTypes>>(); var textBufferListeners = Array.Empty<Lazy<IWebTextBufferListener, IOrderedComponentContentTypes>>();
var bufferManager = Activator.CreateInstance(bufferManagerType, new object[] { contentTypeService, textBufferFactoryService, textBufferListeners }); var bufferManager = Activator.CreateInstance(bufferManagerType, new object[] { contentTypeService, textBufferFactoryService, textBufferListeners });
var joinableTaskFactoryThreadSwitcher = typeof(IdAttribute).Assembly.GetType("Microsoft.WebTools.Shared.Threading.JoinableTaskFactoryThreadSwitcher", throwOnError: true); var loggerProvider = NullLoggerProvider.Instance;
var threadSwitcher = (IThreadSwitcher)Activator.CreateInstance(joinableTaskFactoryThreadSwitcher, new object[] { new JoinableTaskContext().Factory }); var applyFormatEditsHandler = Activator.CreateInstance(editHandlerType, new object[] { bufferManager, textBufferFactoryService, loggerProvider });
var applyFormatEditsHandler = Activator.CreateInstance(editHandlerType, new object[] { bufferManager, threadSwitcher, textBufferFactoryService });
// Make sure the buffer manager knows about the source document // Make sure the buffer manager knows about the source document
var documentUri = @params.TextDocument.Uri;
var contentTypeName = HtmlContentTypeDefinition.HtmlContentType; var contentTypeName = HtmlContentTypeDefinition.HtmlContentType;
var initialContent = generatedHtml; var initialContent = generatedHtml;
var snapshotVersionFromLSP = 0; var snapshotVersionFromLSP = 0;
@ -99,17 +127,6 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting
Assert.IsAssignableFrom<ITextSnapshot>(bufferManager.GetType().GetMethod("CreateBuffer").Invoke(bufferManager, new object[] { oSharpDocUri, contentTypeName, initialContent, snapshotVersionFromLSP })); Assert.IsAssignableFrom<ITextSnapshot>(bufferManager.GetType().GetMethod("CreateBuffer").Invoke(bufferManager, new object[] { oSharpDocUri, contentTypeName, initialContent, snapshotVersionFromLSP }));
var requestType = editHandlerAssembly.GetType("Microsoft.WebTools.Languages.LanguageServer.Server.ContainedLanguage.ApplyFormatEditsParamForOmniSharp", throwOnError: true); var requestType = editHandlerAssembly.GetType("Microsoft.WebTools.Languages.LanguageServer.Server.ContainedLanguage.ApplyFormatEditsParamForOmniSharp", throwOnError: true);
var serializedValue = $@"{{
""Options"": {{
""UseSpaces"": {(@params.Options.InsertSpaces ? "true" : "false")},
""TabSize"": {@params.Options.TabSize},
""IndentSize"": {@params.Options.TabSize}
}},
""Uri"": ""{@params.TextDocument.Uri}"",
""GeneratedChanges"": [
]
}}
";
var request = JsonConvert.DeserializeObject(serializedValue, requestType); var request = JsonConvert.DeserializeObject(serializedValue, requestType);
var resultTask = (Task)applyFormatEditsHandler.GetType() var resultTask = (Task)applyFormatEditsHandler.GetType()

Просмотреть файл

@ -77,6 +77,34 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting
"""); """);
} }
[Fact]
public async Task FormatsSimpleHtmlTag_OnType()
{
await RunOnTypeFormattingTestAsync(
input: """
<html>
<head>
<title>Hello</title>
<script>
var x = 2;$$
</script>
</head>
</html>
""",
expected: """
<html>
<head>
<title>Hello</title>
<script>
var x = 2;
</script>
</head>
</html>
""",
triggerCharacter: ';',
fileKind: FileKinds.Legacy);
}
[Fact] [Fact]
public async Task FormatsRazorHtmlBlock() public async Task FormatsRazorHtmlBlock()
{ {

Просмотреть файл

@ -21,9 +21,12 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsPackageVersion)" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.WebTools.Languages.Html" Version="$(MicrosoftWebToolsLanguagesHtmlPackageVersion)" NoWarn="NU1701" /> <PackageReference Include="Microsoft.WebTools.Languages.Html" Version="$(MicrosoftWebToolsLanguagesHtmlPackageVersion)" NoWarn="NU1701" />
<PackageReference Include="Microsoft.WebTools.Languages.Html.Editor" Version="$(MicrosoftWebToolsLanguagesHtmlPackageVersion)" NoWarn="NU1701" />
<PackageReference Include="Microsoft.WebTools.Languages.LanguageServer.Server" Version="$(MicrosoftWebToolsLanguagesLanguageServerServerPackageVersion)" NoWarn="NU1701" /> <PackageReference Include="Microsoft.WebTools.Languages.LanguageServer.Server" Version="$(MicrosoftWebToolsLanguagesLanguageServerServerPackageVersion)" NoWarn="NU1701" />
<PackageReference Include="Microsoft.WebTools.Languages.Shared" Version="$(MicrosoftWebToolsLanguagesSharedPackageVersion)" NoWarn="NU1701" /> <PackageReference Include="Microsoft.WebTools.Languages.Shared" Version="$(MicrosoftWebToolsLanguagesSharedPackageVersion)" NoWarn="NU1701" />
<PackageReference Include="Microsoft.WebTools.Languages.Shared.Editor" Version="$(MicrosoftWebToolsLanguagesSharedEditorPackageVersion)" NoWarn="NU1701" /> <PackageReference Include="Microsoft.WebTools.Languages.Shared.Editor" Version="$(MicrosoftWebToolsLanguagesSharedEditorPackageVersion)" NoWarn="NU1701" />
<PackageReference Include="Microsoft.WebTools.Languages.Shared.VS" Version="$(MicrosoftWebToolsLanguagesSharedEditorPackageVersion)" NoWarn="NU1701" />
<PackageReference Include="Microsoft.VisualStudio.Shell.Framework" Version="$(MicrosoftVisualStudioShellPackagesVersion)" NoWarn="NU1701" />
<PackageReference Include="Microsoft.VisualStudio.Text.Data" Version="$(MicrosoftVisualStudioTextDataPackageVersion)" /> <PackageReference Include="Microsoft.VisualStudio.Text.Data" Version="$(MicrosoftVisualStudioTextDataPackageVersion)" />
<PackageReference Include="Microsoft.VisualStudio.Text.Implementation" Version="$(MicrosoftVisualStudioTextImplementationPackageVersion)" NoWarn="NU1701" /> <PackageReference Include="Microsoft.VisualStudio.Text.Implementation" Version="$(MicrosoftVisualStudioTextImplementationPackageVersion)" NoWarn="NU1701" />
<PackageReference Include="Microsoft.VisualStudio.Text.Logic" Version="$(MicrosoftVisualStudioTextLogicPackageVersion)" /> <PackageReference Include="Microsoft.VisualStudio.Text.Logic" Version="$(MicrosoftVisualStudioTextLogicPackageVersion)" />
@ -38,12 +41,12 @@
the references have no other affect on build etc. the references have no other affect on build etc.
--> -->
<PackageReference Include="Microsoft.VisualStudio.Imaging" Version="$(MicrosoftVisualStudioShellPackagesVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" /> <PackageReference Include="Microsoft.VisualStudio.Imaging" Version="$(MicrosoftVisualStudioShellPackagesVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" />
<PackageReference Include="Microsoft.VisualStudio.GraphModel" Version="$(MicrosoftVisualStudioShellPackagesVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" />
<PackageReference Include="Microsoft.VisualStudio.Language.Intellisense" Version="$(MicrosoftVisualStudioPackagesVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" /> <PackageReference Include="Microsoft.VisualStudio.Language.Intellisense" Version="$(MicrosoftVisualStudioPackagesVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" />
<PackageReference Include="Microsoft.VisualStudio.Text.Internal" Version="$(MicrosoftVisualStudioPackagesVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" /> <PackageReference Include="Microsoft.VisualStudio.Text.Internal" Version="$(MicrosoftVisualStudioPackagesVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" />
<PackageReference Include="Microsoft.VisualStudio.Text.UI.Wpf" Version="$(MicrosoftVisualStudioPackagesVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" /> <PackageReference Include="Microsoft.VisualStudio.Text.UI.Wpf" Version="$(MicrosoftVisualStudioPackagesVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" />
<PackageReference Include="Microsoft.WebTools.Languages.Css" Version="$(MicrosoftWebToolsLanguagesHtmlPackageVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" /> <PackageReference Include="Microsoft.WebTools.Languages.Css" Version="$(MicrosoftWebToolsLanguagesHtmlPackageVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" />
<PackageReference Include="Microsoft.WebTools.Languages.Css.Editor" Version="$(MicrosoftWebToolsLanguagesHtmlPackageVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" /> <PackageReference Include="Microsoft.WebTools.Languages.Css.Editor" Version="$(MicrosoftWebToolsLanguagesHtmlPackageVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" />
<PackageReference Include="Microsoft.WebTools.Languages.Html.Editor" Version="$(MicrosoftWebToolsLanguagesHtmlPackageVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" />
<!-- <!--
Pinning packages to avoid misaligned reference CI failures. Pinning packages to avoid misaligned reference CI failures.