-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathTripleSlashCompletionCommandHandler.cs
More file actions
315 lines (292 loc) · 14.1 KB
/
Copy pathTripleSlashCompletionCommandHandler.cs
File metadata and controls
315 lines (292 loc) · 14.1 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
namespace CppTripleSlash
{
using EnvDTE;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Language.Intellisense;
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.Runtime.InteropServices;
using System.Text;
public class TripleSlashCompletionCommandHandler : IOleCommandTarget
{
public const string CppTypeName = "C/C++";
private IOleCommandTarget m_nextCommandHandler;
private IWpfTextView m_textView;
private TripleSlashCompletionHandlerProvider m_provider;
private ICompletionSession m_session;
DTE m_dte;
public TripleSlashCompletionCommandHandler(
IVsTextView textViewAdapter,
IWpfTextView textView,
TripleSlashCompletionHandlerProvider provider,
DTE dte)
{
this.m_textView = textView;
this.m_provider = provider;
this.m_dte = dte;
// add the command to the command chain
if (textViewAdapter != null &&
textView != null &&
textView.TextBuffer != null &&
textView.TextBuffer.ContentType.TypeName == CppTypeName)
{
textViewAdapter.AddCommandFilter(this, out m_nextCommandHandler);
}
}
public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
{
return m_nextCommandHandler.QueryStatus(ref pguidCmdGroup, cCmds, prgCmds, pCmdText);
}
public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
{
try
{
if (VsShellUtilities.IsInAutomationFunction(m_provider.ServiceProvider))
{
return m_nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
}
// make a copy of this so we can look at it after forwarding some commands
uint commandID = nCmdID;
char 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);
}
// check for the triple slash
if (typedChar == '/' && m_dte != null)
{
string currentLine = m_textView.TextSnapshot.GetLineFromPosition(
m_textView.Caret.Position.BufferPosition.Position).GetText();
if ((currentLine + "/").Trim() == "///")
{
// Calculate how many spaces
string spaces = currentLine.Replace(currentLine.TrimStart(), "");
TextSelection ts = m_dte.ActiveDocument.Selection as TextSelection;
int oldLine = ts.ActivePoint.Line;
int oldOffset = ts.ActivePoint.LineCharOffset;
ts.LineDown();
ts.EndOfLine();
CodeElement codeElement = null;
FileCodeModel fcm = m_dte.ActiveDocument.ProjectItem.FileCodeModel;
if (fcm != null)
{
codeElement = fcm.CodeElementFromPoint(ts.ActivePoint, vsCMElement.vsCMElementFunction);
}
if (codeElement != null && codeElement is CodeFunction)
{
CodeFunction function = codeElement as CodeFunction;
StringBuilder sb = new StringBuilder("/ <summary>\r\n" + spaces + "/// \r\n" + spaces + "/// </summary>");
foreach (CodeElement child in codeElement.Children)
{
CodeParameter parameter = child as CodeParameter;
if (parameter != null)
{
sb.AppendFormat("\r\n" + spaces + "/// <param name=\"{0}\"></param>", parameter.Name);
}
}
if (function.Type.AsString != "void")
{
sb.AppendFormat("\r\n" + spaces + "/// <returns></returns>");
}
ts.MoveToLineAndOffset(oldLine, oldOffset);
ts.Insert(sb.ToString());
ts.MoveToLineAndOffset(oldLine, oldOffset);
ts.LineDown();
ts.EndOfLine();
return VSConstants.S_OK;
}
else
{
ts.MoveToLineAndOffset(oldLine, oldOffset);
ts.Insert("/ <summary>\r\n" + spaces + "/// \r\n" + spaces + "/// </summary>");
ts.MoveToLineAndOffset(oldLine, oldOffset);
ts.LineDown();
ts.EndOfLine();
return VSConstants.S_OK;
}
}
}
if (m_session != null && !m_session.IsDismissed)
{
// check for a commit character
if (nCmdID == (uint)VSConstants.VSStd2KCmdID.RETURN
|| nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB
|| typedChar == '>')
{
// check for a selection
// if the selection is fully selected, commit the current session
if (m_session.SelectedCompletionSet.SelectionStatus.IsSelected)
{
string selectedCompletion = m_session.SelectedCompletionSet.SelectionStatus.Completion.DisplayText;
m_session.Commit();
TextSelection ts = m_dte.ActiveDocument.Selection as TextSelection;
switch (selectedCompletion)
{
case "<!-->":
ts.CharLeft(false, 3);
break;
case "<![CDATA[>":
ts.CharLeft(false, 3);
break;
case "<c>":
ts.CharLeft(false, 4);
break;
case "<code>":
ts.CharLeft(false, 7);
break;
case "<example>":
ts.CharLeft(false, 10);
break;
case "<exception>":
ts.CharLeft(false, 14);
break;
case "<include>":
ts.CharLeft(false, 21);
break;
case "<list>":
ts.CharLeft(false, 7);
break;
case "<para>":
ts.CharLeft(false, 7);
break;
case "<param>":
ts.CharLeft(false, 10);
break;
case "<paramref>":
ts.CharLeft(false, 13);
break;
case "<permission>":
ts.CharLeft(false, 15);
break;
case "<remarks>":
ts.CharLeft(false, 10);
break;
case "<returns>":
ts.CharLeft(false, 10);
break;
case "<see>":
ts.CharLeft(false, 3);
break;
case "<seealso>":
ts.CharLeft(false, 3);
break;
case "<typeparam>":
ts.CharLeft(false, 14);
break;
case "<typeparamref>":
ts.CharLeft(false, 3);
break;
case "<value>":
ts.CharLeft(false, 8);
break;
default:
break;
}
// also, don't add the character to the buffer
return VSConstants.S_OK;
}
else
{
// if there is no selection, dismiss the session
m_session.Dismiss();
}
}
}
else
{
if (pguidCmdGroup == VSConstants.VSStd2K && nCmdID == (uint)VSConstants.VSStd2KCmdID.RETURN)
{
string currentLine = m_textView.TextSnapshot.GetLineFromPosition(
m_textView.Caret.Position.BufferPosition.Position).GetText();
if (currentLine.TrimStart().StartsWith("///"))
{
TextSelection ts = m_dte.ActiveDocument.Selection as TextSelection;
string spaces = currentLine.Replace(currentLine.TrimStart(), "");
ts.Insert("\r\n" + spaces + "/// ");
return VSConstants.S_OK;
}
}
}
// pass along the command so the char is added to the buffer
int retVal = m_nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
if (typedChar == '<')
{
string currentLine = m_textView.TextSnapshot.GetLineFromPosition(
m_textView.Caret.Position.BufferPosition.Position).GetText();
if (currentLine.TrimStart().StartsWith("///"))
{
if (m_session == null || m_session.IsDismissed) // If there is no active session, bring up completion
{
if (this.TriggerCompletion())
{
m_session.SelectedCompletionSet.SelectBestMatch();
m_session.SelectedCompletionSet.Recalculate();
return VSConstants.S_OK;
}
}
}
}
else if (
commandID == (uint)VSConstants.VSStd2KCmdID.BACKSPACE ||
commandID == (uint)VSConstants.VSStd2KCmdID.DELETE ||
char.IsLetter(typedChar))
{
if (m_session != null && !m_session.IsDismissed) // the completion session is already active, so just filter
{
m_session.SelectedCompletionSet.SelectBestMatch();
m_session.SelectedCompletionSet.Recalculate();
return VSConstants.S_OK;
}
}
return retVal;
}
catch
{
}
return VSConstants.E_FAIL;
}
private bool TriggerCompletion()
{
try
{
if (m_session != null)
{
return false;
}
// the caret must be in a non-projection location
SnapshotPoint? caretPoint =
m_textView.Caret.Position.Point.GetPoint(
textBuffer => (!textBuffer.ContentType.IsOfType("projection")), PositionAffinity.Predecessor);
if (!caretPoint.HasValue)
{
return false;
}
m_session = m_provider.CompletionBroker.CreateCompletionSession(
m_textView,
caretPoint.Value.Snapshot.CreateTrackingPoint(caretPoint.Value.Position, PointTrackingMode.Positive),
true);
// subscribe to the Dismissed event on the session
m_session.Dismissed += this.OnSessionDismissed;
m_session.Start();
return true;
}
catch
{
}
return false;
}
private void OnSessionDismissed(object sender, EventArgs e)
{
if (m_session != null)
{
m_session.Dismissed -= this.OnSessionDismissed;
m_session = null;
}
}
}
}