Quantcast
Channel: Visual Studio Integrate forum
Viewing all articles
Browse latest Browse all 4410

Highlighting Text In The Text Editor: Collapsed vs Expanded

$
0
0
I am trying to learn how to write extensions for the Visual Studio Text Editor. My first Project is one that simply highlights the Async and Await keywords. I currently have the following code:
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports Microsoft.VisualStudio.Text
Imports Microsoft.VisualStudio.Text.Editor
Imports Microsoft.VisualStudio.Text.Formatting

Class AsyncAwaitHighlighterAdornment
	Private WithEvents _view As IWpfTextView
	Private ReadOnly _layer As IAdornmentLayer
	Private ReadOnly _brush As Brush
	Private ReadOnly _pen As Pen

	Public Sub New(ByVal view As IWpfTextView)
		Me._view = view
		Me._layer = view.GetAdornmentLayer("AsyncAwaitHighlighterAdornment")
		Me._brush = New SolidColorBrush(Colors.LightGreen)
		Me._pen = New Pen(New SolidColorBrush(Colors.LightGreen), 0.5)
	End Sub

	Private Sub OnLayoutChanged(ByVal sender As Object, ByVal e As TextViewLayoutChangedEventArgs) Handles _view.LayoutChanged
		Dim textspan As SnapshotSpan
		Dim g As Geometry
		Dim img As Image
		For i As Integer = 0 To Me._view.TextSnapshot.Length - 6
			If Me._view.TextSnapshot.GetText(i, 6) = "Async " OrElse Me._view.TextSnapshot.GetText(i, 6) = "Await " Then
				textspan = New SnapshotSpan(Me._view.TextSnapshot, i, 5)
				g = Me._view.TextViewLines.GetMarkerGeometry(textspan)
				If g IsNot Nothing Then
					img = New Image With {.Source = New DrawingImage(New GeometryDrawing(Me._brush, Me._pen, g))}
					Canvas.SetLeft(img, g.Bounds.Left)
					Canvas.SetTop(img, g.Bounds.Top)
					Me._layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, textspan, Nothing, img, Nothing)
				End If
			End If
		Next
	End Sub
End Class

This code works fine, except for when a method is collapsed, in which it highlights the entire line until you expand it again. How can I restrict Visual Studio 2013 to highlighting only the specified text? I would also like to learn (although this is probably a question for another thread) how to change the foreground color of the text. Thanks.

Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/


Viewing all articles
Browse latest Browse all 4410

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>