public async Task GetNavigatePositionAsync_TagHelperProperty_CorrectRange2() { // Arrange var content = @" <div>@Title</div> @code { [Microsoft.AspNetCore.Components.Parameter] public string [|Title|] { get; set; } } "; TestFileMarkupParser.GetSpan(content, out content, out var selection); SetupDocument(out var codeDocument, out _, content); var expectedRange = selection.AsRange(codeDocument.GetSourceText()); var mappingService = new DefaultRazorDocumentMappingService(LoggerFactory); // Act II var range = await RazorDefinitionEndpoint.TryGetPropertyRangeAsync(codeDocument, "Title", mappingService, Logger, CancellationToken.None).ConfigureAwait(false); Assert.NotNull(range); Assert.Equal(expectedRange, range); }
public async Task GetNavigatePositionAsync_TagHelperProperty_IgnoreInnerProperty() { // Arrange var content = @" <div>@Title</div> @code { private class NotTheDroidsYoureLookingFor { public string Title { get; set; } } public string [|Title|] { get; set; } } "; TestFileMarkupParser.GetSpan(content, out content, out var selection); SetupDocument(out var codeDocument, out _, content); var expectedRange = selection.AsRange(codeDocument.GetSourceText()); var mappingService = new DefaultRazorDocumentMappingService(LoggerFactory); // Act II var range = await RazorDefinitionEndpoint.TryGetPropertyRangeAsync(codeDocument, "Title", mappingService, Logger, CancellationToken.None).ConfigureAwait(false); Assert.NotNull(range); Assert.Equal(expectedRange, range); }
public async Task Handle_UnsupportedCodeDocument_ReturnsNull() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); codeDocument.SetUnsupported(); var uri = new Uri("file://path/test.razor"); var documentResolver = CreateDocumentResolver(uri.AbsolutePath, 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 DocumentRangeFormattingParams() { TextDocument = new TextDocumentIdentifier(uri) }; // Act var result = await endpoint.Handle(@params, CancellationToken.None); // Assert Assert.Null(result); }
private (RazorLanguageKind, TextEdit[]) GetFormattedEdits(RazorCodeDocument codeDocument, string expected, int positionBeforeTriggerChar) { var mappingService = new DefaultRazorDocumentMappingService(); var languageKind = mappingService.GetLanguageKind(codeDocument, positionBeforeTriggerChar); var expectedText = SourceText.From(expected); var(expectedCodeDocument, _) = CreateCodeDocumentAndSnapshot(expectedText, codeDocument.Source.FilePath, fileKind: codeDocument.GetFileKind()); var edits = Array.Empty <TextEdit>(); if (languageKind == RazorLanguageKind.CSharp) { var beforeCSharpText = SourceText.From(codeDocument.GetCSharpDocument().GeneratedCode); var afterCSharpText = SourceText.From(expectedCodeDocument.GetCSharpDocument().GeneratedCode); edits = SourceTextDiffer.GetMinimalTextChanges(beforeCSharpText, afterCSharpText, lineDiffOnly: false).Select(c => c.AsTextEdit(beforeCSharpText)).ToArray(); } else if (languageKind == RazorLanguageKind.Html) { var beforeHtmlText = SourceText.From(codeDocument.GetHtmlDocument().GeneratedHtml); var afterHtmlText = SourceText.From(expectedCodeDocument.GetHtmlDocument().GeneratedHtml); edits = SourceTextDiffer.GetMinimalTextChanges(beforeHtmlText, afterHtmlText, lineDiffOnly: false).Select(c => c.AsTextEdit(beforeHtmlText)).ToArray(); } return(languageKind, edits); }
public async Task Handle_OnTypeFormatting_UnexpectedTriggerCharacter_ReturnsNull() { // 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(LoggerFactory); var optionsMonitor = GetOptionsMonitor(enableFormatting: true); var endpoint = new RazorFormattingEndpoint( Dispatcher, documentResolver, formattingService, documentMappingService, 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.Null(result); }
private OnTypeFormattingStructureValidationPass GetPass(RazorCodeDocument codeDocument) { var mappingService = new DefaultRazorDocumentMappingService(); var client = Mock.Of <IClientLanguageServer>(); var pass = new OnTypeFormattingStructureValidationPass(mappingService, FilePathNormalizer, client, LoggerFactory); return(pass); }
private FormattingContentValidationPass GetPass(RazorCodeDocument codeDocument) { var mappingService = new DefaultRazorDocumentMappingService(); var client = Mock.Of <IClientLanguageServer>(); var pass = new FormattingContentValidationPass(mappingService, FilePathNormalizer, client, LoggerFactory); pass.DebugAssertsEnabled = false; return(pass); }
private FormattingDiagnosticValidationPass GetPass(RazorCodeDocument codeDocument) { var mappingService = new DefaultRazorDocumentMappingService(); var client = Mock.Of <ClientNotifierServiceBase>(); var pass = new FormattingDiagnosticValidationPass(mappingService, FilePathNormalizer, client, LoggerFactory); pass.DebugAssertsEnabled = false; return(pass); }
protected async Task RunCodeActionFormattingTestAsync( string input, TextEdit[] codeActionEdits, string expected, int tabSize = 4, bool insertSpaces = true, string?fileKind = null) { if (codeActionEdits is null) { throw new NotImplementedException("Code action formatting must provide edits."); } // Arrange fileKind ??= FileKinds.Component; TestFileMarkupParser.GetPosition(input, out input, out var positionAfterTrigger); var razorSourceText = SourceText.From(input); var path = "file:///path/to/Document.razor"; var uri = new Uri(path); var(codeDocument, documentSnapshot) = CreateCodeDocumentAndSnapshot(razorSourceText, uri.AbsolutePath, fileKind: fileKind); #pragma warning disable CS0618 // Type or member is obsolete var mappingService = new DefaultRazorDocumentMappingService(); #pragma warning restore CS0618 // Type or member is obsolete var languageKind = mappingService.GetLanguageKind(codeDocument, positionAfterTrigger); if (languageKind == RazorLanguageKind.Html) { throw new NotImplementedException("Code action formatting is not yet supported for HTML in Razor."); } if (!mappingService.TryMapToProjectedDocumentPosition(codeDocument, positionAfterTrigger, out _, out var _)) { throw new InvalidOperationException("Could not map from Razor document to generated document"); } var formattingService = CreateFormattingService(codeDocument); var options = new FormattingOptions() { TabSize = tabSize, InsertSpaces = insertSpaces, }; // Act var edits = await formattingService.FormatCodeActionAsync(uri, documentSnapshot, languageKind, codeActionEdits, options, CancellationToken.None); // Assert var edited = ApplyEdits(razorSourceText, edits); var actual = edited.ToString(); new XUnitVerifier().EqualOrDiff(expected, actual); }
private FormattingContentValidationPass GetPass() { var mappingService = new DefaultRazorDocumentMappingService(LoggerFactory); var client = Mock.Of <ClientNotifierServiceBase>(MockBehavior.Strict); var pass = new FormattingContentValidationPass(mappingService, FilePathNormalizer, client, LoggerFactory) { DebugAssertsEnabled = false }; return(pass); }
private FormattingStructureValidationPass GetPass(RazorCodeDocument codeDocument) { var mappingService = new DefaultRazorDocumentMappingService(); var client = new FormattingLanguageServerClient(); client.AddCodeDocument(codeDocument); var languageServer = Mock.Of <ILanguageServer>(ls => ls.Client == client); var pass = new FormattingStructureValidationPass(mappingService, FilePathNormalizer, languageServer, LoggerFactory); return(pass); }
private RazorFormattingService CreateFormattingService(RazorCodeDocument codeDocument) { var mappingService = new DefaultRazorDocumentMappingService(); var filePathNormalizer = new FilePathNormalizer(); var client = new FormattingLanguageServerClient(); client.AddCodeDocument(codeDocument); var languageServer = Mock.Of <ILanguageServer>(ls => ls.Client == client); return(new DefaultRazorFormattingService(mappingService, filePathNormalizer, languageServer, LoggerFactory)); }
protected async Task <(SourceText, TextEdit[])> GetOnTypeFormattingEditsAsync( string input, char triggerCharacter, int tabSize = 4, bool insertSpaces = true, string fileKind = null) { // Arrange fileKind ??= FileKinds.Component; TestFileMarkupParser.GetPosition(input, out input, out var positionAfterTrigger); var razorSourceText = SourceText.From(input); var path = "file:///path/to/Document.razor"; var uri = new Uri(path); var(codeDocument, documentSnapshot) = CreateCodeDocumentAndSnapshot(razorSourceText, uri.AbsolutePath, fileKind: fileKind); var mappingService = new DefaultRazorDocumentMappingService(); var languageKind = mappingService.GetLanguageKind(codeDocument, positionAfterTrigger); if (!mappingService.TryMapToProjectedDocumentPosition(codeDocument, positionAfterTrigger, out _, out var projectedIndex)) { throw new InvalidOperationException("Could not map from Razor document to generated document"); } var projectedEdits = Array.Empty <TextEdit>(); if (languageKind == RazorLanguageKind.CSharp) { projectedEdits = await GetFormattedCSharpEditsAsync( codeDocument, triggerCharacter, projectedIndex, insertSpaces, tabSize).ConfigureAwait(false); } else if (languageKind == RazorLanguageKind.Html) { throw new NotImplementedException("OnTypeFormatting is not yet supported for HTML in Razor."); } var formattingService = CreateFormattingService(codeDocument); var options = new FormattingOptions() { TabSize = tabSize, InsertSpaces = insertSpaces, }; // Act var edits = await formattingService.ApplyFormattedEditsAsync( uri, documentSnapshot, languageKind, projectedEdits, options, CancellationToken.None); return(razorSourceText, edits); }
public async Task Handle_FormattingDisabled_ReturnsNull() { // Arrange var formattingService = new TestRazorFormattingService(); var documentMappingService = new DefaultRazorDocumentMappingService(LoggerFactory); var optionsMonitor = GetOptionsMonitor(enableFormatting: false); var endpoint = new RazorFormattingEndpoint( Dispatcher, EmptyDocumentResolver, formattingService, documentMappingService, optionsMonitor, LoggerFactory); var @params = new DocumentRangeFormattingParams(); // Act var result = await endpoint.Handle(@params, CancellationToken.None); // Assert Assert.Null(result); }
private RazorFormattingService CreateFormattingService(RazorCodeDocument codeDocument) { var mappingService = new DefaultRazorDocumentMappingService(); var filePathNormalizer = new FilePathNormalizer(); var client = new FormattingLanguageServerClient(); client.AddCodeDocument(codeDocument); var languageServer = Mock.Of <ILanguageServer>(ls => ls.Client == client); var optionsMonitor = new Mock <IOptionsMonitor <RazorLSPOptions> >(); optionsMonitor.SetupGet(o => o.CurrentValue).Returns(RazorLSPOptions.Default); return(new DefaultRazorFormattingService(mappingService, filePathNormalizer, languageServer, optionsMonitor.Object, LoggerFactory)); }
private RazorFormattingService CreateFormattingService(RazorCodeDocument codeDocument) { var mappingService = new DefaultRazorDocumentMappingService(); var client = new FormattingLanguageServerClient(); client.AddCodeDocument(codeDocument); var languageServer = Mock.Of <ILanguageServer>(ls => ls.Client == client); var passes = new List <IFormattingPass>() { new CodeBlockDirectiveFormattingPass(mappingService, FilePathNormalizer, languageServer, LoggerFactory), new CSharpOnTypeFormattingPass(mappingService, FilePathNormalizer, languageServer, LoggerFactory), new FormattingStructureValidationPass(mappingService, FilePathNormalizer, languageServer, LoggerFactory), new FormattingContentValidationPass(mappingService, FilePathNormalizer, languageServer, LoggerFactory), }; return(new DefaultRazorFormattingService(passes, LoggerFactory)); }
private RazorFormattingService CreateFormattingService(RazorCodeDocument codeDocument) { var mappingService = new DefaultRazorDocumentMappingService(); var client = new FormattingLanguageServerClient(TestProjectPath, FileName); client.AddCodeDocument(codeDocument); var passes = new List <IFormattingPass>() { new HtmlFormattingPass(mappingService, FilePathNormalizer, client, LoggerFactory), new CSharpFormattingPass(mappingService, FilePathNormalizer, client, LoggerFactory), new CSharpOnTypeFormattingPass(mappingService, FilePathNormalizer, client, LoggerFactory), new FormattingDiagnosticValidationPass(mappingService, FilePathNormalizer, client, LoggerFactory), new FormattingContentValidationPass(mappingService, FilePathNormalizer, client, LoggerFactory), }; return(new DefaultRazorFormattingService(passes, LoggerFactory)); }
protected async Task RunOnTypeFormattingTestAsync( string input, string expected, char triggerCharacter, int tabSize = 4, bool insertSpaces = true, string?fileKind = null) { // Arrange fileKind ??= FileKinds.Component; TestFileMarkupParser.GetPosition(input, out input, out var positionAfterTrigger); var razorSourceText = SourceText.From(input); var path = "file:///path/to/Document.razor"; var uri = new Uri(path); var(codeDocument, documentSnapshot) = CreateCodeDocumentAndSnapshot(razorSourceText, uri.AbsolutePath, fileKind: fileKind); var mappingService = new DefaultRazorDocumentMappingService(LoggerFactory); var languageKind = mappingService.GetLanguageKind(codeDocument, positionAfterTrigger); var formattingService = CreateFormattingService(codeDocument); var options = new FormattingOptions() { TabSize = tabSize, InsertSpaces = insertSpaces, }; // Act var edits = await formattingService.FormatOnTypeAsync(uri, documentSnapshot, languageKind, Array.Empty <TextEdit>(), options, hostDocumentIndex : positionAfterTrigger, triggerCharacter : triggerCharacter, CancellationToken.None); // Assert var edited = ApplyEdits(razorSourceText, edits); var actual = edited.ToString(); new XUnitVerifier().EqualOrDiff(expected, actual); if (input.Equals(expected)) { Assert.Empty(edits); } }
public async Task Handle_DocumentNotFound_ReturnsNull() { // Arrange var formattingService = new TestRazorFormattingService(); var documentMappingService = new DefaultRazorDocumentMappingService(LoggerFactory); var optionsMonitor = GetOptionsMonitor(enableFormatting: true); var endpoint = new RazorFormattingEndpoint( Dispatcher, EmptyDocumentResolver, formattingService, documentMappingService, optionsMonitor, LoggerFactory); var uri = new Uri("file://path/test.razor"); var @params = new DocumentRangeFormattingParams() { TextDocument = new TextDocumentIdentifier(uri) }; // Act var result = await endpoint.Handle(@params, CancellationToken.None); // Assert Assert.Null(result); }
public async Task Handle_OnTypeFormatting_FormattingDisabled_ReturnsNull() { // Arrange var uri = new Uri("file://path/test.razor"); var formattingService = new TestRazorFormattingService(); var documentMappingService = new DefaultRazorDocumentMappingService(); var adhocWorkspaceFactory = TestAdhocWorkspaceFactory.Instance; var optionsMonitor = GetOptionsMonitor(enableFormatting: false); var endpoint = new RazorFormattingEndpoint( LegacyDispatcher, EmptyDocumentResolver, formattingService, documentMappingService, adhocWorkspaceFactory, optionsMonitor, LoggerFactory); var @params = new DocumentOnTypeFormattingParams() { TextDocument = new TextDocumentIdentifier(uri) }; // Act var result = await endpoint.Handle(@params, CancellationToken.None); // Assert Assert.Null(result); }
private RazorFormattingService CreateFormattingService(RazorCodeDocument codeDocument) { var mappingService = new DefaultRazorDocumentMappingService(LoggerFactory); var dispatcher = new LSPProjectSnapshotManagerDispatcher(LoggerFactory); var versionCache = new DefaultDocumentVersionCache(dispatcher); var client = new FormattingLanguageServerClient(); client.AddCodeDocument(codeDocument); var passes = new List <IFormattingPass>() { new HtmlFormattingPass(mappingService, FilePathNormalizer, client, versionCache, LoggerFactory), new CSharpFormattingPass(mappingService, FilePathNormalizer, client, LoggerFactory), new CSharpOnTypeFormattingPass(mappingService, FilePathNormalizer, client, LoggerFactory), new RazorFormattingPass(mappingService, FilePathNormalizer, client, LoggerFactory), new FormattingDiagnosticValidationPass(mappingService, FilePathNormalizer, client, LoggerFactory), new FormattingContentValidationPass(mappingService, FilePathNormalizer, client, LoggerFactory), }; return(new DefaultRazorFormattingService(passes, LoggerFactory, TestAdhocWorkspaceFactory.Instance)); }
private RazorSemanticTokensInfoService GetDefaultRazorSemanticTokenInfoService( Queue <DocumentSnapshot> documentSnapshots, ProvideSemanticTokensResponse?csharpTokens = null, int?documentVersion = 0) { var responseRouterReturns = new Mock <IResponseRouterReturns>(MockBehavior.Strict); responseRouterReturns .Setup(l => l.Returning <ProvideSemanticTokensResponse?>(It.IsAny <CancellationToken>())) .Returns(Task.FromResult(csharpTokens)); var languageServer = new Mock <ClientNotifierServiceBase>(MockBehavior.Strict); languageServer .Setup(l => l.SendRequestAsync(LanguageServerConstants.RazorProvideSemanticTokensRangeEndpoint, It.IsAny <OSharp.SemanticTokensParams>())) .Returns(Task.FromResult(responseRouterReturns.Object)); var documentMappingService = new DefaultRazorDocumentMappingService(TestLoggerFactory.Instance); var loggingFactory = TestLoggerFactory.Instance; var projectSnapshotManagerDispatcher = Dispatcher; var documentResolver = new TestDocumentResolver(documentSnapshots); var documentVersionCache = new Mock <DocumentVersionCache>(MockBehavior.Strict); documentVersionCache.Setup(c => c.TryGetDocumentVersion(It.IsAny <DocumentSnapshot>(), out documentVersion)) .Returns(true); var testClient = new TestClient(); var errorReporter = new LanguageServerErrorReporter(loggingFactory); var semanticTokensRefreshPublisher = new DefaultWorkspaceSemanticTokensRefreshPublisher(testClient, errorReporter); return(new DefaultRazorSemanticTokensInfoService( languageServer.Object, documentMappingService, projectSnapshotManagerDispatcher, documentResolver, documentVersionCache.Object, loggingFactory)); }
public async Task Handle_FormattingEnabled_InvokesFormattingService() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var uri = new Uri("file://path/test.razor"); var documentResolver = CreateDocumentResolver(uri.GetAbsoluteOrUNCPath(), codeDocument); var formattingService = new TestRazorFormattingService(); var documentMappingService = new DefaultRazorDocumentMappingService(LoggerFactory); var optionsMonitor = GetOptionsMonitor(enableFormatting: true); var endpoint = new RazorFormattingEndpoint( Dispatcher, documentResolver, formattingService, documentMappingService, optionsMonitor, LoggerFactory); var @params = new DocumentRangeFormattingParams() { TextDocument = new TextDocumentIdentifier(uri) }; // Act var result = await endpoint.Handle(@params, CancellationToken.None); // Assert Assert.NotNull(result); Assert.True(formattingService.Called); }
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 } } }));
public RazorProximityExpressionsEndpointTest() { MappingService = new DefaultRazorDocumentMappingService(LoggerFactory); }
public RazorBreakpointSpanEndpointTest() { MappingService = new DefaultRazorDocumentMappingService(LoggerFactory); }