CodeSnippet : Cannot trigger when the beginning of Shortcut is a symbol

SyntaxEditor for WPF Forum

Posted 3 years ago by Sunshine - Appeon
Version: 21.1.1
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="">
	<CodeSnippet Format="1.0.0">
			<Description>Code snippet for #region</Description>
			<Author>Microsoft Corporation</Author>
					<ToolTip>Region name</ToolTip>
			<Code Language="csharp"><![CDATA[#region $name$
	$selected$ $end$

This kind of non-letter at the beginning can not trigger the code snippet by pressing the Tab key

Comments (1)

Answer - Posted 3 years ago by Actipro Software Support - Cleveland, OH, USA


The CodeSnippetProvider.GetPossibleShortcutSnapshotRange method is what gets the "word" before the caret to see if it matches any code snippet shortcuts.  The default implementation gets the word before the caret.

Our C# language in the .NET Languages Add-on makes its own inherited class (CSharpCodeSnippetProvider) that overrides that method and looks for '#' characters like:

protected override TextSnapshotRange GetPossibleShortcutSnapshotRange(TextSnapshotOffset snapshotOffset) {
	var snapshotRange = TextSnapshotRange.Deleted;

	// Get the word right before the offset, and if the word ends at the offset, return it
	var textRange = snapshotOffset.Snapshot.GetWordTextRange(snapshotOffset.Offset - 1);
	if ((!textRange.IsZeroLength) && (textRange.EndOffset == snapshotOffset.Offset)) {
		snapshotRange = new TextSnapshotRange(snapshotOffset.Snapshot, textRange);

		// If the word has a '#' preceding it, include that in the range as well
		if ((snapshotRange.StartOffset > 0) && (snapshotRange.Snapshot[snapshotRange.StartOffset - 1] == '#'))
			snapshotRange = new TextSnapshotRange(snapshotRange.Snapshot, snapshotRange.StartOffset - 1, snapshotRange.EndOffset);

	// NOTE: Additional code here to check the token before the caret isn't a comment or literal (e.g. string)

	return snapshotRange;

Actipro Software Support

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.