forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BicepCompletionHandler.cs
74 lines (63 loc) · 3.47 KB
/
BicepCompletionHandler.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
71
72
73
74
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Bicep.Core.Features;
using Bicep.LanguageServer.CompilationManager;
using Bicep.LanguageServer.Completions;
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 BicepCompletionHandler : CompletionHandlerBase
{
private readonly ILogger<BicepCompletionHandler> logger;
private readonly ICompilationManager compilationManager;
private readonly ICompletionProvider completionProvider;
private readonly IFeatureProviderFactory featureProviderFactory;
private readonly DocumentSelectorFactory documentSelectorFactory;
public BicepCompletionHandler(ILogger<BicepCompletionHandler> logger, ICompilationManager compilationManager, ICompletionProvider completionProvider, IFeatureProviderFactory featureProviderFactory, DocumentSelectorFactory documentSelectorFactory)
{
this.logger = logger;
this.compilationManager = compilationManager;
this.completionProvider = completionProvider;
this.featureProviderFactory = featureProviderFactory;
this.documentSelectorFactory = documentSelectorFactory;
}
public override async Task<CompletionList> Handle(CompletionParams request, CancellationToken cancellationToken)
{
var completions = Enumerable.Empty<CompletionItem>();
var featureProvider = featureProviderFactory.GetFeatureProvider(request.TextDocument.Uri.ToUriEncoded());
var compilationContext = this.compilationManager.GetCompilation(request.TextDocument.Uri);
if (compilationContext is null)
{
// no compilation context or this is a param file and params are disabled
return new CompletionList();
}
int offset = PositionHelper.GetOffset(compilationContext.LineStarts, request.Position);
var completionContext = BicepCompletionContext.Create(featureProvider, compilationContext.Compilation, offset);
try
{
completions = await this.completionProvider.GetFilteredCompletions(compilationContext.Compilation, completionContext, cancellationToken);
}
catch (Exception e)
{
this.logger.LogError("Error with Completion in file {Uri} with {Context}. Underlying exception is: {Exception}", request.TextDocument.Uri, completionContext, e.ToString());
}
return new CompletionList(completions, isIncomplete: false);
}
public override Task<CompletionItem> Handle(CompletionItem request, CancellationToken cancellationToken)
{
return Task.FromResult(request);
}
protected override CompletionRegistrationOptions CreateRegistrationOptions(CompletionCapability capability, ClientCapabilities clientCapabilities) => new()
{
DocumentSelector = documentSelectorFactory.CreateForBicepAndParams(),
AllCommitCharacters = new Container<string>(),
ResolveProvider = false,
TriggerCharacters = new Container<string>(":", " ", ".", "/", "'", "@", "{", "#", "?")
};
}
}