Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get namespace in a Code Snippet

The Microsoft link here lists three methods that we can use.

But how do we get the current namespace? I see there is a similar question, but the answer to that is using Macros, which doesnt solve this specific question.

The NameSpace() to do something like this:

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            .
            .
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>namespace</ID> 
                    <Function>NameSpace()</Function>                     
                </Literal>
            </Declarations>
            <Code Language="csharp">
                <![CDATA[
                    $namespace$
                ]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
like image 771
dushyantp Avatar asked Oct 04 '12 14:10

dushyantp


People also ask

How do you find the code on a snippet?

Test your snippet by opening a C# or Visual Basic project. With a code file open in the editor, choose Snippets > Insert Snippet from the right-click menu, then My Code Snippets. You should see a snippet named Square Root.

How do you use a code snippet?

To insert a snippet into a code window, place the cursor where the snippet is to be inserted, right-click to display the context menu, and select Insert Snippet or type CTRL + K, then X. The Code Snippet Picker is then displayed for you to pick the desired snippet. To cancel the Code Snippet Picker, press the Esc key.

What does the code snippet show?

A code Snippet is a programming term that refers to a small portion of re-usable source code, machine code, or text. Snippets help programmers reduce the time it takes to type in repetitive information while coding. Code Snippets are a feature on most text editors, code editors, and IDEs.


1 Answers

Found out that it can be done and it brings into picture the classes ExpansionProvider & ExpansionFunction

For the above snippet, I had to do something as such:

internal class NameSpaceExpansionFunction : ExpansionFunction
    {
        public NameSpaceExpansionFunction(ExpansionProvider provider)
            : base(provider)
        {
        }

        public override string GetCurrentValue()
        {
           //get namespace
           return namespace;
        }
    }

And the LanguageService tells the snippet file where to look for definition of the function:

public class MyLanguageService : LanguageService
    {
        public override ExpansionFunction CreateExpansionFunction(ExpansionProvider provider,
                                                                  string functionName)
        {
            ExpansionFunction function = null;
            if (String.Compare(functionName, "NameSpace", true) == 0)
            {
                function = new NameSpaceExpansionFunction(provider);
            }
            return function;
        }
    }

This turned out to be more like a tutorial question hence I have provided the links above. Should be helpful. Worked for me :)

like image 129
dushyantp Avatar answered Sep 22 '22 16:09

dushyantp