-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSuggestedActionsSource.cs
More file actions
77 lines (62 loc) · 2.51 KB
/
SuggestedActionsSource.cs
File metadata and controls
77 lines (62 loc) · 2.51 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
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Cycode.VisualStudio.Extension.Shared.Services.SuggestedActions.Actions;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
namespace Cycode.VisualStudio.Extension.Shared.Services.SuggestedActions;
public class SuggestedActionsSource(
ILightBulbBroker lightBulbBroker,
ITextView textView
) : ISuggestedActionsSource {
private readonly ILoggerService _logger = ServiceLocator.GetService<ILoggerService>();
private ErrorTagger.ErrorTagger _tagger;
public void Dispose() {
if (_tagger == null) return;
_tagger.TagsChanged -= ErrorTagsChanged;
}
public bool TryGetTelemetryId(out Guid telemetryId) {
telemetryId = Guid.Empty;
return false;
}
public IEnumerable<SuggestedActionSet> GetSuggestedActions(
ISuggestedActionCategorySet requestedActionCategories,
SnapshotSpan range,
CancellationToken cancellationToken
) {
_logger.Debug("SuggestedActionsSource: GetSuggestedActions");
List<ISuggestedAction> actions = [];
actions.AddRange(_tagger.GetErrorTags(range).Select(tag => new OpenViolationCardAction(tag)));
return new[] {
new SuggestedActionSet(actions)
};
}
public Task<bool> HasSuggestedActionsAsync(
ISuggestedActionCategorySet requestedActionCategories,
SnapshotSpan range,
CancellationToken cancellationToken
) {
if (TryUpdateTagger() == false) return Task.FromResult(false);
return Task.FromResult(_tagger.GetErrorTags(range).Count > 0);
}
public event EventHandler<EventArgs> SuggestedActionsChanged;
private bool TryUpdateTagger() {
if (_tagger != null) return true;
ErrorTagger.ErrorTagger errorTagger = CycodePackage.ErrorTaggerProvider?.GetTagger(textView);
if (errorTagger == null) {
_logger.Debug("SuggestedActionsSource: Tagger is not created yet");
return false;
}
_tagger = errorTagger;
_tagger.TagsChanged += ErrorTagsChanged;
return true;
}
private void ErrorTagsChanged(object sender, SnapshotSpanEventArgs e) {
_logger.Debug("SuggestedActionsSource: ErrorTagsChanged");
ThreadHelper.ThrowIfNotOnUIThread();
lightBulbBroker.DismissSession(textView);
SuggestedActionsChanged?.Invoke(this, EventArgs.Empty);
}
}