Explain to me the following VS 2010 Extension Sample code..

Posted by ealshabaan on Stack Overflow See other posts from Stack Overflow or by ealshabaan
Published on 2011-02-17T07:23:05Z Indexed on 2011/02/17 7:25 UTC
Read the original article Hit count: 186

Filed under:
|

Coders, I am building a VS 2010 extension and I am experimenting around some of the samples that came with the VS 2010 SDK.

One of the sample projects is called TextAdornment. In that project there is a weirdo class that looks like the following:

[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("text")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal sealed class TextAdornment1Factory : IWpfTextViewCreationListener

While I was experimenting with this project, I tried to debug the project to see the flow of the program and I noticed that this class gets hit when I first start the debugging.

Now my question is the following: what makes this class being the first class to get called when VS starts? In other words, why this class gets active and it runs as of some code instantiate an object of this class type?

Here is the only two files in the sample project: TextAdornment1Factory.cs

using System.ComponentModel.Composition;

using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.Utilities;

namespace TextAdornment1 { #region Adornment Factory /// /// Establishes an to place the adornment on and exports the /// that instantiates the adornment on the event of a 's creation /// [Export(typeof(IWpfTextViewCreationListener))] [ContentType("text")] [TextViewRole(PredefinedTextViewRoles.Document)] internal sealed class TextAdornment1Factory : IWpfTextViewCreationListener { /// /// Defines the adornment layer for the adornment. This layer is ordered /// after the selection layer in the Z-order /// [Export(typeof(AdornmentLayerDefinition))] [Name("TextAdornment1")] [Order(After = PredefinedAdornmentLayers.Selection, Before = PredefinedAdornmentLayers.Text)] [TextViewRole(PredefinedTextViewRoles.Document)] public AdornmentLayerDefinition editorAdornmentLayer = null;

    /// <summary>
    /// Instantiates a TextAdornment1 manager when a textView is created.
    /// </summary>
    /// <param name="textView">The <see cref="IWpfTextView"/> upon which the adornment should be placed</param>
    public void TextViewCreated(IWpfTextView textView)
    {
        new TextAdornment1(textView);
    }
}
#endregion //Adornment Factory

}

TextAdornment1.cs

using System.Windows;

using System.Windows.Controls; using System.Windows.Media; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.Text.Formatting;

namespace TextAdornment1 { /// ///TextAdornment1 places red boxes behind all the "A"s in the editor window /// public class TextAdornment1 { IAdornmentLayer _layer; IWpfTextView _view; Brush _brush; Pen _pen;

    ITextView textView;

    public TextAdornment1(IWpfTextView view)
    {
        _view = view;
        _layer = view.GetAdornmentLayer("TextAdornment1");
        textView = view;

        //Listen to any event that changes the layout (text changes, scrolling, etc)
        _view.LayoutChanged += OnLayoutChanged;
        _view.Closed += new System.EventHandler(_view_Closed);
        //selectedText();

        //Create the pen and brush to color the box behind the a's
        Brush brush = new SolidColorBrush(Color.FromArgb(0x20, 0x00, 0x00, 0xff));
        brush.Freeze();
        Brush penBrush = new SolidColorBrush(Colors.Red);
        penBrush.Freeze();
        Pen pen = new Pen(penBrush, 0.5);
        pen.Freeze();

        _brush = brush;
        _pen = pen;
    }

    void _view_Closed(object sender, System.EventArgs e)
    {
        MessageBox.Show(textView.Selection.IsEmpty.ToString());
    }

    /// <summary>
    /// On layout change add the adornment to any reformatted lines
    /// </summary>
    private void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
    {
        foreach (ITextViewLine line in e.NewOrReformattedLines)
        {
            this.CreateVisuals(line);
        }
    }

    private void selectedText()
    {

    }
    /// <summary>
    /// Within the given line add the scarlet box behind the a
    /// </summary>
    private void CreateVisuals(ITextViewLine line)
    {
        //grab a reference to the lines in the current TextView 
        IWpfTextViewLineCollection textViewLines = _view.TextViewLines;
        int start = line.Start;
        int end = line.End;

        //Loop through each character, and place a box around any a 
        for (int i = start; (i < end); ++i)
        {
            if (_view.TextSnapshot[i] == 'a')
            {
                SnapshotSpan span = new SnapshotSpan(_view.TextSnapshot, Span.FromBounds(i, i + 1));
                Geometry g = textViewLines.GetMarkerGeometry(span);
                if (g != null)
                {
                    GeometryDrawing drawing = new GeometryDrawing(_brush, _pen, g);
                    drawing.Freeze();

                    DrawingImage drawingImage = new DrawingImage(drawing);
                    drawingImage.Freeze();

                    Image image = new Image();
                    image.Source = drawingImage;

                    //Align the image with the top of the bounds of the text geometry
                    Canvas.SetLeft(image, g.Bounds.Left);
                    Canvas.SetTop(image, g.Bounds.Top);

                    _layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, span, null, image, null);
                }
            }
        }
    }
}

}

© Stack Overflow or respective owner

Related posts about c#

Related posts about visual-studio-2010