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

SyntaxEditor for WPF Forum

The latest build of this product (v21.1.1) was released 3 months ago, which was before this thread was created.
Posted 22 days ago by Sunshine - Appeon
Version: 21.1.1
Avatar
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>#region</Title>
			<Shortcut>#region</Shortcut>
			<Description>Code snippet for #region</Description>
			<Author>Microsoft Corporation</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
				<SnippetType>SurroundsWith</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>name</ID>
					<ToolTip>Region name</ToolTip>
					<Default>MyRegion</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp"><![CDATA[#region $name$
	$selected$ $end$
#endregion]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

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

Comments (1)

Answer - Posted 21 days ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

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

Add Comment

Please log in to a validated account to post comments.