Clean up unneeded nullable changes

This commit is contained in:
Ryan Brandenburg 2022-10-27 16:53:52 -07:00
Родитель 8afe1f3ad4
Коммит 1f57709169
12 изменённых файлов: 81 добавлений и 304 удалений

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

@ -80,7 +80,6 @@
<package pattern="humanizer.core" />
<package pattern="iced" />
<package pattern="icsharpcode.decompiler" />
<package pattern="inputsimulatorplus" />
<package pattern="mediatr" />
<package pattern="messagepack" />
<package pattern="messagepack.annotations" />

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

@ -84,8 +84,6 @@
<MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>$(RoslynPackageVersion)</MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>
</PropertyGroup>
<PropertyGroup Label="Manual">
<InputSimulatorPlusVersion>1.0.7</InputSimulatorPlusVersion>
<!-- dotnet/runtime packages -->
<MicrosoftExtensionsPackageVersion>6.0.0</MicrosoftExtensionsPackageVersion>
<SystemCollectionsImmutablePackageVersion>6.0.0</SystemCollectionsImmutablePackageVersion>

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

@ -104,7 +104,6 @@ namespace Microsoft.AspNetCore.Razor.Microbenchmarks.LanguageServer
public async Task CleanupServerAsync()
{
var innerServer = RazorLanguageServer.GetInnerLanguageServerForTesting();
await innerServer.ShutdownAsync();
await innerServer.ExitAsync();
}

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

@ -5,10 +5,8 @@ using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Build.Framework;
using Microsoft.VisualStudio.LanguageServer.Client;
using Microsoft.VisualStudio.LanguageServer.ContainedLanguage.Extensions;
using Microsoft.VisualStudio.Text;
@ -46,14 +44,9 @@ namespace Microsoft.VisualStudio.LanguageServer.ContainedLanguage
_documentManager = documentManager;
}
internal record SynchronizedResult<TVirtualDocumentSnapshot>(bool Synchronized, TVirtualDocumentSnapshot? VirtualSnapshot)
internal record SynchronizedResult<TVirtualDocumentSnapshot>(bool Synchronized, TVirtualDocumentSnapshot VirtualSnapshot)
where TVirtualDocumentSnapshot : VirtualDocumentSnapshot
{
public bool TryGetVirtualSnapshot([NotNullWhen(true)] out TVirtualDocumentSnapshot? virtualDocumentSnapshot)
{
virtualDocumentSnapshot = VirtualSnapshot;
return Synchronized;
}
}
public override Task<SynchronizedResult<TVirtualDocumentSnapshot>> TrySynchronizeVirtualDocumentAsync<TVirtualDocumentSnapshot>(
@ -82,11 +75,7 @@ namespace Microsoft.VisualStudio.LanguageServer.ContainedLanguage
Task<bool> onSynchronizedTask;
lock (_documentContextLock)
{
if (!TryGetVirtualDocumentSnapshot<TVirtualDocumentSnapshot>(hostDocumentUri, out var preSyncedSnapshot))
{
return new SynchronizedResult<TVirtualDocumentSnapshot>(false, preSyncedSnapshot);
}
var preSyncedSnapshot = GetVirtualDocumentSnapshot<TVirtualDocumentSnapshot>(hostDocumentUri);
var virtualDocumentUri = preSyncedSnapshot.Uri;
if (!_virtualDocumentContexts.TryGetValue(virtualDocumentUri, out var documentContext))
{
@ -105,11 +94,8 @@ namespace Microsoft.VisualStudio.LanguageServer.ContainedLanguage
}
var onSynchronizedResult = await onSynchronizedTask.ConfigureAwait(false);
TVirtualDocumentSnapshot? virtualDocumentSnapshot = null;
if (onSynchronizedResult)
{
onSynchronizedResult = TryGetVirtualDocumentSnapshot(hostDocumentUri, out virtualDocumentSnapshot);
}
var virtualDocumentSnapshot = GetVirtualDocumentSnapshot<TVirtualDocumentSnapshot>(hostDocumentUri);
return new SynchronizedResult<TVirtualDocumentSnapshot>(onSynchronizedResult, virtualDocumentSnapshot);
}
@ -146,7 +132,7 @@ namespace Microsoft.VisualStudio.LanguageServer.ContainedLanguage
}
}
private bool TryGetVirtualDocumentSnapshot<TVirtualDocumentSnapshot>(Uri hostDocumentUri, [NotNullWhen(true)] out TVirtualDocumentSnapshot? virtualDocumentSnapshot)
private TVirtualDocumentSnapshot GetVirtualDocumentSnapshot<TVirtualDocumentSnapshot>(Uri hostDocumentUri)
where TVirtualDocumentSnapshot : VirtualDocumentSnapshot
{
var normalizedString = hostDocumentUri.GetAbsoluteOrUNCPath();
@ -154,20 +140,15 @@ namespace Microsoft.VisualStudio.LanguageServer.ContainedLanguage
if (!_documentManager.TryGetDocument(normalizedUri, out var documentSnapshot))
{
// Unable to retrieve snapshot for document {normalizedUri} after synchronization
virtualDocumentSnapshot = null;
return false;
throw new InvalidOperationException($"Unable to retrieve snapshot for document {normalizedUri} after synchronization");
}
if (!documentSnapshot.TryGetVirtualDocument<TVirtualDocumentSnapshot>(out var virtualDoc))
{
// Unable to retrieve virtual document for {normalizedUri} after document synchronization
virtualDocumentSnapshot = null;
return false;
throw new InvalidOperationException($"Unable to retrieve virtual document for {normalizedUri} after document synchronization");
}
virtualDocumentSnapshot = virtualDoc;
return true;
return virtualDoc;
}
private void VirtualDocumentBuffer_PostChanged(object sender, EventArgs e)

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

