public void Update_Delete() { // Arrange var textBuffer = new TestTextBuffer(new StringTextSnapshot("Hello World")); var delete = new VisualStudioTextChange(6, 5, string.Empty); var document = new CSharpVirtualDocument(Uri, textBuffer); // Act document.Update(new[] { delete }, hostDocumentVersion: 1); // Assert var text = textBuffer.CurrentSnapshot.GetText(); Assert.Equal("Hello ", text); }
public void Update_Replace() { // Arrange var textBuffer = new TestTextBuffer(new StringTextSnapshot("original")); var replace = new VisualStudioTextChange(0, textBuffer.CurrentSnapshot.Length, "replaced text"); var document = new CSharpVirtualDocument(Uri, textBuffer); // Act document.Update(new[] { replace }, hostDocumentVersion: 1); // Assert var text = textBuffer.CurrentSnapshot.GetText(); Assert.Equal(replace.NewText, text); }
public void Update_Insert() { // Arrange var insert = new VisualStudioTextChange(0, 0, "inserted text"); var textBuffer = new TestTextBuffer(StringTextSnapshot.Empty); var document = new CSharpVirtualDocument(Uri, textBuffer); // Act document.Update(new[] { insert }, hostDocumentVersion: 1); // Assert var text = textBuffer.CurrentSnapshot.GetText(); Assert.Equal(insert.NewText, text); }
public void Update_MultipleEdits() { // Arrange var textBuffer = new TestTextBuffer(new StringTextSnapshot("Hello World")); var replace = new VisualStudioTextChange(6, 5, "Replaced"); var delete = new VisualStudioTextChange(0, 6, string.Empty); var document = new HtmlVirtualDocument(Uri, textBuffer); // Act document.Update(new[] { replace, delete }, hostDocumentVersion: 1); // Assert var text = textBuffer.CurrentSnapshot.GetText(); Assert.Equal("Replaced", text); }
internal async Task <(bool, SumType <CompletionItem[], CompletionList>?)> TryGetProvisionalCompletionsAsync(CompletionParams request, LSPDocumentSnapshot documentSnapshot, ProjectionResult projection, CancellationToken cancellationToken) { SumType <CompletionItem[], CompletionList>?result = null; if (projection.LanguageKind != RazorLanguageKind.Html || request.Context.TriggerKind != CompletionTriggerKind.TriggerCharacter || request.Context.TriggerCharacter != ".") { return(false, result); } if (projection.Position.Character == 0) { // We're at the start of line. Can't have provisional completions here. return(false, result); } var previousCharacterPosition = new Position(projection.Position.Line, projection.Position.Character - 1); var previousCharacterProjection = await _projectionProvider.GetProjectionAsync(documentSnapshot, previousCharacterPosition, cancellationToken).ConfigureAwait(false); if (previousCharacterProjection == null || previousCharacterProjection.LanguageKind != RazorLanguageKind.CSharp) { return(false, result); } if (!(_documentManager is TrackingLSPDocumentManager trackingDocumentManager)) { return(false, result); } // Edit the CSharp projected document to contain a '.'. This allows C# completion to provide valid // completion items for moments when a user has typed a '.' that's typically interpreted as Html. var addProvisionalDot = new VisualStudioTextChange(previousCharacterProjection.PositionIndex, 0, "."); await _joinableTaskFactory.SwitchToMainThreadAsync(); trackingDocumentManager.UpdateVirtualDocument <CSharpVirtualDocument>(documentSnapshot.Uri, new[] { addProvisionalDot }, previousCharacterProjection.HostDocumentVersion); var provisionalCompletionParams = new CompletionParams() { Context = request.Context, Position = new Position(previousCharacterProjection.Position.Line, previousCharacterProjection.Position.Character + 1), TextDocument = new TextDocumentIdentifier() { Uri = previousCharacterProjection.Uri } }; result = await _requestInvoker.ReinvokeRequestOnServerAsync <CompletionParams, SumType <CompletionItem[], CompletionList>?>( Methods.TextDocumentCompletionName, RazorLSPConstants.CSharpContentTypeName, provisionalCompletionParams, cancellationToken).ConfigureAwait(true); // We have now obtained the necessary completion items. We no longer need the provisional change. Revert. var removeProvisionalDot = new VisualStudioTextChange(previousCharacterProjection.PositionIndex, 1, string.Empty); trackingDocumentManager.UpdateVirtualDocument <CSharpVirtualDocument>(documentSnapshot.Uri, new[] { removeProvisionalDot }, previousCharacterProjection.HostDocumentVersion); return(true, result); }