forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBicepDocumentFormattingHandler.cs
70 lines (59 loc) · 3.21 KB
/
BicepDocumentFormattingHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Bicep.Core.PrettyPrint;
using Bicep.Core.PrettyPrintV2;
using Bicep.LanguageServer.CompilationManager;
using Bicep.LanguageServer.Extensions;
using Bicep.LanguageServer.Utils;
using Microsoft.Extensions.Logging;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
namespace Bicep.LanguageServer.Handlers
{
public class BicepDocumentFormattingHandler(
ILogger<BicepDocumentSymbolHandler> logger,
ICompilationManager compilationManager) : DocumentFormattingHandlerBase
{
public override Task<TextEditContainer?> Handle(DocumentFormattingParams request, CancellationToken cancellationToken)
{
CompilationContext? context = compilationManager.GetCompilation(request.TextDocument.Uri);
if (context == null)
{
// we have not yet compiled this document, which shouldn't really happen
logger.LogError("Document formatting request arrived before file {Uri} could be compiled.", request.TextDocument.Uri);
return Task.FromResult<TextEditContainer?>(null);
}
var lexingErrorLookup = context.Compilation.SourceFileGrouping.EntryPoint.LexingErrorLookup;
var parsingErrorLookup = context.Compilation.SourceFileGrouping.EntryPoint.ParsingErrorLookup;
var printerOptions = context.Compilation.GetEntrypointSemanticModel().Configuration.Formatting.Data;
var featureProvider = context.Compilation.FeatureProviderFactory.GetFeatureProvider(request.TextDocument.Uri.ToUriEncoded());
if (featureProvider.LegacyFormatterEnabled)
{
var legacyOptions = PrettyPrintOptions.FromV2Options(printerOptions);
var legacyOutput = PrettyPrinter.PrintProgram(context.ProgramSyntax, legacyOptions, lexingErrorLookup, parsingErrorLookup);
if (legacyOutput is null)
{
return Task.FromResult<TextEditContainer?>(null);
}
return Task.FromResult<TextEditContainer?>(new TextEditContainer(new TextEdit
{
Range = context.ProgramSyntax.Span.ToRange(context.LineStarts),
NewText = legacyOutput,
}));
}
var printerContext = PrettyPrinterV2Context.Create(printerOptions, lexingErrorLookup, parsingErrorLookup);
var output = PrettyPrinterV2.Print(context.ProgramSyntax, printerContext);
return Task.FromResult<TextEditContainer?>(new TextEditContainer(new TextEdit
{
Range = context.ProgramSyntax.Span.ToRange(context.LineStarts),
NewText = output,
}));
}
protected override DocumentFormattingRegistrationOptions CreateRegistrationOptions(DocumentFormattingCapability capability, ClientCapabilities clientCapabilities) => new()
{
DocumentSelector = DocumentSelectorFactory.CreateForBicepAndParams()
};
}
}