How to lower case a Visual Studio Code Snippet variable? [duplicate]
Asked Answered
E

2

22

I've build some snippets to generate a fields for a setting class. I'm now using 2 variables - $setting$ and $Setting$ - to generate names for the property and the backing field. I like to use a single variable, because the only difference is that the backing field is always a lower-cased version.

Current code:

string $setting$;

/// <summary>
/// Gets the $bigComment$.
/// </summary>
/// <value>The $smallComment$.</value>
public string $Setting$
{
    get
    {
        if (String.IsNullOrEmpty($setting$))
        {
            $setting$ = CFW.Common.Configuration.GetAppSetting("$Setting$", $defaultValue$);
        }

        return $setting$;
    }
}

Is this possible?

Experimental answered 25/2, 2014 at 13:7 Comment(0)
E
6

It is not possible to change literals in a Code Snippets. There are some functions available:

GenerateSwitchCases - Generates a switch statement and a set of case statements for the members of the enumeration specified by the EnumerationLiteral parameter. The EnumerationLiteral parameter must be either a reference to an enumeration literal or an enumeration type.

ClassName - Returns the name of the class that contains the inserted snippet.

SimpleTypeName - Reduces the TypeName parameter to its simplest form in the context in which the snippet was invoked.

But they cannot modify literals.

Source: http://msdn.microsoft.com/en-us/library/ms242312(v=vs.110).aspx

Experimental answered 26/2, 2014 at 12:32 Comment(0)
H
-4

According to the official MSDN docs, default values for snippet variables should be defined in the XML of the snippet, not using the variable names.

So you'd have something like this:

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/CodeSnippet">
    <CodeSnippet>
        <Header>
            <!-- Add header information here -->
        </Header>
        <Snippet>
            <!-- Add additional Snippet information here -->
            <Declarations>
                <Literal>
                    <ID>SettingsField</ID>
                    <ToolTip>The settings field.</ToolTip>
                    <Default>settings</Default>
                </Literal>
                <Object>
                    <ID>SettingsProperty</ID>
                    <ToolTip>The settings property.</ToolTip>
                    <Default>Settings</Default>
                </Object>
            </Declarations>
            <Code Language="CSharp">
                <![CDATA[
                    Snippet code with $SettingsField$ and $SettingsProperty$
                ]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
Honduras answered 26/2, 2014 at 12:10 Comment(3)
Yeah, I know. The thing is, I want to use a single literal for both $SettingsField$ and $SettingsProperty$ - because of the only thing that's different is the casing of the first letter.Experimental
If you'd like Visual Studio to automatically make one variable be the camelCase version of the other, I'm afraid that's not possible. Otherwise... how would the generated code look? Different members cannot have the same name.Honduras
@SolalPirelli there are plenty of examples where you want an camel case version as well as lowercase.Korenblat

© 2022 - 2024 — McMap. All rights reserved.