This repository was archived by the owner on May 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInlineCompletionClient.cs
More file actions
279 lines (236 loc) · 8.87 KB
/
InlineCompletionClient.cs
File metadata and controls
279 lines (236 loc) · 8.87 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using Extension.AssistantCompletion;
using Extension.Caching;
using Extension.Logging;
using Extension.SnippetFormats;
using GraphQLClient;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace Extension.InlineCompletion
{
/// <summary>
/// This class is responsible for the inline completion session lifecycle.
/// The main purpose is to provide the user with a good preview of potential snippets returned by the semantic search.
/// The snippet insertion process is passed to the <see cref="ExpansionClient"/>
/// </summary>
internal class InlineCompletionClient : IOleCommandTarget, IDisposable
{
private IOleCommandTarget _nextCommandHandler;
private IWpfTextView _wpfTextView;
private ICodigaClientProvider _clientProvider;
private InlineCompletionView? _completionView;
private ListNavigator<VisualStudioSnippet>? _snippetNavigator;
private ExpansionClient? _expansionClient;
/// <summary>
/// Initialize the client and start listening for commands
/// </summary>
/// <param name="wpfTextView"></param>
/// <param name="expansionClient"></param>
public void Initialize(IWpfTextView wpfTextView, ExpansionClient expansionClient)
{
_clientProvider = new DefaultCodigaClientProvider();
_wpfTextView = wpfTextView;
var vsTextView = wpfTextView.ToIVsTextView();
if (vsTextView == null)
return;
_expansionClient = expansionClient;
vsTextView.AddCommandFilter(this, out _nextCommandHandler);
}
public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
{
int res = 0;
try
{
ThreadHelper.JoinableTaskFactory.Run(async () =>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
});
ThreadHelper.ThrowIfNotOnUIThread();
res = _nextCommandHandler.QueryStatus(ref pguidCmdGroup, cCmds, prgCmds, pCmdText);
}
catch (Exception e)
{
ExtensionLogger.LogException(e);
}
return res;
}
/// <summary>
/// This handles incoming commands during the inline completion session.
/// Commands are NextSnippet(tbd), PreviousSnippet(tbd), Commit [Tab], Cancel [ESC]
/// </summary>
/// <param name="pguidCmdGroup"></param>
/// <param name="nCmdID"></param>
/// <param name="nCmdexecopt"></param>
/// <param name="pvaIn"></param>
/// <param name="pvaOut"></param>
/// <returns></returns>
public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
{
try
{
var codigaSettings = EditorSettingsProvider.GetCurrentCodigaSettings();
if (!codigaSettings.UseInlineCompletion)
{
return _nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
}
var typedChar = char.MinValue;
//make sure the input is a char before getting it
if (pguidCmdGroup == VSConstants.VSStd2K && nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR)
{
typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);
}
if (_completionView != null)
{
return HandleSessionCommand(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
}
var caretPos = _wpfTextView.Caret.Position.BufferPosition.Position;
var triggeringLine = _wpfTextView.TextBuffer.CurrentSnapshot.GetLineFromPosition(caretPos);
var triggeringLineText = triggeringLine.GetText();
var lineTrackingSpan = _wpfTextView.TextBuffer.CurrentSnapshot.CreateTrackingSpan(triggeringLine.Extent.Span, SpanTrackingMode.EdgePositive);
var doc = EditorUtils.ToDocumentView(_wpfTextView);
if (doc == null)
return _nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
var language = LanguageUtils.ParseFromFileName(DocumentHelper.GetFileName(doc, _wpfTextView));
var shouldTriggerCompletion = char.IsWhiteSpace(typedChar)
&& EditorUtils.IsSemanticSearchComment(triggeringLineText, language)
&& _completionView == null;
if (!shouldTriggerCompletion)
{
return _nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
}
// start inline completion session
// query snippets based on keywords
var sign = LanguageUtils.GetCommentSign(language);
var term = triggeringLineText.Replace(sign, "").Trim();
var languages = new ReadOnlyCollection<string>(new[] { language.GetName() });
if(!_clientProvider.TryGetClient(out var client))
return _nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
ThreadHelper.JoinableTaskFactory.RunAsync(async () =>
{
try
{
await client.GetRecipesForClientSemanticAsync(term, languages, false, 10, 0)
.ContinueWith(OnQueryFinished, TaskScheduler.Default);
}
catch (CodigaAPIException e)
{
ExtensionLogger.LogException(e);
}
});
_completionView = new InlineCompletionView(_wpfTextView, lineTrackingSpan);
// start drawing the adornments for the instructions
_completionView.StartDrawingInstructions();
return _nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
}
catch (Exception e)
{
ExtensionLogger.LogException(e);
Dispose();
return _nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
}
}
/// <summary>
/// Calls the ExpansionClient to insert the selected snippet.
/// </summary>
/// <returns></returns>
private int CommitCurrentSnippet()
{
try
{
_expansionClient?.StartExpansion(_wpfTextView, _snippetNavigator.CurrentItem, true);
}
catch(Exception e)
{
ExtensionLogger.LogException(e);
return VSConstants.S_FALSE;
}
return VSConstants.S_OK;
}
/// <summary>
/// Callback for when the API query returns with its results.
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
private async Task OnQueryFinished(Task<System.Collections.Generic.IReadOnlyCollection<CodigaSnippet>> result)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var setting = EditorSettingsProvider.GetCurrentIndentationSettings();
var snippets = result.Result.Select(s => SnippetParser.FromCodigaSnippet(s, setting));
if (_completionView == null)
return;
if (snippets.Any())
{
_snippetNavigator = new ListNavigator<VisualStudioSnippet>(snippets.ToList());
var previewCode = SnippetParser.GetPreviewCode(_snippetNavigator.CurrentItem);
var currentIndex = _snippetNavigator.IndexOf(_snippetNavigator.CurrentItem) + 1;
_completionView.UpdateView(previewCode, currentIndex, _snippetNavigator.Count);
}
else
{
_snippetNavigator = new ListNavigator<VisualStudioSnippet>(new List<VisualStudioSnippet>());
_completionView.ShowPreview = false;
_completionView.UpdateView(null, 0, 0);
}
}
/// <summary>
/// Handles all the commands during an open inline session.
/// </summary>
/// <param name="nCmdID"></param>
/// <returns></returns>
private int HandleSessionCommand(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
{
if (nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB)
{
_completionView?.RemoveInstructions();
_completionView = null;
CommitCurrentSnippet();
return VSConstants.S_OK;
}
else if(nCmdID == (uint)VSConstants.VSStd2KCmdID.RIGHT)
{
if(_snippetNavigator == null || _snippetNavigator.Count == 0)
return VSConstants.S_OK;
// get next snippet
var next = _snippetNavigator.Next();
var i = _snippetNavigator.IndexOf(next);
var c = _snippetNavigator.Count;
var previewCode = SnippetParser.GetPreviewCode(next);
_completionView?.UpdateView(previewCode, i + 1, c);
return VSConstants.S_OK;
}
else if (nCmdID == (uint)VSConstants.VSStd2KCmdID.LEFT)
{
if (_snippetNavigator == null || _snippetNavigator.Count == 0)
return VSConstants.S_OK;
// get previous snippet
var previous = _snippetNavigator.Previous();
var i = _snippetNavigator.IndexOf(previous);
var c = _snippetNavigator.Count;
var previewCode = SnippetParser.GetPreviewCode(previous);
_completionView?.UpdateView(previewCode, i + 1, c);
return VSConstants.S_OK;
}
else if((pguidCmdGroup == VSConstants.VSStd2K && nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR) ||
nCmdID == (uint)VSConstants.VSStd2KCmdID.CANCEL)
{
_completionView?.RemoveInstructions();
_completionView = null;
_snippetNavigator = null;
}
return _nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
}
public void Dispose()
{
_wpfTextView.ToIVsTextView()?.RemoveCommandFilter(this);
}
}
}