added some development tools
[windows-sources.git] / developer / VSSDK / Samples / LightBulb / TestLightBulb / TestSuggestedActionsSource.cs
blobc9987d31c8cbea20df79cf153c7dd7c7611098c4
1 using Microsoft.VisualStudio.Language.Intellisense;
2 using Microsoft.VisualStudio.Text;
3 using Microsoft.VisualStudio.Text.Editor;
4 using Microsoft.VisualStudio.Text.Operations;
5 using System;
6 using System.Collections.Generic;
7 using System.Linq;
8 using System.Threading;
9 using System.Threading.Tasks;
11 namespace TestLightBulb
13 class TestSuggestedActionsSource : ISuggestedActionsSource
15 private readonly TestSuggestedActionsSourceProvider _factory;
16 private readonly ITextBuffer _textBuffer;
17 private readonly ITextView _textView;
19 public TestSuggestedActionsSource(TestSuggestedActionsSourceProvider testSuggestedActionsSourceProvider, ITextView textView, ITextBuffer textBuffer)
21 _factory = testSuggestedActionsSourceProvider;
22 _textBuffer = textBuffer;
23 _textView = textView;
26 #pragma warning disable 0067
27 public event EventHandler<EventArgs> SuggestedActionsChanged;
28 #pragma warning restore 0067
30 public void Dispose()
34 public IEnumerable<SuggestedActionSet> GetSuggestedActions(ISuggestedActionCategorySet requestedActionCategories, SnapshotSpan range, CancellationToken cancellationToken)
36 TextExtent extent;
37 if (TryGetWordUnderCaret(out extent) && extent.IsSignificant)
39 ITrackingSpan trackingSpan = range.Snapshot.CreateTrackingSpan(extent.Span, SpanTrackingMode.EdgeInclusive);
40 var upperAction = new UpperCaseSuggestedAction(trackingSpan);
41 var lowerAction = new LowerCaseSuggestedAction(trackingSpan);
42 return new SuggestedActionSet[] { new SuggestedActionSet(new ISuggestedAction[] { upperAction, lowerAction }) };
44 return Enumerable.Empty<SuggestedActionSet>();
47 public Task<bool> HasSuggestedActionsAsync(ISuggestedActionCategorySet requestedActionCategories, SnapshotSpan range, CancellationToken cancellationToken)
49 return Task.Factory.StartNew(() =>
51 TextExtent extent;
52 if (TryGetWordUnderCaret(out extent))
54 // don't display the tag if the extent has whitespace
55 return extent.IsSignificant;
57 return false;
58 });
61 public bool TryGetTelemetryId(out Guid telemetryId)
63 telemetryId = Guid.Empty;
64 return false;
67 private bool TryGetWordUnderCaret(out TextExtent wordExtent)
69 ITextCaret caret = _textView.Caret;
70 SnapshotPoint point;
72 if (caret.Position.BufferPosition > 0)
74 point = caret.Position.BufferPosition - 1;
76 else
78 wordExtent = default(TextExtent);
79 return false;
82 ITextStructureNavigator navigator = _factory.NavigatorService.GetTextStructureNavigator(_textBuffer);
84 wordExtent = navigator.GetExtentOfWord(point);
85 return true;