Click here to Skip to main content
Click here to Skip to main content

Getting started with Visual Studio Editor Margin Extensions

, 14 Jun 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
This article will help you to start with Visual Studio Editor Margin Extensions, Sample extension 'File Path On Footer'.

File Path On Footer AddIn for Visual Studio by Shemeer NS


Extending visual studio is fun.

As a developer I used to spend much time on Visual Studio and automate many problems that is relevent to me. Visual Studio is a powerful editor and using extensibility project templates it is very easy to extend Visual Studio. We can plug many things to the IDE as a Visual Studio Package or a Visual Studio AddIn based on our needs.

There are thousands of visual studio extensions available in Visual Studio gallery. If you want something additional in Visual Studio or if you like to extend or automate something in Visual Studio, then choose the most appropriate project template from the extensibilty to start with it. In this article we will explore how to get started with a Visual Studio Editor Margin Extension. Editor extensions are used to customize the appearance and behavior of the Visual Studio editor.

From Visual Studio 2010 you can create much more advanced customizations than changing the start page or creating snippets.

Why not in older versions? because from Visual Studio 2010,

  • Screens have been rewritten in WPF and managed code.
  • The IDE API has been refactored for easier use.
  • The IDE API is fully documented.
  • New immutable text snapshots make it easier to obtain accurate snapshots of the text editor.

In this article we are going to customize the appearance of editor margins by using editor margin extensions.

You might be interested in the below plugins that are created by me,

Pre-requisite for Extension Development

Before we start we need to make sure that all required softwares are installed in our box, Visual Studio 2010 has been written from the ground up for extensibility and customization, So I'm taking VS2010 as the base for major extension development.

Available Extensibility Project Templates

After you install the Visual Studio customization SDK, a number of new extensibility projects are available for you to create. These projects are templates that demonstrate how to perform various Hello World-type customizations that you can then build on.

Available Extensibility Project Templates

The following are the current availbel extensibility projects templateds,

  • Visual Studio Package - Create a VSPackage loadable in Visual Studio.
  • C# Item Template - A project for building and packaging a C# Item Template.
  • C# Project Template - A project for building and packaging a C# Project Template.
  • Editor Classifier - This template gets you started with creating your own syntax highlighting using an editor calssifier. It also includes classifier type and classification format exports that allow you to define your own text properties for your syntax coloring.
  • Editor Margin - This template creates a bare bone editor margin that places a green box across the bottom of the editor's viewport.
  • Editor Text Adornament - This template provides a bare bones editor visual that is positioned relative to text inside the editor. This basic text-realtive adornament puts a box behind all the "A"s in the file.
  • Editor Viewport Adornament - This template provides a bare bones editor visual that is positioned relative to the editore's visual surface. This basic basic viewport- relative adornament puts a purple box in the top right hand corner of the editor's viewport.
  • VSIX Project - This template creates an empty VSIX project which can be used to package up content inside a VSIX container.
  • Windows Forms Toolbox Control - Creates a project to build and package a Toolbox Control
  • WPF Toolbox Control - Creates a project to build and package a WPF Toolbox Control.

The editor provides extension points that you can extend as Managed Extensibility Framework (MEF) component parts. These are the main extension point categories:

  • Content types
  • Classification types and classification formats
  • Margins and scrollbars
  • Tags
  • Adornments
  • Mouse processors
  • Drop handlers
  • Options
  • IntelliSense

Note: Visual Studio add-ins are deprecated in Visual Studio 2013. You can still use existing add-ins, but you can't create new ones. Visual Studio recommend that you convert add-ins to VSPackages if you want to modify or distribute them.

Managed Extensibility Framework (MEF)

Extensions make heavy use of a technology called Managed Extensibility Framework (MEF). The Managed Extensibility Framework (MEF) is a composition layer for .NET that improves the flexibility, maintainability and testability of large applications. MEF can be used for third-party plugin extensibility, or it can bring the benefits of a loosely-coupled plugin-like architecture for regular applications.

MEF is a part of the Microsoft .NET Framework, with types primarily under the System.ComponentModel.Composition.* namespaces.

MEF is open source -

Editor Margin Extension

New Project Visual Studio 2013

Let us create a simple editor margin extension. To start with that, Open up Visual Studio and create a new Editor Margin project called EditorMargin1.

Select the Editor Margin Project Template

Click OK, now we have the basic project ready,

Editor Margin Extension Solution Explorer

Opn EditorMargin1Factory.cs and note how it utilizes the MEF [Export] attribute,

