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 pathInlineCompletionView.cs
More file actions
246 lines (207 loc) · 6.94 KB
/
InlineCompletionView.cs
File metadata and controls
246 lines (207 loc) · 6.94 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
using Extension.Logging;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Formatting;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using FontFamily = System.Windows.Media.FontFamily;
namespace Extension.InlineCompletion
{
/// <summary>
/// This class is responsible for drawing the adornments for the inlince completion instructions
/// that provides the users with the keyboard shortcuts they need to know.
/// </summary>
internal sealed class InlineCompletionView
{
private const string Preview_Tag = "preview";
private const string Instructions_Tag = "instructions";
/// <summary>
/// The layer of the adornment.
/// </summary>
private readonly IAdornmentLayer _layer;
/// <summary>
/// Text view where the adornment is created.
/// </summary>
private readonly IWpfTextView _view;
private readonly FontSettings _settings;
private ITrackingSpan _triggeringLine;
private string? _currentSnippetCode;
private int _currentSnippetIndex = 0;
private int _totalSnippetCount = 0;
private SolidColorBrush _textBrush = new SolidColorBrush(Colors.DarkGreen);
private SolidColorBrush _textBackgroundBrush = new SolidColorBrush(Colors.DarkGray);
private FontFamily _fontFamily = Fonts.SystemFontFamilies.First();
public const string PreviewLayerName = "InlineCompletionLayer";
public bool ShowPreview { get; set; } = true;
public bool ShowInstructions { get; set; } = true;
/// <summary>
/// Initializes a new instance of the <see cref="InlineCompletionView"/> class.
/// </summary>
/// <param name="view">Text view to create the adornment for</param>
public InlineCompletionView(IWpfTextView view, ITrackingSpan triggeringLine)
{
if (view == null)
{
throw new ArgumentNullException("view");
}
_layer = view.GetAdornmentLayer(PreviewLayerName);
try
{
_settings = EditorSettingsProvider.GetCurrentFontSettings();
}
catch(ArgumentException e)
{
ExtensionLogger.LogException(e);
}
_view = view;
_triggeringLine = triggeringLine;
if (_settings != null)
{
_textBrush = new SolidColorBrush(_settings.CommentColor);
_textBackgroundBrush = new SolidColorBrush(_settings.TextBackgroundColor);
_fontFamily = new FontFamily(_settings.FontFamily);
}
_textBrush.Opacity = 0.7;
}
internal void StartDrawingInstructions()
{
_currentSnippetCode = null;
_view.LayoutChanged += OnLayoutChanged;
}
/// <summary>
/// Draws the instructions for the completion session by adding a TextBlock to the adornment layer.
/// </summary>
private void DrawCompletionInstructions()
{
var triggeringLine = GetTriggeringLine();
if (triggeringLine == null)
return;
var geometry = _view.TextViewLines.GetMarkerGeometry(triggeringLine.Extent);
if (geometry == null)
return;
var textBlock = new TextBlock
{
Width = 600,
Foreground = _textBrush,
Height = geometry.Bounds.Height,
FontFamily = _fontFamily,
FontSize = triggeringLine.Baseline,
Text = $"[{_currentSnippetIndex}/{_totalSnippetCount}] [←]Previous [→]Next [Tab]Commit [ESC]Cancel"
};
Canvas.SetLeft(textBlock, geometry.Bounds.Width + geometry.Bounds.Height);
Canvas.SetTop(textBlock, geometry.Bounds.Top);
_layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, triggeringLine.Extent, Instructions_Tag, textBlock, (tag, ui) => { });
}
/// <summary>
/// Refresh adornments whenever the layout changes
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
{
if (_layer.IsEmpty)
{
UpdateView(_currentSnippetCode, _currentSnippetIndex, _totalSnippetCount);
}
}
/// <summary>
/// Updates the binding fields and initializes a refresh on the adornments
/// </summary>
/// <param name="code"></param>
/// <param name="current"></param>
/// <param name="total"></param>
internal void UpdateView(string? code, int current, int total)
{
_currentSnippetCode = code;
_currentSnippetIndex = current;
_totalSnippetCount = total;
if(!_layer.IsEmpty)
_layer.RemoveAllAdornments();
try
{
if (ShowInstructions)
DrawCompletionInstructions();
if (ShowPreview)
DrawSnippetPreview();
}
catch (Exception e)
{
ExtensionLogger.LogException(e);
}
}
/// <summary>
/// Draws the box that contains the code
/// </summary>
private void DrawSnippetPreview()
{
var triggeringLine = GetTriggeringLine();
var geometry = _view.TextViewLines.GetMarkerGeometry(triggeringLine.Extent);
var wholeLineSpan = new SnapshotSpan(triggeringLine.Snapshot, new Span(triggeringLine.Start, triggeringLine.Length));
var lineText = wholeLineSpan.GetText();
var lineTextTrimmed = lineText.Trim();
if (string.IsNullOrEmpty(lineTextTrimmed))
return;
var firstChar = lineTextTrimmed.First();
var position = lineText.IndexOf(firstChar);
var firstCharacterSpan = new SnapshotSpan(triggeringLine.Snapshot, new Span(triggeringLine.Start + position, 1));
var onlyTextG = _view.TextViewLines.GetMarkerGeometry(firstCharacterSpan);
var content = _currentSnippetCode;
if (_currentSnippetCode == null)
{
content = "fetching snippets...";
}
var loc = content.Split('\n').Length;
double height = loc * geometry.Bounds.Height;
var textBlock = new TextBlock
{
Width = _view.ViewportWidth,
Foreground = _textBrush,
FontStyle = FontStyles.Italic,
Focusable = false,
Background = _textBackgroundBrush,
FontFamily = _fontFamily,
FontSize = triggeringLine.Baseline,
Height = height,
Text = content
};
var border = new Border
{
BorderThickness = new Thickness(1, 0, 0, 1),
BorderBrush = _textBrush,
Child = textBlock,
};
Canvas.SetLeft(border, onlyTextG.Bounds.Left);
Canvas.SetTop(border, geometry.Bounds.Bottom);
_layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, triggeringLine.Extent, Preview_Tag, border, (tag, ui) => { });
}
/// <summary>
/// Removes all adornments from the layer
/// </summary>
internal void RemoveInstructions()
{
_currentSnippetCode = null;
_currentSnippetIndex = 0;
_totalSnippetCount = 0;
_view.LayoutChanged -= OnLayoutChanged;
_layer.RemoveAllAdornments();
}
/// <summary>
/// Finds the line on which the inline completion was triggered.
/// </summary>
private ITextViewLine GetTriggeringLine()
{
var lineSpan = _triggeringLine.GetSpan(_view.TextSnapshot);
var textViewLines = _view.TextViewLines.GetTextViewLinesIntersectingSpan(lineSpan);
if (!textViewLines.Any())
{
if(_view.TextViewLines.Any())
return _view.TextViewLines.First();
throw new ArgumentOutOfRangeException(nameof(_triggeringLine), "Cannot map tracking span to a valid view line");
}
return textViewLines.First();
}
}
}