Skip to content

Commit cdf1373

Browse files
Merge remote-tracking branch 'remotes/community-origin/master'
2 parents a66df78 + 2d3406d commit cdf1373

File tree

14 files changed

+159
-131
lines changed

14 files changed

+159
-131
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,22 @@ catch (Exception ex)
7878
}
7979
```
8080

81+
### Dependency Injection
82+
To incorporate a DI container into your extension, see the complimentary project: [
83+
Community.VisualStudio.Toolkit.DependencyInjection](https://github.com/VsixCommunity/Community.VisualStudio.Toolkit.DependencyInjection)
84+
8185
## Purpose
8286
This package attempts to solve multiple issues with the current extensibility model.
8387

8488
### Too much boilerplate is needed to do simple things
8589
Base classes, helper methods, and extension methods encapsulate the complexity so you don't have to.
8690

8791
### It's difficult to find what services and components to use
88-
Now the most commmon services are all easy to get to from the main `VS` object.
89-
92+
Now the most common services are all easy to get to from the main `VS` object.
9093

9194
### Best practices change with each version of VS. I can't keep up
9295
The underlying implementation of the project uses the best practices for each version of VS it supports. This ensures that your extension is much more likely to handle threading correctly, and avoid hangs and crashes.
9396

94-
9597
### The API is dated and has lots of ugly COM legacy noise
9698
The most common APIs of the old complex COM nature are wrapped to expose a modern async API. This makes it much easier to code against the API and you can avoid the `EnvDTE` object for most scenarios.
9799

src/toolkit/Community.VisualStudio.Toolkit.Shared/ExtensionMethods/IVsTextViewExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static class IVsTextViewExtensions
2626
/// Converts an <see cref="IVsTextView"/> to a <see cref="DocumentView"/>.
2727
/// </summary>
2828
/// <returns><see langword="null"/> if the textView is null or the conversion failed.</returns>
29-
internal static DocumentView ToDocumentView(this IVsTextView textView)
29+
public static DocumentView ToDocumentView(this IVsTextView textView)
3030
{
3131
if (textView == null || textView is not IVsTextViewEx nativeView)
3232
{
@@ -62,7 +62,7 @@ internal static DocumentView ToDocumentView(this IVsTextView textView)
6262
/// Converts an <see cref="IWpfTextView"/> to a <see cref="DocumentView"/>.
6363
/// </summary>
6464
/// <returns><see langword="null"/> if the textView is null or the conversion failed.</returns>
65-
internal static DocumentView ToDocumentView(this IWpfTextView textView)
65+
public static DocumentView ToDocumentView(this IWpfTextView textView)
6666
{
6767
ThreadHelper.ThrowIfNotOnUIThread();
6868
IVsTextView? nativeView = textView.ToIVsTextView();
@@ -89,7 +89,7 @@ internal static DocumentView ToDocumentView(this IWpfTextView textView)
8989
/// Converts the <see cref="IVsTextView"/> to an <see cref="IWpfTextView"/>/
9090
/// </summary>
9191
/// <returns></returns>
92-
internal static IWpfTextView? ToIWpfTextView(this IVsTextView nativeView)
92+
public static IWpfTextView? ToIWpfTextView(this IVsTextView nativeView)
9393
{
9494
ThreadHelper.ThrowIfNotOnUIThread();
9595
IVsEditorAdaptersFactoryService? editorAdapter = VS.GetMefService<IVsEditorAdaptersFactoryService>();
@@ -110,7 +110,7 @@ internal static DocumentView ToDocumentView(this IWpfTextView textView)
110110
/// Converts the <see cref="IVsTextView"/> to an <see cref="IWpfTextView"/>/
111111
/// </summary>
112112
/// <returns></returns>
113-
internal static IVsTextView? ToIVsTextView(this IWpfTextView view)
113+
public static IVsTextView? ToIVsTextView(this IWpfTextView view)
114114
{
115115
ThreadHelper.ThrowIfNotOnUIThread();
116116
IVsEditorAdaptersFactoryService? editorAdapter = VS.GetMefService<IVsEditorAdaptersFactoryService>();

src/toolkit/Community.VisualStudio.Toolkit.Shared/ExtensionMethods/TaskExtensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,24 @@ public static void FireAndForget(this JoinableTask joinableTask, bool logOnFailu
4141
{
4242
FireAndForget(joinableTask.Task, logOnFailure);
4343
}
44+
45+
/// <summary>
46+
/// Schedules a delegate for background execution on the UI thread without inheriting any claim to the UI thread from its caller.
47+
/// </summary>
48+
/// <remarks>
49+
/// StartOnIdle is a included in later versions of the SDK, but this shim is to add support to VS 14+
50+
/// </remarks>
51+
public static JoinableTask StartOnIdleShim(this JoinableTaskFactory joinableTaskFactory, Action action, VsTaskRunContext priority = VsTaskRunContext.UIThreadBackgroundPriority)
52+
{
53+
using (joinableTaskFactory.Context.SuppressRelevance())
54+
{
55+
return joinableTaskFactory.RunAsync(priority, async delegate
56+
{
57+
await System.Threading.Tasks.Task.Yield();
58+
await joinableTaskFactory.SwitchToMainThreadAsync();
59+
action();
60+
});
61+
}
62+
}
4463
}
4564
}

src/toolkit/Community.VisualStudio.Toolkit.Shared/LanguageService/DefaultAuthoringScope.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/toolkit/Community.VisualStudio.Toolkit.Shared/LanguageService/DefaultColorizer.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/toolkit/Community.VisualStudio.Toolkit.Shared/LanguageService/DefaultSource.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/toolkit/Community.VisualStudio.Toolkit.Shared/LanguageService/LanguageBase.cs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ public LanguageBase(object site)
5353
_languageServiceId = GetType().GUID;
5454
}
5555

56+
/// <inheritdoc/>
57+
public override int GetLanguageName(out string name)
58+
{
59+
name = Name;
60+
return VSConstants.S_OK;
61+
}
62+
5663
/// <inheritdoc/>
5764
public abstract override string Name { get; }
5865

@@ -61,18 +68,6 @@ public LanguageBase(object site)
6168
/// </summary>
6269
public abstract string[] FileExtensions { get; }
6370

64-
/// <inheritdoc/>
65-
public override Source CreateSource(IVsTextLines buffer)
66-
{
67-
return new DefaultSource(this, buffer, new DefaultColorizer(this, buffer, null));
68-
}
69-
70-
/// <inheritdoc/>
71-
public override TypeAndMemberDropdownBars CreateDropDownHelper(IVsTextView forView)
72-
{
73-
return base.CreateDropDownHelper(forView);
74-
}
75-
7671
/// <summary>
7772
/// Set the default preferences for this language.
7873
/// </summary>
@@ -92,21 +87,14 @@ public override LanguagePreferences GetLanguagePreferences()
9287
}
9388

9489
/// <inheritdoc/>
95-
public override IScanner GetScanner(IVsTextLines buffer)
96-
{
97-
return null!;
98-
}
90+
public override IScanner GetScanner(IVsTextLines buffer) => null!;
9991

10092
/// <inheritdoc/>
101-
public override AuthoringScope ParseSource(ParseRequest req)
102-
{
103-
return new DefaultAuthoringScope();
104-
}
93+
public override AuthoringScope ParseSource(ParseRequest req) => null!;
10594

10695
/// <inheritdoc/>
10796
public override string GetFormatFilterList()
10897
{
109-
// Constructs a string similar to: Foo File (*.foo, *.bar)|*.foo;*.bar";
11098
IEnumerable<string> normalized = FileExtensions.Select(f => $"*{f}");
11199
string first = string.Join(", ", normalized);
112100
string second = string.Join(";", normalized);

src/toolkit/Community.VisualStudio.Toolkit.Shared/MEF/SameWordHighlighterBase.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Microsoft.VisualStudio.Text.Editor;
88
using Microsoft.VisualStudio.Text.Operations;
99
using Microsoft.VisualStudio.Text.Tagging;
10-
using Microsoft.VisualStudio.Threading;
1110

1211
namespace Community.VisualStudio.Toolkit
1312
{
@@ -108,7 +107,7 @@ private void UpdateAtCaretPosition(CaretPosition caretPosition)
108107

109108
if (word.HasValue && word.Value.IsSignificant && word.Value.Span.Length > 1)
110109
{
111-
StartOnIdle(ThreadHelper.JoinableTaskFactory, () =>
110+
ThreadHelper.JoinableTaskFactory.StartOnIdleShim(() =>
112111
{
113112
UpdateWordAdornments(word.Value);
114113
}, VsTaskRunContext.UIThreadIdlePriority).FireAndForget();
@@ -201,20 +200,6 @@ public IEnumerable<ITagSpan<HighlightWordTag>> GetTags(NormalizedSnapshotSpanCol
201200
}
202201
}
203202

204-
//Schedules a delegate for background execution on the UI thread without inheriting any claim to the UI thread from its caller.
205-
private static JoinableTask StartOnIdle(JoinableTaskFactory joinableTaskFactory, Action action, VsTaskRunContext priority = VsTaskRunContext.UIThreadBackgroundPriority)
206-
{
207-
using (joinableTaskFactory.Context.SuppressRelevance())
208-
{
209-
return joinableTaskFactory.RunAsync(priority, async delegate
210-
{
211-
await System.Threading.Tasks.Task.Yield();
212-
await joinableTaskFactory.SwitchToMainThreadAsync();
213-
action();
214-
});
215-
}
216-
}
217-
218203
public void Dispose()
219204
{
220205
if (!_isDisposed)

src/toolkit/Community.VisualStudio.Toolkit.Shared/MEF/TokenErrorTaggerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public override IEnumerable<ITagSpan<IErrorTag>> GetTags(NormalizedSnapshotSpanC
5959
}
6060
}
6161

62-
private static string GetErrorType(IList<ErrorListItem> errors)
62+
private static string GetErrorType(IEnumerable<ErrorListItem> errors)
6363
{
6464
return errors.FirstOrDefault()?.ErrorCategory ?? PredefinedErrorTypeNames.SyntaxError;
6565
}

src/toolkit/Community.VisualStudio.Toolkit.Shared/MEF/TokenOutliningTaggerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override IEnumerable<ITagSpan<IStructureTag>> GetTags(NormalizedSnapshotS
3232
{
3333
foreach (IMappingTagSpan<TokenTag> tag in Tags!.GetTags(spans))
3434
{
35-
if (!tag.Tag.SupportOutlining || tag.Tag.GetOutliningText == null)
35+
if (tag.Tag.GetOutliningText == null)
3636
{
3737
continue;
3838
}

0 commit comments

Comments
 (0)