Code snippet dynamic parameter setting.

SyntaxEditor for WPF Forum

The latest build of this product (v21.1.1) was released 1 month ago, which was before this thread was created.
Posted 23 days ago by Sunshine - Appeon
Version: 21.1.1
Avatar

Does the code snippet support the setting of dynamic parameters?

For example, I want to insert a constructor.

In C# usually enter 'ctor', and then press Tab.

public class Class1
{
	public Class1()
	{

        }
}

But the result I got is:

public class Class1
{
	public DefaultClassName()
	{

        }
}

When inserting the fragment, I have no chance to set the value of DefaultClassName, it will only use the default value in the snippet file.

What should I do to achieve the desired effect?

[Modified 23 days ago]

Comments (6)

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

Hello,

It doesn't appear to support that feature at the moment.  Can you paste the source of this particular code snippet so we can make sure we're looking at the same thing?  Also, what API would you ideally like to have added to support this feature?


Actipro Software Support

Posted 21 days ago by Sunshine - Appeon
Avatar

This is a sample snippet file for the constructor.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>ctor</Title>
			<Shortcut>ctor</Shortcut>
			<Description>Code snippet for constructor</Description>
			<Author>Microsoft Corporation</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal IsEditable="False">
					<ID>classname</ID>
					<ToolTip>Class name</ToolTip>
					<Default>ClassNamePlaceholder</Default>
					<Function>ClassName()</Function>
				</Literal>
			</Declarations>
			<Code Language="csharp"><![CDATA[public $classname$ ()
{
	$end$
}]]></Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

From my point of view, only a user-defined method is needed to return the actual value corresponding to each ID.

When formatting the content of the code segment, please call the method implemented by the user to obtain the ID mapping table. Use the data in the mapping table to replace the actual content of the ID instead of directly replacing the default value.

Do you think there is a problem with this method. I think this is both effective and easy to implement.

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

Hello,

We already are fully populating the information in the ICodeSnippet, including the ICodeSnippetLiteralDeclaration.FunctionInvocation property.  Based on what you've described, you would like us to have some kind of callback method so that when we go to retrieve the default value for a literal declaration, we pass in the ICodeSnippetDeclaration and the default text value is returned.  That callback would have a default implementation of returning the ICodeSnippetDeclaration.DefaultText, but you would have an opportunity to override the result.  Is that what you need?

Where would you ideally like to see this implemented and what context information (ICodeSnippet, IEditorView, etc.) would you require passed in to make a successful implementation of this?


Actipro Software Support

Posted 15 days ago by Sunshine - Appeon
Avatar

Yes, I need an opportunity to override the default value in the code snippet.In the language of C#, some parameters of the code snippet must be dynamic.

I only need ICodeSnippet, IEditorView information to complete this requirement.

When can this feature be supported?

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

Hello,

For the next maintenance release we are adding a ICodeSnippetTemplateSessionEventSink.NotifySessionOpening method.  So if you make a class that inherits ICodeSnippetTemplateSessionEventSink and register an instance of that class on your syntax language you can then add code like the following into the new NotifySessionOpening method to adjust the default text for a declaration prior to the default text being inserted:

public void NotifySessionOpening(ICodeSnippetTemplateSession session) {
	if ((session.CodeSnippet.Title == "ctor") && (session.CodeSnippet.Declarations.Count > 0)) {
		if ((session.CodeSnippet.Declarations[0] is CodeSnippetDeclarationBase decl) && (decl.FunctionInvocation == "ClassName()")) {
			// session.View will get access to the editor and document...
			//   Determine the class name and set the result in the following line...
			decl.DefaultText = "FindClassNameAndSetHere";
		}
	}
}


Actipro Software Support

Posted 6 days ago by Sunshine - Appeon
Avatar

Okay, I will give feedback after trying this feature in the next version

Add Comment

Please log in to a validated account to post comments.