Exemple #1
0
        public async Task <FormattingContext> WithTextAsync(SourceText changedText)
        {
            if (changedText is null)
            {
                throw new ArgumentNullException(nameof(changedText));
            }

            var engine        = OriginalSnapshot.Project.GetProjectEngine();
            var importSources = new List <RazorSourceDocument>();

            var imports = OriginalSnapshot.GetImports();

            foreach (var import in imports)
            {
                var sourceText = await import.GetTextAsync();

                var source = sourceText.GetRazorSourceDocument(import.FilePath, import.TargetPath);
                importSources.Add(source);
            }

            var changedSourceDocument = changedText.GetRazorSourceDocument(OriginalSnapshot.FilePath, OriginalSnapshot.TargetPath);

            var codeDocument = engine.ProcessDesignTime(changedSourceDocument, OriginalSnapshot.FileKind, importSources, OriginalSnapshot.Project.TagHelpers);

            ValidateComponents(CodeDocument, codeDocument);

            var newContext = Create(Uri, OriginalSnapshot, codeDocument, Options, _workspaceFactory, IsFormatOnType);

            return(newContext);
        }
Exemple #2
0
        public async Task <FormattingContext> WithTextAsync(SourceText changedText)
        {
            if (changedText is null)
            {
                throw new ArgumentNullException(nameof(changedText));
            }

            if (_engine is null)
            {
                await InitializeProjectEngineAsync().ConfigureAwait(false);
            }

            var changedSourceDocument = changedText.GetRazorSourceDocument(OriginalSnapshot.FilePath, OriginalSnapshot.TargetPath);

            var codeDocument = _engine !.ProcessDesignTime(changedSourceDocument, OriginalSnapshot.FileKind, _importSources, OriginalSnapshot.Project.TagHelpers);

            DEBUG_ValidateComponents(CodeDocument, codeDocument);

            var newContext = new FormattingContext(
                _engine,
                _importSources !,
                _workspaceFactory,
                Uri,
                OriginalSnapshot,
                codeDocument,
                Options,
                IsFormatOnType,
                AutomaticallyAddUsings,
                HostDocumentIndex,
                TriggerCharacter);

            return(newContext);
        }
        private static (RazorCodeDocument, DocumentSnapshot) CreateCodeDocumentAndSnapshot(SourceText text, string path, IReadOnlyList <TagHelperDescriptor>?tagHelpers = null, string?fileKind = default, bool allowDiagnostics = false)
        {
            fileKind ??= FileKinds.Component;
            tagHelpers ??= Array.Empty <TagHelperDescriptor>();
            if (fileKind == FileKinds.Component)
            {
                tagHelpers = tagHelpers.Concat(s_defaultComponents).ToArray();
            }

            var sourceDocument = text.GetRazorSourceDocument(path, path);

            // Yes I know "BlazorServer_31 is weird, but thats what is in the taghelpers.json file
            const string DefaultImports = @"
@using BlazorServer_31
@using BlazorServer_31.Pages
@using BlazorServer_31.Shared
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
";

            var importsPath       = new Uri("file:///path/to/_Imports.razor").AbsolutePath;
            var importsSourceText = SourceText.From(DefaultImports);
            var importsDocument   = importsSourceText.GetRazorSourceDocument(importsPath, importsPath);
            var importsSnapshot   = new Mock <DocumentSnapshot>(MockBehavior.Strict);

            importsSnapshot.Setup(d => d.GetTextAsync()).Returns(Task.FromResult(importsSourceText));
            importsSnapshot.Setup(d => d.FilePath).Returns(importsPath);
            importsSnapshot.Setup(d => d.TargetPath).Returns(importsPath);

            var projectEngine = RazorProjectEngine.Create(builder =>
            {
                builder.SetRootNamespace("Test");
                builder.Features.Add(new DefaultTypeNameFeature());
                RazorExtensions.Register(builder);
            });
            var codeDocument = projectEngine.ProcessDesignTime(sourceDocument, fileKind, new[] { importsDocument }, tagHelpers);

            if (!allowDiagnostics)
            {
                Assert.False(codeDocument.GetCSharpDocument().Diagnostics.Any(), "Error creating document:" + Environment.NewLine + string.Join(Environment.NewLine, codeDocument.GetCSharpDocument().Diagnostics));
            }

            var documentSnapshot = new Mock <DocumentSnapshot>(MockBehavior.Strict);

            documentSnapshot.Setup(d => d.GetGeneratedOutputAsync()).Returns(Task.FromResult(codeDocument));
            documentSnapshot.Setup(d => d.GetImports()).Returns(new[] { importsSnapshot.Object });
            documentSnapshot.Setup(d => d.Project.GetProjectEngine()).Returns(projectEngine);
            documentSnapshot.Setup(d => d.FilePath).Returns(path);
            documentSnapshot.Setup(d => d.TargetPath).Returns(path);
            documentSnapshot.Setup(d => d.Project.TagHelpers).Returns(tagHelpers);
            documentSnapshot.Setup(d => d.FileKind).Returns(fileKind);

            return(codeDocument, documentSnapshot.Object);
        }
        private static RazorCodeDocument CreateCodeDocument(SourceText text, string path, IReadOnlyList <TagHelperDescriptor> tagHelpers = null, string fileKind = default)
        {
            fileKind ??= FileKinds.Component;
            tagHelpers ??= Array.Empty <TagHelperDescriptor>();
            var sourceDocument = text.GetRazorSourceDocument(path, path);
            var projectEngine  = RazorProjectEngine.Create(builder => { });
            var codeDocument   = projectEngine.ProcessDesignTime(sourceDocument, fileKind, Array.Empty <RazorSourceDocument>(), tagHelpers);

            return(codeDocument);
        }
Exemple #5
0
        private static (RazorCodeDocument, DocumentSnapshot) CreateCodeDocumentAndSnapshot(SourceText text, string path, IReadOnlyList <TagHelperDescriptor> tagHelpers = null, string fileKind = default)
        {
            fileKind ??= FileKinds.Component;
            tagHelpers ??= Array.Empty <TagHelperDescriptor>();
            var sourceDocument = text.GetRazorSourceDocument(path, path);
            var projectEngine  = RazorProjectEngine.Create(builder => { builder.SetRootNamespace("Test"); });
            var codeDocument   = projectEngine.ProcessDesignTime(sourceDocument, fileKind, Array.Empty <RazorSourceDocument>(), tagHelpers);

            var documentSnapshot = new Mock <DocumentSnapshot>();

            documentSnapshot.Setup(d => d.GetGeneratedOutputAsync()).Returns(Task.FromResult(codeDocument));
            documentSnapshot.Setup(d => d.Project.GetProjectEngine()).Returns(projectEngine);
            documentSnapshot.Setup(d => d.TargetPath).Returns(path);
            documentSnapshot.Setup(d => d.Project.TagHelpers).Returns(tagHelpers);
            documentSnapshot.Setup(d => d.FileKind).Returns(fileKind);

            return(codeDocument, documentSnapshot.Object);
        }