@ -24,7 +24,6 @@ using Microsoft.VisualStudio.LanguageServer.Protocol;
using Microsoft.VisualStudio.LanguageServerClient.Razor.Extensions;
using Microsoft.VisualStudio.LanguageServerClient.Razor.HtmlCSharp;
using Microsoft.VisualStudio.LanguageServerClient.Razor.WrapWithTag;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Threading;
using Newtonsoft.Json.Linq;
@ -171,20 +170,20 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
await _joinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
var htmlResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
var (synchronized, htmlDocument) = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
request.HostDocumentVersion,
request.TextDocument.Uri,
cancellationToken);
if (!htmlResult.TryGetVirtualSnapshot(out var htmlDocument))
var languageServerName = RazorLSPConstants.HtmlLanguageServerName;
var projectedUri = htmlDocument.Uri;
if (!synchronized)
{
Debug.Fail("RangeFormatting not synchronized.");
return response;
}
var languageServerName = RazorLSPConstants.HtmlLanguageServerName;
var projectedUri = htmlDocument.Uri;
var formattingParams = new DocumentFormattingParams()
{
TextDocument = new TextDocumentIdentifier() { Uri = projectedUri },
@ -211,10 +210,10 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
var hostDocumentUri = request.TextDocument.Uri;
var languageServerName = RazorLSPConstants.HtmlLanguageServerName;
var htmlResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
var (synchronized, htmlDocument) = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
request.HostDocumentVersion, hostDocumentUri, cancellationToken);
if (!htmlResult.TryGetVirtualSnapshot(out var htmlDocument))
if (!synchronized)
{
return response;
}
@ -252,7 +251,7 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
await _joinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
var hostDocumentUri = new Uri(request.HostDocumentFilePath);
var csharpResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<CSharpVirtualDocumentSnapshot>(
var (synchronized, csharpDocument) = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<CSharpVirtualDocumentSnapshot>(
request.HostDocumentVersion,
hostDocumentUri,
cancellationToken);
@ -260,7 +259,7 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
string languageServerName;
Uri projectedUri;
if (!csharpResult.TryGetVirtualSnapshot(out var csharpDocument))
if (!synchronized)
{
// Document could not be synchronized
return response;
@ -436,14 +435,9 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
throw new ArgumentNullException(nameof(documentColorParams));
}
var htmlResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
var (synchronized, htmlDoc) = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
documentColorParams.RequiredHostDocumentVersion, documentColorParams.TextDocument.Uri, cancellationToken);
if (!htmlResult.TryGetVirtualSnapshot(out var htmlDoc))
{
return null;
}
documentColorParams.TextDocument.Uri = htmlDoc.Uri;
var htmlTextBuffer = htmlDoc.Snapshot.TextBuffer;
var requests = _requestInvoker.ReinvokeRequestOnMultipleServersAsync<DocumentColorParams, ColorInformation[]>(
@ -519,12 +513,12 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
var response = new VSInternalWrapWithTagResponse(wrapWithParams.Range, Array.Empty<TextEdit>());
var htmlResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
var (synchronized, htmlDocument) = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
wrapWithParams.TextDocument.Version,
wrapWithParams.TextDocument.Uri,
cancellationToken);
if (!htmlResult.TryGetVirtualSnapshot(out var htmlDocument))
if (!synchronized)
{
Debug.Fail("Document was not synchronized");
return response;
@ -603,10 +597,10 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
var csharpRanges = new List<FoldingRange>();
var csharpTask = Task.Run(async () =>
{
var csharpResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<CSharpVirtualDocumentSnapshot>(
var (synchronized, csharpSnapshot) = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<CSharpVirtualDocumentSnapshot>(
foldingRangeParams.HostDocumentVersion, foldingRangeParams.TextDocument.Uri, cancellationToken);
if (csharpResult.TryGetVirtualSnapshot(out var csharpSnapshot))
if (synchronized)
{
var csharpRequestParams = new FoldingRangeParams()
{
@ -640,10 +634,10 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
var htmlTask = Task.CompletedTask;
htmlTask = Task.Run(async () =>
{
var htmlResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
var (synchronized, htmlDocument) = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
foldingRangeParams.HostDocumentVersion, foldingRangeParams.TextDocument.Uri, cancellationToken);
if (htmlResult.TryGetVirtualSnapshot(out var htmlDocument))
if (synchronized)
{
var htmlRequestParams = new FoldingRangeParams()
{
@ -721,20 +715,12 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
hostDocumentVersion,
hostDocumentUri,
cancellationToken);
if (syncResult.TryGetVirtualSnapshot(out var virtualSnapshot))
languageServerName = RazorLSPConstants.RazorCSharpLanguageServerName;
presentationParams.TextDocument = new TextDocumentIdentifier
{
languageServerName = RazorLSPConstants.RazorCSharpLanguageServerName;
presentationParams.TextDocument = new TextDocumentIdentifier
{
Uri = virtualSnapshot.Uri,
};
document = virtualSnapshot;
}
else
{
// Unable to synchronize
return null;
}
Uri = syncResult.VirtualSnapshot.Uri,
};
document = syncResult.VirtualSnapshot;
}
else if (kind == RazorLanguageKind.Html)
{
@ -742,20 +728,12 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
hostDocumentVersion,
hostDocumentUri,
cancellationToken);
if (syncResult.TryGetVirtualSnapshot(out var virtualSnapshot))
languageServerName = RazorLSPConstants.HtmlLanguageServerName;
presentationParams.TextDocument = new TextDocumentIdentifier
{
languageServerName = RazorLSPConstants.HtmlLanguageServerName;
presentationParams.TextDocument = new TextDocumentIdentifier
{
Uri = virtualSnapshot.Uri,
};
document = virtualSnapshot;
}
else
{
// Unable to synchronize
return null;
}
Uri = syncResult.VirtualSnapshot.Uri,
};
document = syncResult.VirtualSnapshot;
}
else
{
@ -782,41 +760,26 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
var hostDocumentUri = request.HostDocument.Uri;
string languageServerName;
Uri? projectedUri;
Uri projectedUri;
bool synchronized;
VirtualDocumentSnapshot virtualDocumentSnapshot;
if (request.ProjectedKind == RazorLanguageKind.Html)
{
var htmlResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
(synchronized, virtualDocumentSnapshot) = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
request.HostDocument.Version,
request.HostDocument.Uri,
cancellationToken);
if (htmlResult.TryGetVirtualSnapshot(out var htmlSnapshot))
{
languageServerName = RazorLSPConstants.HtmlLanguageServerName;
projectedUri = htmlSnapshot.Uri;
virtualDocumentSnapshot = htmlSnapshot;
}
else
{
return null;
}
languageServerName = RazorLSPConstants.HtmlLanguageServerName;
projectedUri = virtualDocumentSnapshot.Uri;
}
else if (request.ProjectedKind == RazorLanguageKind.CSharp)
{
var csharpResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<CSharpVirtualDocumentSnapshot>(
(synchronized, virtualDocumentSnapshot) = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<CSharpVirtualDocumentSnapshot>(
request.HostDocument.Version,
hostDocumentUri,
cancellationToken);
if (csharpResult.TryGetVirtualSnapshot(out var csharpSnapshot))
{
languageServerName = RazorLSPConstants.RazorCSharpLanguageServerName;
projectedUri = csharpSnapshot.Uri;
virtualDocumentSnapshot = csharpSnapshot;
}
else
{
return null;
}
languageServerName = RazorLSPConstants.RazorCSharpLanguageServerName;
projectedUri = virtualDocumentSnapshot.Uri;
}
else
{
@ -824,6 +787,11 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
return null;
}
if (!synchronized)
{
return null;
}
var completionParams = new CompletionParams()
{
Context = request.Context,
@ -933,34 +901,22 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
public override async Task<JToken?> ProvideResolvedCompletionItemAsync(DelegatedCompletionItemResolveParams request, CancellationToken cancellationToken)
{
string languageServerName;
VirtualDocumentSnapshot? virtualDocumentSnapshot;
bool synchronized;
VirtualDocumentSnapshot virtualDocumentSnapshot;
if (request.OriginatingKind == RazorLanguageKind.Html)
{
var htmlResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
(synchronized, virtualDocumentSnapshot) = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
request.HostDocument.Version,
request.HostDocument.Uri,
cancellationToken);
if (!htmlResult.TryGetVirtualSnapshot(out var htmlSnapshot))
{
return null;
}
virtualDocumentSnapshot = htmlSnapshot;
languageServerName = RazorLSPConstants.HtmlLanguageServerName;
}
else if (request.OriginatingKind == RazorLanguageKind.CSharp)
{
var csharpResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<CSharpVirtualDocumentSnapshot>(
(synchronized, virtualDocumentSnapshot) = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<CSharpVirtualDocumentSnapshot>(
request.HostDocument.Version,
request.HostDocument.Uri,
cancellationToken);
if (!csharpResult.TryGetVirtualSnapshot(out var csharpSnapshot))
{
return null;
}
virtualDocumentSnapshot = csharpSnapshot;
languageServerName = RazorLSPConstants.RazorCSharpLanguageServerName;
}
else
@ -969,6 +925,12 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
return null;
}
if (!synchronized)
{
// Document was not synchronized
return null;
}
var completionResolveParams = request.CompletionItem;
var textBuffer = virtualDocumentSnapshot.Snapshot.TextBuffer;
@ -1120,36 +1082,24 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
{
string languageServerName;
bool synchronized;
VirtualDocumentSnapshot virtualDocumentSnapshot;
if (request.ProjectedKind == RazorLanguageKind.Html)
{
var htmlResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
(synchronized, virtualDocumentSnapshot) = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(
request.HostDocument.Version,
request.HostDocument.Uri,
rejectOnNewerParallelRequest: false,
cancellationToken);
if (!htmlResult.TryGetVirtualSnapshot(out var htmlSnapshot))
{
return null;
}
virtualDocumentSnapshot = htmlSnapshot;
languageServerName = RazorLSPConstants.HtmlLanguageServerName;
}
else if (request.ProjectedKind == RazorLanguageKind.CSharp)
{
var csharpResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<CSharpVirtualDocumentSnapshot>(
(synchronized, virtualDocumentSnapshot) = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<CSharpVirtualDocumentSnapshot>(
request.HostDocument.Version,
request.HostDocument.Uri,
rejectOnNewerParallelRequest: false,
cancellationToken);
if(!csharpResult.TryGetVirtualSnapshot(out var csharpSnapshot))
{
return null;
}
virtualDocumentSnapshot = csharpSnapshot;
languageServerName = RazorLSPConstants.RazorCSharpLanguageServerName;
}
else
@ -1158,6 +1108,11 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor
return null;
}
if (!synchronized)
{
return null;
}
return new DelegationRequestDetails(languageServerName, virtualDocumentSnapshot.Uri, virtualDocumentSnapshot.Snapshot.TextBuffer);
}

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

@ -144,33 +144,26 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor.HtmlCSharp
}
else
{
bool synchronized;
if (languageResponse.Kind == RazorLanguageKind.CSharp)
{
var csharpResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<CSharpVirtualDocumentSnapshot>(documentSnapshot.Version, documentSnapshot.Uri, rejectOnNewerParallelRequest, cancellationToken).ConfigureAwait(false);
if (!csharpResult.TryGetVirtualSnapshot(out var csharpSnapshot))
{
_logHubLogger?.LogInformation("Could not synchronize.");
return null;
}
virtualDocument = csharpSnapshot;
(synchronized, virtualDocument) = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<CSharpVirtualDocumentSnapshot>(documentSnapshot.Version, documentSnapshot.Uri, rejectOnNewerParallelRequest, cancellationToken).ConfigureAwait(false);
}
else if (languageResponse.Kind == RazorLanguageKind.Html)
{
var htmlResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(documentSnapshot.Version, documentSnapshot.Uri, rejectOnNewerParallelRequest, cancellationToken).ConfigureAwait(false);
if (!htmlResult.TryGetVirtualSnapshot(out var htmlSnapshot))
{
_logHubLogger?.LogInformation("Could not synchronize.");
return null;
}
virtualDocument = htmlSnapshot;
(synchronized, virtualDocument) = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<HtmlVirtualDocumentSnapshot>(documentSnapshot.Version, documentSnapshot.Uri, rejectOnNewerParallelRequest, cancellationToken).ConfigureAwait(false);
}
else
{
_logHubLogger?.LogInformation("Could not find projection for {languageResponseKind:G}.", languageResponse.Kind);
return null;
}
if (!synchronized)
{
_logHubLogger?.LogInformation("Could not synchronize.");
return null;
}
}
var result = new ProjectionResult()

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

@ -97,11 +97,11 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor.HtmlCSharp
return clearedDiagnosticReport;
}
var csharpResult = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<CSharpVirtualDocumentSnapshot>(
var (synchronized, csharpDoc)= await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<CSharpVirtualDocumentSnapshot>(
documentSnapshot.Version,
request.TextDocument.Uri,
cancellationToken).ConfigureAwait(false);
if (!csharpResult.TryGetVirtualSnapshot(out var csharpDoc))
if (!synchronized)
{
_logger.LogInformation("Failed to synchronize document {hostDocument}.", request.TextDocument.Uri);

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

@ -36,6 +36,7 @@ public class RazorLanguageServerTest : TestBase
// We turn this into a Set to handle cases like Completion where we have two handlers, only one of which will be registered
// CLaSP will throw if two handlers register for the same method, so if THAT doesn't hold it's a CLaSP bug, not a Razor bug.
var typeMethods = handlerTypes.Select(t => GetMethodFromType(t)).ToHashSet();
if (registeredMethods.Length != typeMethods.Count)
{
var unregisteredHandlers = typeMethods.Where(t => !registeredMethods.Any(m => m.MethodName == t));

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

@ -14,7 +14,7 @@ namespace Microsoft.VisualStudio.Razor.IntegrationTests
// Open the file
await TestServices.SolutionExplorer.OpenFileAsync(RazorProjectConstants.BlazorProjectName, RazorProjectConstants.CounterRazorFile, ControlledHangMitigatingCancellationToken);
await TestServices.Editor.PlaceCaretAsync("IncrementCou", charsOffset: 1, ControlledHangMitigatingCancellationToken);
await TestServices.Editor.PlaceCaretAsync("IncrementCount", charsOffset: -1, ControlledHangMitigatingCancellationToken);
// Act
await TestServices.Editor.InvokeGoToImplementationAsync(ControlledHangMitigatingCancellationToken);
@ -31,7 +31,7 @@ namespace Microsoft.VisualStudio.Razor.IntegrationTests
// Change text to refer back to Program class
await TestServices.Editor.SetTextAsync(@"<SurveyPrompt Title=""@nameof(Program)", ControlledHangMitigatingCancellationToken);
await TestServices.Editor.PlaceCaretAsync("Progra", charsOffset: 1, ControlledHangMitigatingCancellationToken);
await TestServices.Editor.PlaceCaretAsync("Program", charsOffset: -1, ControlledHangMitigatingCancellationToken);
// Act
await TestServices.Editor.InvokeGoToImplementationAsync(ControlledHangMitigatingCancellationToken);

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

@ -1,160 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using Microsoft.VisualStudio.Threading;
using System;
using WindowsInput;
using System.Collections.Immutable;
using WindowsInput.Native;
namespace Microsoft.VisualStudio.Extensibility.Testing
{
internal struct InputKey
{
public readonly ImmutableArray<VirtualKeyCode> Modifiers;
public readonly VirtualKeyCode VirtualKeyCode;
public readonly char? Character;
public readonly string? Text;
public InputKey(VirtualKeyCode virtualKeyCode)
{
Modifiers = ImmutableArray<VirtualKeyCode>.Empty;
VirtualKeyCode = virtualKeyCode;
Character = null;
Text = null;
}
public InputKey(VirtualKeyCode virtualKeyCode, ImmutableArray<VirtualKeyCode> modifiers)
{
Modifiers = modifiers;
VirtualKeyCode = virtualKeyCode;
Character = null;
Text = null;
}
public InputKey(char character)
{
Modifiers = ImmutableArray<VirtualKeyCode>.Empty;
VirtualKeyCode = 0;
Character = character;
Text = null;
}
public InputKey(string text)
{
Modifiers = ImmutableArray<VirtualKeyCode>.Empty;
VirtualKeyCode = 0;
Character = null;
Text = text;
}
public static implicit operator InputKey(VirtualKeyCode virtualKeyCode)
=> new(virtualKeyCode);
public static implicit operator InputKey(char character)
=> new(character);
public static implicit operator InputKey(string text)
=> new(text);
public void Apply(IInputSimulator simulator)
{
if (Character is { } c)
{
if (c == '\n')
simulator.Keyboard.KeyPress(VirtualKeyCode.RETURN);
else if (c == '\t')
simulator.Keyboard.KeyPress(VirtualKeyCode.TAB);
else
simulator.Keyboard.TextEntry(c);
return;
}
else if (Text is not null)
{
var offset = 0;
while (offset < Text.Length)
{
if (Text[offset] == '\r' && offset < Text.Length - 1 && Text[offset + 1] == '\n')
{
// Treat \r\n as a single RETURN character
offset++;
continue;
}
else if (Text[offset] == '\n')
{
simulator.Keyboard.KeyPress(VirtualKeyCode.RETURN);
offset++;
continue;
}
else if (Text[offset] == '\t')
{
simulator.Keyboard.KeyPress(VirtualKeyCode.TAB);
offset++;
continue;
}
else
{
var nextSpecial = Text.IndexOfAny(new[] { '\r', '\n', '\t' }, offset);
var endOfCurrentSegment = nextSpecial < 0 ? Text.Length : nextSpecial;
simulator.Keyboard.TextEntry(Text[offset..endOfCurrentSegment]);
offset = endOfCurrentSegment;
}
}
return;
}
if (Modifiers.IsEmpty)
{
simulator.Keyboard.KeyPress(VirtualKeyCode);
}
else
{
simulator.Keyboard.ModifiedKeyStroke(Modifiers, VirtualKeyCode);
}
}
}
[TestService]
internal partial class InputInProcess
{
internal Task SendAsync(InputKey key, CancellationToken cancellationToken)
=> SendAsync(new InputKey[] { key }, cancellationToken);
internal Task SendAsync(InputKey[] keys, CancellationToken cancellationToken)
{
return SendAsync(
simulator =>
{
foreach (var key in keys)
{
key.Apply(simulator);
}
}, cancellationToken);
}
internal async Task SendAsync(Action<IInputSimulator> callback, CancellationToken cancellationToken)
{
// AbstractSendKeys runs synchronously, so switch to a background thread before the call
await TaskScheduler.Default;
TestServices.JoinableTaskFactory.Run(async () =>
{
await TestServices.Editor.ActivateAsync(cancellationToken);
});
callback(new InputSimulator());
TestServices.JoinableTaskFactory.Run(async () =>
{
await WaitForApplicationIdleAsync(cancellationToken);
});
}
internal void Send(string keys)
{
SendKeys.Send(keys);

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

@ -25,7 +25,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="InputSimulatorPlus" Version="$(InputSimulatorPlusVersion)" />
<PackageReference Include="Microsoft.VisualStudio.Editor" Version="$(MicrosoftVisualStudioEditorPackageVersion)" />
<PackageReference Include="Microsoft.VisualStudio.Extensibility.Testing.Xunit" Version="$(MicrosoftVisualStudioExtensibilityTestingXunitVersion)" />
<PackageReference Include="Microsoft.VisualStudio.Extensibility.Testing.SourceGenerator" Version="$(MicrosoftVisualStudioExtensibilityTestingSourceGeneratorVersion)" />

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

@ -116,8 +116,7 @@ A
// Act
await TestServices.Editor.PlaceCaretAsync("@onclick='thing'", charsOffset: 1, ControlledHangMitigatingCancellationToken);
await TestServices.Input.SendAsync(WindowsInput.Native.VirtualKeyCode.RETURN, ControlledHangMitigatingCancellationToken);
TestServices.Input.Send("{ENTER}");
// Assert
await TestServices.Editor.VerifyTextContainsAsync(@"
<button class='someclass' @onclick='thing'