Skip to content

Commit

Permalink
Merge branch 'master' into feature/fonts-and-colors
Browse files Browse the repository at this point in the history
  • Loading branch information
reduckted committed Aug 17, 2024
2 parents 6496bcc + 2f8d335 commit 203c1ef
Show file tree
Hide file tree
Showing 18 changed files with 445 additions and 167 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Create a file called **nuget.config** in your solution folder, and paste in the
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="Toolkit CI" value="https://ci.appveyor.com/nuget/community-visualstudio-toolkit" />
</packageSources>
</configuration>
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ deploy:
- provider: NuGet
artifact: /package/
api_key:
secure: 4z/fHvSKQx2UG4a1aMzIN53MmzruQxIFtp91QzSAcSoEfuvNef7nDM2n8iBduo6u
secure: TCK4iz4NnVAK6iAZbpocgig7CBxDTywbtFXcj61NfCr2aEHkT1mPuSj7/yLEkkp9
on:
branch: master
appveyor_repo_commit_message_extended: /\[release\]/
18 changes: 17 additions & 1 deletion demo/VSSDK.TestExtension/MEF/HighlightWord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,25 @@
using Community.VisualStudio.Toolkit;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Text.Editor;

using Microsoft.VisualStudio.Text.Classification;
using System.Windows.Media;
namespace TestExtension.MEF
{

[Export(typeof(EditorFormatDefinition))]
[Name("MarkerFormatDefinition/HighlightWordFormatDefinition")]
[UserVisible(true)]
internal class HighlightWordFormatDefinition : MarkerFormatDefinition
{
public HighlightWordFormatDefinition()
{
this.BackgroundColor = Colors.LightBlue;
this.ForegroundColor = Colors.DarkBlue;
this.DisplayName = "Highlight Word";
this.ZOrder = 5;
}
}

/// <summary>
/// This class demonstrates a HighlightWord tagger for text files
/// and it only highlights whole words starting with a Letter
Expand Down
8 changes: 8 additions & 0 deletions demo/VSSDK.TestExtension/ToolWindows/RunnerWindow.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -30,6 +31,13 @@ public Pane()
{
BitmapImageMoniker = KnownMonikers.StatusInformation;
ToolBar = new CommandID(PackageGuids.TestExtension, PackageIds.RunnerWindowToolbar);
WindowFrameAvailable += (_, _) => Debug.WriteLine("RunnerWindow frame is now available");
}

public override void OnToolWindowCreated()
{
base.OnToolWindowCreated();
GetWindowFrame().OnShow += (_, args) => Debug.WriteLine($"RunnerWindow state changed: {args.Reason}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ public CommandInterceptor(CommandID cmd, Func<CommandProgression> func)

public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
{
if (prgCmds[0].cmdID == _cmd.ID)
{
prgCmds[0].cmdf = (uint)OLECMDF.OLECMDF_ENABLED | (uint)OLECMDF.OLECMDF_SUPPORTED;
return VSConstants.S_OK;
}
return (int)Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal DocumentView(IVsWindowFrame nativeFrame)
public WindowFrame? WindowFrame { get; }

/// <summary>
/// The text view loaded int he window frame.
/// The text view loaded in the window frame.
/// </summary>
public IWpfTextView? TextView { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -43,30 +43,6 @@ public static async Task<IEnumerable<object>> RegisterCommandsAsync(this AsyncPa
return commands;
}

/// <summary>
/// Automatically calls the <see cref="BaseCommand{T}.InitializeAsync(AsyncPackage)"/> method for every command that has the <see cref="CommandAttribute"/> applied.
/// </summary>
/// <param name="package"></param>
/// <param name="assemblies"></param>
/// <returns></returns>
public static void RegisterToolWindows(this AsyncPackage package, params Assembly[] assemblies)
{
Type baseToolWindowType = typeof(BaseToolWindow<>);
IEnumerable<Type> toolWindowTypes = IncludePackageAssembly(assemblies, package).SelectMany(x => x.GetTypes())
.Where(x =>
!x.IsAbstract
&& x.IsAssignableToGenericType(baseToolWindowType));

foreach (Type toolWindowtype in toolWindowTypes)
{
MethodInfo initializeMethod = toolWindowtype.GetMethod(
"Initialize",
BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);

initializeMethod.Invoke(null, new object[] { package });
}
}

/// <summary>
/// Finds, creates and registers <see cref="BaseFontAndColorProvider"/> implementations.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.Shell;

namespace Community.VisualStudio.Toolkit
{
/// <summary>
/// Extensions for an <see cref="ToolkitPackage"/>
/// </summary>
public static class ToolkitPackageExtensions
{
/// <summary>
/// Automatically calls the <see cref="BaseToolWindow{T}.Initialize(ToolkitPackage)"/>
/// method for every <see cref="BaseToolWindow{T}"/> in the package or provided assemblies.
/// </summary>
/// <param name="package">The package that contains the tool windows to register.</param>
/// <param name="assemblies">The additional assemblies to look for tool windows in.</param>
public static void RegisterToolWindows(this ToolkitPackage package, params Assembly[] assemblies)
{
List<Assembly> assembliesList = assemblies.ToList();
Assembly packageAssembly = package.GetType().Assembly;
if (!assembliesList.Contains(packageAssembly))
assembliesList.Add(packageAssembly);

Type baseToolWindowType = typeof(BaseToolWindow<>);
IEnumerable<Type> toolWindowTypes = assembliesList.SelectMany(x => x.GetTypes())
.Where(x =>
!x.IsAbstract
&& x.IsAssignableToGenericType(baseToolWindowType));

foreach (Type? toolWindowtype in toolWindowTypes)
{
MethodInfo initializeMethod = toolWindowtype.GetMethod(
"Initialize",
BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);

initializeMethod.Invoke(null, new object[] { package });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class ContentTypes
public const string Json = "JSON";
public const string Xaml = "XAML";
public const string Xml = "XML";
public const string Repl = "REPL";
public const string Output = "Output";
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ public void WriteLine(string value)
});
}

/// <summary>
/// Writes the given text to the Output window pane.
/// </summary>
/// <param name="value">The text value to write.</param>
public void Write(string value)
{
ThreadHelper.JoinableTaskFactory.Run(async () =>
{
await WriteAsync(value);
});
}

/// <summary>
/// Writes a new line to the Output window pane.
/// </summary>
Expand All @@ -249,6 +261,15 @@ public Task WriteLineAsync()
/// </summary>
/// <param name="value">The text value to write. May be an empty string, in which case a newline is written.</param>
public async Task WriteLineAsync(string value)
{
await WriteAsync(value + Environment.NewLine);
}

/// <summary>
/// Writes the given text to the Output window pane.
/// </summary>
/// <param name="value">The text value to write.</param>
public async Task WriteAsync(string value)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

Expand All @@ -261,11 +282,11 @@ public async Task WriteLineAsync(string value)

if (_pane is IVsOutputWindowPaneNoPump nopump)
{
nopump.OutputStringNoPump(value + Environment.NewLine);
nopump.OutputStringNoPump(value);
}
else
{
ErrorHandler.ThrowOnFailure(_pane.OutputStringThreadSafe(value + Environment.NewLine));
ErrorHandler.ThrowOnFailure(_pane.OutputStringThreadSafe(value));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where
{
ITextStructureNavigator? navigator = _textStructureNavigatorSelector?.GetTextStructureNavigator(textView.TextBuffer);

var tagger = textView.Properties.GetOrCreateSingletonProperty(() =>
var tagger = buffer.Properties.GetOrCreateSingletonProperty(() =>
new SameWordHighlighterTagger(textView, buffer, _textSearchService, navigator, this));
tagger.Counter += 1;
tagger.RegisterEvents(textView);

return (ITagger<T>)tagger;
}
Expand All @@ -62,10 +62,9 @@ internal class HighlightWordTag : TextMarkerTag
public HighlightWordTag(string tagName) : base(tagName) { }
}

internal class SameWordHighlighterTagger : ITagger<HighlightWordTag>, IDisposable
internal class SameWordHighlighterTagger : ITagger<HighlightWordTag>
{
internal int Counter;
private readonly ITextView _view;
private readonly ITextBuffer _buffer;
private readonly ITextSearchService? _textSearchService;
private readonly ITextStructureNavigator? _textStructureNavigator;
Expand All @@ -74,30 +73,52 @@ internal class SameWordHighlighterTagger : ITagger<HighlightWordTag>, IDisposabl
private SnapshotSpan? _currentWord;
private SnapshotPoint _requestedPoint;
private bool _isDisposed;
private string _fileName="";
private readonly object _syncLock = new();

public SameWordHighlighterTagger(ITextView view, ITextBuffer sourceBuffer, ITextSearchService? textSearchService,
ITextStructureNavigator? textStructureNavigator, SameWordHighlighterBase tagger)
{
_view = view;
_fileName = sourceBuffer.GetFileName();
//System.Diagnostics.Debug.WriteLine("Create new tagger for "+_fileName);
_buffer = sourceBuffer;
_textSearchService = textSearchService;
_textStructureNavigator = textStructureNavigator;
_tagger = tagger;
_wordSpans = new NormalizedSnapshotSpanCollection();
_currentWord = null;
_view.Caret.PositionChanged += CaretPositionChanged;
_view.LayoutChanged += ViewLayoutChanged;
Counter = 0;
}

internal void RegisterEvents(ITextView textView)
{

textView.Caret.PositionChanged += CaretPositionChanged;
textView.LayoutChanged += ViewLayoutChanged;
textView.Closed += TextView_Closed;
Counter += 1;
//System.Diagnostics.Debug.WriteLine($"RegisterEvents {_fileName}: #{Counter} ");
}
internal void UnRegisterEvents(ITextView textView)
{
textView.Caret.PositionChanged -= CaretPositionChanged;
textView.LayoutChanged -= ViewLayoutChanged;
textView.Closed -= TextView_Closed;
Counter -= 1;
//System.Diagnostics.Debug.WriteLine($"UnRegisterEvents {_fileName}: #{Counter} ");
}
private void ViewLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
{
if (e.NewSnapshot != e.OldSnapshot)
{
UpdateAtCaretPosition(_view.Caret.Position);
var view = (ITextView)sender;
UpdateAtCaretPosition(view.Caret.Position);
}
}
private void TextView_Closed(object sender, EventArgs e)
{
UnRegisterEvents((ITextView)sender);
}

private void CaretPositionChanged(object sender, CaretPositionChangedEventArgs e)
{
Expand Down Expand Up @@ -228,20 +249,6 @@ public IEnumerable<ITagSpan<HighlightWordTag>> GetTags(NormalizedSnapshotSpanCol
}
}

public void Dispose()
{
if (!_isDisposed)
{
this.Counter -= 1;
if (this.Counter == 0)
{
_view.Caret.PositionChanged -= CaretPositionChanged;
_view.LayoutChanged -= ViewLayoutChanged;
_isDisposed = true;
}
}
}

public event EventHandler<SnapshotSpanEventArgs>? TagsChanged;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ExtensionMethods\IVsSolutionExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ExtensionMethods\IVsTextViewExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ExtensionMethods\TaskExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ExtensionMethods\ToolkitPackageExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ExtensionMethods\TypeExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ExtensionMethods\WindowExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FontsAndColors\BaseFontAndColorCategory.cs" />
Expand Down
Loading

0 comments on commit 203c1ef

Please sign in to comment.