Beispiel #1
0
 public Task <TextEdit[]> GetTextDocumentFormattingOnTypeAsync(DocumentOnTypeFormattingParams documentOnTypeFormattingParams, CancellationToken cancellationToken)
 => _requestHandlerProvider.ExecuteRequestAsync <DocumentOnTypeFormattingParams, TextEdit[]>(_queue, Methods.TextDocumentOnTypeFormattingName,
                                                                                             documentOnTypeFormattingParams, _clientCapabilities, _clientName, cancellationToken);
Beispiel #2
0
 public Task <TextEdit[]> GetTextDocumentFormattingOnTypeAsync(DocumentOnTypeFormattingParams documentOnTypeFormattingParams, CancellationToken cancellationToken)
 => _protocol.ExecuteRequestAsync <DocumentOnTypeFormattingParams, TextEdit[]>(Methods.TextDocumentOnTypeFormattingName,
                                                                               documentOnTypeFormattingParams, _clientCapabilities, _clientName, cancellationToken);
Beispiel #3
0
        public async Task Handle_OnTypeFormatting_ReturnsValidResults()
        {
            // Arrange
            var content        = @"
@{
 if(true){}
}";
            var sourceMappings = new List <SourceMapping> {
                new SourceMapping(new SourceSpan(17, 0), new SourceSpan(17, 0))
            };
            var codeDocument           = CreateCodeDocument(content, sourceMappings);
            var uri                    = new Uri("file://path/test.razor");
            var documentResolver       = CreateDocumentResolver(uri.GetAbsoluteOrUNCPath(), codeDocument);
            var formattingService      = new TestRazorFormattingService();
            var documentMappingService = new DefaultRazorDocumentMappingService();
            var adhocWorkspaceFactory  = TestAdhocWorkspaceFactory.Instance;
            var optionsMonitor         = GetOptionsMonitor(enableFormatting: true);
            var endpoint               = new RazorFormattingEndpoint(
                LegacyDispatcher, documentResolver, formattingService, documentMappingService, adhocWorkspaceFactory, optionsMonitor, LoggerFactory);
            var @params = new DocumentOnTypeFormattingParams()
            {
                TextDocument = new TextDocumentIdentifier(uri),
                Character    = "}",
                Position     = new Position(2, 11),
                Options      = new FormattingOptions {
                    InsertSpaces = true, TabSize = 4
                }
            };

            // Act
            var result = await endpoint.Handle(@params, CancellationToken.None);

            // Assert
            Assert.NotNull(result);
            Assert.Collection(Iterate(result.GetEnumerator()),
                              edit => Assert.Equal(edit, new TextEdit {
                NewText = "   ", Range = new Range {
                    Start = new Position {
                        Line = 2, Character = 1
                    }, End = new Position {
                        Line = 2, Character = 1
                    }
                }
            }),
                              edit => Assert.Equal(edit, new TextEdit {
                NewText = " ", Range = new Range {
                    Start = new Position {
                        Line = 2, Character = 3
                    }, End = new Position {
                        Line = 2, Character = 3
                    }
                }
            }),
                              edit => Assert.Equal(edit, new TextEdit {
                NewText = " ", Range = new Range {
                    Start = new Position {
                        Line = 2, Character = 9
                    }, End = new Position {
                        Line = 2, Character = 9
                    }
                }
            }),
                              edit => Assert.Equal(edit, new TextEdit {
                NewText = " ", Range = new Range {
                    Start = new Position {
                        Line = 2, Character = 10
                    }, End = new Position {
                        Line = 2, Character = 10
                    }
                }
            }));
Beispiel #4
0
        public async Task HandleRequestAsync_InvokesServer_RemapsAndAppliesEdits()
        {
            // Arrange
            var documentManager = new TestDocumentManager();

            documentManager.AddDocument(Uri, Mock.Of <LSPDocumentSnapshot>(s => s.Uri == Uri && s.Snapshot == Mock.Of <ITextSnapshot>()));

            var invokedServer    = false;
            var mappedTextEdits  = false;
            var appliedTextEdits = false;
            var requestInvoker   = new Mock <LSPRequestInvoker>(MockBehavior.Strict);

            requestInvoker
            .Setup(r => r.RequestServerAsync <DocumentOnTypeFormattingParams, TextEdit[]>(Methods.TextDocumentOnTypeFormattingName, It.IsAny <LanguageServerKind>(), It.IsAny <DocumentOnTypeFormattingParams>(), It.IsAny <CancellationToken>()))
            .Callback <string, LanguageServerKind, DocumentOnTypeFormattingParams, CancellationToken>((method, serverKind, formattingParams, ct) =>
            {
                Assert.True(formattingParams.Options.OtherOptions.ContainsKey(LanguageServerConstants.ExpectsCursorPlaceholderKey));
                invokedServer = true;
            })
            .Returns(Task.FromResult <TextEdit[]>(new[] { new TextEdit()
                                                          {
                                                              Range = new Range(), NewText = "sometext"
                                                          } }));

            var projectionResult = new ProjectionResult()
            {
                LanguageKind = RazorLanguageKind.Html,
            };
            var projectionProvider = new Mock <LSPProjectionProvider>();

            projectionProvider.Setup(p => p.GetProjectionAsync(It.IsAny <LSPDocumentSnapshot>(), It.IsAny <Position>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(projectionResult));

            var documentMappingProvider = new Mock <LSPDocumentMappingProvider>(MockBehavior.Strict);

            documentMappingProvider
            .Setup(d => d.MapToDocumentRangeAsync(RazorLanguageKind.Html, Uri, It.IsAny <Range>(), It.IsAny <CancellationToken>()))
            .Callback(() => { mappedTextEdits = true; })
            .Returns(Task.FromResult(new RazorMapToDocumentRangeResponse()));

            var editorService = new Mock <LSPEditorService>(MockBehavior.Strict);

            editorService.Setup(e => e.ApplyTextEditsAsync(Uri, It.IsAny <ITextSnapshot>(), It.IsAny <IEnumerable <TextEdit> >())).Callback(() => { appliedTextEdits = true; })
            .Returns(Task.CompletedTask);

            var handler = new TestOnTypeFormattingHandler(JoinableTaskContext, documentManager, requestInvoker.Object, projectionProvider.Object, documentMappingProvider.Object, editorService.Object);
            var request = new DocumentOnTypeFormattingParams()
            {
                Character    = "=",
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                Options = new FormattingOptions()
                {
                    OtherOptions = new Dictionary <string, object>()
                },
                Position = new Position(1, 4)
            };

            // Act
            var edits = await handler.HandleRequestAsync(request, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.True(invokedServer);
            Assert.True(mappedTextEdits);
            Assert.True(appliedTextEdits);
            Assert.Empty(edits);
        }
 public override Task <TextEditContainer> Handle(DocumentOnTypeFormattingParams request, CancellationToken cancellationToken) => _handler.Invoke(request, cancellationToken);
 public abstract Task <TextEditContainer> Handle(DocumentOnTypeFormattingParams request, CancellationToken cancellationToken);
Beispiel #7
0
        public Task <TextEdit[]?> GetTextDocumentFormattingOnTypeAsync(DocumentOnTypeFormattingParams documentOnTypeFormattingParams, CancellationToken cancellationToken)
        {
            Contract.ThrowIfNull(_clientCapabilities, $"{nameof(InitializeAsync)} has not been called.");

            return(RequestDispatcher.ExecuteRequestAsync <DocumentOnTypeFormattingParams, TextEdit[]>(Queue, Methods.TextDocumentOnTypeFormattingName, documentOnTypeFormattingParams, _clientCapabilities, ClientName, cancellationToken));
        }
 public static Task <TextEditContainer> DocumentOnTypeFormat(this ILanguageClientDocument mediator, DocumentOnTypeFormattingParams @params, CancellationToken cancellationToken = default)
 {
     return(mediator.SendRequest(@params, cancellationToken));
 }
        public async Task HandleRequestAsync_InvokesCSharpLanguageServer_RemapsResults()
        {
            // Arrange
            var invokedCSharpServer = false;
            var remapped            = false;
            var expectedEdit        = new TextEdit();
            var remappedEdit        = new TextEdit();
            var documentManager     = new TestDocumentManager();
            var snapshot            = new StringTextSnapshot(@"
@code {
public string _foo;
}");

            documentManager.AddDocument(Uri, Mock.Of <LSPDocumentSnapshot>(m => m.Snapshot == snapshot, MockBehavior.Strict));
            var requestInvoker = new Mock <LSPRequestInvoker>(MockBehavior.Strict);

            requestInvoker
            .Setup(r => r.ReinvokeRequestOnServerAsync <DocumentOnTypeFormattingParams, TextEdit[]>(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <DocumentOnTypeFormattingParams>(), It.IsAny <CancellationToken>()))
            .Callback <string, string, DocumentOnTypeFormattingParams, CancellationToken>((method, serverContentType, onTypeFormattingParams, ct) =>
            {
                Assert.Equal(Methods.TextDocumentOnTypeFormattingName, method);
                Assert.Equal(RazorLSPConstants.CSharpContentTypeName, serverContentType);
                invokedCSharpServer = true;
            })
            .Returns(Task.FromResult(new[] { expectedEdit }));

            var projectionResult = new ProjectionResult()
            {
                LanguageKind = RazorLanguageKind.CSharp,
            };
            var projectionProvider = new Mock <LSPProjectionProvider>(MockBehavior.Strict);

            projectionProvider.Setup(p => p.GetProjectionAsync(It.IsAny <LSPDocumentSnapshot>(), It.IsAny <Position>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(projectionResult));
            var mappingProvider = new Mock <LSPDocumentMappingProvider>(MockBehavior.Strict);

            mappingProvider
            .Setup(m => m.RemapFormattedTextEditsAsync(It.IsAny <Uri>(), It.IsAny <TextEdit[]>(), It.IsAny <FormattingOptions>(), It.IsAny <bool>(), It.IsAny <CancellationToken>()))
            .Callback(() => { remapped = true; })
            .Returns(Task.FromResult(new[] { remappedEdit }));

            var formatOnTypeHandler = new OnTypeFormattingHandler(documentManager, requestInvoker.Object, projectionProvider.Object, mappingProvider.Object, LoggerProvider);
            var formattingRequest   = new DocumentOnTypeFormattingParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                Position  = new Position(2, 19),
                Character = ";",
                Options   = new FormattingOptions()
            };

            // Act
            var result = await formatOnTypeHandler.HandleRequestAsync(formattingRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.True(invokedCSharpServer);
            Assert.True(remapped);
            var edit = Assert.Single(result);

            Assert.Same(remappedEdit, edit);
        }