/vs-extensibility-skills:listening-text-view-eventsfrom vs-extensibility-skills
Detects editor open, close, and text-change events in Visual Studio extensions, covering VisualStudio.Extensibility, VSIX Community Toolkit, and legacy VSSDK approaches.
Ships in vs-extensibility-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Text view listeners let you react when editors are opened, closed, or when their content changes. This is a common entry point for extensions that need to initialize per-editor state. Common scenarios:
Text view listeners are the foundational hook that most editor extensions use as their entry point. Adornments, taggers, classifiers, and margins all need to know when an editor opens — but text view listeners are also useful on their own for per-document state initialization or for extensions that need to react to text changes without producing visual output. The VisualStudio.Extensibility model provides ITextViewOpenClosedListener as a simpler, out-of-process alternative.
When to use text view listeners vs. alternatives:
ITextBuffer.Changed subscription (see vs-editor-tagger)Any extension that uses MEF editor exports must declare the MEF asset type in the .vsixmanifest file. Without this, Visual Studio will not discover your MEF components and your listener will not load.
Add this inside the <Assets> element of source.extension.vsixmanifest:
<Asset Type="Microsoft.VisualStudio.MefComponent"
d:Source="Project"
d:ProjectName="%CurrentProject%"
Path="|%CurrentProject%|" />
Note: The VisualStudio.Extensibility approach does NOT require this MEF asset entry — it uses its own discovery mechanism.
The new model provides ITextViewOpenClosedListener and ITextViewChangedListener for reacting to editor events out-of-process.
NuGet package: Microsoft.VisualStudio.Extensibility
Key namespace: Microsoft.VisualStudio.Extensibility.Editor
using Microsoft.VisualStudio.Extensibility;
using Microsoft.VisualStudio.Extensibility.Editor;
namespace MyExtension;
[VisualStudioContribution]
internal sealed class MyTextViewListener
: ExtensionPart, ITextViewOpenClosedListener
{
public TextViewExtensionConfiguration TextViewExtensionConfiguration => new()
{
AppliesTo = new[]
{
DocumentFilter.FromDocumentType("CSharp"),
},
};
public Task TextViewOpenedAsync(ITextViewSnapshot textView, CancellationToken cancellationToken)
{
// Called when a matching text view is opened
System.Diagnostics.Debug.WriteLine($"Opened: {textView.Document.Uri}");
return Task.CompletedTask;
}
public Task TextViewClosedAsync(ITextViewSnapshot textView, CancellationToken cancellationToken)
{
// Called when the text view is closed
System.Diagnostics.Debug.WriteLine($"Closed: {textView.Document.Uri}");
return Task.CompletedTask;
}
}
using Microsoft.VisualStudio.Extensibility;
using Microsoft.VisualStudio.Extensibility.Editor;
namespace MyExtension;
[VisualStudioContribution]
internal sealed class MyTextChangeListener
: ExtensionPart, ITextViewChangedListener
{
public TextViewExtensionConfiguration TextViewExtensionConfiguration => new()
{
AppliesTo = new[]
{
DocumentFilter.FromGlobPattern("**/*.cs"),
},
};
public Task TextViewChangedAsync(
TextViewChangedArgs args,
CancellationToken cancellationToken)
{
// React to text changes
foreach (var change in args.AfterTextView.Document.AsTextDocument().Changes)
{
System.Diagnostics.Debug.WriteLine(
$"Changed at position {change.Position}: '{change.OldText}' -> '{change.NewText}'");
}
return Task.CompletedTask;
}
}
Use DocumentFilter to scope which files trigger your listener:
// By document/content type
DocumentFilter.FromDocumentType("CSharp")
DocumentFilter.FromDocumentType("code")
// By file glob pattern
DocumentFilter.FromGlobPattern("**/*.cs")
DocumentFilter.FromGlobPattern("**/appsettings*.json")
The Community Toolkit does not wrap text view creation listeners — use the standard VSSDK MEF pattern below.
NuGet packages: Microsoft.VisualStudio.SDK, Microsoft.VisualStudio.Editor, Microsoft.VisualStudio.Text.UI.Wpf
Key namespaces: Microsoft.VisualStudio.Text.Editor, Microsoft.VisualStudio.Utilities
IWpfTextViewCreationListenerusing System.ComponentModel.Composition;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Utilities;
namespace MyExtension;
[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("CSharp")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal sealed class MyTextViewCreationListener : IWpfTextViewCreationListener
{
public void TextViewCreated(IWpfTextView textView)
{
// Called when a C# document editor is created
// Subscribe to text changes
textView.TextBuffer.Changed += OnTextBufferChanged;
// Subscribe to editor close
textView.Closed += OnTextViewClosed;
}
private void OnTextBufferChanged(object sender, Microsoft.VisualStudio.Text.TextContentChangedEventArgs e)
{
foreach (var change in e.Changes)
{
System.Diagnostics.Debug.WriteLine(
$"Changed: '{change.OldText}' -> '{change.NewText}'");
}
}
private void OnTextViewClosed(object sender, System.EventArgs e)
{
if (sender is IWpfTextView textView)
{
// Clean up event handlers to avoid leaks
textView.TextBuffer.Changed -= OnTextBufferChanged;
textView.Closed -= OnTextViewClosed;
}
}
}
ITextViewCreationListener (non-WPF)For scenarios that don't need WPF access:
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Utilities;
namespace MyExtension;
[Export(typeof(ITextViewCreationListener))]
[ContentType("text")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal sealed class MyTextViewListener : ITextViewCreationListener
{
public void TextViewCreated(ITextView textView)
{
// Works for any text view, not just WPF views
}
}
Use [TextViewRole] to filter which editor instances trigger your listener:
| Role | Description |
|---|---|
PredefinedTextViewRoles.Document | Main document editors |
PredefinedTextViewRoles.Interactive | Interactive windows (e.g., C# Interactive) |
PredefinedTextViewRoles.Editable | Any editable view |
PredefinedTextViewRoles.PrimaryDocument | Primary (not secondary/split) document view |
PredefinedTextViewRoles.EmbeddedPeekTextView | Peek Definition views |
using Microsoft.VisualStudio.Text;
// Inside TextViewCreated:
if (textView.TextBuffer.Properties.TryGetProperty(
typeof(ITextDocument), out ITextDocument document))
{
string filePath = document.FilePath;
}
Closed handler to prevent memory leaks.[ContentType] to limit which file types trigger your listener.[TextViewRole] to avoid triggering in peek windows, diff views, etc.TextViewCreated runs on the UI thread — keep it fast.textView.TextBuffer.Properties to store per-view state.IWpfTextViewCreationListener..vsixmanifest for the VSSDK approach.ITextViewOpenClosedListener and ITextViewChangedListener with DocumentFilter for scoping. No MEF asset required.IWpfTextViewCreationListener or ITextViewCreationListener via MEF. Subscribe to TextBuffer.Changed and Closed events. Unsubscribe on close.source.extension.vsixmanifest for the VSSDK approach.PredefinedTextViewRoles.Document to avoid triggering in non-document editors..vsixmanifest. Verify [ContentType] and [TextViewRole] match. For Extensibility, ensure the [VisualStudioContribution] attribute is present.[TextViewRole(PredefinedTextViewRoles.Document)]. Without it, the listener fires for all editor instances.ITextView.Closed. Always subscribe to Closed early and clean up there.ITextBuffer.Changed (VSSDK/Toolkit) or implement ITextViewChangedListener (Extensibility) — text view creation alone doesn't track ongoing edits.Do NOT do heavy work in the text view creation callback — it runs on the UI thread and blocks the editor from appearing.
Do NOT forget the
[TextViewRole]attribute — without it, your listener triggers for every text view instance (peek, diff, embedded), which may not be intended.
Do NOT forget to unsubscribe from events when the text view closes — leaked subscriptions cause memory leaks and crashes.
Do NOT forget the
MefComponentasset type in.vsixmanifestfor in-process extensions.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
Generated from the plugin's indexed repository (madskristensen/vs-agent-plugins) — no native marketplace selected
npx claudepluginhub p/madskristensen-vs-extensibility-skills41 skills in this plugin
/adding-codelens-indicatorsAdd custom CodeLens indicators that display inline information above code elements in the Visual Studio editor. Use when the user asks how to add a CodeLens indicator, show inline data above methods or classes, create a custom CodeLens provider, implement IAsyncCodeLensDataPointProvider, display git history or test status in CodeLens, or add a details popup to CodeLens entries. Covers VSSDK / VSIX Community Toolkit (in-process + out-of-process CodeLens service). VisualStudio.Extensibility (out-of-process) does not have a dedicated CodeLens API./adding-commandsAdd commands (menu items, toolbar buttons) to Visual Studio extensions. Use when the user asks how to create a command, add a menu item, add a toolbar button, register a command handler, wire up a .vsct file, use KnownMonikers icons on buttons, or place commands in menus/toolbars in a Visual Studio IDE extension. Covers VisualStudio.Extensibility (out-of-process), VSIX Community Toolkit (in-process), and legacy VSSDK (in-process) approaches./adding-context-menusAdd native-looking context menus to custom WPF UI in Visual Studio extensions (tool windows, custom tree views, list views). Use when the user wants to show a right-click context menu from their own UI that looks native to VS. Covers defining VSCT context menus (type="Context"), grouping commands with separators, and programmatically showing them via IVsUIShell.ShowContextMenu. Do NOT use regular WPF ContextMenu — it won't match VS theming or support command routing. Covers VSSDK and VSIX Community Toolkit (in-process). VisualStudio.Extensibility (out-of-process) does not support VSCT-based context menus on custom UI./adding-editor-adornmentsAdd visual adornments (decorations, overlays, highlights) to the Visual Studio text editor. Use when the user asks how to draw on the editor, add inline decorations, highlight text visually, add background colors to lines, show icons in the editor, create viewport-relative overlays, or render custom WPF visuals on top of editor text. Covers VisualStudio.Extensibility (out-of-process), VSIX Community Toolkit (in-process), and legacy VSSDK (in-process) approaches./adding-editor-classifiersAdd custom syntax highlighting and text classification to the Visual Studio editor. Use when the user asks how to colorize text, add syntax highlighting, create a classifier, highlight keywords, change text colors in the editor, create a custom language colorizer, or implement classification types. Covers VisualStudio.Extensibility (out-of-process), VSIX Community Toolkit (in-process), and legacy VSSDK (in-process) approaches./adding-editor-marginsAdd custom margins to the Visual Studio text editor (gutter icons, side panels, bottom bars). Use when the user asks how to add a gutter, editor margin, side panel, bottom bar, line number gutter, custom glyph margin, or editor chrome. Covers VisualStudio.Extensibility (out-of-process), VSIX Community Toolkit (in-process), and legacy VSSDK (in-process) approaches.Other plugins' skills for the same job