private static Document GetCSharpDocument(RazorCodeDocument codeDocument, FormattingOptions options)
        {
            var adhocWorkspace = new AdhocWorkspace();
            var csharpOptions  = adhocWorkspace.Options
                                 .WithChangedOption(CodeAnalysis.Formatting.FormattingOptions.TabSize, LanguageNames.CSharp, (int)options.TabSize)
                                 .WithChangedOption(CodeAnalysis.Formatting.FormattingOptions.IndentationSize, LanguageNames.CSharp, (int)options.TabSize)
                                 .WithChangedOption(CodeAnalysis.Formatting.FormattingOptions.UseTabs, LanguageNames.CSharp, !options.InsertSpaces);

            adhocWorkspace.TryApplyChanges(adhocWorkspace.CurrentSolution.WithOptions(csharpOptions));

            var project          = adhocWorkspace.AddProject("TestProject", LanguageNames.CSharp);
            var csharpSourceText = codeDocument.GetCSharpSourceText();
            var csharpDocument   = adhocWorkspace.AddDocument(project.Id, "TestDocument", csharpSourceText);

            return(csharpDocument);
        }
Example #2
0
        public async Task <TextEdit[]> FormatAsync(
            RazorCodeDocument codeDocument,
            Range range,
            Uri uri,
            LSPFormattingOptions options)
        {
            var @params = new RazorDocumentRangeFormattingParams()
            {
                Kind                 = RazorLanguageKind.Html,
                ProjectedRange       = range,
                HostDocumentFilePath = _filePathNormalizer.Normalize(uri.GetAbsoluteOrUNCPath()),
                Options              = options
            };

            var result = await _server.Client.SendRequest <RazorDocumentRangeFormattingParams, RazorDocumentRangeFormattingResponse>(
                LanguageServerConstants.RazorRangeFormattingEndpoint, @params);

            return(result.Edits);
        }