[Order(After = PredefinedMarginNames.HorizontalScrollBar)] //Ensure that the margin occurs below the horizontal scrollbar
[MarginContainer(PredefinedMarginNames.Bottom)] //Set the container to the bottom of the editor window
[ContentType("text")] //Show this margin for all text-based types
internal sealed class MarginFactory : IWpfTextViewMarginProvider
    public IWpfTextViewMargin CreateMargin(IWpfTextViewHost textViewHost, IWpfTextViewMargin containerMargin)
        return new EditorMargin1(textViewHost.TextView);

The other attributes contain various bits of metadata utilized by the IDE like margin position and order of the margin etc.

Lets take a look at the major attributes,

Name- Associates a name with an editor extension part.

FileExtension - Identifies a file extension.

Order - the priority of the format. Valid values are from Priority.

ContentType - Declares an association between an extension and a particular content type.

Read more about these attributes from MSDN - Editor Points

If we run the application right away we will get a compile time error, ohh.

The VSIX manifest must containe a value for Publisher, Author

To countinue our application we need make sure that the Author(or Publisher) value is populated in source.extension.vsixmanifest.

Changing the Auhtor of a Visual Studio Extension

Now we are good to go, running the application and opening a file in visual studio experimental instance will show a green box at the bottom of editor as below,

Hello World  Editor Margin

The code reponsible for the above green box is given below (EditorMargin1.cs)

public EditorMargin1(IWpfTextView textView)
    _textView = textView;

    this.Height = 20;
    this.ClipToBounds = true;
    this.Background = new SolidColorBrush(Colors.LightGreen);

    // Add a green colored label that says "Hello World!"
    Label label = new Label();
    label.Background = new SolidColorBrush(Colors.LightGreen);
    label.Content = "Hello World!";


Now lets change the default behaviour and show the file path instead of green box. The following code changes are done to the EditorMargin1.cs,

private string _filePath = string.Empty;

/// <summary>
/// Creates a <see cref="EditorMargin1"/> for a given <see cref="IWpfTextView"/>.
/// </summary>
/// <param name="textView">The <see cref="IWpfTextView"/> to attach the margin to.</param>
public EditorMargin1(IWpfTextView textView)
    _textView = textView;

    ITextBuffer TextBuffer = _textView.TextBuffer;

    ITextDocument TextDocument = GetTextDocument(TextBuffer);

    if (TextDocument == null || TextDocument.FilePath == null || TextDocument.FilePath.Equals("Temp.txt"))

    _filePath = TextDocument.FilePath;

    this.Height = 20;
    this.ClipToBounds = true;
    this.Background = new SolidColorBrush(Color.FromArgb(255, 232, 232, 236));

    Label label = new Label();
    label.Background = this.Background;
    label.Content = _filePath; 


public ITextDocument GetTextDocument(ITextBuffer TextBuffer)
    if (TextBuffer == null)
        return null;

    ITextDocument textDoc;
    var rc = TextBuffer.Properties.TryGetProperty<ITextDocument>(
        typeof(ITextDocument), out textDoc);
    if (rc == true)
        return textDoc;
        return null;

In the above code, instead of adding a green Hello World label to the bottom, I have added the file name that was extracted from the ITextBuffer.

File Path On Footer Visual Studio IDE Editor



In this article I have tried to explain How to create a Visual Studio Editor Margin Extension with a simple sample, I hope you have enjoyed this article and got some value addition to your knowledge.

I have put my time and efforts on all of my articles, please don't forget to mark your votes, suggestions and feedback to improve the quality of this and upcoming articles.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shemeer NS
Software Developer (Senior)
India India
Technology Specialist | CodeProject MVP | Visual Studio Gallery Contributor | Author | Geek | Netizen | Husband | ChessPlayer
Most of my articles are listed on top 5 of the respective 'Best articles of the month' and some of my articles are published on ASP.NET WebSite's Article of the Day section.
Check my contributions in Visual Studio Gallery and Code Project
Technical Blog:
Twitter :
Google+ :
Follow on   Twitter   Google+

Comments and Discussions

QuestionGreat, my vote of 5 PinmemberMember 1063000815-Jul-14 0:50 
AnswerRe: Great, my vote of 5 PinmvpShemeer NS17-Jul-14 0:37 
GeneralRe: Great, my vote of 5 PinmemberMember 106300088-Aug-14 6:26 
Questionupdates for vs 2012 - vs 2013 Pinmemberkiquenet.com11-Jul-14 2:06 
AnswerRe: updates for vs 2012 - vs 2013 PinmvpShemeer NS11-Jul-14 6:35 
QuestionNice! PinprofessionalVolynsky Alex15-Jun-14 8:22 
AnswerRe: Nice! PinmvpShemeer NS15-Jun-14 20:07 
GeneralRe: Nice! PinprofessionalVolynsky Alex15-Jun-14 23:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150301.1 | Last Updated 14 Jun 2014
Article Copyright 2014 by Shemeer NS
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid