Example #1
0
        public HtmlFormattingPass(
            RazorDocumentMappingService documentMappingService,
            FilePathNormalizer filePathNormalizer,
            ClientNotifierServiceBase server,
            ILoggerFactory loggerFactory)
            : base(documentMappingService, filePathNormalizer, server)
        {
            if (loggerFactory is null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            _logger = loggerFactory.CreateLogger <HtmlFormattingPass>();

            HtmlFormatter = new HtmlFormatter(server, filePathNormalizer);
        }
        public async override Task <FormattingResult> ExecuteAsync(FormattingContext context, FormattingResult result, CancellationToken cancellationToken)
        {
            if (context.IsFormatOnType)
            {
                // We don't want to handle OnTypeFormatting here.
                return(result);
            }

            var originalText = context.SourceText;

            var htmlEdits = await HtmlFormatter.FormatAsync(context, context.Range, cancellationToken);

            var normalizedEdits = NormalizeTextEdits(originalText, htmlEdits);
            var mappedEdits     = RemapTextEdits(context.CodeDocument, normalizedEdits, RazorLanguageKind.Html);
            var changes         = mappedEdits.Select(e => e.AsTextChange(originalText));

            var changedText    = originalText;
            var changedContext = context;

            if (changes.Any())
            {
                changedText = originalText.WithChanges(changes);
                // Create a new formatting context for the changed razor document.
                changedContext = await context.WithTextAsync(changedText);
            }

            var indentationChanges = AdjustRazorIndentation(changedContext);

            if (indentationChanges.Count > 0)
            {
                // Apply the edits that adjust indentation.
                changedText = changedText.WithChanges(indentationChanges);
            }

            var finalChanges = SourceTextDiffer.GetMinimalTextChanges(originalText, changedText, lineDiffOnly: false);
            var finalEdits   = finalChanges.Select(f => f.AsTextEdit(originalText)).ToArray();

            return(new FormattingResult(finalEdits));
        }
        public DefaultRazorFormattingService(
            RazorDocumentMappingService documentMappingService,
            FilePathNormalizer filePathNormalizer,
            ILanguageServer server,
            IOptionsMonitor <RazorLSPOptions> optionsMonitor,
            ILoggerFactory loggerFactory)
        {
            if (documentMappingService is null)
            {
                throw new ArgumentNullException(nameof(documentMappingService));
            }

            if (filePathNormalizer is null)
            {
                throw new ArgumentNullException(nameof(filePathNormalizer));
            }

            if (server is null)
            {
                throw new ArgumentNullException(nameof(server));
            }

            if (optionsMonitor is null)
            {
                throw new ArgumentNullException(nameof(optionsMonitor));
            }

            if (loggerFactory is null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            _server          = server;
            _csharpFormatter = new CSharpFormatter(documentMappingService, server, filePathNormalizer);
            _htmlFormatter   = new HtmlFormatter(server, filePathNormalizer);
            _optionsMonitor  = optionsMonitor;
            _logger          = loggerFactory.CreateLogger <DefaultRazorFormattingService>();
        }
        public FormattingPassBase(
            RazorDocumentMappingService documentMappingService,
            FilePathNormalizer filePathNormalizer,
            ILanguageServer server)
        {
            if (documentMappingService is null)
            {
                throw new ArgumentNullException(nameof(documentMappingService));
            }

            if (filePathNormalizer is null)
            {
                throw new ArgumentNullException(nameof(filePathNormalizer));
            }

            if (server is null)
            {
                throw new ArgumentNullException(nameof(server));
            }

            _documentMappingService = documentMappingService;
            CSharpFormatter         = new CSharpFormatter(documentMappingService, server, filePathNormalizer);
            HtmlFormatter           = new HtmlFormatter(server, filePathNormalizer);
        }