/vs-extensibility-skills:managing-files-documentsfrom vs-extensibility-skills
Opens, reads, edits, and saves files programmatically in Visual Studio extensions. Covers VisualStudio.Extensibility, VSIX Community Toolkit, and 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.
A document is the in-memory representation of a file opened in Visual Studio. Extensions commonly need to open files, read or edit text, and react to document lifecycle events (open, save, close).
Document operations are fundamental to nearly every extension — whether you're formatting on save, analyzing text changes, or opening files programmatically. The key abstraction is the Running Document Table (RDT), which tracks all open documents in VS. The VisualStudio.Extensibility model simplifies this with IDocumentEventsListener and EditorExtensibility, while the VSSDK/Toolkit approaches give direct access to ITextBuffer and the RDT.
When to use this vs. alternatives:
| Task | VisualStudio.Extensibility | Community Toolkit | VSSDK |
|---|---|---|---|
| Open a file | DocumentsExtensibility.OpenDocumentAsync | VS.Documents.OpenAsync | VsShellUtilities.OpenDocument |
| Get active document text | context.GetActiveTextViewAsync | VS.Documents.GetActiveDocumentViewAsync | IVsTextManager.GetActiveView |
| Edit text | EditorExtensibility.EditAsync | DocumentView.TextBuffer.Insert/Replace/Delete | ITextBuffer.Insert/Replace/Delete |
| Listen for events | IDocumentEventsListener | VS.Events.DocumentEvents | IVsRunningDocTableEvents |
Documents are accessed via DocumentsExtensibility and text editing via EditorExtensibility. The extension runs out-of-process, so document access is through snapshots.
NuGet package: Microsoft.VisualStudio.Extensibility
Key namespaces: Microsoft.VisualStudio.Extensibility, Microsoft.VisualStudio.Extensibility.Editor
DocumentsExtensibility documents = this.Extensibility.Documents();
Uri uri = new Uri(@"C:/path/to/File.cs", UriKind.Absolute);
DocumentSnapshot document = await documents.OpenDocumentAsync(uri, cancellationToken);
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken ct)
{
using ITextViewSnapshot textView = await context.GetActiveTextViewAsync(ct);
if (textView is null) return;
// Read the full document text
ITextDocumentSnapshot document = textView.Document;
string fullText = document.Text;
// Read line by line
foreach (ITextLineSnapshot line in document)
{
string lineText = line.Text;
}
}
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken ct)
{
using ITextViewSnapshot textView = await context.GetActiveTextViewAsync(ct);
EditorExtensibility editor = this.Extensibility.Editor();
await editor.EditAsync(
batch =>
{
ITextDocumentSnapshot doc = textView.Document;
// Insert text at a position
doc.AsEditable(batch).Insert(0, "// Auto-generated header\n");
// Replace selected text
doc.AsEditable(batch).Replace(textView.Selection.Extent, "replaced text");
// Delete a range
doc.AsEditable(batch).Delete(new Span(0, 10));
},
ct);
}
DocumentsExtensibility documents = this.Extensibility.Documents();
Uri moniker = await context.GetSelectedPathAsync(cancellationToken);
DocumentSnapshot document = await documents.GetDocumentAsync(moniker, cancellationToken);
ITextDocumentSnapshot snapshot = await document.AsTextDocumentAsync(this.Extensibility, cancellationToken);
string text = snapshot.Text;
bool isDirty = document.IsDirty;
Implement IDocumentEventsListener and subscribe via DocumentsExtensibility.SubscribeAsync:
internal class DocumentEventSubscription : IDisposable, IDocumentEventsListener
{
private IDisposable? _subscription;
public static async Task<DocumentEventSubscription> CreateAsync(
VisualStudioExtensibility extensibility, CancellationToken ct)
{
var instance = new DocumentEventSubscription();
DocumentsExtensibility documents = extensibility.Documents();
instance._subscription = await documents.SubscribeAsync(
instance,
filterRegex: null, // null = all documents; use regex to filter by URI pattern
ct);
return instance;
}
public void Dispose() => _subscription?.Dispose();
Task IDocumentEventsListener.OpenedAsync(DocumentEventArgs e, CancellationToken ct)
{
// A document was opened
Uri moniker = e.Moniker;
return Task.CompletedTask;
}
Task IDocumentEventsListener.ClosedAsync(DocumentEventArgs e, CancellationToken ct)
=> Task.CompletedTask;
Task IDocumentEventsListener.SavingAsync(DocumentEventArgs e, CancellationToken ct)
=> Task.CompletedTask; // Before save — can inspect but not cancel
Task IDocumentEventsListener.SavedAsync(DocumentEventArgs e, CancellationToken ct)
=> Task.CompletedTask; // After save completed
Task IDocumentEventsListener.RenamedAsync(RenamedDocumentEventArgs e, CancellationToken ct)
{
Uri oldUri = e.OldMoniker;
Uri newUri = e.Moniker;
return Task.CompletedTask;
}
Task IDocumentEventsListener.ShownAsync(DocumentEventArgs e, CancellationToken ct)
=> Task.CompletedTask;
Task IDocumentEventsListener.HiddenAsync(DocumentEventArgs e, CancellationToken ct)
=> Task.CompletedTask;
}
The Toolkit wraps VSSDK document APIs with friendly helpers on the VS.Documents static class. Documents are represented by DocumentView, which combines the IWpfTextView and ITextBuffer.
NuGet package: Community.VisualStudio.Toolkit
Key namespace: Community.VisualStudio.Toolkit
// Open in the main document well
await VS.Documents.OpenAsync(@"C:/path/to/File.cs");
// Open in the Preview (provisional) tab
await VS.Documents.OpenInPreviewTabAsync(@"C:/path/to/File.cs");
// Open via the project system (respects custom editors)
await VS.Documents.OpenViaProjectAsync(@"C:/path/to/File.cs");
protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
{
DocumentView docView = await VS.Documents.GetActiveDocumentViewAsync();
if (docView?.TextView == null) return; // Not a text window
// Full document text
string fullText = docView.TextBuffer?.CurrentSnapshot.GetText();
// File path
string filePath = docView.FilePath;
// Caret position
SnapshotPoint caretPos = docView.TextView.Caret.Position.BufferPosition;
int line = caretPos.GetContainingLine().LineNumber;
int column = caretPos.Position - caretPos.GetContainingLine().Start.Position;
}
protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
{
DocumentView docView = await VS.Documents.GetActiveDocumentViewAsync();
if (docView?.TextView == null) return;
SnapshotPoint position = docView.TextView.Caret.Position.BufferPosition;
docView.TextBuffer?.Insert(position, DateTime.Now.ToString("yyyy-MM-dd"));
}
protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
{
DocumentView docView = await VS.Documents.GetActiveDocumentViewAsync();
if (docView?.TextView == null) return;
var selection = docView.TextView.Selection;
if (selection.IsEmpty) return;
var span = selection.SelectedSpans[0];
string selectedText = span.GetText();
docView.TextBuffer?.Replace(span, selectedText.ToUpperInvariant());
}
// Get a file's content as a SolutionItem
PhysicalFile file = await PhysicalFile.FromFileAsync(@"C:/path/to/File.cs");
DocumentView docView = await VS.Documents.GetActiveDocumentViewAsync();
string filePath = docView.TextBuffer?.GetFileName();
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await this.RegisterCommandsAsync();
VS.Events.DocumentEvents.Opened += OnDocumentOpened;
VS.Events.DocumentEvents.Closed += OnDocumentClosed;
VS.Events.DocumentEvents.Saved += OnDocumentSaved;
VS.Events.DocumentEvents.BeforeDocumentWindowShow += OnBeforeShow;
VS.Events.DocumentEvents.AfterDocumentWindowHide += OnAfterHide;
}
private void OnDocumentOpened(string filePath) { /* Document was opened */ }
private void OnDocumentClosed(string filePath) { /* Document was closed */ }
private void OnDocumentSaved(string filePath) { /* Document was saved */ }
private void OnBeforeShow(DocumentView docView) { /* About to become visible */ }
private void OnAfterHide(DocumentView docView) { /* Just became hidden */ }
The low-level VSSDK provides full control via IVsTextManager, IVsInvisibleEditorManager, ITextBuffer, and IVsRunningDocTableEvents.
NuGet package: Microsoft.VisualStudio.SDK
Key namespaces: Microsoft.VisualStudio.Shell, Microsoft.VisualStudio.Shell.Interop, Microsoft.VisualStudio.Text, Microsoft.VisualStudio.TextManager.Interop
// Simple approach using VsShellUtilities
VsShellUtilities.OpenDocument(ServiceProvider.GlobalProvider, @"C:/path/to/File.cs");
With full control (get the view and buffer):
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
VsShellUtilities.OpenDocument(
ServiceProvider.GlobalProvider,
@"C:/path/to/File.cs",
Guid.Empty, // logical view GUID (Empty = default)
out IVsUIHierarchy hierarchy,
out uint itemId,
out IVsWindowFrame windowFrame,
out IVsTextView textView);
windowFrame.Show();
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var textManager = (IVsTextManager)await package.GetServiceAsync(typeof(SVsTextManager));
textManager.GetActiveView(1, null, out IVsTextView activeView);
// Get the ITextBuffer via the adapter service
var adapterService = package.GetService<SVsEditorAdaptersFactoryService, IVsEditorAdaptersFactoryService>();
ITextBuffer buffer = adapterService.GetDataBuffer((IVsTextBuffer)activeView.GetBuffer(out var vsBuffer));
// Must be on the UI thread for ITextBuffer edits
ITextBuffer buffer = /* obtained from adapter or MEF */;
// Use an edit session for multiple edits as a single undo unit
using (ITextEdit edit = buffer.CreateEdit())
{
edit.Insert(0, "// Auto-generated header\n");
edit.Replace(new Span(10, 5), "replaced");
edit.Delete(new Span(100, 20));
edit.Apply(); // Commits all changes as one undo step
}
Use IVsInvisibleEditorManager to get an ITextBuffer without showing a tab:
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var invisibleEditorManager = (IVsInvisibleEditorManager)
await package.GetServiceAsync(typeof(SVsInvisibleEditorManager));
invisibleEditorManager.RegisterInvisibleEditor(
@"C:/path/to/File.cs",
pProject: null,
dwFlags: 0,
pFactory: null,
out IVsInvisibleEditor invisibleEditor);
// Get the text buffer from the invisible editor
var docDataGuid = typeof(IVsTextLines).GUID;
invisibleEditor.GetDocData(fEnsureWritable: 0, ref docDataGuid, out IntPtr docDataPtr);
var vsTextLines = (IVsTextLines)Marshal.GetObjectForIUnknown(docDataPtr);
Marshal.Release(docDataPtr);
var adapterService = package.GetService<SVsEditorAdaptersFactoryService, IVsEditorAdaptersFactoryService>();
ITextBuffer buffer = adapterService.GetDocumentBuffer(vsTextLines);
string text = buffer.CurrentSnapshot.GetText();
public class MyRunningDocTableEvents : IVsRunningDocTableEvents3
{
private readonly RunningDocumentTable _rdt;
private readonly uint _cookie;
public MyRunningDocTableEvents(IServiceProvider serviceProvider)
{
_rdt = new RunningDocumentTable(serviceProvider);
_cookie = _rdt.Advise(this);
}
public int OnAfterSave(uint docCookie)
{
RunningDocumentInfo info = _rdt.GetDocumentInfo(docCookie);
string filePath = info.Moniker;
// React to save
return VSConstants.S_OK;
}
public int OnBeforeSave(uint docCookie) => VSConstants.S_OK;
public int OnAfterFirstDocumentLock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining) => VSConstants.S_OK;
public int OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining) => VSConstants.S_OK;
public int OnAfterAttributeChange(uint docCookie, uint grfAttribs) => VSConstants.S_OK;
public int OnAfterAttributeChangeEx(uint docCookie, uint grfAttribs, IVsHierarchy pHierOld, uint itemidOld, string pszMkDocumentOld, IVsHierarchy pHierNew, uint itemidNew, string pszMkDocumentNew) => VSConstants.S_OK;
public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame) => VSConstants.S_OK;
public int OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame) => VSConstants.S_OK;
public void Dispose()
{
_rdt.Unadvise(_cookie);
}
}
DocumentsExtensibility + EditorExtensibility.EditAsync() for safe, out-of-process document manipulation.VS.Documents.GetActiveDocumentViewAsync() and VS.Documents.OpenAsync() for the simplest API surface.ITextBuffer.CreateEdit() for batched edits and IVsInvisibleEditorManager for reading files without opening a visible tab.null when getting the active document — the user may not have a text file open.ITextEdit in VSSDK/Toolkit, EditAsync batch in Extensibility) so they form one undo unit.VS.Events.DocumentEvents is being subscribed to in InitializeAsync. For VSSDK, ensure you're advising on IVsRunningDocTableEvents via IVsRunningDocumentTable.AdviseRunningDocTableEvents.ITextBuffer.Replace() (VSSDK/Toolkit) or EditorExtensibility.EditAsync (Extensibility) to apply changes to the live document.ITextEdit session (ITextBuffer.CreateEdit() → apply changes → edit.Apply()) so they're treated as one undo unit.ITextDocument.FilePath or the RDT's moniker.OpenDocumentAsync / VS.Documents.OpenAsync opens the document but may not bring the window to the foreground. Call IVsWindowFrame.Show() or activate the document view.Do NOT use
System.IO.File.ReadAllTextfor files open in VS — the in-memory buffer may have unsaved changes. Use VS document/text buffer APIs.
Do NOT modify files on disk while open in VS without going through document APIs — causes out-of-sync warnings and corrupts undo history.
Do NOT forget to dispose
ITextEditobjects — always useusingblocks for text edit sessions.
Do NOT assume the active document is a text document — it could be a designer, binary, or image file.
What SKILL.md sets; muted values are Claude Code's defaults
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