Hit Testing
SyntaxEditor makes it easy to hit test any Point
in the editor and return detailed information about what is at that location.
Performing a Hit Test
A hit test can be performed by first obtaining a Point
relative to the SyntaxEditor's coordinates and then calling the SyntaxEditor.HitTest method.
This code gets the current location of the mouse and performs a hit test against it:
IHitTestResult result = editor.HitTest(Mouse.GetPosition(editor));
Hit Test Results
The HitTest method returns an object of type IHitTestResult. This object provides information about exactly what is at the location being hit tested.
SyntaxEditor and Location
The SyntaxEditor and Location properties return the SyntaxEditor instance and location that were hit-tested.
Result Type
The Type property returns a HitTestResultType enumeration value. This value gives a high-level categorization of the result.
View and Margin
The View property returns an IEditorView if the location was over a view.
If the location was over a margin in the view, the ViewMargin property returns the IEditorViewMargin.
Offset and Position
The Offset and Position properties return the offset and TextPosition of the character at the location. If the location is not over a character, the nearest character is returned. If the location is not over a view, then the offset is -1
.
See information below for how to determine if the offset/position are for an actual character or for a close character.
View Line and Text Area Location
The ViewLine property returns the ITextViewLine that contains the Position.
The TextAreaLocation returns the location, but in text area-relative coordinates.
Snapshot and Reader
The Snapshot property returns the ITextSnapshot that was current when the hit test took place. The GetReader method can be used to obtain an ITextSnapshotReader that is initialized to the hit test offset.
Intra-Text Spacer
The IntraTextSpacerTag property returns the TagSnapshotRangeViewTextAreaOverIntraTextSpacer
.
VisualElement
The VisualElement property returns the IUIElement
, if any, that is related to the hit test result.
Determining if the Hit Test is Over a Character
The Type property is useful for determining if the location is over a character in the text area. When its value is ViewTextAreaOverCharacter
, the location is directly over a character within an editor view. When its value is ViewTextAreaOverLine
, the location is not over a character within an editor view, but it is in the whitespace past the end of a view line. When its value is ViewTextArea
, the location is not over any editor view lines, but it is in the text area.
Thus, the Offset and Position properties return the exact character the location is over when Type is ViewTextAreaOverCharacter
. Otherwise, the offset/position of the closest character is returned.
Scanning Text Around the Hit Test Result
The ITextSnapshotReader returned by the GetReader method can be used to scan text and tokens near the hit test result.
See the Scanning Text Using a Reader topic for more information on scanning text.