Example #1
0
    internal void OnLayoutChanged(object sender, Microsoft.VisualStudio.Text.Editor.TextViewLayoutChangedEventArgs e)
    {
        // Raised whenever the rendered text displayed in the ITextView changes - whenever the view does a layout
        // (which happens when DisplayTextLineContainingBufferPosition is called or in response to text or classification
        // changes), and also when the view scrolls or when its size changes.
        // Responsible for adding the adornment to any reformatted lines.

        // This code overlays the document version on line 0 of the file
        if (DocumentId == null)
        {
            var dte            = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
            var activeDocument = dte?.ActiveDocument; // sometimes we're constructed/invoked before ActiveDocument has been set
            if (activeDocument != null)
            {
                DocumentId = Workspace.CurrentSolution.GetDocumentIdsWithFilePath(activeDocument.FullName).FirstOrDefault();
            }
        }

        if (Adornment == null)
        {
            var line = e.NewOrReformattedLines.SingleOrDefault(l => l.Start.GetContainingLine().LineNumber == 0);
            if (line == null)
            {
                return;
            }
            var geometry = View.TextViewLines.GetMarkerGeometry(line.Extent);
            if (geometry == null)
            {
                return;
            }
            Adornment = new System.Windows.Controls.TextBlock {
                Width = 400, Height = geometry.Bounds.Height, Background = System.Windows.Media.Brushes.Yellow, Opacity = 0.5
            };
            System.Windows.Controls.Canvas.SetLeft(Adornment, 300);
            System.Windows.Controls.Canvas.SetTop(Adornment, geometry.Bounds.Top);
            View.GetAdornmentLayer("TextAdornment1").AddAdornment(Microsoft.VisualStudio.Text.Editor.AdornmentPositioningBehavior.TextRelative, line.Extent, null, Adornment, (tag, ui) => Adornment = null);
        }

        if (DocumentId != null)
        {
            var document = Workspace.CurrentSolution.GetDocument(DocumentId);
            if (document == null)
            {
                return;
            }
            Microsoft.CodeAnalysis.VersionStamp version;
            if (!document.TryGetTextVersion(out version))
            {
                version = Microsoft.CodeAnalysis.VersionStamp.Default;
            }
            Adornment.Text = version.ToString();
        }
    }
Example #2
0
    async void OnLayoutChanged(object sender, Microsoft.VisualStudio.Text.Editor.TextViewLayoutChangedEventArgs e)
    {
        // Raised whenever the rendered text displayed in the ITextView changes - whenever the view does a layout
        // (which happens when DisplayTextLineContainingBufferPosition is called or in response to text or classification
        // changes), and also when the view scrolls or when its size changes.
        // Responsible for adding the adornment to any reformatted lines.

        if (!Initialize())
        {
            return;
        }
        var start = View.TextViewLines.FirstVisibleLine.Start.GetContainingLine().LineNumber;
        var end   = View.TextViewLines.LastVisibleLine.End.GetContainingLine().LineNumber;
        await ReplayHost.WatchAsync(Document.FilePath, start, end - start + 1);
    }