-
Notifications
You must be signed in to change notification settings - Fork 418
/
OmniSharpCompletionHandler.cs
144 lines (130 loc) · 7.79 KB
/
OmniSharpCompletionHandler.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Models.v1.Completion;
using static OmniSharp.LanguageServerProtocol.Helpers;
using CompletionItem = OmniSharp.Extensions.LanguageServer.Protocol.Models.CompletionItem;
using CompletionItemKind = OmniSharp.Extensions.LanguageServer.Protocol.Models.CompletionItemKind;
using CompletionItemTag = OmniSharp.Extensions.LanguageServer.Protocol.Models.CompletionItemTag;
using CompletionTriggerKind = OmniSharp.Extensions.LanguageServer.Protocol.Models.CompletionTriggerKind;
using InsertTextFormat = OmniSharp.Extensions.LanguageServer.Protocol.Models.InsertTextFormat;
using OmnisharpCompletionItem = OmniSharp.Models.v1.Completion.CompletionItem;
using OmnisharpCompletionItemKind = OmniSharp.Models.v1.Completion.CompletionItemKind;
using OmnisharpCompletionItemTag = OmniSharp.Models.v1.Completion.CompletionItemTag;
using OmnisharpCompletionTriggerKind = OmniSharp.Models.v1.Completion.CompletionTriggerKind;
using OmnisharpInsertTextFormat = OmniSharp.Models.v1.Completion.InsertTextFormat;
#nullable enable
namespace OmniSharp.LanguageServerProtocol.Handlers
{
class OmniSharpCompletionHandler : CompletionHandlerBase
{
const string AfterInsertCommandName = "csharp.completion.afterInsert";
public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers)
{
foreach (var (selector, completionHandler, completionResolveHandler) in handlers
.OfType<Mef.IRequestHandler<CompletionRequest, CompletionResponse>,
Mef.IRequestHandler<CompletionResolveRequest, CompletionResolveResponse>>())
{
if (completionHandler != null && completionResolveHandler != null)
yield return new OmniSharpCompletionHandler(completionHandler, completionResolveHandler, selector);
}
}
private readonly Mef.IRequestHandler<CompletionRequest, CompletionResponse> _completionHandler;
private readonly Mef.IRequestHandler<CompletionResolveRequest, CompletionResolveResponse> _completionResolveHandler;
private readonly TextDocumentSelector _documentSelector;
public OmniSharpCompletionHandler(
Mef.IRequestHandler<CompletionRequest, CompletionResponse> completionHandler,
Mef.IRequestHandler<CompletionResolveRequest, CompletionResolveResponse> completionResolveHandler,
TextDocumentSelector documentSelector)
{
_completionHandler = completionHandler;
_completionResolveHandler = completionResolveHandler;
_documentSelector = documentSelector;
}
public override async Task<CompletionList> Handle(CompletionParams request, CancellationToken token)
{
var omnisharpRequest = new CompletionRequest()
{
FileName = Helpers.FromUri(request.TextDocument.Uri),
Column = Convert.ToInt32(request.Position.Character),
Line = Convert.ToInt32(request.Position.Line),
CompletionTrigger = Helpers.ConvertEnum<CompletionTriggerKind, OmnisharpCompletionTriggerKind>(request.Context?.TriggerKind ?? CompletionTriggerKind.Invoked),
TriggerCharacter = request.Context?.TriggerCharacter is { Length: > 0 } str ? str[0] : null
};
var omnisharpResponse = await _completionHandler.Handle(omnisharpRequest);
return new CompletionList(omnisharpResponse.Items.Select(ToLSPCompletionItem), isIncomplete: omnisharpResponse.IsIncomplete);
}
public override async Task<CompletionItem> Handle(CompletionItem request, CancellationToken cancellationToken)
{
var resolveRequest = new CompletionResolveRequest
{
Item = ToOmnisharpCompletionItem(request)
};
var result = await _completionResolveHandler.Handle(resolveRequest);
Debug.Assert(result.Item != null);
return ToLSPCompletionItem(result.Item!);
}
protected override CompletionRegistrationOptions CreateRegistrationOptions(CompletionCapability capability, ClientCapabilities clientCapabilities)
{
return new CompletionRegistrationOptions()
{
DocumentSelector = _documentSelector,
ResolveProvider = true,
TriggerCharacters = new[] { ".", " " },
};
}
private CompletionItem ToLSPCompletionItem(OmnisharpCompletionItem omnisharpCompletionItem)
=> new CompletionItem
{
Label = omnisharpCompletionItem.Label,
Kind = ConvertEnum<OmnisharpCompletionItemKind, CompletionItemKind>(omnisharpCompletionItem.Kind),
Tags = omnisharpCompletionItem.Tags is { } tags
? Container<CompletionItemTag>.From(tags.Select(ConvertEnum<OmnisharpCompletionItemTag, CompletionItemTag>))
: null,
Detail = omnisharpCompletionItem.Detail,
Documentation = omnisharpCompletionItem.Documentation is null
? (StringOrMarkupContent?)null
: new MarkupContent { Value = omnisharpCompletionItem.Documentation, Kind = MarkupKind.Markdown },
Preselect = omnisharpCompletionItem.Preselect,
SortText = omnisharpCompletionItem.SortText,
FilterText = omnisharpCompletionItem.FilterText,
InsertTextFormat = ConvertEnum<OmnisharpInsertTextFormat, InsertTextFormat>(omnisharpCompletionItem.InsertTextFormat),
TextEdit = Helpers.ToTextEdit(omnisharpCompletionItem.TextEdit),
CommitCharacters = omnisharpCompletionItem.CommitCharacters is { } chars
? Container<string>.From(chars.Select(i => i.ToString()))
: null,
AdditionalTextEdits = omnisharpCompletionItem.AdditionalTextEdits is { } edits
? TextEditContainer.From(edits.Select(e => Helpers.ToTextEdit(e)))
: null,
Data = JToken.FromObject(omnisharpCompletionItem.Data),
Command = omnisharpCompletionItem.HasAfterInsertStep
? Command.Create(AfterInsertCommandName)
: null,
};
private OmnisharpCompletionItem ToOmnisharpCompletionItem(CompletionItem completionItem)
=> new OmnisharpCompletionItem
{
Label = completionItem.Label,
Kind = ConvertEnum<CompletionItemKind, OmnisharpCompletionItemKind>(completionItem.Kind),
Tags = completionItem.Tags?.Select(ConvertEnum<CompletionItemTag, OmnisharpCompletionItemTag>).ToList(),
Detail = completionItem.Detail,
Documentation = completionItem.Documentation?.MarkupContent!.Value,
Preselect = completionItem.Preselect,
SortText = completionItem.SortText,
FilterText = completionItem.FilterText,
InsertTextFormat = ConvertEnum<InsertTextFormat, OmnisharpInsertTextFormat>(completionItem.InsertTextFormat),
TextEdit = Helpers.FromTextEdit(completionItem.TextEdit!.TextEdit),
CommitCharacters = completionItem.CommitCharacters?.Select(i => i[0]).ToList(),
AdditionalTextEdits = completionItem.AdditionalTextEdits?.Select(e => Helpers.FromTextEdit(e)).ToList(),
Data = completionItem.Data!.ToObject<(long, int)>()
};
}
}