зеркало из https://github.com/dotnet/razor.git
Merge pull request #7877 from davidwengier/UpdateWebToolsTestBits
This commit is contained in:
Коммит
08b7662c45
|
@ -73,7 +73,7 @@
|
|||
-->
|
||||
<PropertyGroup>
|
||||
<!-- 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 -->
|
||||
<Tooling_MicrosoftCodeAnalysisTestingVersion>1.1.2-beta1.22512.1</Tooling_MicrosoftCodeAnalysisTestingVersion>
|
||||
<MicrosoftVisualStudioShellPackagesVersion>17.2.32330.158</MicrosoftVisualStudioShellPackagesVersion>
|
||||
|
@ -118,9 +118,9 @@
|
|||
<MicrosoftVisualStudioTextDataPackageVersion>$(MicrosoftVisualStudioPackagesVersion)</MicrosoftVisualStudioTextDataPackageVersion>
|
||||
<MicrosoftVisualStudioTextImplementationPackageVersion>$(MicrosoftVisualStudioPackagesVersion)</MicrosoftVisualStudioTextImplementationPackageVersion>
|
||||
<MicrosoftVisualStudioTextLogicPackageVersion>$(MicrosoftVisualStudioPackagesVersion)</MicrosoftVisualStudioTextLogicPackageVersion>
|
||||
<MicrosoftVisualStudioThreadingPackageVersion>17.3.1-alpha</MicrosoftVisualStudioThreadingPackageVersion>
|
||||
<MicrosoftVisualStudioThreadingPackageVersion>17.4.27</MicrosoftVisualStudioThreadingPackageVersion>
|
||||
<MicrosoftVisualStudioWebPackageVersion>16.10.0-preview-1-31008-014</MicrosoftVisualStudioWebPackageVersion>
|
||||
<MicrosoftVisualStudioValidationPackageVersion>17.0.53</MicrosoftVisualStudioValidationPackageVersion>
|
||||
<MicrosoftVisualStudioValidationPackageVersion>17.0.64</MicrosoftVisualStudioValidationPackageVersion>
|
||||
<MicrosoftWebToolsLanguagesHtmlPackageVersion>$(Tooling_HtmlEditorPackageVersion)</MicrosoftWebToolsLanguagesHtmlPackageVersion>
|
||||
<MicrosoftWebToolsLanguagesLanguageServerServerPackageVersion>$(Tooling_HtmlEditorPackageVersion)</MicrosoftWebToolsLanguagesLanguageServerServerPackageVersion>
|
||||
<MicrosoftWebToolsLanguagesSharedPackageVersion>$(Tooling_HtmlEditorPackageVersion)</MicrosoftWebToolsLanguagesSharedPackageVersion>
|
||||
|
|
|
@ -20,14 +20,13 @@ using Microsoft.CodeAnalysis.Formatting;
|
|||
using Microsoft.CodeAnalysis.Razor;
|
||||
using Microsoft.CodeAnalysis.Razor.Workspaces.Extensions;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.VisualStudio.LanguageServer.Protocol;
|
||||
using Microsoft.VisualStudio.Text;
|
||||
using Microsoft.VisualStudio.Threading;
|
||||
using Microsoft.VisualStudio.Utilities;
|
||||
using Microsoft.Web.Editor;
|
||||
using Microsoft.WebTools.Languages.Shared.ContentTypes;
|
||||
using Microsoft.WebTools.Languages.Shared.Editor.Composition;
|
||||
using Microsoft.WebTools.Languages.Shared.Editor.Text;
|
||||
using Microsoft.WebTools.Shared;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using OmniSharp.Extensions.LanguageServer.Protocol;
|
||||
|
@ -48,29 +47,60 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting
|
|||
_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>();
|
||||
|
||||
// TODO: Update WebTools dependency and call via reflection
|
||||
|
||||
return response;
|
||||
var request = $@"{{
|
||||
""Options"": {{
|
||||
""UseSpaces"": {(@params.Options.InsertSpaces ? "true" : "false")},
|
||||
""TabSize"": {@params.Options.TabSize},
|
||||
""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)
|
||||
{
|
||||
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();
|
||||
|
||||
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 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);
|
||||
|
@ -86,12 +116,10 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting
|
|||
var textBufferFactoryService = exportProvider.GetExportedValue<ITextBufferFactoryService>();
|
||||
var textBufferListeners = Array.Empty<Lazy<IWebTextBufferListener, IOrderedComponentContentTypes>>();
|
||||
var bufferManager = Activator.CreateInstance(bufferManagerType, new object[] { contentTypeService, textBufferFactoryService, textBufferListeners });
|
||||
var joinableTaskFactoryThreadSwitcher = typeof(IdAttribute).Assembly.GetType("Microsoft.WebTools.Shared.Threading.JoinableTaskFactoryThreadSwitcher", throwOnError: true);
|
||||
var threadSwitcher = (IThreadSwitcher)Activator.CreateInstance(joinableTaskFactoryThreadSwitcher, new object[] { new JoinableTaskContext().Factory });
|
||||
var applyFormatEditsHandler = Activator.CreateInstance(editHandlerType, new object[] { bufferManager, threadSwitcher, textBufferFactoryService });
|
||||
var loggerProvider = NullLoggerProvider.Instance;
|
||||
var applyFormatEditsHandler = Activator.CreateInstance(editHandlerType, new object[] { bufferManager, textBufferFactoryService, loggerProvider });
|
||||
|
||||
// Make sure the buffer manager knows about the source document
|
||||
var documentUri = @params.TextDocument.Uri;
|
||||
var contentTypeName = HtmlContentTypeDefinition.HtmlContentType;
|
||||
var initialContent = generatedHtml;
|
||||
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 }));
|
||||
|
||||
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 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]
|
||||
public async Task FormatsRazorHtmlBlock()
|
||||
{
|
||||
|
|
|
@ -21,9 +21,12 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsPackageVersion)" />
|
||||
<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.Shared" Version="$(MicrosoftWebToolsLanguagesSharedPackageVersion)" 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.Implementation" Version="$(MicrosoftVisualStudioTextImplementationPackageVersion)" NoWarn="NU1701" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Text.Logic" Version="$(MicrosoftVisualStudioTextLogicPackageVersion)" />
|
||||
|
@ -38,12 +41,12 @@
|
|||
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.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.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.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.Html.Editor" Version="$(MicrosoftWebToolsLanguagesHtmlPackageVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" />
|
||||
|
||||
<!--
|
||||
Pinning packages to avoid misaligned reference CI failures.
|
||||
|
|
Загрузка…
Ссылка в новой задаче